style: updated eslint config
This commit is contained in:
parent
aa06808ab2
commit
6786b9b487
@ -23,7 +23,7 @@
|
||||
"devDependencies": {
|
||||
"babel": "5.8.3",
|
||||
"eslint": "2.10.2",
|
||||
"eslint-config-pichak": "1.0.1",
|
||||
"eslint-config-pichak": "1.1.0",
|
||||
"ghooks": "1.0.3",
|
||||
"grunt": "0.4.5",
|
||||
"grunt-babel": "5.0.3",
|
||||
|
@ -14,13 +14,13 @@ export default (server, a, b, names, options) => {
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
let instanceb = await b.findOne({
|
||||
const instanceb = await b.findOne({
|
||||
where: {
|
||||
id: request.params.bid,
|
||||
},
|
||||
});
|
||||
|
||||
let instancea = await a.findOne({
|
||||
const instancea = await a.findOne({
|
||||
where: {
|
||||
id: request.params.aid,
|
||||
},
|
||||
|
@ -73,7 +73,7 @@ export const list = (server, a, b, names) => {
|
||||
};
|
||||
|
||||
export const scope = (server, a, b, names) => {
|
||||
let scopes = Object.keys(b.options.scopes);
|
||||
const scopes = Object.keys(b.options.scopes);
|
||||
|
||||
server.route({
|
||||
method: 'GET',
|
||||
@ -112,7 +112,7 @@ export const scope = (server, a, b, names) => {
|
||||
};
|
||||
|
||||
export const scopeScope = (server, a, b, names) => {
|
||||
let scopes = {
|
||||
const scopes = {
|
||||
a: Object.keys(a.options.scopes),
|
||||
b: Object.keys(b.options.scopes),
|
||||
};
|
||||
@ -126,7 +126,7 @@ export const scopeScope = (server, a, b, names) => {
|
||||
const include = parseInclude(request);
|
||||
const where = parseWhere(request);
|
||||
|
||||
let list = await b.scope(request.params.scopeb).findAll({
|
||||
const list = await b.scope(request.params.scopeb).findAll({
|
||||
where,
|
||||
include: include.concat({
|
||||
model: a.scope(request.params.scopea),
|
||||
@ -175,7 +175,7 @@ export const destroy = (server, a, b, names) => {
|
||||
};
|
||||
|
||||
export const destroyScope = (server, a, b, names) => {
|
||||
let scopes = Object.keys(b.options.scopes);
|
||||
const scopes = Object.keys(b.options.scopes);
|
||||
|
||||
server.route({
|
||||
method: 'DELETE',
|
||||
|
10
src/crud.js
10
src/crud.js
@ -53,7 +53,7 @@ export const get = (server, model) => {
|
||||
async handler(request, reply) {
|
||||
const include = parseInclude(request);
|
||||
const where = parseWhere(request);
|
||||
const {id} = request.params;
|
||||
const { id } = request.params;
|
||||
if (id) where.id = id;
|
||||
|
||||
const instance = await model.findOne({ where, include });
|
||||
@ -73,7 +73,7 @@ export const get = (server, model) => {
|
||||
};
|
||||
|
||||
export const scope = (server, model) => {
|
||||
let scopes = Object.keys(model.options.scopes);
|
||||
const scopes = Object.keys(model.options.scopes);
|
||||
|
||||
server.route({
|
||||
method: 'GET',
|
||||
@ -156,7 +156,7 @@ export const destroyAll = (server, model) => {
|
||||
};
|
||||
|
||||
export const destroyScope = (server, model) => {
|
||||
let scopes = Object.keys(model.options.scopes);
|
||||
const scopes = Object.keys(model.options.scopes);
|
||||
|
||||
server.route({
|
||||
method: 'DELETE',
|
||||
@ -167,7 +167,7 @@ export const destroyScope = (server, model) => {
|
||||
const include = parseInclude(request);
|
||||
const where = parseWhere(request);
|
||||
|
||||
let list = await model.scope(request.params.scope).findAll({ include, where });
|
||||
const list = await model.scope(request.params.scope).findAll({ include, where });
|
||||
|
||||
await Promise.all(list.map(instance => instance.destroy()));
|
||||
|
||||
@ -190,7 +190,7 @@ export const update = (server, model) => {
|
||||
|
||||
@error
|
||||
async handler(request, reply) {
|
||||
const {id} = request.params;
|
||||
const { id } = request.params;
|
||||
const instance = await model.findOne({
|
||||
where: {
|
||||
id,
|
||||
|
@ -1,10 +1,10 @@
|
||||
export default (target, key, descriptor) => {
|
||||
let fn = descriptor.value;
|
||||
const fn = descriptor.value;
|
||||
|
||||
descriptor.value = async (request, reply) => {
|
||||
try {
|
||||
await fn(request, reply);
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
reply(e);
|
||||
}
|
||||
|
43
src/index.js
43
src/index.js
@ -9,8 +9,8 @@ import qs from 'qs';
|
||||
const register = (server, options = {}, next) => {
|
||||
options.prefix = options.prefix || '';
|
||||
|
||||
let db = server.plugins['hapi-sequelize'].db;
|
||||
let models = db.sequelize.models;
|
||||
const db = server.plugins['hapi-sequelize'].db;
|
||||
const models = db.sequelize.models;
|
||||
|
||||
const onRequest = function (request, reply) {
|
||||
const uri = request.raw.req.url;
|
||||
@ -26,9 +26,9 @@ const register = (server, options = {}, next) => {
|
||||
method: onRequest,
|
||||
});
|
||||
|
||||
for (let modelName of Object.keys(models)) {
|
||||
let model = models[modelName];
|
||||
let { plural, singular } = model.options.name;
|
||||
for (const modelName of Object.keys(models)) {
|
||||
const model = models[modelName];
|
||||
const { plural, singular } = model.options.name;
|
||||
model._plural = plural.toLowerCase();
|
||||
model._singular = singular.toLowerCase();
|
||||
|
||||
@ -37,29 +37,30 @@ const register = (server, options = {}, next) => {
|
||||
|
||||
crud(server, model, options);
|
||||
|
||||
for (let key of Object.keys(model.associations)) {
|
||||
let association = model.associations[key];
|
||||
let { source, target } = association;
|
||||
for (const key of Object.keys(model.associations)) {
|
||||
const association = model.associations[key];
|
||||
const { source, target } = association;
|
||||
|
||||
let sourceName = source.options.name;
|
||||
const sourceName = source.options.name;
|
||||
|
||||
const names = (rev) => {
|
||||
const arr = [{
|
||||
plural: sourceName.plural.toLowerCase(),
|
||||
singular: sourceName.singular.toLowerCase(),
|
||||
original: sourceName,
|
||||
}, {
|
||||
plural: association.options.name.plural.toLowerCase(),
|
||||
singular: association.options.name.singular.toLowerCase(),
|
||||
original: association.options.name,
|
||||
}];
|
||||
plural: sourceName.plural.toLowerCase(),
|
||||
singular: sourceName.singular.toLowerCase(),
|
||||
original: sourceName,
|
||||
}, {
|
||||
plural: association.options.name.plural.toLowerCase(),
|
||||
singular: association.options.name.singular.toLowerCase(),
|
||||
original: association.options.name,
|
||||
}];
|
||||
|
||||
return rev ? { b: arr[0], a: arr[1] } : { a: arr[0], b: arr[1] };
|
||||
};
|
||||
|
||||
let targetAssociations = target.associations[sourceName.plural] || target.associations[sourceName.singular];
|
||||
let sourceType = association.associationType,
|
||||
targetType = (targetAssociations || {}).associationType;
|
||||
const targetAssociations = target.associations[sourceName.plural]
|
||||
|| target.associations[sourceName.singular];
|
||||
const sourceType = association.associationType,
|
||||
targetType = (targetAssociations || {}).associationType;
|
||||
|
||||
try {
|
||||
if (sourceType === 'BelongsTo' && (targetType === 'BelongsTo' || !targetType)) {
|
||||
@ -83,7 +84,7 @@ const register = (server, options = {}, next) => {
|
||||
|
||||
associations.associate(server, source, target, names(), options);
|
||||
associations.associate(server, target, source, names(1), options);
|
||||
} catch(e) {
|
||||
} catch (e) {
|
||||
// There might be conflicts in case of models associated with themselves and some other
|
||||
// rare cases.
|
||||
}
|
||||
|
@ -8,10 +8,11 @@ export const parseInclude = request => {
|
||||
const noRequestModels = !request.models;
|
||||
|
||||
if (noGetDb && noRequestModels) {
|
||||
return new Error('`request.getDb` or `request.models` are not defined. Be sure to load hapi-sequelize before hapi-sequelize-crud.');
|
||||
return new Error('`request.getDb` or `request.models` are not defined.'
|
||||
+ 'Be sure to load hapi-sequelize before hapi-sequelize-crud.');
|
||||
}
|
||||
|
||||
const {models} = noGetDb ? request : request.getDb();
|
||||
const { models } = noGetDb ? request : request.getDb();
|
||||
|
||||
return include.map(a => {
|
||||
if (typeof a === 'string') return models[a];
|
||||
|
Loading…
Reference in New Issue
Block a user