Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F996350
_batch_action.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
_batch_action.ts
View Options
import
App
from
"../../app/app"
;
import
Context
from
"../../context"
;
import
ArrayCartesian
from
"../../utils/array-cartesian.js"
;
import
PromiseIterateGenerator
from
"../../utils/promise-iterate-generator.js"
;
import
{
LooseObject
}
from
"../types"
;
type
BatchActionBatchType
=
"batch"
;
type
BatchActionCartesianType
=
"cartesian"
;
export
type
BatchActionParams
=
{
__multiple
:
boolean
;
mode
:
BatchActionBatchType
|
BatchActionCartesianType
;
sources
:
Array
<
Array
<
any
>>
;
};
export
default
async
function
batchAction
(
app
:
App
,
context
:
Context
,
params
:
BatchActionParams
,
callback
:
Function
)
{
// callback is a function that will be called for each instance infered from the description, taking context and inferred body as arguments
const
mode
=
params
.
mode
||
"batch"
;
// can be: "cartesian" or "batch";
if
(
mode
===
"batch"
)
{
throw
new
Error
(
"BATCH mode not implemented yet"
);
}
if
(
mode
!==
"cartesian"
)
{
throw
new
Error
(
`Incorrect mode:
${
mode
}
`
);
}
const
field_names
:
Array
<
string
>
=
[];
const
possible_field_values
:
Array
<
any
>
=
[];
const
to_await
=
[];
for
(
const
source
of
params
.
sources
)
{
if
(
source
[
0
]
===
"literal"
)
{
for
(
const
field_name
in
source
[
1
])
{
field_names
.
push
(
field_name
);
possible_field_values
.
push
([
source
[
1
][
field_name
]]);
}
}
else
if
(
source
[
0
]
===
"collection_fields"
)
{
const
collection_name
=
source
[
1
].
collection
;
const
filter
=
source
[
1
].
filter
||
{};
const
fields
=
source
[
1
].
fields
||
[];
const
map_to
=
source
[
1
].
map_to
;
const
promise
=
app
.
run_action
(
context
,
[
"collections"
,
collection_name
],
"show"
,
{
filter
,
})
.
then
(
function
(
sealious_response
)
{
for
(
const
i
in
map_to
)
{
const
field_in_collection
=
fields
[
i
];
const
field_name
=
map_to
[
i
];
field_names
.
push
(
field_name
);
possible_field_values
.
push
(
sealious_response
.
items
.
map
(
(
resource
:
LooseObject
)
=>
{
return
resource
[
field_in_collection
];
}
)
);
}
});
to_await
.
push
(
promise
);
}
}
await
Promise
.
all
(
to_await
);
return
PromiseIterateGenerator
(
new
ArrayCartesian
(
possible_field_values
),
function
(
values
:
Array
<
any
>
)
{
const
body
:
LooseObject
=
{};
for
(
let
i
=
0
;
i
<
field_names
.
length
;
++
i
)
{
body
[
field_names
[
i
]]
=
values
[
i
];
}
return
callback
(
context
,
body
);
}
);
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Tue, Dec 24, 14:02 (16 h, 53 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
557200
Default Alt Text
_batch_action.ts (2 KB)
Attached To
Mode
rS Sealious
Attached
Detach File
Event Timeline
Log In to Comment