microgen-sequelize
Advanced tools
Comparing version 0.0.2 to 0.0.3
146
lib/index.js
const errors = require('@feathersjs/errors'); | ||
const { _ } = require('@feathersjs/commons'); | ||
const { select, AdapterService } = require('@feathersjs/adapter-commons'); | ||
const pluralize = require('pluralize') | ||
@@ -23,3 +24,6 @@ const hooks = require('./hooks'); | ||
$or: Op.or, | ||
$and: Op.and | ||
$overlap: Op.overlap, | ||
$regex: Op.iRegexp, | ||
$contains: Op.contains, | ||
$contained: Op.contained | ||
}; | ||
@@ -48,2 +52,3 @@ }; | ||
this.raw = options.raw !== false; | ||
this.sequelize = Sequelize | ||
} | ||
@@ -78,9 +83,97 @@ | ||
changeKey (object, oldKey, newKey) { | ||
delete Object.assign(object, { [newKey]: object[oldKey] })[oldKey]; | ||
} | ||
manyToManyTransformer (data, option) { | ||
const type = { | ||
'$addToSet': 'array_append', | ||
'$pull': 'array_remove' | ||
} | ||
Object.keys(data[option]).forEach(key => { | ||
let value = data[option][key] | ||
if (typeof value === "object" && !Array.isArray(value)) { | ||
if (value["$in"] && value["$in"].length === 1) { | ||
value = value["$in"][0] | ||
} | ||
} | ||
data[key] = this.sequelize.fn(type[option], this.sequelize.col(key), value) | ||
delete data[option] | ||
}) | ||
} | ||
relationQuery (table, rawKey) { | ||
let key = rawKey.toString().replace("Id", ""); | ||
const isRelation = pluralize.isPlural(key) | ||
if (isRelation) { | ||
key = `${pluralize.singular(key)}Id` | ||
return { | ||
key: `ARRAY["${key}"::TEXT]`, | ||
query: `unnest("${table}"."${rawKey}") AS "${key}"`, | ||
} | ||
} | ||
return false | ||
} | ||
joinQuery (mainQuery, relations) { | ||
const table = this.applyScope({}).getTableName() | ||
if (relations.length > 0) { | ||
relations.forEach((relation) => { | ||
mainQuery = mainQuery.replace(" WHERE", `, ${relation.query} WHERE`) | ||
mainQuery = mainQuery.replace(`"${table}"."${relation.key.replace(/"/g, "")}"`, relation.key) | ||
}) | ||
if (mainQuery.includes("ORDER BY")) { | ||
mainQuery = mainQuery.replace(" ORDER BY", " GROUP BY id ORDER BY") | ||
} else if (mainQuery.includes("LIMIT")) { | ||
mainQuery = mainQuery.replace(" LIMIT", " GROUP BY id LIMIT") | ||
} else if (mainQuery.includes("OFFSET")) { | ||
mainQuery = mainQuery.replace(" OFFSET", " GROUP BY id OFFSET") | ||
} else { | ||
mainQuery = mainQuery.replace(";", " GROUP BY id;") | ||
} | ||
} | ||
if (!mainQuery.includes("ORDER BY")) { | ||
if (mainQuery.includes("LIMIT")) { | ||
mainQuery = mainQuery.replace(" LIMIT", ` ORDER BY "createdAt" ASC LIMIT`) | ||
} else if (mainQuery.includes("OFFSET")) { | ||
mainQuery = mainQuery.replace(" OFFSET", ` ORDER BY "createdAt" ASC OFFSET`) | ||
} else { | ||
mainQuery = mainQuery.replace(";", ` ORDER BY "createdAt" ASC;`) | ||
} | ||
} | ||
return mainQuery | ||
} | ||
filterQuery (params = {}) { | ||
const table = this.applyScope(params).getTableName() | ||
if(params.query) { | ||
delete params.query['$addFields']; | ||
delete params.query['relations']; | ||
// delete params.query['relations']; | ||
// const { relations } = params.query | ||
for(let p in params.query){ | ||
delete params.query[p]['$options']; | ||
if(p == '_id') this.changeKey(params.query, '_id', 'id') | ||
if(p == '$or') { | ||
params.query['$or'].forEach(param => { | ||
for(let p in param){ | ||
delete param[p]['$options']; | ||
} | ||
}) | ||
} | ||
} | ||
} | ||
const filtered = super.filterQuery(params); | ||
const relations = [] | ||
const operators = this.options.operators; | ||
@@ -97,5 +190,20 @@ const convertOperators = query => { | ||
const converted = Object.keys(query).reduce((result, prop) => { | ||
if (prop === "relations") { | ||
return {} | ||
} | ||
const value = query[prop]; | ||
const key = operators[prop] ? operators[prop] : prop; | ||
let key = operators[prop] ? operators[prop] : prop; | ||
const manyRelation = this.relationQuery(table, key); | ||
if (manyRelation) { | ||
key = manyRelation.key | ||
relations.push(manyRelation) | ||
if (value && value["$in"]) { | ||
value["$contained"] = [...value["$in"]] | ||
delete value["$in"] | ||
} | ||
} | ||
result[key] = convertOperators(value); | ||
@@ -114,2 +222,3 @@ | ||
filtered.query = convertOperators(filtered.query); | ||
filtered.relations = relations; | ||
@@ -123,5 +232,7 @@ return filtered; | ||
if (id === null) { | ||
return this._find(Object.assign(params, { | ||
const result = this._find(Object.assign(params, { | ||
paginate: false | ||
})); | ||
return result | ||
} | ||
@@ -133,3 +244,3 @@ | ||
_find (params = {}) { | ||
const { filters, query: where, paginate } = this.filterQuery(params); | ||
const { filters, query: where, paginate, relations } = this.filterQuery(params); | ||
const order = utils.getOrder(filters.$sort); | ||
@@ -146,2 +257,4 @@ | ||
filters.$select = params.select; | ||
if (filters.$select) { | ||
@@ -165,2 +278,5 @@ q.attributes = filters.$select; | ||
let relationQuery = Model.queryGenerator.selectQuery(Model.getTableName(), q, Model) | ||
relationQuery = this.joinQuery(relationQuery, relations) | ||
if (paginate && paginate.default) { | ||
@@ -170,13 +286,11 @@ delete q['distinct']; | ||
return Model.findAndCountAll(q).then(async result => { | ||
return { | ||
total: result.count, | ||
return Model.sequelize.query(relationQuery).then(res => ({ | ||
total: res[0].length, | ||
limit: filters.$limit, | ||
skip: filters.$skip || 0, | ||
data: result.rows | ||
}; | ||
}).catch(utils.errorHandler); | ||
data: res[0] | ||
})).catch(utils.errorHandler); | ||
} | ||
return Model.findAll(q).catch(utils.errorHandler); | ||
return Model.sequelize.query(relationQuery).then(res => res[0]).catch(utils.errorHandler); | ||
} | ||
@@ -264,2 +378,10 @@ | ||
if(data['$addToSet']) { | ||
this.manyToManyTransformer(data, '$addToSet') | ||
} | ||
if(data['$pull']) { | ||
this.manyToManyTransformer(data, '$pull') | ||
} | ||
return Model.update(_.omit(data, this.id), seqOptions) | ||
@@ -266,0 +388,0 @@ .then(() => { |
{ | ||
"name": "microgen-sequelize", | ||
"description": "A service adapter for Sequelize an SQL ORM", | ||
"version": "0.0.2", | ||
"version": "0.0.3", | ||
"homepage": "https://github.com/feathersjs-ecosystem/feathers-sequelize", | ||
@@ -6,0 +6,0 @@ "main": "lib/", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
102185
535
1