Page MenuHomeSealhub

_batch_action.ts
No OneTemporary

_batch_action.ts

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

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)

Event Timeline