aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/models/utils.ts
diff options
context:
space:
mode:
Diffstat (limited to 'server/models/utils.ts')
-rw-r--r--server/models/utils.ts17
1 files changed, 9 insertions, 8 deletions
diff --git a/server/models/utils.ts b/server/models/utils.ts
index 1bf61d2a6..1606453e0 100644
--- a/server/models/utils.ts
+++ b/server/models/utils.ts
@@ -14,22 +14,23 @@ function getSort (value: string) {
14 return [ field, direction ] 14 return [ field, direction ]
15} 15}
16 16
17function addMethodsToModel (model: any, classMethods: Function[], instanceMethods: Function[] = []) {
18 classMethods.forEach(m => model[m.name] = m)
19 instanceMethods.forEach(m => model.prototype[m.name] = m)
20}
21
22function getSortOnModel (model: any, value: string) { 17function getSortOnModel (model: any, value: string) {
23 let sort = getSort(value) 18 let sort = getSort(value)
24 19
25 if (model) return [ { model: model }, sort[0], sort[1] ] 20 if (model) return [ model, sort[0], sort[1] ]
26 return sort 21 return sort
27} 22}
28 23
24function throwIfNotValid (value: any, validator: (value: any) => boolean, fieldName = 'value') {
25 if (validator(value) === false) {
26 throw new Error(`"${value}" is not a valid ${fieldName}.`)
27 }
28}
29
29// --------------------------------------------------------------------------- 30// ---------------------------------------------------------------------------
30 31
31export { 32export {
32 addMethodsToModel,
33 getSort, 33 getSort,
34 getSortOnModel 34 getSortOnModel,
35 throwIfNotValid
35} 36}