@spinajs/orm
Advanced tools
Comparing version 1.2.75 to 1.2.77
@@ -19,2 +19,3 @@ import { RawQuery } from './builders'; | ||
* On duplicate entry ignore & fetch only model primary key | ||
* On duplicate check not only for pkeys but also for unique constrain | ||
*/ | ||
@@ -29,3 +30,4 @@ OnDuplicateIgnore = 0, | ||
*/ | ||
None = 2 | ||
OnDuplicateThrow = 2, | ||
None = 3 | ||
} | ||
@@ -32,0 +34,0 @@ /** |
@@ -25,2 +25,3 @@ "use strict"; | ||
* On duplicate entry ignore & fetch only model primary key | ||
* On duplicate check not only for pkeys but also for unique constrain | ||
*/ | ||
@@ -35,3 +36,4 @@ InsertBehaviour[InsertBehaviour["OnDuplicateIgnore"] = 0] = "OnDuplicateIgnore"; | ||
*/ | ||
InsertBehaviour[InsertBehaviour["None"] = 2] = "None"; | ||
InsertBehaviour[InsertBehaviour["OnDuplicateThrow"] = 2] = "OnDuplicateThrow"; | ||
InsertBehaviour[InsertBehaviour["None"] = 3] = "None"; | ||
})(InsertBehaviour = exports.InsertBehaviour || (exports.InsertBehaviour = {})); | ||
@@ -38,0 +40,0 @@ /** |
@@ -96,2 +96,9 @@ import { IModelDescrtiptor, InsertBehaviour } from './interfaces'; | ||
/** | ||
* | ||
* Attachess model to proper relation an sets foreign key | ||
* | ||
* @param data - model to attach | ||
*/ | ||
attach(data: ModelBase): void; | ||
/** | ||
* Extracts all data from model. It takes only properties that exists in DB | ||
@@ -98,0 +105,0 @@ */ |
@@ -60,2 +60,18 @@ "use strict"; | ||
this[this.PrimaryKeyName] = newVal; | ||
this.ModelDescriptor.Relations.forEach((r) => { | ||
const rel = this[r.Name]; | ||
if (!rel) | ||
return; | ||
switch (r.Type) { | ||
case interfaces_1.RelationType.One: | ||
rel[r.ForeignKey] = newVal; | ||
break; | ||
case interfaces_1.RelationType.Many: | ||
rel.forEach((rVal) => (rVal[r.ForeignKey] = newVal)); | ||
break; | ||
case interfaces_1.RelationType.ManyToMany: | ||
// TODO: rethink this | ||
break; | ||
} | ||
}); | ||
} | ||
@@ -176,2 +192,26 @@ /** | ||
/** | ||
* | ||
* Attachess model to proper relation an sets foreign key | ||
* | ||
* @param data - model to attach | ||
*/ | ||
attach(data) { | ||
// TODO: refactor this, to not check every time for relation | ||
// do this as map or smth | ||
for (const [_, v] of this.ModelDescriptor.Relations.entries()) { | ||
if (v.TargetModel.name === data.constructor.name) { | ||
data[v.ForeignKey] = this.PrimaryKeyValue; | ||
switch (v.Type) { | ||
case interfaces_1.RelationType.One: | ||
this[v.Name] = data; | ||
break; | ||
case interfaces_1.RelationType.Many: | ||
case interfaces_1.RelationType.ManyToMany: | ||
this[v.Name].push(data); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Extracts all data from model. It takes only properties that exists in DB | ||
@@ -184,3 +224,3 @@ */ | ||
const val = this[c.Name]; | ||
if (!c.Nullable && (val === null || val === undefined || val === '')) { | ||
if (c.PrimaryKey && !c.Nullable && (val === null || val === undefined || val === '')) { | ||
throw new exceptions_1.OrmException(`Field ${c.Name} cannot be null`); | ||
@@ -220,2 +260,3 @@ } | ||
async insert(insertBehaviour = interfaces_1.InsertBehaviour.None) { | ||
var _a; | ||
const self = this; | ||
@@ -233,3 +274,12 @@ const { query, description } = _createQuery(this.constructor, builders_1.InsertQueryBuilder); | ||
// ignore fired, we dont have insert ID | ||
if (insertBehaviour !== interfaces_1.InsertBehaviour.None && id === 0 && !this.PrimaryKeyValue) { | ||
if (insertBehaviour === interfaces_1.InsertBehaviour.OnDuplicateThrow && id === 0) { | ||
throw new exceptions_1.OrmException(`Duplicated entry in db for unique keys: ${description.Columns.filter((c) => c.Unique) | ||
.map((c) => { | ||
self[c.Name]; | ||
}) | ||
.join(',')}`); | ||
} | ||
else if (insertBehaviour === interfaces_1.InsertBehaviour.OnDuplicateIgnore && !this.PrimaryKeyValue) { | ||
// if OnDuplicateIgnore is set && we dont have pkey value, refresh pkey | ||
// based on unique column constrains | ||
const { query, description } = _createQuery(this.constructor, builders_1.SelectQueryBuilder, false); | ||
@@ -247,3 +297,3 @@ const idRes = await query | ||
else { | ||
this.PrimaryKeyValue = id; | ||
this.PrimaryKeyValue = (_a = this.PrimaryKeyValue) !== null && _a !== void 0 ? _a : id; | ||
} | ||
@@ -250,0 +300,0 @@ } |
{ | ||
"name": "@spinajs/orm", | ||
"version": "1.2.75", | ||
"version": "1.2.77", | ||
"description": "framework orm module", | ||
@@ -54,3 +54,3 @@ "main": "lib/index.js", | ||
}, | ||
"gitHead": "a50aef463274b1217b09fb37307d2db98d22c209" | ||
"gitHead": "d5de22590f032ca82db60ebb9a7eb9fe7e7688ba" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
308953
5428