@sdflc/backend-helpers 1.0.77 → 1.0.78

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.
Files changed (106) hide show
  1. package/README.md +178 -178
  2. package/build/BaseCore.d.ts +88 -88
  3. package/build/BaseCore.js +819 -819
  4. package/build/BaseGateway.d.ts +49 -49
  5. package/build/BaseGateway.js +484 -484
  6. package/build/DataCache.d.ts +14 -14
  7. package/build/DataCache.js +104 -104
  8. package/build/Database.d.ts +32 -32
  9. package/build/Database.js +198 -198
  10. package/build/DbMigratorAndSeeder.d.ts +49 -49
  11. package/build/DbMigratorAndSeeder.js +238 -228
  12. package/build/RedisDataLoader.d.ts +41 -41
  13. package/build/RedisDataLoader.js +248 -248
  14. package/build/apiContext.d.ts +11 -11
  15. package/build/apiContext.js +143 -143
  16. package/build/{BaseController.d.ts → baseController.d.ts} +17 -17
  17. package/build/{BaseController.js → baseController.js} +66 -66
  18. package/build/checkItHelpers.d.ts +3 -3
  19. package/build/checkItHelpers.js +28 -28
  20. package/build/commonValidators.d.ts +17 -17
  21. package/build/commonValidators.js +298 -298
  22. package/build/configBulder.d.ts +2 -2
  23. package/build/configBulder.js +32 -32
  24. package/build/constants.d.ts +30 -30
  25. package/build/constants.js +80 -81
  26. package/build/dbHelpers.d.ts +43 -43
  27. package/build/dbHelpers.js +166 -166
  28. package/build/dbSeeder.d.ts +2 -2
  29. package/build/dbSeeder.js +92 -82
  30. package/build/dbTableHelpers.d.ts +11 -11
  31. package/build/dbTableHelpers.js +110 -110
  32. package/build/downloadHelpers.d.ts +2 -2
  33. package/build/downloadHelpers.js +83 -83
  34. package/build/index.d.ts +21 -21
  35. package/build/index.js +37 -37
  36. package/build/interfaces/ApiContext.d.ts +24 -24
  37. package/build/interfaces/ApiContext.js +2 -2
  38. package/build/interfaces/ApiContextInterface.d.ts +12 -12
  39. package/build/interfaces/ApiContextInterface.js +2 -2
  40. package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -38
  41. package/build/interfaces/BaseCoreActionsInterface.js +2 -2
  42. package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -12
  43. package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -2
  44. package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -13
  45. package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -2
  46. package/build/interfaces/BaseCorePropsInterface.d.ts +36 -36
  47. package/build/interfaces/BaseCorePropsInterface.js +2 -2
  48. package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -13
  49. package/build/interfaces/BaseCoreValidatorsInterface.js +2 -2
  50. package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -39
  51. package/build/interfaces/BaseGatewayPropsInterface.js +2 -2
  52. package/build/interfaces/BuildConfigOptions.d.ts +6 -6
  53. package/build/interfaces/BuildConfigOptions.js +2 -2
  54. package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -17
  55. package/build/interfaces/BuildDefaultResolversOptions.js +2 -2
  56. package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -6
  57. package/build/interfaces/BuildListQueryArgsInterface.js +2 -2
  58. package/build/interfaces/CacheInterface.d.ts +15 -15
  59. package/build/interfaces/CacheInterface.js +2 -2
  60. package/build/interfaces/CommonValidatorArgs.d.ts +3 -3
  61. package/build/interfaces/CommonValidatorArgs.js +2 -2
  62. package/build/interfaces/CoreActionInterface.d.ts +14 -14
  63. package/build/interfaces/CoreActionInterface.js +2 -2
  64. package/build/interfaces/CreateUserTokenArgs.d.ts +11 -11
  65. package/build/interfaces/CreateUserTokenArgs.js +2 -2
  66. package/build/interfaces/CreateUserTokenResult.d.ts +4 -4
  67. package/build/interfaces/CreateUserTokenResult.js +2 -2
  68. package/build/interfaces/DatabaseOptions.d.ts +22 -22
  69. package/build/interfaces/DatabaseOptions.js +2 -2
  70. package/build/interfaces/DatabaseReadInterface.d.ts +14 -14
  71. package/build/interfaces/DatabaseReadInterface.js +2 -2
  72. package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -9
  73. package/build/interfaces/DbFieldAddDefaultInterface.js +2 -2
  74. package/build/interfaces/DownloadFileArgs.d.ts +6 -6
  75. package/build/interfaces/DownloadFileArgs.js +2 -2
  76. package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -19
  77. package/build/interfaces/ExtractCredentialsArgs.js +2 -2
  78. package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -5
  79. package/build/interfaces/JwtTokenSecretAndSettings.js +2 -2
  80. package/build/interfaces/ListArgsInterface.d.ts +5 -5
  81. package/build/interfaces/ListArgsInterface.js +2 -2
  82. package/build/interfaces/LoaderInterface.d.ts +4 -4
  83. package/build/interfaces/LoaderInterface.js +2 -2
  84. package/build/interfaces/PaginationInterface.d.ts +7 -7
  85. package/build/interfaces/PaginationInterface.js +2 -2
  86. package/build/interfaces/SortItemInterface.d.ts +4 -4
  87. package/build/interfaces/SortItemInterface.js +2 -2
  88. package/build/interfaces/UserTokenParams.d.ts +8 -8
  89. package/build/interfaces/UserTokenParams.js +2 -2
  90. package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -13
  91. package/build/interfaces/VerifyUserTokenArgs.js +2 -2
  92. package/build/interfaces/index.d.ts +28 -28
  93. package/build/interfaces/index.js +43 -43
  94. package/build/knexConfigBuilder.d.ts +1 -1
  95. package/build/knexConfigBuilder.js +80 -80
  96. package/build/passwordHelpers.d.ts +5 -5
  97. package/build/passwordHelpers.js +30 -30
  98. package/build/redisClient.d.ts +17 -17
  99. package/build/redisClient.js +97 -97
  100. package/build/resolversHelpers.d.ts +3 -3
  101. package/build/resolversHelpers.js +62 -62
  102. package/build/userToken.d.ts +8 -8
  103. package/build/userToken.js +86 -86
  104. package/build/utils.d.ts +2 -2
  105. package/build/utils.js +21 -21
  106. package/package.json +71 -71
@@ -1,30 +1,30 @@
1
- declare const FIELDS: any;
2
- declare const ORDER_BYS: {
3
- ASC: string;
4
- DESC: string;
5
- };
6
- declare const HEADERS: any;
7
- declare enum ID_TYPES {
8
- UNKNOWN = 0,
9
- INT = 1,
10
- UUID = 2,
11
- TEXT = 3
12
- }
13
- declare const ID_FIELD: any;
14
- declare const DEFAULT_ORDER_NO = "10000000";
15
- declare const DEFAULT_ORDER_NO_STEP = "1000";
16
- declare const DEFAULT_JWT_ALGORITHM = "HS256";
17
- declare const DEFAULT_JWT_ISSUER = "microservices-auth";
18
- declare const ACTIONS: {
19
- LIST: string;
20
- GET: string;
21
- GET_MANY: string;
22
- CREATE: string;
23
- CREATE_MANY: string;
24
- UPDATE: string;
25
- UPDATE_MANY: string;
26
- SET: string;
27
- REMOVE: string;
28
- REMOVE_MANY: string;
29
- };
30
- export { ID_FIELD, FIELDS, ORDER_BYS, HEADERS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, ID_TYPES, DEFAULT_JWT_ALGORITHM, DEFAULT_JWT_ISSUER, ACTIONS, };
1
+ declare const FIELDS: any;
2
+ declare const ORDER_BYS: {
3
+ ASC: string;
4
+ DESC: string;
5
+ };
6
+ declare const HEADERS: any;
7
+ declare enum ID_TYPES {
8
+ UNKNOWN = 0,
9
+ INT = 1,
10
+ UUID = 2,
11
+ TEXT = 3
12
+ }
13
+ declare const ID_FIELD: any;
14
+ declare const DEFAULT_ORDER_NO = "10000000";
15
+ declare const DEFAULT_ORDER_NO_STEP = "1000";
16
+ declare const DEFAULT_JWT_ALGORITHM = "HS256";
17
+ declare const DEFAULT_JWT_ISSUER = "microservices-auth";
18
+ declare const ACTIONS: {
19
+ LIST: string;
20
+ GET: string;
21
+ GET_MANY: string;
22
+ CREATE: string;
23
+ CREATE_MANY: string;
24
+ UPDATE: string;
25
+ UPDATE_MANY: string;
26
+ SET: string;
27
+ REMOVE: string;
28
+ REMOVE_MANY: string;
29
+ };
30
+ export { ID_FIELD, FIELDS, ORDER_BYS, HEADERS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, ID_TYPES, DEFAULT_JWT_ALGORITHM, DEFAULT_JWT_ISSUER, ACTIONS, };
@@ -1,81 +1,80 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ACTIONS = exports.DEFAULT_JWT_ISSUER = exports.DEFAULT_JWT_ALGORITHM = exports.ID_TYPES = exports.DEFAULT_ORDER_NO_STEP = exports.DEFAULT_ORDER_NO = exports.HEADERS = exports.ORDER_BYS = exports.FIELDS = exports.ID_FIELD = void 0;
4
- const FIELDS = {
5
- ID: 'id',
6
- STATUS: 'status',
7
- VERSION: 'version',
8
- CREATED_BY: 'created_by',
9
- UPDATED_BY: 'updated_by',
10
- REMOVED_BY: 'removed_by',
11
- CREATED_AT: 'created_at',
12
- UPDATED_AT: 'updated_at',
13
- REMOVED_AT: 'removed_at',
14
- REMOVED_AT_STR: 'removed_at_str',
15
- LANG: 'LANG',
16
- ACCOUNT_ID: 'account_id',
17
- ROLE_ID: 'role_id',
18
- USER_ID: 'user_id',
19
- ORDER_NO: 'order_no',
20
- SPACE_ID: 'space_id',
21
- DISPLAY_NAME: 'display_name',
22
- NAME: 'name',
23
- USERNAME: 'username',
24
- EMAIL: 'email',
25
- PHONE: 'phone',
26
- };
27
- exports.FIELDS = FIELDS;
28
- const ORDER_BYS = {
29
- ASC: 'asc',
30
- DESC: 'desc',
31
- };
32
- exports.ORDER_BYS = ORDER_BYS;
33
- const HEADERS = {
34
- AUTHORIZATION: 'authorization',
35
- FINGERPRINT: 'x-fingerprint',
36
- SPACE_ID: 'x-space-id',
37
- API_KEY: 'x-api-key',
38
- MANAGEMENT_API_KEY: 'x-management-api-key',
39
- ACCOUNT_ID: 'x-account-id',
40
- ROLE_ID: 'x-role-id',
41
- USER_ID: 'x-user-id',
42
- USER_AGENT: 'user-agent',
43
- REFERER: 'referer',
44
- IP_ADDRESS: 'x-forwarded-for',
45
- ACCEPT_LANGUAGE: 'accept-language',
46
- REQUESTED_LANGUAGE: 'x-lang',
47
- CONTENT_TYPE: 'content-type',
48
- CONTENT_LENGTH: 'content-length',
49
- };
50
- exports.HEADERS = HEADERS;
51
- var ID_TYPES;
52
- (function (ID_TYPES) {
53
- ID_TYPES[ID_TYPES["UNKNOWN"] = 0] = "UNKNOWN";
54
- ID_TYPES[ID_TYPES["INT"] = 1] = "INT";
55
- ID_TYPES[ID_TYPES["UUID"] = 2] = "UUID";
56
- ID_TYPES[ID_TYPES["TEXT"] = 3] = "TEXT";
57
- })(ID_TYPES || (ID_TYPES = {}));
58
- exports.ID_TYPES = ID_TYPES;
59
- const ID_FIELD = FIELDS.ID;
60
- exports.ID_FIELD = ID_FIELD;
61
- const DEFAULT_ORDER_NO = '10000000';
62
- exports.DEFAULT_ORDER_NO = DEFAULT_ORDER_NO;
63
- const DEFAULT_ORDER_NO_STEP = '1000';
64
- exports.DEFAULT_ORDER_NO_STEP = DEFAULT_ORDER_NO_STEP;
65
- const DEFAULT_JWT_ALGORITHM = 'HS256';
66
- exports.DEFAULT_JWT_ALGORITHM = DEFAULT_JWT_ALGORITHM;
67
- const DEFAULT_JWT_ISSUER = 'microservices-auth';
68
- exports.DEFAULT_JWT_ISSUER = DEFAULT_JWT_ISSUER;
69
- const ACTIONS = {
70
- LIST: 'LIST',
71
- GET: 'GET',
72
- GET_MANY: 'GET_MANY',
73
- CREATE: 'CREATE',
74
- CREATE_MANY: 'CREATE_MANY',
75
- UPDATE: 'UPDATE',
76
- UPDATE_MANY: 'UPDATE_MANY',
77
- SET: 'SET',
78
- REMOVE: 'REMOVE',
79
- REMOVE_MANY: 'REMOVE_MANY',
80
- };
81
- exports.ACTIONS = ACTIONS;
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ACTIONS = exports.DEFAULT_JWT_ISSUER = exports.DEFAULT_JWT_ALGORITHM = exports.ID_TYPES = exports.DEFAULT_ORDER_NO_STEP = exports.DEFAULT_ORDER_NO = exports.HEADERS = exports.ORDER_BYS = exports.FIELDS = exports.ID_FIELD = void 0;
4
+ const FIELDS = {
5
+ ID: 'id',
6
+ STATUS: 'status',
7
+ VERSION: 'version',
8
+ CREATED_BY: 'created_by',
9
+ UPDATED_BY: 'updated_by',
10
+ REMOVED_BY: 'removed_by',
11
+ CREATED_AT: 'created_at',
12
+ UPDATED_AT: 'updated_at',
13
+ REMOVED_AT: 'removed_at',
14
+ REMOVED_AT_STR: 'removed_at_str',
15
+ LANG: 'LANG',
16
+ ACCOUNT_ID: 'account_id',
17
+ ROLE_ID: 'role_id',
18
+ USER_ID: 'user_id',
19
+ ORDER_NO: 'order_no',
20
+ SPACE_ID: 'space_id',
21
+ DISPLAY_NAME: 'display_name',
22
+ NAME: 'name',
23
+ USERNAME: 'username',
24
+ EMAIL: 'email',
25
+ PHONE: 'phone',
26
+ };
27
+ exports.FIELDS = FIELDS;
28
+ const ORDER_BYS = {
29
+ ASC: 'asc',
30
+ DESC: 'desc',
31
+ };
32
+ exports.ORDER_BYS = ORDER_BYS;
33
+ const HEADERS = {
34
+ AUTHORIZATION: 'authorization',
35
+ FINGERPRINT: 'x-fingerprint',
36
+ SPACE_ID: 'x-space-id',
37
+ API_KEY: 'x-api-key',
38
+ MANAGEMENT_API_KEY: 'x-management-api-key',
39
+ ACCOUNT_ID: 'x-account-id',
40
+ ROLE_ID: 'x-role-id',
41
+ USER_ID: 'x-user-id',
42
+ USER_AGENT: 'user-agent',
43
+ REFERER: 'referer',
44
+ IP_ADDRESS: 'x-forwarded-for',
45
+ ACCEPT_LANGUAGE: 'accept-language',
46
+ REQUESTED_LANGUAGE: 'x-lang',
47
+ CONTENT_TYPE: 'content-type',
48
+ CONTENT_LENGTH: 'content-length',
49
+ };
50
+ exports.HEADERS = HEADERS;
51
+ var ID_TYPES;
52
+ (function (ID_TYPES) {
53
+ ID_TYPES[ID_TYPES["UNKNOWN"] = 0] = "UNKNOWN";
54
+ ID_TYPES[ID_TYPES["INT"] = 1] = "INT";
55
+ ID_TYPES[ID_TYPES["UUID"] = 2] = "UUID";
56
+ ID_TYPES[ID_TYPES["TEXT"] = 3] = "TEXT";
57
+ })(ID_TYPES || (exports.ID_TYPES = ID_TYPES = {}));
58
+ const ID_FIELD = FIELDS.ID;
59
+ exports.ID_FIELD = ID_FIELD;
60
+ const DEFAULT_ORDER_NO = '10000000';
61
+ exports.DEFAULT_ORDER_NO = DEFAULT_ORDER_NO;
62
+ const DEFAULT_ORDER_NO_STEP = '1000';
63
+ exports.DEFAULT_ORDER_NO_STEP = DEFAULT_ORDER_NO_STEP;
64
+ const DEFAULT_JWT_ALGORITHM = 'HS256';
65
+ exports.DEFAULT_JWT_ALGORITHM = DEFAULT_JWT_ALGORITHM;
66
+ const DEFAULT_JWT_ISSUER = 'microservices-auth';
67
+ exports.DEFAULT_JWT_ISSUER = DEFAULT_JWT_ISSUER;
68
+ const ACTIONS = {
69
+ LIST: 'LIST',
70
+ GET: 'GET',
71
+ GET_MANY: 'GET_MANY',
72
+ CREATE: 'CREATE',
73
+ CREATE_MANY: 'CREATE_MANY',
74
+ UPDATE: 'UPDATE',
75
+ UPDATE_MANY: 'UPDATE_MANY',
76
+ SET: 'SET',
77
+ REMOVE: 'REMOVE',
78
+ REMOVE_MANY: 'REMOVE_MANY',
79
+ };
80
+ exports.ACTIONS = ACTIONS;
@@ -1,43 +1,43 @@
1
- /**
2
- * Removes all the records from provided table.
3
- * If appSettings has cleanTable object with table names then
4
- * it is used to add extra filters to clear the table to avoid for example users data
5
- * @param table Name of table from which to remove all the records
6
- * @param knexHandle Knex handle object with configured connection to DB
7
- * @param appSettings settings with db related information
8
- */
9
- declare const dbCleanTable: (table: string, knexHandle: any, appSettings: any) => Promise<void>;
10
- /**
11
- * Returns a list of tables in db schedm specified in settings
12
- * @param knexHandle Knex handle object with configured connection to DB
13
- * @param appSettings settings with db related information
14
- * @returns arrays of table names in the provided db schema
15
- */
16
- declare const dbListTables: (knexHandle: any, appSettings: any) => Promise<any>;
17
- /**
18
- * Removes all the records from provided list of tables
19
- * @param tablesToClean array of table names
20
- * @param knexHandle Knex handle object with configured connection to DB
21
- * @param appSettings settings with db related information
22
- * @returns promise
23
- */
24
- declare const dbClean: (tablesToClean: any[], knexHandle: any, appSettings: any) => Promise<unknown>;
25
- /**
26
- * Selects all the tables in the schema and drops them.
27
- * This approach does not work in case we have constraints.
28
- * TODO: Remove all constraints and then drop tables.
29
- */
30
- declare const dbWipe: (knexHandle: any, appSettings: any) => any;
31
- /**
32
- * Drop all tables, run migrations, then seed
33
- */
34
- declare const dbReset: (knexHandle: any, appSettings: any) => any;
35
- /**
36
- * Go from array of objects which contain keys of object names which have an array value of seeded objects
37
- * to a single object with keys of object names and arrays of seeded objects
38
- * ie from [{table1: [], table2: [] }, { table3: [] }]
39
- * to { table1: [], table2: [], table3: []}
40
- */
41
- declare const dbTransformSeedsToObject: (generatedSeedArrays: any[]) => any;
42
- declare const dbRunMigrations: (knexHandle: any, appSettings: any) => any;
43
- export { dbCleanTable, dbListTables, dbClean, dbWipe, dbReset, dbTransformSeedsToObject, dbRunMigrations };
1
+ /**
2
+ * Removes all the records from provided table.
3
+ * If appSettings has cleanTable object with table names then
4
+ * it is used to add extra filters to clear the table to avoid for example users data
5
+ * @param table Name of table from which to remove all the records
6
+ * @param knexHandle Knex handle object with configured connection to DB
7
+ * @param appSettings settings with db related information
8
+ */
9
+ declare const dbCleanTable: (table: string, knexHandle: any, appSettings: any) => Promise<void>;
10
+ /**
11
+ * Returns a list of tables in db schedm specified in settings
12
+ * @param knexHandle Knex handle object with configured connection to DB
13
+ * @param appSettings settings with db related information
14
+ * @returns arrays of table names in the provided db schema
15
+ */
16
+ declare const dbListTables: (knexHandle: any, appSettings: any) => Promise<any>;
17
+ /**
18
+ * Removes all the records from provided list of tables
19
+ * @param tablesToClean array of table names
20
+ * @param knexHandle Knex handle object with configured connection to DB
21
+ * @param appSettings settings with db related information
22
+ * @returns promise
23
+ */
24
+ declare const dbClean: (tablesToClean: any[], knexHandle: any, appSettings: any) => Promise<unknown>;
25
+ /**
26
+ * Selects all the tables in the schema and drops them.
27
+ * This approach does not work in case we have constraints.
28
+ * TODO: Remove all constraints and then drop tables.
29
+ */
30
+ declare const dbWipe: (knexHandle: any, appSettings: any) => any;
31
+ /**
32
+ * Drop all tables, run migrations, then seed
33
+ */
34
+ declare const dbReset: (knexHandle: any, appSettings: any) => any;
35
+ /**
36
+ * Go from array of objects which contain keys of object names which have an array value of seeded objects
37
+ * to a single object with keys of object names and arrays of seeded objects
38
+ * ie from [{table1: [], table2: [] }, { table3: [] }]
39
+ * to { table1: [], table2: [], table3: []}
40
+ */
41
+ declare const dbTransformSeedsToObject: (generatedSeedArrays: any[]) => any;
42
+ declare const dbRunMigrations: (knexHandle: any, appSettings: any) => any;
43
+ export { dbCleanTable, dbListTables, dbClean, dbWipe, dbReset, dbTransformSeedsToObject, dbRunMigrations };
@@ -1,166 +1,166 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __importDefault = (this && this.__importDefault) || function (mod) {
12
- return (mod && mod.__esModule) ? mod : { "default": mod };
13
- };
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
16
- const bluebird_1 = __importDefault(require("bluebird"));
17
- const lodash_1 = require("lodash");
18
- const SQLS = {
19
- pg: {
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
- `,
38
- },
39
- mssql: {
40
- listTablesSchema: (appSettings) => `
41
- SELECT TABLE_NAME as table_name
42
- FROM INFORMATION_SCHEMA.TABLES
43
- WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
44
- `,
45
- listTables: () => `
46
- SELECT TABLE_NAME as table_name
47
- FROM INFORMATION_SCHEMA.TABLES
48
- `,
49
- dropTablesSchema: (appSettings) => `
50
- SELECT 'DROP TABLE IF EXISTS [${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}].[' + TABLE_NAME + '];' as cmd
51
- FROM INFORMATION_SCHEMA.TABLES
52
- WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
53
- `,
54
- dropTables: () => `
55
- SELECT 'DROP TABLE IF EXISTS ' + TABLE_NAME + ';' as cmd
56
- FROM INFORMATION_SCHEMA.TABLES';
57
- `,
58
- },
59
- };
60
- /**
61
- * Removes all the records from provided table.
62
- * If appSettings has cleanTable object with table names then
63
- * it is used to add extra filters to clear the table to avoid for example users data
64
- * @param table Name of table from which to remove all the records
65
- * @param knexHandle Knex handle object with configured connection to DB
66
- * @param appSettings settings with db related information
67
- */
68
- const dbCleanTable = (table, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
69
- var _a;
70
- const query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
71
- const cleanTableFilters = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTableFilters) === null || _a === void 0 ? void 0 : _a[table];
72
- if (cleanTableFilters != undefined) {
73
- if (Array.isArray(cleanTableFilters.where)) {
74
- cleanTableFilters.where.forEach((whereItem) => {
75
- query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
76
- });
77
- }
78
- }
79
- yield query;
80
- });
81
- exports.dbCleanTable = dbCleanTable;
82
- /**
83
- * Returns a list of tables in db schedm specified in settings
84
- * @param knexHandle Knex handle object with configured connection to DB
85
- * @param appSettings settings with db related information
86
- * @returns arrays of table names in the provided db schema
87
- */
88
- const dbListTables = (knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
89
- const skipTables = (appSettings || {}).skipTables || [];
90
- let query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
91
- if (!appSettings.dbSchema) {
92
- query = SQLS[appSettings.dbType].listTables(appSettings);
93
- }
94
- const tables = yield knexHandle.raw(query);
95
- return (tables.rows || [])
96
- .map((row) => row.table_name)
97
- .filter((table) => skipTables.indexOf(table) === -1);
98
- });
99
- exports.dbListTables = dbListTables;
100
- /**
101
- * Removes all the records from provided list of tables
102
- * @param tablesToClean array of table names
103
- * @param knexHandle Knex handle object with configured connection to DB
104
- * @param appSettings settings with db related information
105
- * @returns promise
106
- */
107
- const dbClean = (tablesToClean, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
108
- for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
109
- yield dbCleanTable(tablesToClean[idx], knexHandle, appSettings);
110
- }
111
- return new bluebird_1.default((resolve) => {
112
- resolve(tablesToClean);
113
- });
114
- });
115
- exports.dbClean = dbClean;
116
- /**
117
- * Selects all the tables in the schema and drops them.
118
- * This approach does not work in case we have constraints.
119
- * TODO: Remove all constraints and then drop tables.
120
- */
121
- const dbWipe = (knexHandle, appSettings) => {
122
- let query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
123
- if (!appSettings.dbSchema) {
124
- query = SQLS[appSettings.dbType].dropTables(appSettings);
125
- }
126
- return knexHandle
127
- .raw(query)
128
- .then((result) => result.rows.map((r) => r.cmd))
129
- .then((commands) => bluebird_1.default.each(commands, (cmd) => knexHandle.raw(cmd)));
130
- };
131
- exports.dbWipe = dbWipe;
132
- /**
133
- * Drop all tables, run migrations, then seed
134
- */
135
- const dbReset = (knexHandle, appSettings) => {
136
- return dbWipe(knexHandle, appSettings)
137
- .then(() => dbRunMigrations(knexHandle, appSettings))
138
- .catch((error) => console.log('An exception has occurred on resetting database: ', error));
139
- };
140
- exports.dbReset = dbReset;
141
- /**
142
- * Go from array of objects which contain keys of object names which have an array value of seeded objects
143
- * to a single object with keys of object names and arrays of seeded objects
144
- * ie from [{table1: [], table2: [] }, { table3: [] }]
145
- * to { table1: [], table2: [], table3: []}
146
- */
147
- const dbTransformSeedsToObject = (generatedSeedArrays) => (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
148
- for (const key in cur) {
149
- acc[key] = cur[key];
150
- }
151
- return acc;
152
- }, {});
153
- exports.dbTransformSeedsToObject = dbTransformSeedsToObject;
154
- const dbRunMigrations = (knexHandle, appSettings) => {
155
- return knexHandle.migrate
156
- .latest()
157
- .then((result) => __awaiter(void 0, void 0, void 0, function* () {
158
- if (appSettings && appSettings.runDbSeed && result[1].length > 0) {
159
- yield knexHandle.seed.run();
160
- }
161
- console.log('Ran', result[1].length, 'migration(s)');
162
- return knexHandle.migrate.currentVersion();
163
- }))
164
- .then((version) => console.log('Current version of database: ', version));
165
- };
166
- exports.dbRunMigrations = dbRunMigrations;
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.dbRunMigrations = exports.dbTransformSeedsToObject = exports.dbReset = exports.dbWipe = exports.dbClean = exports.dbListTables = exports.dbCleanTable = void 0;
16
+ const bluebird_1 = __importDefault(require("bluebird"));
17
+ const lodash_1 = require("lodash");
18
+ const SQLS = {
19
+ pg: {
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
+ `,
38
+ },
39
+ mssql: {
40
+ listTablesSchema: (appSettings) => `
41
+ SELECT TABLE_NAME as table_name
42
+ FROM INFORMATION_SCHEMA.TABLES
43
+ WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
44
+ `,
45
+ listTables: () => `
46
+ SELECT TABLE_NAME as table_name
47
+ FROM INFORMATION_SCHEMA.TABLES
48
+ `,
49
+ dropTablesSchema: (appSettings) => `
50
+ SELECT 'DROP TABLE IF EXISTS [${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}].[' + TABLE_NAME + '];' as cmd
51
+ FROM INFORMATION_SCHEMA.TABLES
52
+ WHERE TABLE_SCHEMA = '${appSettings === null || appSettings === void 0 ? void 0 : appSettings.dbSchema}';
53
+ `,
54
+ dropTables: () => `
55
+ SELECT 'DROP TABLE IF EXISTS ' + TABLE_NAME + ';' as cmd
56
+ FROM INFORMATION_SCHEMA.TABLES';
57
+ `,
58
+ },
59
+ };
60
+ /**
61
+ * Removes all the records from provided table.
62
+ * If appSettings has cleanTable object with table names then
63
+ * it is used to add extra filters to clear the table to avoid for example users data
64
+ * @param table Name of table from which to remove all the records
65
+ * @param knexHandle Knex handle object with configured connection to DB
66
+ * @param appSettings settings with db related information
67
+ */
68
+ const dbCleanTable = (table, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
69
+ var _a;
70
+ const query = knexHandle(table).withSchema(appSettings.dbSchema).delete();
71
+ const cleanTableFilters = (_a = appSettings === null || appSettings === void 0 ? void 0 : appSettings.cleanTableFilters) === null || _a === void 0 ? void 0 : _a[table];
72
+ if (cleanTableFilters != undefined) {
73
+ if (Array.isArray(cleanTableFilters.where)) {
74
+ cleanTableFilters.where.forEach((whereItem) => {
75
+ query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
76
+ });
77
+ }
78
+ }
79
+ yield query;
80
+ });
81
+ exports.dbCleanTable = dbCleanTable;
82
+ /**
83
+ * Returns a list of tables in db schedm specified in settings
84
+ * @param knexHandle Knex handle object with configured connection to DB
85
+ * @param appSettings settings with db related information
86
+ * @returns arrays of table names in the provided db schema
87
+ */
88
+ const dbListTables = (knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
89
+ const skipTables = (appSettings || {}).skipTables || [];
90
+ let query = SQLS[appSettings.dbType].listTablesSchema(appSettings);
91
+ if (!appSettings.dbSchema) {
92
+ query = SQLS[appSettings.dbType].listTables(appSettings);
93
+ }
94
+ const tables = yield knexHandle.raw(query);
95
+ return (tables.rows || [])
96
+ .map((row) => row.table_name)
97
+ .filter((table) => skipTables.indexOf(table) === -1);
98
+ });
99
+ exports.dbListTables = dbListTables;
100
+ /**
101
+ * Removes all the records from provided list of tables
102
+ * @param tablesToClean array of table names
103
+ * @param knexHandle Knex handle object with configured connection to DB
104
+ * @param appSettings settings with db related information
105
+ * @returns promise
106
+ */
107
+ const dbClean = (tablesToClean, knexHandle, appSettings) => __awaiter(void 0, void 0, void 0, function* () {
108
+ for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
109
+ yield dbCleanTable(tablesToClean[idx], knexHandle, appSettings);
110
+ }
111
+ return new bluebird_1.default((resolve) => {
112
+ resolve(tablesToClean);
113
+ });
114
+ });
115
+ exports.dbClean = dbClean;
116
+ /**
117
+ * Selects all the tables in the schema and drops them.
118
+ * This approach does not work in case we have constraints.
119
+ * TODO: Remove all constraints and then drop tables.
120
+ */
121
+ const dbWipe = (knexHandle, appSettings) => {
122
+ let query = SQLS[appSettings.dbType].dropTablesSchema(appSettings);
123
+ if (!appSettings.dbSchema) {
124
+ query = SQLS[appSettings.dbType].dropTables(appSettings);
125
+ }
126
+ return knexHandle
127
+ .raw(query)
128
+ .then((result) => result.rows.map((r) => r.cmd))
129
+ .then((commands) => bluebird_1.default.each(commands, (cmd) => knexHandle.raw(cmd)));
130
+ };
131
+ exports.dbWipe = dbWipe;
132
+ /**
133
+ * Drop all tables, run migrations, then seed
134
+ */
135
+ const dbReset = (knexHandle, appSettings) => {
136
+ return dbWipe(knexHandle, appSettings)
137
+ .then(() => dbRunMigrations(knexHandle, appSettings))
138
+ .catch((error) => console.log('An exception has occurred on resetting database: ', error));
139
+ };
140
+ exports.dbReset = dbReset;
141
+ /**
142
+ * Go from array of objects which contain keys of object names which have an array value of seeded objects
143
+ * to a single object with keys of object names and arrays of seeded objects
144
+ * ie from [{table1: [], table2: [] }, { table3: [] }]
145
+ * to { table1: [], table2: [], table3: []}
146
+ */
147
+ const dbTransformSeedsToObject = (generatedSeedArrays) => (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
148
+ for (const key in cur) {
149
+ acc[key] = cur[key];
150
+ }
151
+ return acc;
152
+ }, {});
153
+ exports.dbTransformSeedsToObject = dbTransformSeedsToObject;
154
+ const dbRunMigrations = (knexHandle, appSettings) => {
155
+ return knexHandle.migrate
156
+ .latest()
157
+ .then((result) => __awaiter(void 0, void 0, void 0, function* () {
158
+ if (appSettings && appSettings.runDbSeed && result[1].length > 0) {
159
+ yield knexHandle.seed.run();
160
+ }
161
+ console.log('Ran', result[1].length, 'migration(s)');
162
+ return knexHandle.migrate.currentVersion();
163
+ }))
164
+ .then((version) => console.log('Current version of database: ', version));
165
+ };
166
+ exports.dbRunMigrations = dbRunMigrations;
@@ -1,2 +1,2 @@
1
- declare const dbSeed: (dirname: string, seedFiles: any, knexHandle: any, config: any) => Promise<any>;
2
- export { dbSeed };
1
+ declare const dbSeed: (dirname: string, seedFiles: any, knexHandle: any, config: any) => Promise<any>;
2
+ export { dbSeed };