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