feat(simple): simple CRUD REST API (no associations)
feat(associations): one-to-one associations feat(associations): one-to-many associations
This commit is contained in:
60
src/index.js
Normal file
60
src/index.js
Normal file
@ -0,0 +1,60 @@
|
||||
import crud, { associations } from './crud';
|
||||
|
||||
const register = (server, options = {}, next) => {
|
||||
options.prefix = options.prefix || '';
|
||||
|
||||
let db = server.plugins['hapi-sequelize'].db;
|
||||
let models = db.sequelize.models;
|
||||
|
||||
for (let modelName of Object.keys(models)) {
|
||||
let model = models[modelName];
|
||||
let { plural, singular } = model.options.name;
|
||||
model._plural = plural.toLowerCase();
|
||||
model._singular = singular.toLowerCase();
|
||||
|
||||
// Join tables
|
||||
if (model.options.name.singular !== model.name) continue;
|
||||
|
||||
crud(server, model, options);
|
||||
|
||||
for (let key of Object.keys(model.associations)) {
|
||||
let association = model.associations[key];
|
||||
let { associationType, source, target } = association;
|
||||
|
||||
let sourceName = source.options.name;
|
||||
let targetName = target.options.name;
|
||||
|
||||
target._plural = targetName.plural.toLowerCase();
|
||||
target._singular = targetName.singular.toLowerCase();
|
||||
|
||||
let targetAssociations = target.associations[sourceName.plural] || target.associations[sourceName.singular];
|
||||
let sourceType = association.associationType,
|
||||
targetType = (targetAssociations || {}).associationType;
|
||||
|
||||
try {
|
||||
if (sourceType === 'BelongsTo' && (targetType === 'BelongsTo' || !targetType)) {
|
||||
associations.oneToOne(server, source, target, options);
|
||||
associations.oneToOne(server, target, source, options);
|
||||
}
|
||||
|
||||
if (sourceType === 'BelongsTo' && (targetType === 'HasMany')) {
|
||||
associations.oneToOne(server, source, target, options);
|
||||
associations.oneToOne(server, target, source, options);
|
||||
associations.oneToMany(server, target, source, options);
|
||||
}
|
||||
} catch(e) {
|
||||
// There might be conflicts in case of models associated with themselves and some other
|
||||
// rare cases.
|
||||
}
|
||||
console.log(sourceName.singular, sourceType, targetName.singular, ' & ', targetName.singular, targetType, sourceName.singular);
|
||||
}
|
||||
}
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
register.attributes = {
|
||||
pkg: require('../package.json')
|
||||
}
|
||||
|
||||
export { register };
|
Reference in New Issue
Block a user