diff --git a/table-provider.js b/table-provider.js index 6fbd313..af799ad 100644 --- a/table-provider.js +++ b/table-provider.js @@ -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", + }; + }, }, };