@opengis/fastify-table
Version:
core-plugins
36 lines (35 loc) • 1.27 kB
JavaScript
/* eslint-disable no-param-reassign */
import config from "../../../../../config.js";
import setToken from "../../../../plugins/crud/funcs/setToken.js";
import { handlebarsSync } from "../../../../helpers/index.js";
export default function formatSchema(schema, user, opt) {
function parseDataTables(obj) {
if (obj?.add) {
const obj1 = obj.add?.obj?.includes("{{") && opt
? handlebarsSync.compile(obj.add.obj)(opt)
: obj.add?.obj;
if (obj.add?.obj)
obj.add.obj = obj1;
const [token] = setToken({
ids: [
JSON.stringify({
...obj.add,
table: obj.add?.table || obj.add?.model,
}),
],
uid: user.uid,
array: 1,
});
Object.assign(obj.add, {
token,
api: `${config.prefix || "/api"}/table/${token}`,
});
}
else if (obj?.type === "DataTable" && obj.colModel?.length) {
obj.colModel.forEach(parseDataTables);
}
}
Object.keys(schema || {})
.map((key) => ({ key, ...schema[key] }))
.forEach(parseDataTables);
}