@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 1.94 kB
JavaScript
import e from"../../libs/repositories/document-bricks.mjs";import{getFieldDatabaseConfig as t,isStorageMode as n}from"../../libs/collection/custom-fields/storage/index.mjs";import{getBricksTableSchema as r}from"../../libs/collection/schema/runtime/runtime-schema-selectors.mjs";const i=async(i,a)=>{let o=new e(i.db.client,i.config.db),s={},c=await r(i,a.collection.key);if(c.error)return c;for(let e of a.tables){if(e.data.length===0)continue;for(let t of e.data){if(`parent_id_ref`in t&&typeof t.parent_id_ref==`number`&&t.parent_id_ref<0){let e=s[t.parent_id_ref];e?t.parent_id=e:`parent_id`in t&&typeof t.parent_id==`number`&&t.parent_id<0&&s[t.parent_id]&&(t.parent_id=s[t.parent_id])}if(`parent_id`in t&&typeof t.parent_id==`number`&&t.parent_id<0){let e=s[t.parent_id];e&&(t.parent_id=e)}if(`brick_id`in t&&typeof t.brick_id==`number`&&t.brick_id<0){let e=s[t.brick_id];e&&(t.brick_id=e)}}let r=e.data.some(e=>`parent_id_ref`in e),i=e.data.some(e=>`brick_id_ref`in e),a=[];a.push(`id`),r&&a.push(`parent_id_ref`),i&&a.push(`brick_id_ref`);let l=c.data.find(t=>t.name===e.table),u=l?t(l.type):null,d=u!==null&&n(u,`tree-table`),f=await o.createMultiple({data:e.data,returning:a},{tableName:e.table,columns:l?.columns.reduce((e,t)=>(e[t.name]=t.type,e),{})||{}});if(f.error)return f;if(f.data?.length)for(let t=0;t<f.data.length;t++){let n=f.data[t],r=e.data[t];if(!n||typeof n.id!=`number`)continue;let i=!1;`parent_id_ref`in n&&typeof n.parent_id_ref==`number`&&n.parent_id_ref<0&&d&&(s[n.parent_id_ref]=n.id,i=!0),`brick_id_ref`in n&&typeof n.brick_id_ref==`number`&&n.brick_id_ref<0&&(s[n.brick_id_ref]=n.id,i=!0),!(i||!r)&&(`parent_id_ref`in r&&typeof r.parent_id_ref==`number`&&r.parent_id_ref<0&&d&&(s[r.parent_id_ref]=n.id),`brick_id_ref`in r&&typeof r.brick_id_ref==`number`&&r.brick_id_ref<0&&(s[r.brick_id_ref]=n.id))}}return{error:void 0,data:void 0}};export{i as default};
//# sourceMappingURL=insert-brick-tables.mjs.map