-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathhelper.js
More file actions
31 lines (27 loc) · 837 Bytes
/
helper.js
File metadata and controls
31 lines (27 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import Sequelize from 'sequelize';
export const MODEL = 'MODEL';
export const ASSOCIATION = 'ASSOCIATION';
export const SEQUELIZE = 'SEQUELIZE';
export function methods(version) {
return {
findByPk: /^[56]/.test(version) ? ['findByPk'] :
/^[4]/.test(version) ? ['findByPk', 'findById'] :
['findById', 'findByPrimary'],
findOne: ['findOne'],
};
}
export function method(target, alias) {
if (type(target) === MODEL) {
return methods(target.sequelize.constructor.version)[alias][0];
}
throw new Error('Unknown target');
}
export function type(target) {
if (target.associationType) {
return ASSOCIATION;
} else if (/(SequelizeModel|class extends Model)/.test(target.toString()) || Sequelize.Model.isPrototypeOf(target)) {
return MODEL;
} else {
return SEQUELIZE;
}
}