cormo
Advanced tools
Comparing version 0.16.6 to 0.16.7
@@ -54,2 +54,7 @@ import { ColumnPropertyInternal } from '../model'; | ||
} | ||
export interface AdapterUpsertOptions { | ||
transaction?: Transaction; | ||
node?: 'master' | 'read'; | ||
ignore_on_update?: string[]; | ||
} | ||
/** | ||
@@ -56,0 +61,0 @@ * Base class for adapters |
@@ -498,5 +498,6 @@ "use strict"; | ||
const update_ops = { | ||
$inc: {}, | ||
$set: {}, | ||
$setOnInsert: {}, | ||
$unset: {}, | ||
$inc: {}, | ||
}; | ||
@@ -507,6 +508,9 @@ for (const key in conditions) { | ||
} | ||
this._buildUpdateOps(schema, update_ops, data, '', data); | ||
this._buildUpdateOps(schema, update_ops, data, '', data, options.ignore_on_update); | ||
if (Object.keys(update_ops.$set).length === 0) { | ||
delete update_ops.$set; | ||
} | ||
if (Object.keys(update_ops.$setOnInsert).length === 0) { | ||
delete update_ops.$setOnInsert; | ||
} | ||
if (Object.keys(update_ops.$unset).length === 0) { | ||
@@ -816,3 +820,3 @@ delete update_ops.$unset; | ||
/** @internal */ | ||
_buildUpdateOps(schema, update_ops, data, path, object) { | ||
_buildUpdateOps(schema, update_ops, data, path, object, ignore_on_update) { | ||
for (const column in object) { | ||
@@ -827,6 +831,16 @@ const value = object[column]; | ||
if (value.$inc != null) { | ||
update_ops.$inc[path + column] = value.$inc; | ||
if (ignore_on_update === null || ignore_on_update === void 0 ? void 0 : ignore_on_update.includes(column)) { | ||
update_ops.$setOnInsert[path + column] = value.$inc; | ||
} | ||
else { | ||
update_ops.$inc[path + column] = value.$inc; | ||
} | ||
} | ||
else { | ||
update_ops.$set[path + column] = value; | ||
if (ignore_on_update === null || ignore_on_update === void 0 ? void 0 : ignore_on_update.includes(column)) { | ||
update_ops.$setOnInsert[path + column] = value; | ||
} | ||
else { | ||
update_ops.$set[path + column] = value; | ||
} | ||
} | ||
@@ -833,0 +847,0 @@ } |
@@ -440,4 +440,6 @@ "use strict"; | ||
async upsert(model, data, conditions, options) { | ||
var _a; | ||
const table_name = this._connection.models[model].table_name; | ||
const insert_data = {}; | ||
const update_data = {}; | ||
for (const key in data) { | ||
@@ -451,2 +453,5 @@ const value = data[key]; | ||
} | ||
if (!((_a = options.ignore_on_update) === null || _a === void 0 ? void 0 : _a.includes(key))) { | ||
update_data[key] = value; | ||
} | ||
} | ||
@@ -462,8 +467,15 @@ for (const condition of conditions) { | ||
let places = ''; | ||
[fields, places] = this._buildUpdateSet(model, insert_data, values, true); | ||
let sql = `INSERT INTO \`${table_name}\` (${fields}) VALUES (${places})`; | ||
[fields] = this._buildPartialUpdateSet(model, data, values); | ||
sql += ` ON DUPLICATE KEY UPDATE ${fields}`; | ||
let sql = ''; | ||
if (Object.keys(update_data).length === 0) { | ||
[fields, places] = this._buildUpdateSet(model, insert_data, values, true); | ||
sql = `INSERT IGNORE \`${table_name}\` (${fields}) VALUES (${places})`; | ||
} | ||
else { | ||
[fields, places] = this._buildUpdateSet(model, insert_data, values, true); | ||
sql = `INSERT INTO \`${table_name}\` (${fields}) VALUES (${places})`; | ||
[fields] = this._buildPartialUpdateSet(model, update_data, values); | ||
sql += ` ON DUPLICATE KEY UPDATE ${fields}`; | ||
} | ||
try { | ||
await this.query(sql, values, options.transaction); | ||
await this.query(sql, values, { transaction: options.transaction, node: options.node }); | ||
} | ||
@@ -470,0 +482,0 @@ catch (error) { |
@@ -47,7 +47,5 @@ "use strict"; | ||
async upsert(model, data, conditions, options) { | ||
const count = await this.updatePartial(model, data, conditions, options); | ||
if (count > 0) { | ||
return; | ||
} | ||
var _a; | ||
const insert_data = {}; | ||
const update_data = {}; | ||
for (const key in data) { | ||
@@ -61,2 +59,5 @@ const value = data[key]; | ||
} | ||
if (!((_a = options.ignore_on_update) === null || _a === void 0 ? void 0 : _a.includes(key))) { | ||
update_data[key] = value; | ||
} | ||
} | ||
@@ -69,10 +70,26 @@ for (const condition of conditions) { | ||
} | ||
try { | ||
return await this.create(model, insert_data, {}); | ||
if (Object.keys(update_data).length === 0) { | ||
try { | ||
return await this.create(model, insert_data, {}); | ||
} | ||
catch (error) { | ||
if (!/duplicated/.test(error.message)) { | ||
throw error; | ||
} | ||
} | ||
} | ||
catch (error) { | ||
if (!/duplicated/.test(error.message)) { | ||
throw error; | ||
else { | ||
const count = await this.updatePartial(model, update_data, conditions, options); | ||
if (count > 0) { | ||
return; | ||
} | ||
return await this.updatePartial(model, data, conditions, options); | ||
try { | ||
return await this.create(model, insert_data, {}); | ||
} | ||
catch (error) { | ||
if (!/duplicated/.test(error.message)) { | ||
throw error; | ||
} | ||
return await this.updatePartial(model, update_data, conditions, options); | ||
} | ||
} | ||
@@ -79,0 +96,0 @@ } |
@@ -355,4 +355,4 @@ /// <reference types="node" /> | ||
private _update; | ||
private _applyDefaultValues; | ||
static applyDefaultValues(obj: any): string[]; | ||
} | ||
export { BaseModel }; |
@@ -488,3 +488,3 @@ "use strict"; | ||
}); | ||
records.forEach((record) => record._applyDefaultValues()); | ||
records.forEach((record) => this.applyDefaultValues(record)); | ||
await Promise.all(records.map((record) => record.validate())); | ||
@@ -833,3 +833,3 @@ for (const record of records) { | ||
if (!this._is_persisted) { | ||
this._applyDefaultValues(); | ||
ctor.applyDefaultValues(this); | ||
} | ||
@@ -1007,21 +1007,21 @@ if (options.validate !== false) { | ||
} | ||
_applyDefaultValues() { | ||
const ctor = this.constructor; | ||
// apply default values | ||
const schema = ctor._schema; | ||
for (const column in schema) { | ||
const property = schema[column]; | ||
if (property.primary_key) { | ||
static applyDefaultValues(obj) { | ||
const applied_columns = []; | ||
for (const column in this._schema) { | ||
const property = this._schema[column]; | ||
if (property.primary_key || property.default_value == null) { | ||
continue; | ||
} | ||
const value = util.getPropertyOfPath(this, property._parts); | ||
if (value == null && property.default_value !== undefined) { | ||
const value = util.getPropertyOfPath(obj, property._parts); | ||
if (value == null) { | ||
if (lodash_1.default.isFunction(property.default_value)) { | ||
util.setPropertyOfPath(this, property._parts, property.default_value()); | ||
util.setPropertyOfPath(obj, property._parts, property.default_value()); | ||
} | ||
else { | ||
util.setPropertyOfPath(this, property._parts, property.default_value); | ||
util.setPropertyOfPath(obj, property._parts, property.default_value); | ||
} | ||
applied_columns.push(property._dbname_dot); | ||
} | ||
} | ||
return applied_columns; | ||
} | ||
@@ -1028,0 +1028,0 @@ } |
@@ -64,3 +64,5 @@ /// <reference types="node" /> | ||
update(updates: object): PromiseLike<number>; | ||
upsert(updates: object): PromiseLike<void>; | ||
upsert(updates: object, options?: { | ||
ignore_on_update: string[]; | ||
}): PromiseLike<void>; | ||
delete(options?: any): PromiseLike<number>; | ||
@@ -104,3 +106,5 @@ } | ||
update(updates: object): PromiseLike<number>; | ||
upsert(updates: object): PromiseLike<void>; | ||
upsert(updates: object, options?: { | ||
ignore_on_update: string[]; | ||
}): PromiseLike<void>; | ||
delete(options?: any): PromiseLike<number>; | ||
@@ -144,3 +148,5 @@ } | ||
update(updates: object): PromiseLike<number>; | ||
upsert(updates: object): PromiseLike<void>; | ||
upsert(updates: object, options?: { | ||
ignore_on_update: string[]; | ||
}): PromiseLike<void>; | ||
delete(options?: any): PromiseLike<number>; | ||
@@ -285,3 +291,5 @@ } | ||
*/ | ||
upsert(updates: any): Promise<void>; | ||
upsert(updates: any, options?: { | ||
ignore_on_update: string[]; | ||
}): Promise<void>; | ||
/** | ||
@@ -288,0 +296,0 @@ * Executes the query as a delete operation |
@@ -344,3 +344,4 @@ "use strict"; | ||
*/ | ||
async upsert(updates) { | ||
async upsert(updates, options) { | ||
var _a; | ||
this._setUsed(); | ||
@@ -358,4 +359,6 @@ await this._model._checkReady(); | ||
} | ||
this._connection.log(this._name, 'upsert', { data, conditions: this._conditions, options: this._options }); | ||
return await this._adapter.upsert(this._name, data, this._conditions, this._options); | ||
const default_applied_columns = this._model.applyDefaultValues(data); | ||
options = Object.assign(Object.assign({}, this._options), { ignore_on_update: ((_a = options === null || options === void 0 ? void 0 : options.ignore_on_update) !== null && _a !== void 0 ? _a : []).concat(default_applied_columns) }); | ||
this._connection.log(this._name, 'upsert', { data, conditions: this._conditions, options }); | ||
return await this._adapter.upsert(this._name, data, this._conditions, options); | ||
} | ||
@@ -362,0 +365,0 @@ /** |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "0.16.6", | ||
"version": "0.16.7", | ||
"keywords": [ | ||
@@ -71,3 +71,3 @@ "orm", | ||
}, | ||
"gitHead": "41ba662e20d840c38481c1bdf26d0ed7a1a80a86" | ||
"gitHead": "6634478b992a10448b3735231cea56093942662b" | ||
} |
349223
9506