91 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-05-18 10:16:03 +02:00
'use strict';
const fields = require('../fields');
2021-06-02 15:53:22 +02:00
const createMetadata = (models = []) => {
2021-05-18 10:16:03 +02:00
/*
primarykey => id only so not needed
timestamps => not optional anymore
options => options are handled on the layer above
filters => not in v1
private fields ? => handled on a different layer
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
*/
const metadata = {};
models.forEach(model => {
const modelMetadata = {
2021-06-02 15:53:22 +02:00
tableName: model.collectionName,
2021-05-18 10:16:03 +02:00
attributes: {
id: {
type: 'integer',
},
createdAt: {
columnName: 'created_at',
default: () => new Date(),
},
updatedAt: {
columnName: 'created_at',
},
2021-06-02 15:53:22 +02:00
...Object.keys(model.attributes)
.map(attributeName => {
const attribute = model.attributes[attributeName];
// if relation
// if compo
// if dz
// if scalar
return {
[attributeName]: {
// find type
type: attribute.type,
default: attribute.default,
// field: fields.get(attribute.type)(attribute), // field type with parser / formatter / validator ...
column: {
columnType: attribute.columnType,
columnName: attribute.columnName,
indexes: {},
unique: Boolean(attribute.unique),
nullable: Boolean(attribute.notNull),
},
},
};
})
.reduce((acc, obj) => Object.assign(acc, obj), {}),
2021-05-18 10:16:03 +02:00
},
};
metadata[model.uid] = modelMetadata;
});
2021-06-02 15:53:22 +02:00
return metadata;
2021-05-18 10:16:03 +02:00
};
module.exports = createMetadata;