cormo
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -416,6 +416,11 @@ "use strict"; | ||
/** @internal */ | ||
async create(model_name, data, _options) { | ||
async create(model_name, data, options) { | ||
let result; | ||
try { | ||
result = await this._collection(model_name).insertOne(data, { safe: true }); | ||
if (options.use_id_in_data) { | ||
result = await this._collection(model_name).insertOne({ ...data, _id: data.id }, { safe: true }); | ||
} | ||
else { | ||
result = await this._collection(model_name).insertOne(data, { safe: true }); | ||
} | ||
} | ||
@@ -451,3 +456,8 @@ catch (error) { | ||
try { | ||
result = await this._collection(model_name).insertMany(data, { safe: true }); | ||
if (options.use_id_in_data) { | ||
result = await this._collection(model_name).insertMany(data.map((item) => ({ ...item, _id: item.id })), { safe: true }); | ||
} | ||
else { | ||
result = await this._collection(model_name).insertMany(data, { safe: true }); | ||
} | ||
} | ||
@@ -454,0 +464,0 @@ catch (e) { |
@@ -409,3 +409,3 @@ "use strict"; | ||
const values = []; | ||
const [fields, places] = this._buildUpdateSet(model_name, data, values, true); | ||
const [fields, places] = this._buildUpdateSet(model_name, data, values, true, options.use_id_in_data); | ||
const sql = `INSERT INTO \`${table_name}\` (${fields}) VALUES (${places})`; | ||
@@ -439,3 +439,3 @@ let result; | ||
let places_sub; | ||
[fields, places_sub] = this._buildUpdateSet(model_name, item, values, true); | ||
[fields, places_sub] = this._buildUpdateSet(model_name, item, values, true, options.use_id_in_data); | ||
places.push('(' + places_sub + ')'); | ||
@@ -453,3 +453,8 @@ }); | ||
if (id) { | ||
return data.map((item, i) => id + i); | ||
if (options.use_id_in_data) { | ||
return data.map((item) => item.id); | ||
} | ||
else { | ||
return data.map((item, i) => id + i); | ||
} | ||
} | ||
@@ -1073,3 +1078,3 @@ else { | ||
/** @internal */ | ||
_buildUpdateSet(model_name, data, values, insert = false) { | ||
_buildUpdateSet(model_name, data, values, insert = false, use_id_in_data) { | ||
const model_class = this._connection.models[model_name]; | ||
@@ -1089,2 +1094,7 @@ if (!model_class) { | ||
} | ||
if (use_id_in_data && data.id) { | ||
fields.push('id'); | ||
places.push('?'); | ||
values.push(data.id); | ||
} | ||
return [fields.join(','), places.join(',')]; | ||
@@ -1091,0 +1101,0 @@ } |
@@ -288,3 +288,3 @@ "use strict"; | ||
const values = []; | ||
const [fields, places] = this._buildUpdateSet(model_name, data, values, true); | ||
const [fields, places] = this._buildUpdateSet(model_name, data, values, true, options.use_id_in_data); | ||
const sql = `INSERT INTO "${table_name}" (${fields}) VALUES (${places}) RETURNING id`; | ||
@@ -318,3 +318,3 @@ let result; | ||
let places_sub; | ||
[fields, places_sub] = this._buildUpdateSet(model_name, item, values, true); | ||
[fields, places_sub] = this._buildUpdateSet(model_name, item, values, true, options.use_id_in_data); | ||
places.push('(' + places_sub + ')'); | ||
@@ -816,3 +816,3 @@ }); | ||
/** @internal */ | ||
_buildUpdateSet(model_name, data, values, insert = false) { | ||
_buildUpdateSet(model_name, data, values, insert = false, use_id_in_data) { | ||
const model_class = this._connection.models[model_name]; | ||
@@ -832,2 +832,7 @@ if (!model_class) { | ||
} | ||
if (use_id_in_data && data.id) { | ||
values.push(data.id); | ||
fields.push('id'); | ||
places.push('$' + values.length); | ||
} | ||
return [fields.join(','), places.join(',')]; | ||
@@ -834,0 +839,0 @@ } |
@@ -89,3 +89,8 @@ "use strict"; | ||
try { | ||
id = await this._client.incr(`${(0, inflector_1.tableize)(model_name)}:_lastid`); | ||
if (options.use_id_in_data) { | ||
id = data.id; | ||
} | ||
else { | ||
id = await this._client.incr(`${(0, inflector_1.tableize)(model_name)}:_lastid`); | ||
} | ||
} | ||
@@ -92,0 +97,0 @@ catch (error) { |
@@ -250,3 +250,3 @@ "use strict"; | ||
const values = []; | ||
const [fields, places] = this._buildUpdateSet(model_name, data, values, true); | ||
const [fields, places] = this._buildUpdateSet(model_name, data, values, true, options.use_id_in_data); | ||
const sql = `INSERT INTO "${table_name}" (${fields}) VALUES (${places})`; | ||
@@ -287,3 +287,3 @@ let id; | ||
/** @internal */ | ||
async createBulk(model_name, data, _options) { | ||
async createBulk(model_name, data, options) { | ||
const model_class = this._connection.models[model_name]; | ||
@@ -299,3 +299,3 @@ if (!model_class) { | ||
let places_sub; | ||
[fields, places_sub] = this._buildUpdateSet(model_name, item, values, true); | ||
[fields, places_sub] = this._buildUpdateSet(model_name, item, values, true, options.use_id_in_data); | ||
return places.push('(' + places_sub + ')'); | ||
@@ -752,3 +752,3 @@ }); | ||
/** @internal */ | ||
_buildUpdateSet(model_name, data, values, insert = false) { | ||
_buildUpdateSet(model_name, data, values, insert = false, use_id_in_data) { | ||
const model_class = this._connection.models[model_name]; | ||
@@ -768,2 +768,7 @@ if (!model_class) { | ||
} | ||
if (use_id_in_data && data.id) { | ||
fields.push('id'); | ||
places.push('?'); | ||
values.push(data.id); | ||
} | ||
return [fields.join(','), places.join(',')]; | ||
@@ -770,0 +775,0 @@ } |
@@ -121,3 +121,8 @@ /// <reference types="node" /> | ||
*/ | ||
static build<M extends BaseModel>(this: new (data_arg?: any) => M, data?: ModelValueObject<M>): M; | ||
static build<M extends BaseModel>(this: new (data_arg?: any) => M, data: ModelValueObjectWithId<M>, options: { | ||
use_id_in_data: true; | ||
}): M; | ||
static build<M extends BaseModel>(this: new (data_arg?: any) => M, data?: ModelValueObject<M>, options?: { | ||
use_id_in_data?: boolean; | ||
}): M; | ||
/** | ||
@@ -200,5 +205,11 @@ * Deletes all records from the database | ||
*/ | ||
static create<M extends BaseModel>(this: (new (data_arg?: any) => M) & typeof BaseModel, data: ModelValueObjectWithId<M>, options: { | ||
transaction?: Transaction; | ||
skip_log?: boolean; | ||
use_id_in_data: true; | ||
}): Promise<M>; | ||
static create<M extends BaseModel>(this: (new (data_arg?: any) => M) & typeof BaseModel, data?: ModelValueObject<M>, options?: { | ||
transaction?: Transaction; | ||
skip_log?: boolean; | ||
use_id_in_data?: boolean; | ||
}): Promise<M>; | ||
@@ -208,4 +219,9 @@ /** | ||
*/ | ||
static createBulk<M extends BaseModel>(this: (new (data_arg?: any) => M) & typeof BaseModel, data: Array<ModelValueObjectWithId<M>>, options: { | ||
transaction?: Transaction; | ||
use_id_in_data: true; | ||
}): Promise<M[]>; | ||
static createBulk<M extends BaseModel>(this: (new (data_arg?: any) => M) & typeof BaseModel, data?: Array<ModelValueObject<M>>, options?: { | ||
transaction?: Transaction; | ||
use_id_in_data?: boolean; | ||
}): Promise<M[]>; | ||
@@ -354,2 +370,3 @@ /** | ||
validate?: boolean; | ||
use_id_in_data?: boolean; | ||
}): Promise<this>; | ||
@@ -356,0 +373,0 @@ /** |
@@ -205,8 +205,13 @@ "use strict"; | ||
} | ||
/** | ||
* Creates a record. | ||
* 'Model.build(data)' is the same as 'new Model(data)' | ||
*/ | ||
static build(data) { | ||
return new this(data); | ||
static build(data, options) { | ||
const model = new this(data); | ||
if (options?.use_id_in_data && data?.id) { | ||
Object.defineProperty(model, 'id', { | ||
configurable: true, | ||
enumerable: true, | ||
value: data.id, | ||
writable: false, | ||
}); | ||
} | ||
return model; | ||
} | ||
@@ -398,13 +403,6 @@ /** | ||
} | ||
/** | ||
* Creates a record and saves it to the database | ||
* 'Model.create(data)' is the same as 'Model.build(data).save()' | ||
*/ | ||
static async create(data, options) { | ||
await this._checkReady(); | ||
return await this.build(data).save(options); | ||
return await this.build(data, options).save(options); | ||
} | ||
/** | ||
* Creates multiple records and saves them to the database. | ||
*/ | ||
static async createBulk(data, options) { | ||
@@ -419,3 +417,3 @@ await this._checkReady(); | ||
const records = data.map((item) => { | ||
return this.build(item); | ||
return this.build(item, options); | ||
}); | ||
@@ -612,3 +610,6 @@ records.forEach((record) => this.applyDefaultValues(record)); | ||
this._connection.log(this._name, 'createBulk', data_array); | ||
const ids = await this._adapter.createBulk(this._name, data_array, { transaction: options.transaction }); | ||
const ids = await this._adapter.createBulk(this._name, data_array, { | ||
transaction: options.transaction, | ||
use_id_in_data: options.use_id_in_data, | ||
}); | ||
records.forEach((record, i) => { | ||
@@ -968,3 +969,6 @@ Object.defineProperty(record, 'id', { | ||
} | ||
const id = await ctor._adapter.create(ctor._name, data, { transaction: options.transaction || this._transaction }); | ||
const id = await ctor._adapter.create(ctor._name, data, { | ||
transaction: options.transaction || this._transaction, | ||
use_id_in_data: options.use_id_in_data, | ||
}); | ||
Object.defineProperty(this, 'id', { | ||
@@ -971,0 +975,0 @@ configurable: false, |
{ | ||
"name": "cormo", | ||
"description": "ORM framework for Node.js", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"keywords": [ | ||
@@ -69,3 +69,3 @@ "orm", | ||
}, | ||
"gitHead": "012c58745515c8b36ffdb14b772348705b8b7ab7" | ||
"gitHead": "c7a2acac6a47457a8297b167b7d9770733c25490" | ||
} |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
383129
10491
6