-
Notifications
You must be signed in to change notification settings - Fork 283
/
sequelize-service.ts
95 lines (79 loc) · 2.74 KB
/
sequelize-service.ts
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import {basename, extname, join, parse} from "path";
import * as glob from "glob";
import {uniqueFilter} from "../../shared/array";
import {ModelMatch, SequelizeOptions} from "./sequelize-options";
import {ModelCtor} from "../../model/model/model";
/**
* Prepares sequelize config passed to original sequelize constructor
*/
export function prepareOptions(options: SequelizeOptions): SequelizeOptions {
if (options.validateOnly) {
return getValidationOnlyOptions(options);
}
return {...options as SequelizeOptions};
}
export function prepareArgs(...args: any[]) {
const lastArg = args[args.length - 1];
const options = lastArg && typeof lastArg === 'object'
? prepareOptions(lastArg) : undefined;
if (options) {
args[args.length - 1] = options;
}
return {preparedArgs: args, options};
}
function getValidationOnlyOptions(options: SequelizeOptions): SequelizeOptions {
return {
...options,
dialect: 'sqlite',
dialectModulePath: __dirname + '/../validation-only/db-dialect-dummy'
};
}
/**
* Determines models from value
*/
export function getModels(
arg: Array<ModelCtor | string>,
modelMatch: ModelMatch,
): ModelCtor[] {
const hasSupportedExtension = path => ['.ts', '.js'].indexOf(extname(path)) !== -1;
if (arg && typeof arg[0] === 'string') {
return arg.reduce((models: any[], dir) => {
if (!glob.hasMagic(dir) && !hasSupportedExtension(dir)) dir = join(dir as string, '/*');
const _models = glob
.sync(dir as string)
.filter(isImportable)
.map(getFullfilepathWithoutExtension)
.filter(uniqueFilter)
.map(fullPath => {
const module = require(fullPath);
const fileName = basename(fullPath);
const matchedMemberKey = Object.keys(module).find(m => modelMatch(fileName, m));
const matchedMember = matchedMemberKey ? module[matchedMemberKey] : undefined;
if (!matchedMember && !module.default) {
throw new Error(`No default export defined for file "${fileName}" or ` +
`export does not satisfy filename.`);
}
return matchedMember || module.default;
});
models.push(..._models);
return models;
}, [])
;
}
return arg as ModelCtor[];
}
/**
* Checks if specified filename is importable or not;
* Which means that, it needs to have a specific file extension
*/
function isImportable(file: string): boolean {
const filePart = file.slice(-3);
return filePart === '.js' || (filePart === '.ts' && file.slice(-5) !== '.d.ts');
}
/**
* Return the value of the full path with filename, without extension
*/
function getFullfilepathWithoutExtension(file: string): string {
const parsedFile = parse(file);
return join(parsedFile.dir, parsedFile.name);
}