cormo
Advanced tools
Comparing version 0.19.1 to 0.19.2
@@ -76,2 +76,8 @@ import { ColumnPropertyInternal } from '../model'; | ||
} | ||
export interface AdapterDeleteOptions { | ||
orders: any[]; | ||
limit?: number; | ||
skip?: number; | ||
transaction?: Transaction; | ||
} | ||
/** | ||
@@ -78,0 +84,0 @@ * Base class for adapters |
@@ -710,8 +710,15 @@ "use strict"; | ||
/** @internal */ | ||
async delete(model, conditions_arg, options) { | ||
const model_class = this._connection.models[model]; | ||
async delete(model_name, conditions_arg, options) { | ||
const model_class = this._connection.models[model_name]; | ||
if (options && (options.orders.length > 0 || options.limit || options.skip)) { | ||
const [conditions_find, fields, orders, client_options] = this._buildConditionsForFind(model_name, conditions_arg, Object.assign(Object.assign({}, options), { lean: true, joins: [], conditions_of_group: [] })); | ||
const cursor = await this._collection(model_name).find(conditions_find, Object.assign(Object.assign({}, client_options), { projection: { _id: 1 } })); | ||
const records = await cursor.toArray(); | ||
const ids = records.map(this._getModelID); | ||
conditions_arg = [{ id: { $in: ids } }]; | ||
} | ||
const conditions = _buildWhere(model_class._schema, conditions_arg); | ||
try { | ||
// console.log(JSON.stringify(conditions)) | ||
const result = await this._collection(model).deleteMany(conditions, { safe: true }); | ||
const result = await this._collection(model_name).deleteMany(conditions, { safe: true }); | ||
return result.deletedCount; | ||
@@ -718,0 +725,0 @@ } |
@@ -620,9 +620,36 @@ "use strict"; | ||
/** @internal */ | ||
async delete(model, conditions, options) { | ||
async delete(model_name, conditions, options) { | ||
const model_class = this._connection.models[model_name]; | ||
const params = []; | ||
const table_name = this._connection.models[model].table_name; | ||
const table_name = model_class.table_name; | ||
let sql = `DELETE FROM \`${table_name}\``; | ||
if (conditions.length > 0) { | ||
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params); | ||
sql += ' WHERE ' + this._buildWhere(model_class._schema, '', {}, conditions, params); | ||
} | ||
if (options && options.orders.length > 0) { | ||
const schema = model_class._schema; | ||
const orders = options.orders.map((order) => { | ||
let column; | ||
if (order[0] === '-') { | ||
column = order.slice(1); | ||
order = 'DESC'; | ||
} | ||
else { | ||
column = order; | ||
order = 'ASC'; | ||
} | ||
column = (schema[column] && schema[column]._dbname_us) || column; | ||
return `\`${column}\` ${order}`; | ||
}); | ||
sql += ' ORDER BY ' + orders.join(','); | ||
} | ||
if (options && options.limit) { | ||
sql += ' LIMIT ' + options.limit; | ||
if (options && options.skip) { | ||
sql += ' OFFSET ' + options.skip; | ||
} | ||
} | ||
else if (options && options.skip) { | ||
sql += ' LIMIT 2147483647 OFFSET ' + options.skip; | ||
} | ||
let result; | ||
@@ -629,0 +656,0 @@ try { |
@@ -464,9 +464,43 @@ "use strict"; | ||
/** @internal */ | ||
async delete(model, conditions, options) { | ||
async delete(model_name, conditions, options) { | ||
const model_class = this._connection.models[model_name]; | ||
const nested = options && (options.orders.length > 0 || options.limit || options.skip); | ||
const params = []; | ||
const table_name = this._connection.models[model].table_name; | ||
const table_name = model_class.table_name; | ||
let sql = `DELETE FROM "${table_name}"`; | ||
if (nested) { | ||
sql += ` WHERE id IN (SELECT id FROM "${table_name}"`; | ||
} | ||
if (conditions.length > 0) { | ||
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params); | ||
sql += ' WHERE ' + this._buildWhere(model_class._schema, '', {}, conditions, params); | ||
} | ||
if (options && options.orders.length > 0) { | ||
const schema = model_class._schema; | ||
const orders = options.orders.map((order) => { | ||
let column; | ||
if (order[0] === '-') { | ||
column = order.slice(1); | ||
order = 'DESC'; | ||
} | ||
else { | ||
column = order; | ||
order = 'ASC'; | ||
} | ||
column = (schema[column] && schema[column]._dbname_us) || column; | ||
return `"${column}" ${order}`; | ||
}); | ||
sql += ' ORDER BY ' + orders.join(','); | ||
} | ||
if (options && options.limit) { | ||
sql += ' LIMIT ' + options.limit; | ||
if (options && options.skip) { | ||
sql += ' OFFSET ' + options.skip; | ||
} | ||
} | ||
else if (options && options.skip) { | ||
sql += ' LIMIT ALL OFFSET ' + options.skip; | ||
} | ||
if (nested) { | ||
sql += ')'; | ||
} | ||
let result; | ||
@@ -473,0 +507,0 @@ try { |
@@ -53,3 +53,3 @@ "use strict"; | ||
async drop(model) { | ||
await this.delete(model, [], {}); | ||
await this.delete(model, [], { orders: [] }); | ||
} | ||
@@ -56,0 +56,0 @@ /** @internal */ |
@@ -453,9 +453,43 @@ "use strict"; | ||
/** @internal */ | ||
async delete(model, conditions, options) { | ||
async delete(model_name, conditions, options) { | ||
const model_class = this._connection.models[model_name]; | ||
const nested = options && (options.orders.length > 0 || options.limit || options.skip); | ||
const params = []; | ||
const table_name = this._connection.models[model].table_name; | ||
const table_name = model_class.table_name; | ||
let sql = `DELETE FROM "${table_name}"`; | ||
if (nested) { | ||
sql += ` WHERE id IN (SELECT id FROM "${table_name}"`; | ||
} | ||
if (conditions.length > 0) { | ||
sql += ' WHERE ' + this._buildWhere(this._connection.models[model]._schema, '', {}, conditions, params); | ||
sql += ' WHERE ' + this._buildWhere(model_class._schema, '', {}, conditions, params); | ||
} | ||
if (options && options.orders.length > 0) { | ||
const schema = model_class._schema; | ||
const orders = options.orders.map((order) => { | ||
let column; | ||
if (order[0] === '-') { | ||
column = order.slice(1); | ||
order = 'DESC'; | ||
} | ||
else { | ||
column = order; | ||
order = 'ASC'; | ||
} | ||
column = (schema[column] && schema[column]._dbname_us) || column; | ||
return `"${column}" ${order}`; | ||
}); | ||
sql += ' ORDER BY ' + orders.join(','); | ||
} | ||
if (options && options.limit) { | ||
sql += ' LIMIT ' + options.limit; | ||
if (options && options.skip) { | ||
sql += ' OFFSET ' + options.skip; | ||
} | ||
} | ||
else if (options && options.skip) { | ||
sql += ' LIMIT 2147483647 OFFSET ' + options.skip; | ||
} | ||
if (nested) { | ||
sql += ')'; | ||
} | ||
try { | ||
@@ -462,0 +496,0 @@ return await new Promise((resolve, reject) => { |
@@ -359,2 +359,3 @@ /// <reference types="node" /> | ||
private _getAdapterFindOptions; | ||
private _getAdapterDeleteOptions; | ||
private _execAndInclude; | ||
@@ -361,0 +362,0 @@ private _validateAndBuildSaveData; |
@@ -423,3 +423,3 @@ "use strict"; | ||
await this._doArchiveAndIntegrity(options); | ||
return await this._adapter.delete(this._name, this._conditions, { transaction: this._options.transaction }); | ||
return await this._adapter.delete(this._name, this._conditions, this._getAdapterDeleteOptions()); | ||
} | ||
@@ -589,2 +589,28 @@ async _exec(find_options, options) { | ||
} | ||
_getAdapterDeleteOptions() { | ||
const orders = []; | ||
if (typeof this._options.orders === 'string') { | ||
const avaliable_columns = ['id']; | ||
avaliable_columns.push(...Object.keys(this._model._schema)); | ||
if (this._options.group_fields) { | ||
avaliable_columns.push(...Object.keys(this._options.group_fields)); | ||
} | ||
this._options.orders.split(/\s+/).forEach((order) => { | ||
let asc = true; | ||
if (order.startsWith('-')) { | ||
asc = false; | ||
order = order.slice(1); | ||
} | ||
if (avaliable_columns.indexOf(order) >= 0) { | ||
orders.push(asc ? order : '-' + order); | ||
} | ||
}); | ||
} | ||
return { | ||
orders, | ||
limit: this._options.limit, | ||
skip: this._options.skip, | ||
transaction: this._options.transaction, | ||
}; | ||
} | ||
async _execAndInclude(options) { | ||
@@ -591,0 +617,0 @@ const records = await this._exec(this._getAdapterFindOptions(), options); |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.19.1", | ||
"version": "0.19.2", | ||
"keywords": [ | ||
@@ -56,3 +56,2 @@ "orm", | ||
"coffeescript": "^2.6.1", | ||
"microtime": "^3.0.0", | ||
"mocha": "^9.2.0", | ||
@@ -59,0 +58,0 @@ "mongodb": "^4.1.4", |
371374
23
10065