diff options
author | Chocobozzz <me@florianbigard.com> | 2017-12-12 17:53:50 +0100 |
---|---|---|
committer | Chocobozzz <me@florianbigard.com> | 2017-12-13 16:50:33 +0100 |
commit | 3fd3ab2d34d512b160a5e6084d7609be7b4f4452 (patch) | |
tree | e5ca358287fca6ecacce83defcf23af1e8e9f419 /server/models/utils.ts | |
parent | c893d4514e6ecbf282c7985fe5f82b8acd8a1137 (diff) | |
download | PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.gz PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.tar.zst PeerTube-3fd3ab2d34d512b160a5e6084d7609be7b4f4452.zip |
Move models to typescript-sequelize
Diffstat (limited to 'server/models/utils.ts')
-rw-r--r-- | server/models/utils.ts | 17 |
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 | ||
17 | function 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 | |||
22 | function getSortOnModel (model: any, value: string) { | 17 | function 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 | ||
24 | function 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 | ||
31 | export { | 32 | export { |
32 | addMethodsToModel, | ||
33 | getSort, | 33 | getSort, |
34 | getSortOnModel | 34 | getSortOnModel, |
35 | throwIfNotValid | ||
35 | } | 36 | } |