@sdflc/backend-helpers 1.0.49 → 1.0.50

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,46 +8,33 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
39
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
40
13
  };
41
14
  Object.defineProperty(exports, "__esModule", { value: true });
42
15
  exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
43
- var bluebird_1 = __importDefault(require("bluebird"));
44
- var lodash_1 = require("lodash");
45
- var SQLS = {
16
+ const bluebird_1 = __importDefault(require("bluebird"));
17
+ const lodash_1 = require("lodash");
18
+ const SQLS = {
46
19
  pg: {
47
- listTablesSchema: function (appSettings) { return "\n SELECT TABLE_NAME as table_name\n FROM INFORMATION_SCHEMA.TABLES\n WHERE TABLE_SCHEMA = '".concat(appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema, "';\n "); },
48
- listTables: function () { return "\n SELECT TABLE_NAME as table_name\n FROM INFORMATION_SCHEMA.TABLES\n "; },
49
- dropTablesSchema: function (appSettings) { return "\n SELECT 'DROP TABLE IF EXISTS ".concat(appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema, ".' || TABLE_NAME || ';' as cmd\n FROM INFORMATION_SCHEMA.TABLES\n WHERE TABLE_SCHEMA = '").concat(appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema, "';\n "); },
50
- dropTables: function () { return "\n SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd\n FROM INFORMATION_SCHEMA.TABLES';\n "; },
20
+ listTablesSchema: (appSettings) => `
21
+ SELECT TABLE_NAME as table_name
22
+ FROM INFORMATION_SCHEMA.TABLES
23
+ WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
24
+ `,
25
+ listTables: () => `
26
+ SELECT TABLE_NAME as table_name
27
+ FROM INFORMATION_SCHEMA.TABLES
28
+ `,
29
+ dropTablesSchema: (appSettings) => `
30
+ SELECT 'DROP TABLE IF EXISTS ${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}.' || TABLE_NAME || ';' as cmd
31
+ FROM INFORMATION_SCHEMA.TABLES
32
+ WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
33
+ `,
34
+ dropTables: () => `
35
+ SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
36
+ FROM INFORMATION_SCHEMA.TABLES';
37
+ `,
51
38
  },
52
39
  };
53
40
  /**
@@ -58,28 +45,19 @@ var SQLS = {
58
45
  * @param knexHandle Knex handle object with configured connection to DB
59
46
  * @param appSettings settings with db related information
60
47
  */
61
- var dbCleanTable = function (table, knexHandle, appSettings) { return __awaiter(void 0, void 0, void 0, function () {
62
- var query, cleanTableFilters;
48
+ const dbCleanTable = (table, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
63
49
  var _a;
64
- return __generator(this, function (_b) {
65
- switch (_b.label) {
66
- case 0:
67
- query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
68
- cleanTableFilters = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTableFilters) === null || _a === void 0 ? void 0 : _a[table];
69
- if (cleanTableFilters != undefined) {
70
- if (Array.isArray(cleanTableFilters.where)) {
71
- cleanTableFilters.where.forEach(function (whereItem) {
72
- query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
73
- });
74
- }
75
- }
76
- return [4 /*yield*/, query];
77
- case 1:
78
- _b.sent();
79
- return [2 /*return*/];
50
+ const query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
51
+ const cleanTableFilters = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTableFilters) === null || _a === void 0 ? void 0 : _a[table];
52
+ if (cleanTableFilters != undefined) {
53
+ if (Array.isArray(cleanTableFilters.where)) {
54
+ cleanTableFilters.where.forEach((whereItem) => {
55
+ query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
56
+ });
80
57
  }
81
- });
82
- }); };
58
+ }
59
+ yield query;
60
+ });
83
61
  exports.dbCleanTable = dbCleanTable;
84
62
  /**
85
63
  * Returns a list of tables in db schedm specified in settings
@@ -87,25 +65,17 @@ exports.dbCleanTable = dbCleanTable;
87
65
  * @param appSettings settings with db related information
88
66
  * @returns arrays of table names in the provided db schema
89
67
  */
90
- var dbListTables = function (knexHandle, appSettings) { return __awaiter(void 0, void 0, void 0, function () {
91
- var skipTables, query, tables;
92
- return __generator(this, function (_a) {
93
- switch (_a.label) {
94
- case 0:
95
- skipTables = (appSettings || {}).skipTables || [];
96
- query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
97
- if (!appSettings.dbSchema) {
98
- query = SQLS[appSettings.dbType].listTables(appSettings);
99
- }
100
- return [4 /*yield*/, knexHandle.raw(query)];
101
- case 1:
102
- tables = _a.sent();
103
- return [2 /*return*/, (tables.rows || [])
104
- .map(function (row) { return row.table_name; })
105
- .filter(function (table) { return skipTables.indexOf(table) === -1; })];
106
- }
107
- });
108
- }); };
68
+ const dbListTables = (knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
69
+ const skipTables = (appSettings || {}).skipTables || [];
70
+ let query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
71
+ if (!appSettings.dbSchema) {
72
+ query = SQLS[appSettings.dbType].listTables(appSettings);
73
+ }
74
+ const tables = yield knexHandle.raw(query);
75
+ return (tables.rows || [])
76
+ .map((row) => row.table_name)
77
+ .filter((table) => skipTables.indexOf(table) === -1);
78
+ });
109
79
  exports.dbListTables = dbListTables;
110
80
  /**
111
81
  * Removes all the records from provided list of tables
@@ -114,52 +84,38 @@ exports.dbListTables = dbListTables;
114
84
  * @param appSettings settings with db related information
115
85
  * @returns promise
116
86
  */
117
- var dbClean = function (tablesToClean, knexHandle, appSettings) { return __awaiter(void 0, void 0, void 0, function () {
118
- var idx, len;
119
- return __generator(this, function (_a) {
120
- switch (_a.label) {
121
- case 0:
122
- idx = 0, len = tablesToClean.length;
123
- _a.label = 1;
124
- case 1:
125
- if (!(idx < len)) return [3 /*break*/, 4];
126
- return [4 /*yield*/, dbCleanTable(tablesToClean[idx], knexHandle, appSettings)];
127
- case 2:
128
- _a.sent();
129
- _a.label = 3;
130
- case 3:
131
- ++idx;
132
- return [3 /*break*/, 1];
133
- case 4: return [2 /*return*/, new bluebird_1.default(function (resolve) {
134
- resolve(tablesToClean);
135
- })];
136
- }
87
+ const dbClean = (tablesToClean, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
88
+ for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
89
+ yield dbCleanTable(tablesToClean[idx], knexHandle, appSettings);
90
+ }
91
+ return new bluebird_1.default((resolve) => {
92
+ resolve(tablesToClean);
137
93
  });
138
- }); };
94
+ });
139
95
  exports.dbClean = dbClean;
140
96
  /**
141
97
  * Selects all the tables in the schema and drops them.
142
98
  * This approach does not work in case we have constraints.
143
99
  * TODO: Remove all constraints and then drop tables.
144
100
  */
145
- var dbWipe = function (knexHandle, appSettings) {
146
- var query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
101
+ const dbWipe = (knexHandle, appSettings) => {
102
+ let query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
147
103
  if (!appSettings.dbSchema) {
148
104
  query = SQLS[appSettings.dbType].dropTables(appSettings);
149
105
  }
150
106
  return knexHandle
151
107
  .raw(query)
152
- .then(function (result) { return result.rows.map(function (r) { return r.cmd; }); })
153
- .then(function (commands) { return bluebird_1.default.each(commands, function (cmd) { return knexHandle.raw(cmd); }); });
108
+ .then((result) => result.rows.map((r) => r.cmd))
109
+ .then((commands) => bluebird_1.default.each(commands, (cmd) => knexHandle.raw(cmd)));
154
110
  };
155
111
  exports.dbWipe = dbWipe;
156
112
  /**
157
113
  * Drop all tables, run migrations, then seed
158
114
  */
159
- var dbReset = function (knexHandle, appSettings) {
115
+ const dbReset = (knexHandle, appSettings) => {
160
116
  return dbWipe(knexHandle, appSettings)
161
- .then(function () { return dbRunMigrations(knexHandle, appSettings); })
162
- .catch(function (error) { return console.log('An exception has occurred on resetting database: ', error); });
117
+ .then(() => dbRunMigrations(knexHandle, appSettings))
118
+ .catch((error) => console.log('An exception has occurred on resetting database: ', error));
163
119
  };
164
120
  exports.dbReset = dbReset;
165
121
  /**
@@ -168,33 +124,23 @@ exports.dbReset = dbReset;
168
124
  * ie from [{table1: [], table2: [] }, { table3: [] }]
169
125
  * to { table1: [], table2: [], table3: []}
170
126
  */
171
- var dbTransformSeedsToObject = function (generatedSeedArrays) {
172
- return (0, lodash_1.reduce)(generatedSeedArrays, function (acc, cur) {
173
- for (var key in cur) {
174
- acc[key] = cur[key];
175
- }
176
- return acc;
177
- }, {});
178
- };
127
+ const dbTransformSeedsToObject = (generatedSeedArrays) => (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
128
+ for (const key in cur) {
129
+ acc[key] = cur[key];
130
+ }
131
+ return acc;
132
+ }, {});
179
133
  exports.dbTransformSeedsToObject = dbTransformSeedsToObject;
180
- var dbRunMigrations = function (knexHandle, appSettings) {
134
+ const dbRunMigrations = (knexHandle, appSettings) => {
181
135
  return knexHandle.migrate
182
136
  .latest()
183
- .then(function (result) { return __awaiter(void 0, void 0, void 0, function () {
184
- return __generator(this, function (_a) {
185
- switch (_a.label) {
186
- case 0:
187
- if (!(appSettings && appSettings.runDbSeed && result[1].length > 0)) return [3 /*break*/, 2];
188
- return [4 /*yield*/, knexHandle.seed.run()];
189
- case 1:
190
- _a.sent();
191
- _a.label = 2;
192
- case 2:
193
- console.log('Ran', result[1].length, 'migration(s)');
194
- return [2 /*return*/, knexHandle.migrate.currentVersion()];
195
- }
196
- });
197
- }); })
198
- .then(function (version) { return console.log('Current version of database: ', version); });
137
+ .then((result) => __awaiter(void 0, void 0, void 0, function* () {
138
+ if (appSettings && appSettings.runDbSeed && result[1].length > 0) {
139
+ yield knexHandle.seed.run();
140
+ }
141
+ console.log('Ran', result[1].length, 'migration(s)');
142
+ return knexHandle.migrate.currentVersion();
143
+ }))
144
+ .then((version) => console.log('Current version of database: ', version));
199
145
  };
200
146
  exports.dbRunMigrations = dbRunMigrations;
package/build/dbSeeder.js CHANGED
@@ -31,104 +31,52 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
33
  };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (_) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
34
  var __importDefault = (this && this.__importDefault) || function (mod) {
62
35
  return (mod && mod.__esModule) ? mod : { "default": mod };
63
36
  };
64
37
  Object.defineProperty(exports, "__esModule", { value: true });
65
38
  exports.dbSeed = void 0;
66
- var path_1 = __importDefault(require("path"));
67
- var lodash_1 = require("lodash");
68
- var dbHelpers_1 = require("./dbHelpers");
69
- var generateSeeds = function (dirname, seedFiles) { return __awaiter(void 0, void 0, void 0, function () {
70
- var seedGroups, seedFileNames, seedFnExports, seedFns, seedFnsMap, i, seedFileName;
71
- return __generator(this, function (_a) {
72
- switch (_a.label) {
73
- case 0:
74
- seedGroups = [];
75
- seedFileNames = (0, lodash_1.flatten)(seedFiles);
76
- return [4 /*yield*/, Promise.all((0, lodash_1.map)(seedFileNames, function (filename) { return Promise.resolve().then(function () { return __importStar(require(path_1.default.join(dirname, "./objects/".concat(filename, ".js")))); }); }))];
77
- case 1:
78
- seedFnExports = _a.sent();
79
- seedFns = (0, lodash_1.map)(seedFnExports, 'default');
80
- seedFnsMap = {};
81
- for (i = 0; i < seedFileNames.length; i++) {
82
- seedFileName = seedFileNames[i];
83
- seedFnsMap[seedFileName] = seedFns[i];
84
- }
85
- seedFiles.reduce(function (seeds, fileGroup) {
86
- seedGroups = seedGroups.concat(fileGroup.reduce(function (group, filename) {
87
- var _a;
88
- var seedFn = seedFnsMap[filename];
89
- var seed = seedFn(seeds);
90
- var filenameCamel = (0, lodash_1.camelCase)(filename);
91
- seeds[filenameCamel] = seed;
92
- return Object.assign((_a = {}, _a[filenameCamel] = seed, _a), group);
93
- }, {}));
94
- return seeds;
95
- }, {});
96
- return [2 /*return*/, seedGroups];
97
- }
98
- });
99
- }); };
100
- var removeSeedsFromDb = function (seedFiles, knexHandle, config) {
101
- var tablesToReset = (0, lodash_1.flatten)(seedFiles).reverse();
39
+ const path_1 = __importDefault(require("path"));
40
+ const lodash_1 = require("lodash");
41
+ const dbHelpers_1 = require("./dbHelpers");
42
+ const generateSeeds = (dirname, seedFiles) => __awaiter(void 0, void 0, void 0, function* () {
43
+ let seedGroups = [];
44
+ const seedFileNames = (0, lodash_1.flatten)(seedFiles);
45
+ const seedFnExports = yield Promise.all((0, lodash_1.map)(seedFileNames, (filename) => Promise.resolve().then(() => __importStar(require(path_1.default.join(dirname, `./objects/${filename}.js`))))));
46
+ const seedFns = (0, lodash_1.map)(seedFnExports, 'default');
47
+ const seedFnsMap = {};
48
+ for (let i = 0; i < seedFileNames.length; i++) {
49
+ const seedFileName = seedFileNames[i];
50
+ seedFnsMap[seedFileName] = seedFns[i];
51
+ }
52
+ seedFiles.reduce((seeds, fileGroup) => {
53
+ seedGroups = seedGroups.concat(fileGroup.reduce((group, filename) => {
54
+ const seedFn = seedFnsMap[filename];
55
+ const seed = seedFn(seeds);
56
+ const filenameCamel = (0, lodash_1.camelCase)(filename);
57
+ seeds[filenameCamel] = seed;
58
+ return Object.assign({ [filenameCamel]: seed }, group);
59
+ }, {}));
60
+ return seeds;
61
+ }, {});
62
+ return seedGroups;
63
+ });
64
+ const removeSeedsFromDb = (seedFiles, knexHandle, config) => {
65
+ const tablesToReset = (0, lodash_1.flatten)(seedFiles).reverse();
102
66
  console.log('Deleting records from tables: ', tablesToReset);
103
67
  return (0, dbHelpers_1.dbClean)(tablesToReset, knexHandle, config);
104
68
  };
105
- var insertRecords = function (seedGroups, knexHandle, config) {
106
- var dbSchema = (config || {}).dbSchema;
69
+ const insertRecords = (seedGroups, knexHandle, config) => {
70
+ const { dbSchema } = config || {};
107
71
  console.log('Inserting seeds records tables tables...');
108
- return seedGroups.reduce(function (previousGroup, group) { return __awaiter(void 0, void 0, void 0, function () {
109
- return __generator(this, function (_a) {
110
- switch (_a.label) {
111
- case 0: return [4 /*yield*/, previousGroup];
112
- case 1:
113
- _a.sent();
114
- return [2 /*return*/, Promise.all(Object.keys(group).map(function (table) { return knexHandle(table).withSchema(dbSchema).insert(group[table]); }))];
115
- }
116
- });
117
- }); }, Promise.resolve());
72
+ return seedGroups.reduce((previousGroup, group) => __awaiter(void 0, void 0, void 0, function* () {
73
+ yield previousGroup;
74
+ return Promise.all(Object.keys(group).map((table) => knexHandle(table).withSchema(dbSchema).insert(group[table])));
75
+ }), Promise.resolve());
118
76
  };
119
- var dbSeed = function (dirname, seedFiles, knexHandle, config) { return __awaiter(void 0, void 0, void 0, function () {
120
- var groups;
121
- return __generator(this, function (_a) {
122
- switch (_a.label) {
123
- case 0: return [4 /*yield*/, generateSeeds(dirname, seedFiles)];
124
- case 1:
125
- groups = _a.sent();
126
- return [4 /*yield*/, removeSeedsFromDb(seedFiles, knexHandle, config)];
127
- case 2:
128
- _a.sent();
129
- return [4 /*yield*/, insertRecords(groups, knexHandle, config)];
130
- case 3: return [2 /*return*/, _a.sent()];
131
- }
132
- });
133
- }); };
77
+ const dbSeed = (dirname, seedFiles, knexHandle, config) => __awaiter(void 0, void 0, void 0, function* () {
78
+ const groups = yield generateSeeds(dirname, seedFiles);
79
+ yield removeSeedsFromDb(seedFiles, knexHandle, config);
80
+ return yield insertRecords(groups, knexHandle, config);
81
+ });
134
82
  exports.dbSeed = dbSeed;
@@ -1,10 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.defaultFields = exports.addLang = exports.addAccountId = exports.addUserId = exports.addSpaceId = exports.addAmount = exports.addOrderNo = exports.dbFieldAddDefaults = void 0;
4
- var utils_1 = require("@sdflc/utils");
5
- var constants_1 = require("./constants");
6
- var dbFieldAddDefaults = function (table, opt) {
7
- var _a = opt || {}, addUserInfo = _a.addUserInfo, addVersion = _a.addVersion, addStatus = _a.addStatus, skipTimestamps = _a.skipTimestamps, skipRemovedAtStr = _a.skipRemovedAtStr;
4
+ const utils_1 = require("@sdflc/utils");
5
+ const constants_1 = require("./constants");
6
+ const dbFieldAddDefaults = (table, opt) => {
7
+ const { addUserInfo, addVersion, addStatus, skipTimestamps, skipRemovedAtStr } = opt || {};
8
8
  if (addStatus) {
9
9
  table
10
10
  .integer(constants_1.FIELDS.STATUS)
@@ -38,30 +38,30 @@ var dbFieldAddDefaults = function (table, opt) {
38
38
  }
39
39
  };
40
40
  exports.dbFieldAddDefaults = dbFieldAddDefaults;
41
- var addOrderNo = function (table, opt) {
42
- var chain = table.text(constants_1.FIELDS.ORDER_NO).notNullable().defaultTo('10000000');
41
+ const addOrderNo = (table, opt) => {
42
+ const chain = table.text(constants_1.FIELDS.ORDER_NO).notNullable().defaultTo('10000000');
43
43
  if ((opt === null || opt === void 0 ? void 0 : opt.addIndex) === true) {
44
44
  table.index([constants_1.FIELDS.ORDER_NO]);
45
45
  }
46
46
  return chain;
47
47
  };
48
48
  exports.addOrderNo = addOrderNo;
49
- var addAmount = function (table, fieldName) {
49
+ const addAmount = (table, fieldName) => {
50
50
  return table.decimal(fieldName, 19, 4);
51
51
  };
52
52
  exports.addAmount = addAmount;
53
- var addSpaceId = function (table, opt) {
54
- var chain = table.text(constants_1.FIELDS.SPACE_ID).notNullable().comment('Reference to the space table');
53
+ const addSpaceId = (table, opt) => {
54
+ const chain = table.text(constants_1.FIELDS.SPACE_ID).notNullable().comment('Reference to the space table');
55
55
  if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
56
- table.foreign(constants_1.FIELDS.SPACE_ID).references("space.".concat(constants_1.FIELDS.ID)).onUpdate('CASCADE').onDelete('CASCADE');
56
+ table.foreign(constants_1.FIELDS.SPACE_ID).references(`space.${constants_1.FIELDS.ID}`).onUpdate('CASCADE').onDelete('CASCADE');
57
57
  }
58
58
  return chain;
59
59
  };
60
60
  exports.addSpaceId = addSpaceId;
61
- var addUserId = function (table, opt) {
62
- var chain = table.uuid(constants_1.FIELDS.USER_ID).notNullable().comment('Reference to the user table');
61
+ const addUserId = (table, opt) => {
62
+ const chain = table.uuid(constants_1.FIELDS.USER_ID).notNullable().comment('Reference to the user table');
63
63
  if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
64
- table.foreign(constants_1.FIELDS.USER_ID).references("user.".concat(constants_1.FIELDS.ID)).onUpdate('CASCADE').onDelete('CASCADE');
64
+ table.foreign(constants_1.FIELDS.USER_ID).references(`user.${constants_1.FIELDS.ID}`).onUpdate('CASCADE').onDelete('CASCADE');
65
65
  }
66
66
  if ((opt === null || opt === void 0 ? void 0 : opt.addIndex) === true) {
67
67
  table.index([constants_1.FIELDS.USER_ID]);
@@ -69,10 +69,10 @@ var addUserId = function (table, opt) {
69
69
  return chain;
70
70
  };
71
71
  exports.addUserId = addUserId;
72
- var addAccountId = function (table, opt) {
73
- var chain = table.uuid(constants_1.FIELDS.ACCOUNT_ID).notNullable().comment('Reference to the user table');
72
+ const addAccountId = (table, opt) => {
73
+ const chain = table.uuid(constants_1.FIELDS.ACCOUNT_ID).notNullable().comment('Reference to the user table');
74
74
  if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
75
- table.foreign(constants_1.FIELDS.ACCOUNT_ID).references("account.".concat(constants_1.FIELDS.ID)).onUpdate('CASCADE').onDelete('CASCADE');
75
+ table.foreign(constants_1.FIELDS.ACCOUNT_ID).references(`account.${constants_1.FIELDS.ID}`).onUpdate('CASCADE').onDelete('CASCADE');
76
76
  }
77
77
  if ((opt === null || opt === void 0 ? void 0 : opt.addIndex) === true) {
78
78
  table.index([constants_1.FIELDS.ACCOUNT_ID]);
@@ -80,10 +80,10 @@ var addAccountId = function (table, opt) {
80
80
  return chain;
81
81
  };
82
82
  exports.addAccountId = addAccountId;
83
- var addLang = function (table, opt) {
84
- var chain = table.text(constants_1.FIELDS.LANG).defaultTo('en').notNullable().comment('Language, example: en, ru, en_CA, etc');
83
+ const addLang = (table, opt) => {
84
+ const chain = table.text(constants_1.FIELDS.LANG).defaultTo('en').notNullable().comment('Language, example: en, ru, en_CA, etc');
85
85
  if ((opt === null || opt === void 0 ? void 0 : opt.addForeignKey) === true) {
86
- table.foreign(constants_1.FIELDS.LANG).references("language.".concat(constants_1.FIELDS.ID)).onUpdate('CASCADE').onDelete('CASCADE');
86
+ table.foreign(constants_1.FIELDS.LANG).references(`language.${constants_1.FIELDS.ID}`).onUpdate('CASCADE').onDelete('CASCADE');
87
87
  }
88
88
  if ((opt === null || opt === void 0 ? void 0 : opt.addIndex) === true) {
89
89
  table.index([constants_1.FIELDS.LANG]);
@@ -91,13 +91,12 @@ var addLang = function (table, opt) {
91
91
  return chain;
92
92
  };
93
93
  exports.addLang = addLang;
94
- var defaultFields = function (opt) {
95
- var _a;
96
- var addUserInfo = (opt || {}).addUserInfo;
97
- var obj = (_a = {},
98
- _a[constants_1.FIELDS.CREATED_AT] = '2021-01-01T00:00:00Z',
99
- _a[constants_1.FIELDS.UPDATED_AT] = '2021-01-01T00:00:00Z',
100
- _a);
94
+ const defaultFields = (opt) => {
95
+ const { addUserInfo } = opt || {};
96
+ const obj = {
97
+ [constants_1.FIELDS.CREATED_AT]: '2021-01-01T00:00:00Z',
98
+ [constants_1.FIELDS.UPDATED_AT]: '2021-01-01T00:00:00Z', // dh.dbUtcNow(),
99
+ };
101
100
  if (addUserInfo === true) {
102
101
  obj[constants_1.FIELDS.CREATED_BY] = utils_1.UUID_EMPTY;
103
102
  obj[constants_1.FIELDS.UPDATED_BY] = utils_1.UUID_EMPTY;
@@ -1,39 +1,28 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __importDefault = (this && this.__importDefault) || function (mod) {
14
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
15
4
  };
16
5
  Object.defineProperty(exports, "__esModule", { value: true });
17
6
  exports.buildKnexConfig = void 0;
18
- var path_1 = __importDefault(require("path"));
19
- var lodash_1 = require("lodash");
20
- var utils_1 = require("@sdflc/utils");
21
- var defaultMigrationsTable = 'migrations';
22
- var defaultMigrationsFolder = 'migrations';
23
- var defaultMigrationsTemplateFileName = 'migrations-template.js';
24
- var defaultSeedsFolder = 'seeds';
25
- var camelResponse = function (result) {
7
+ const path_1 = __importDefault(require("path"));
8
+ const lodash_1 = require("lodash");
9
+ const utils_1 = require("@sdflc/utils");
10
+ const defaultMigrationsTable = 'migrations';
11
+ const defaultMigrationsFolder = 'migrations';
12
+ const defaultMigrationsTemplateFileName = 'migrations-template.js';
13
+ const defaultSeedsFolder = 'seeds';
14
+ const camelResponse = (result) => {
26
15
  if (!result || result.rows) {
27
16
  return result;
28
17
  }
29
18
  return (0, utils_1.camelKeys)(result);
30
19
  };
31
- var buildKnexConfig = function (dirName, appConfig) {
32
- var dbType = appConfig.dbType, dbHost = appConfig.dbHost, dbPort = appConfig.dbPort, dbName = appConfig.dbName, dbSchema = appConfig.dbSchema, dbUser = appConfig.dbUser, dbPassword = appConfig.dbPassword, debug = appConfig.debug, nodeEnv = appConfig.nodeEnv, migrationsTemplateFileName = appConfig.migrationsTemplateFileName, migrationsFolder = appConfig.migrationsFolder, migrationsTable = appConfig.migrationsTable, useSchemaForMigrationsTable = appConfig.useSchemaForMigrationsTable, seedsFolder = appConfig.seedsFolder, seedsFolderDev = appConfig.seedsFolderDev, seedsFolderProd = appConfig.seedsFolderProd;
33
- var sharedConfig = {
20
+ const buildKnexConfig = (dirName, appConfig) => {
21
+ const { dbType, dbHost, dbPort, dbName, dbSchema, dbUser, dbPassword, debug, nodeEnv, migrationsTemplateFileName, migrationsFolder, migrationsTable, useSchemaForMigrationsTable, seedsFolder, seedsFolderDev, seedsFolderProd, } = appConfig;
22
+ const sharedConfig = {
34
23
  client: dbType || 'pg',
35
24
  postProcessResponse: camelResponse,
36
- wrapIdentifier: function (value, origImpl) { return (value === '*' ? origImpl(value) : origImpl((0, lodash_1.snakeCase)(value))); },
25
+ wrapIdentifier: (value, origImpl) => (value === '*' ? origImpl(value) : origImpl((0, lodash_1.snakeCase)(value))),
37
26
  connection: {
38
27
  host: dbHost,
39
28
  port: dbPort,
@@ -43,19 +32,19 @@ var buildKnexConfig = function (dirName, appConfig) {
43
32
  //ssl: database.ssl,
44
33
  },
45
34
  };
46
- var config = {
47
- development: __assign(__assign({}, sharedConfig), { debug: debug, migrations: {
35
+ const config = {
36
+ development: Object.assign(Object.assign({}, sharedConfig), { debug, migrations: {
48
37
  stub: path_1.default.join(dirName, migrationsTemplateFileName || defaultMigrationsTemplateFileName),
49
38
  directory: path_1.default.join(dirName, migrationsFolder || defaultMigrationsFolder),
50
39
  tableName: migrationsTable ||
51
- "".concat(useSchemaForMigrationsTable ? "".concat(dbSchema, ".") : '').concat(migrationsTable || defaultMigrationsTable),
40
+ `${useSchemaForMigrationsTable ? `${dbSchema}.` : ''}${migrationsTable || defaultMigrationsTable}`,
52
41
  }, seeds: {
53
42
  directory: path_1.default.join(dirName, seedsFolderDev || seedsFolder || defaultSeedsFolder),
54
43
  } }),
55
- production: __assign(__assign({}, sharedConfig), { debug: debug, migrations: {
44
+ production: Object.assign(Object.assign({}, sharedConfig), { debug, migrations: {
56
45
  directory: path_1.default.join(dirName, migrationsFolder || defaultMigrationsFolder),
57
46
  tableName: migrationsTable ||
58
- "".concat(useSchemaForMigrationsTable ? "".concat(dbSchema, ".") : '').concat(migrationsTable || defaultMigrationsTable),
47
+ `${useSchemaForMigrationsTable ? `${dbSchema}.` : ''}${migrationsTable || defaultMigrationsTable}`,
59
48
  }, seeds: {
60
49
  directory: path_1.default.join(dirName, seedsFolderProd || seedsFolder || defaultSeedsFolder),
61
50
  } }),