225 lines
5.8 KiB
JavaScript
Raw Normal View History

2021-07-01 14:32:50 +02:00
/**
* @module metadata
*
*/
2021-05-18 10:16:03 +02:00
'use strict';
2021-06-17 16:17:15 +02:00
const _ = require('lodash/fp');
2021-07-01 14:32:50 +02:00
const types = require('../types');
const { createRelation } = require('./relations');
2021-06-17 16:17:15 +02:00
class Metadata extends Map {
add(meta) {
return this.set(meta.uid, meta);
}
}
2021-05-18 10:16:03 +02:00
2021-07-01 14:32:50 +02:00
/**
* Create Metadata from models configurations
*
* timestamps => not optional anymore but auto added. Auto added on the content type or in the db layer ?
*
* options => options are handled on the layer above. Options convert to fields on the CT
*
* filters => not in v1
*
* attributes
*
* - type
* - mapping field name - column name
* - mapping field type - column type
* - formatter / parser => coming from field type so no
* - indexes / checks / contstraints
* - relations => reference to the target model (function or string to avoid circular deps ?)
* - name of the LEFT/RIGHT side foreign keys
* - name of join table
*
* - compo/dz => reference to the components
* - validators
* - hooks
* - default value
* - required -> should add a not null option instead of the API required
* - unique -> should add a DB unique option instead of the unique in the API (Unique by locale or something else for example)
*
* lifecycles
*
* private fields ? => handled on a different layer
* @param {object[]} models
* @returns {Metadata}
*/
2021-06-02 15:53:22 +02:00
const createMetadata = (models = []) => {
2021-06-17 16:17:15 +02:00
// TODO: reorder to make sure we can create everything or delete everything in the right order
// TODO: allow passing the join config in the attribute
// TODO: allow passing column config in the attribute
const metadata = new Metadata();
2021-05-18 10:16:03 +02:00
2021-06-17 16:17:15 +02:00
// init pass
2021-06-30 22:52:12 +02:00
for (const model of _.cloneDeep(models)) {
2021-06-17 16:17:15 +02:00
metadata.add({
singularName: model.singularName,
uid: model.uid,
tableName: model.tableName,
2021-05-18 10:16:03 +02:00
attributes: {
2021-06-17 16:17:15 +02:00
// TODO: check if there isn't an attribute with an id already
2021-05-18 10:16:03 +02:00
id: {
2021-06-17 16:17:15 +02:00
type: 'increments',
2021-05-18 10:16:03 +02:00
},
2021-06-17 16:17:15 +02:00
...model.attributes,
},
lifecycles: model.lifecycles || {},
2021-06-17 16:17:15 +02:00
});
}
// build compos / relations
for (const meta of metadata.values()) {
if (hasComponentsOrDz(meta)) {
const compoLinkModelMeta = createCompoLinkModelMeta(meta);
meta.componentLink = compoLinkModelMeta;
metadata.add(compoLinkModelMeta);
}
for (const [attributeName, attribute] of Object.entries(meta.attributes)) {
2021-06-18 12:27:47 +02:00
try {
if (types.isComponent(attribute.type)) {
// convert component to relation
Object.assign(attribute, {
type: 'relation',
relation: attribute.repeatable === true ? 'oneToMany' : 'oneToOne',
target: attribute.component,
joinTable: {
name: meta.componentLink.tableName,
joinColumn: {
name: 'entity_id',
referencedColumn: 'id',
},
inverseJoinColumn: {
name: 'component_id',
referencedColumn: 'id',
},
on: {
field: attributeName,
},
2021-06-17 16:17:15 +02:00
},
2021-06-18 12:27:47 +02:00
});
continue;
}
2021-06-17 16:17:15 +02:00
2021-06-18 12:27:47 +02:00
if (types.isDynamicZone(attribute.type)) {
2021-07-21 18:14:16 +02:00
//
Object.assign(attribute, {
type: 'relation',
relation: 'morphToMany',
// TODO: handle restrictions at some point
// target: attribute.components,
2021-07-21 18:14:16 +02:00
joinTable: {
name: meta.componentLink.tableName,
joinColumn: {
name: 'entity_id',
referencedColumn: 'id',
},
morphColumn: {
idColumn: {
name: 'component_id',
referencedColumn: 'id',
},
typeColumn: {
name: 'component_type',
},
2021-07-26 22:02:48 +02:00
typeField: '__component',
2021-07-21 18:14:16 +02:00
},
on: {
field: attributeName,
},
},
});
2021-06-18 12:27:47 +02:00
continue;
}
if (types.isRelation(attribute.type)) {
createRelation(attributeName, attribute, meta, metadata);
continue;
}
} catch (error) {
throw new Error(
`Error on attribute ${attributeName} in model ${meta.singularName}(${meta.uid}): ${error.message}`
);
2021-06-17 16:17:15 +02:00
}
2021-06-18 12:27:47 +02:00
}
}
return metadata;
};
2021-06-17 16:17:15 +02:00
2021-07-01 14:32:50 +02:00
const hasComponentsOrDz = model => {
return Object.values(model.attributes).some(
({ type }) => types.isComponent(type) || types.isDynamicZone(type)
);
2021-05-18 10:16:03 +02:00
};
2021-06-17 16:17:15 +02:00
// NOTE: we might just move the compo logic outside this layer too at some point
const createCompoLinkModelMeta = baseModelMeta => {
return {
// TODO: make sure there can't be any conflicts with a prefix
// singularName: 'compo',
2021-06-25 12:07:32 +02:00
uid: `${baseModelMeta.tableName}_components`,
2021-06-17 16:17:15 +02:00
tableName: `${baseModelMeta.tableName}_components`,
attributes: {
id: {
type: 'increments',
},
entity_id: {
type: 'integer',
column: {
unsigned: true,
},
},
component_id: {
type: 'integer',
column: {
unsigned: true,
},
},
component_type: {
type: 'string',
},
field: {
type: 'string',
},
order: {
type: 'integer',
column: {
unsigned: true,
defaultTo: [0],
},
},
},
indexes: [
{
name: `${baseModelMeta.tableName}_field_index`,
columns: ['field'],
},
{
name: `${baseModelMeta.tableName}_component_type_index`,
columns: ['component_type'],
},
],
foreignKeys: [
{
2021-06-24 18:28:36 +02:00
name: `${baseModelMeta.tableName}_entity_fk`,
2021-06-17 16:17:15 +02:00
columns: ['entity_id'],
referencedColumns: ['id'],
referencedTable: baseModelMeta.tableName,
onDelete: 'CASCADE',
},
],
};
};
2021-05-18 10:16:03 +02:00
module.exports = createMetadata;