orange-dragonfly-model 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/components/model.js +28 -0
- package/package.json +1 -1
package/components/model.js
CHANGED
|
@@ -12,6 +12,14 @@ class Model extends ORM.ActiveRecord {
|
|
|
12
12
|
return false
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Returns list of unique keys
|
|
17
|
+
* @returns Array[] List of unique keys
|
|
18
|
+
*/
|
|
19
|
+
static get UNIQUE_KEYS () {
|
|
20
|
+
return []
|
|
21
|
+
}
|
|
22
|
+
|
|
15
23
|
/**
|
|
16
24
|
* Returns schema for the model (Orange Dragonfly Validator format)
|
|
17
25
|
* @return object
|
|
@@ -158,6 +166,25 @@ class Model extends ORM.ActiveRecord {
|
|
|
158
166
|
return await this.save(new_data)
|
|
159
167
|
}
|
|
160
168
|
|
|
169
|
+
/**
|
|
170
|
+
* Checks uniqueness of the object based on UNIQUE_KEYS
|
|
171
|
+
*/
|
|
172
|
+
async checkUniqueness (exception_mode = false) {
|
|
173
|
+
for (const fields of this.constructor.UNIQUE_KEYS) {
|
|
174
|
+
if (!await this.isUnique(fields)) {
|
|
175
|
+
if (exception_mode) {
|
|
176
|
+
const ex = new ValidationException('Object is not unique')
|
|
177
|
+
for (const field of fields) {
|
|
178
|
+
ex.info[field] = 'Part of the unique key'
|
|
179
|
+
}
|
|
180
|
+
throw ex
|
|
181
|
+
}
|
|
182
|
+
return false
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return true
|
|
186
|
+
}
|
|
187
|
+
|
|
161
188
|
async _preSave () {
|
|
162
189
|
if (this.constructor.IGNORE_EXTRA_FIELDS) {
|
|
163
190
|
const rules = this.constructor.validation_rules
|
|
@@ -166,6 +193,7 @@ class Model extends ORM.ActiveRecord {
|
|
|
166
193
|
}
|
|
167
194
|
}
|
|
168
195
|
await super._preSave()
|
|
196
|
+
await this.checkUniqueness(true)
|
|
169
197
|
await this.validate()
|
|
170
198
|
}
|
|
171
199
|
|