UNPKG

3.07 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getModels = exports.prepareArgs = exports.prepareOptions = void 0;
4const path_1 = require("path");
5const glob = require("glob");
6const array_1 = require("../../shared/array");
7/**
8 * Prepares sequelize config passed to original sequelize constructor
9 */
10function prepareOptions(options) {
11 if (options.validateOnly) {
12 return getValidationOnlyOptions(options);
13 }
14 return Object.assign({}, options);
15}
16exports.prepareOptions = prepareOptions;
17function prepareArgs(...args) {
18 const lastArg = args[args.length - 1];
19 const options = lastArg && typeof lastArg === 'object' ? prepareOptions(lastArg) : undefined;
20 if (options) {
21 args[args.length - 1] = options;
22 }
23 return { preparedArgs: args, options };
24}
25exports.prepareArgs = prepareArgs;
26function getValidationOnlyOptions(options) {
27 return Object.assign(Object.assign({}, options), { dialect: 'sqlite', dialectModulePath: __dirname + '/../validation-only/db-dialect-dummy' });
28}
29/**
30 * Determines models from value
31 */
32function getModels(arg, modelMatch) {
33 const hasSupportedExtension = (path) => ['.ts', '.js'].indexOf((0, path_1.extname)(path)) !== -1;
34 if (arg && typeof arg[0] === 'string') {
35 return arg.reduce((models, dir) => {
36 if (!glob.hasMagic(dir) && !hasSupportedExtension(dir))
37 dir = (0, path_1.join)(dir, '/*');
38 const _models = glob
39 .sync(dir)
40 .filter(isImportable)
41 .map(getFullfilepathWithoutExtension)
42 .filter(array_1.uniqueFilter)
43 .map((fullPath) => {
44 // eslint-disable-next-line @typescript-eslint/no-var-requires
45 const module = require(fullPath);
46 const fileName = (0, path_1.basename)(fullPath);
47 const matchedMemberKey = Object.keys(module).find((m) => modelMatch(fileName, m));
48 const matchedMember = matchedMemberKey ? module[matchedMemberKey] : undefined;
49 if (!matchedMember && !module.default) {
50 throw new Error(`No default export defined for file "${fileName}" or ` +
51 `export does not satisfy filename.`);
52 }
53 return matchedMember || module.default;
54 });
55 models.push(..._models);
56 return models;
57 }, []);
58 }
59 return arg;
60}
61exports.getModels = getModels;
62/**
63 * Checks if specified filename is importable or not;
64 * Which means that, it needs to have a specific file extension
65 */
66function isImportable(file) {
67 const filePart = file.slice(-3);
68 return filePart === '.js' || (filePart === '.ts' && file.slice(-5) !== '.d.ts');
69}
70/**
71 * Return the value of the full path with filename, without extension
72 */
73function getFullfilepathWithoutExtension(file) {
74 const parsedFile = (0, path_1.parse)(file);
75 return (0, path_1.join)(parsedFile.dir, parsedFile.name);
76}
77//# sourceMappingURL=sequelize-service.js.map
\No newline at end of file