Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions table-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,16 +175,48 @@ const configuration_workflow = (req) =>
module.exports = {
"JavaScript code": {
configuration_workflow,
fields: (cfg) => cfg?.columns || [],
get_table: (cfg) => ({
getRows: async (where, opts) => {
const rows = await runCode(cfg.code, where, opts);
return Array.isArray(rows) ? rows : [];
},
countRows: async (where, opts) => {
const rows = await runCode(cfg.code, where, opts);
return Array.isArray(rows) ? rows.length : 0;
},
}),
fields: async (cfg) => {
const parsed = typeof cfg === "string" ? JSON.parse(cfg) : cfg;
let columns = parsed?.columns?.length ? parsed.columns : null;
if (!columns) {
if (!parsed?.code) return [];
try {
const rows = await runCode(parsed.code, {}, {});
if (!Array.isArray(rows) || !rows.length) return [];
const sampleRow = rows[0];
columns = Object.keys(sampleRow).map((name) => ({
name,
label: Field.nameToLabel(name),
type: jsTypeGuess(sampleRow[name]),
}));
} catch {
return [];
}
}
if (!columns.some((f) => f.primary_key))
columns = [
{ name: "_idx", label: "Index", type: "Integer", primary_key: true },
...columns,
];
return columns;
},
get_table: (cfg) => {
const parsed = typeof cfg === "string" ? JSON.parse(cfg) : cfg;
const hasPK = parsed?.columns?.some((f) => f.primary_key);
return {
getRows: async (where, opts) => {
const rows = await runCode(cfg.code, where, opts);
if (!Array.isArray(rows)) return [];
return hasPK ? rows : rows.map((r, i) => ({ _idx: i, ...r }));
},
countRows: async (where, opts) => {
const rows = await runCode(cfg.code, where, opts);
return Array.isArray(rows) ? rows.length : 0;
},
updateRow: async () => "Updates are not supported on JavaScript code provider tables",
insertRow: async () => "Inserts are not supported on JavaScript code provider tables",
deleteRows: async () => "Deletes are not supported on JavaScript code provider tables",
};
},
},
};
Loading