2016-01-18 18:08:43 +03:30
|
|
|
import crud, { associations } from './crud';
|
2016-03-02 12:13:27 +03:30
|
|
|
import url from 'url';
|
|
|
|
import qs from 'qs';
|
2016-01-18 18:08:43 +03:30
|
|
|
|
|
|
|
const register = (server, options = {}, next) => {
|
|
|
|
options.prefix = options.prefix || '';
|
|
|
|
|
|
|
|
let db = server.plugins['hapi-sequelize'].db;
|
|
|
|
let models = db.sequelize.models;
|
|
|
|
|
2016-03-02 12:13:27 +03:30
|
|
|
const onRequest = function (request, reply) {
|
|
|
|
const uri = request.raw.req.url;
|
|
|
|
const parsed = url.parse(uri, false);
|
|
|
|
parsed.query = qs.parse(parsed.query);
|
|
|
|
request.setUrl(parsed);
|
|
|
|
|
|
|
|
return reply.continue();
|
|
|
|
};
|
|
|
|
|
|
|
|
server.ext({
|
|
|
|
type: 'onRequest',
|
|
|
|
method: onRequest
|
|
|
|
});
|
|
|
|
|
2016-01-18 18:08:43 +03:30
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2016-01-19 10:03:29 +03:30
|
|
|
if (sourceType === 'BelongsTo' && targetType === 'HasMany') {
|
2016-01-18 18:08:43 +03:30
|
|
|
associations.oneToOne(server, source, target, options);
|
|
|
|
associations.oneToOne(server, target, source, options);
|
|
|
|
associations.oneToMany(server, target, source, options);
|
|
|
|
}
|
2016-01-19 10:03:29 +03:30
|
|
|
|
|
|
|
if (sourceType === 'BelongsToMany' && targetType === 'BelongsToMany') {
|
|
|
|
associations.oneToOne(server, source, target, options);
|
|
|
|
associations.oneToOne(server, target, source, options);
|
|
|
|
|
|
|
|
associations.oneToMany(server, source, target, options);
|
|
|
|
associations.oneToMany(server, target, source, options);
|
|
|
|
}
|
2016-01-20 12:47:47 +03:30
|
|
|
|
|
|
|
associations.associate(server, source, target, options);
|
|
|
|
associations.associate(server, target, source, options);
|
2016-01-18 18:08:43 +03:30
|
|
|
} catch(e) {
|
|
|
|
// There might be conflicts in case of models associated with themselves and some other
|
|
|
|
// rare cases.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
|
|
|
|
register.attributes = {
|
|
|
|
pkg: require('../package.json')
|
|
|
|
}
|
|
|
|
|
|
|
|
export { register };
|