@sdflc/backend-helpers 1.0.106 → 1.0.108

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 (209) hide show
  1. package/build/BaseCore.d.ts +88 -0
  2. package/build/BaseCore.js +736 -0
  3. package/build/BaseGateway.d.ts +56 -0
  4. package/build/BaseGateway.js +477 -0
  5. package/build/DataCache.d.ts +16 -0
  6. package/build/DataCache.js +113 -0
  7. package/build/Database.d.ts +36 -0
  8. package/build/Database.js +195 -0
  9. package/build/DbMigratorAndSeeder.d.ts +49 -0
  10. package/build/DbMigratorAndSeeder.js +212 -0
  11. package/build/RedisDataLoader.d.ts +41 -0
  12. package/build/RedisDataLoader.js +212 -0
  13. package/build/apiContext.d.ts +11 -0
  14. package/build/apiContext.js +146 -0
  15. package/build/baseController.d.ts +17 -0
  16. package/build/baseController.js +63 -0
  17. package/build/checkItHelpers.d.ts +3 -0
  18. package/build/checkItHelpers.js +28 -0
  19. package/build/commonValidators.d.ts +17 -0
  20. package/build/commonValidators.js +311 -0
  21. package/build/configBulder.d.ts +2 -0
  22. package/build/configBulder.js +36 -0
  23. package/build/constants.d.ts +30 -0
  24. package/build/constants.js +80 -0
  25. package/build/dbHelpers.d.ts +43 -0
  26. package/build/dbHelpers.js +156 -0
  27. package/build/dbSeeder.d.ts +2 -0
  28. package/build/dbSeeder.js +83 -0
  29. package/build/dbTableHelpers.d.ts +14 -0
  30. package/build/dbTableHelpers.js +118 -0
  31. package/build/downloadHelpers.d.ts +2 -0
  32. package/build/downloadHelpers.js +73 -0
  33. package/build/esm/BaseCore.d.ts +88 -0
  34. package/build/esm/BaseCore.js +730 -0
  35. package/build/esm/BaseGateway.d.ts +56 -0
  36. package/build/esm/BaseGateway.js +471 -0
  37. package/build/esm/DataCache.d.ts +16 -0
  38. package/build/esm/DataCache.js +107 -0
  39. package/build/esm/Database.d.ts +36 -0
  40. package/build/esm/Database.js +189 -0
  41. package/build/esm/DbMigratorAndSeeder.d.ts +49 -0
  42. package/build/esm/DbMigratorAndSeeder.js +173 -0
  43. package/build/esm/RedisDataLoader.d.ts +41 -0
  44. package/build/esm/RedisDataLoader.js +206 -0
  45. package/build/esm/apiContext.d.ts +11 -0
  46. package/build/esm/apiContext.js +138 -0
  47. package/build/esm/baseController.d.ts +17 -0
  48. package/build/esm/baseController.js +57 -0
  49. package/build/esm/checkItHelpers.d.ts +3 -0
  50. package/build/esm/checkItHelpers.js +23 -0
  51. package/build/esm/commonValidators.d.ts +17 -0
  52. package/build/esm/commonValidators.js +291 -0
  53. package/build/esm/configBulder.d.ts +2 -0
  54. package/build/esm/configBulder.js +32 -0
  55. package/build/esm/constants.d.ts +30 -0
  56. package/build/esm/constants.js +69 -0
  57. package/build/esm/dbHelpers.d.ts +43 -0
  58. package/build/esm/dbHelpers.js +144 -0
  59. package/build/esm/dbSeeder.d.ts +2 -0
  60. package/build/esm/dbSeeder.js +44 -0
  61. package/build/esm/dbTableHelpers.d.ts +14 -0
  62. package/build/esm/dbTableHelpers.js +106 -0
  63. package/build/esm/downloadHelpers.d.ts +2 -0
  64. package/build/esm/downloadHelpers.js +66 -0
  65. package/build/esm/index.d.ts +21 -0
  66. package/build/esm/index.js +21 -0
  67. package/build/esm/interfaces/ApiContext.d.ts +25 -0
  68. package/build/esm/interfaces/ApiContext.js +1 -0
  69. package/build/esm/interfaces/ApiContextInterface.d.ts +12 -0
  70. package/build/esm/interfaces/ApiContextInterface.js +1 -0
  71. package/build/esm/interfaces/BaseCoreActionsInterface.d.ts +38 -0
  72. package/build/esm/interfaces/BaseCoreActionsInterface.js +1 -0
  73. package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
  74. package/build/esm/interfaces/BaseCoreDoAuthOnActionInterface.js +1 -0
  75. package/build/esm/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
  76. package/build/esm/interfaces/BaseCoreDoingWhatInterface.js +1 -0
  77. package/build/esm/interfaces/BaseCorePropsInterface.d.ts +37 -0
  78. package/build/esm/interfaces/BaseCorePropsInterface.js +1 -0
  79. package/build/esm/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
  80. package/build/esm/interfaces/BaseCoreValidatorsInterface.js +1 -0
  81. package/build/esm/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
  82. package/build/esm/interfaces/BaseGatewayPropsInterface.js +1 -0
  83. package/build/esm/interfaces/BuildConfigOptions.d.ts +6 -0
  84. package/build/esm/interfaces/BuildConfigOptions.js +1 -0
  85. package/build/esm/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
  86. package/build/esm/interfaces/BuildDefaultResolversOptions.js +1 -0
  87. package/build/esm/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
  88. package/build/esm/interfaces/BuildListQueryArgsInterface.js +1 -0
  89. package/build/esm/interfaces/CacheInterface.d.ts +15 -0
  90. package/build/esm/interfaces/CacheInterface.js +1 -0
  91. package/build/esm/interfaces/CommonValidatorArgs.d.ts +3 -0
  92. package/build/esm/interfaces/CommonValidatorArgs.js +1 -0
  93. package/build/esm/interfaces/CoreActionInterface.d.ts +16 -0
  94. package/build/esm/interfaces/CoreActionInterface.js +1 -0
  95. package/build/esm/interfaces/CreateUserTokenArgs.d.ts +11 -0
  96. package/build/esm/interfaces/CreateUserTokenArgs.js +1 -0
  97. package/build/esm/interfaces/CreateUserTokenResult.d.ts +4 -0
  98. package/build/esm/interfaces/CreateUserTokenResult.js +1 -0
  99. package/build/esm/interfaces/DatabaseOptions.d.ts +26 -0
  100. package/build/esm/interfaces/DatabaseOptions.js +1 -0
  101. package/build/esm/interfaces/DatabaseReadInterface.d.ts +14 -0
  102. package/build/esm/interfaces/DatabaseReadInterface.js +1 -0
  103. package/build/esm/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
  104. package/build/esm/interfaces/DbFieldAddDefaultInterface.js +1 -0
  105. package/build/esm/interfaces/DownloadFileArgs.d.ts +6 -0
  106. package/build/esm/interfaces/DownloadFileArgs.js +1 -0
  107. package/build/esm/interfaces/ExtractCredentialsArgs.d.ts +19 -0
  108. package/build/esm/interfaces/ExtractCredentialsArgs.js +1 -0
  109. package/build/esm/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
  110. package/build/esm/interfaces/JwtTokenSecretAndSettings.js +1 -0
  111. package/build/esm/interfaces/ListArgsInterface.d.ts +7 -0
  112. package/build/esm/interfaces/ListArgsInterface.js +1 -0
  113. package/build/esm/interfaces/LoaderInterface.d.ts +7 -0
  114. package/build/esm/interfaces/LoaderInterface.js +1 -0
  115. package/build/esm/interfaces/PaginationInterface.d.ts +10 -0
  116. package/build/esm/interfaces/PaginationInterface.js +1 -0
  117. package/build/esm/interfaces/SortItemInterface.d.ts +4 -0
  118. package/build/esm/interfaces/SortItemInterface.js +1 -0
  119. package/build/esm/interfaces/UserTokenParams.d.ts +8 -0
  120. package/build/esm/interfaces/UserTokenParams.js +1 -0
  121. package/build/esm/interfaces/VerifyUserTokenArgs.d.ts +13 -0
  122. package/build/esm/interfaces/VerifyUserTokenArgs.js +1 -0
  123. package/build/esm/interfaces/index.d.ts +28 -0
  124. package/build/esm/interfaces/index.js +27 -0
  125. package/build/esm/knexConfigBuilder.d.ts +1 -0
  126. package/build/esm/knexConfigBuilder.js +84 -0
  127. package/build/esm/passwordHelpers.d.ts +5 -0
  128. package/build/esm/passwordHelpers.js +13 -0
  129. package/build/esm/redisClient.d.ts +17 -0
  130. package/build/esm/redisClient.js +97 -0
  131. package/build/esm/resolversHelpers.d.ts +3 -0
  132. package/build/esm/resolversHelpers.js +66 -0
  133. package/build/esm/userToken.d.ts +8 -0
  134. package/build/esm/userToken.js +80 -0
  135. package/build/esm/utils.d.ts +2 -0
  136. package/build/esm/utils.js +13 -0
  137. package/build/index.d.ts +21 -0
  138. package/build/index.js +37 -0
  139. package/build/interfaces/ApiContext.d.ts +25 -0
  140. package/build/interfaces/ApiContext.js +2 -0
  141. package/build/interfaces/ApiContextInterface.d.ts +12 -0
  142. package/build/interfaces/ApiContextInterface.js +2 -0
  143. package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -0
  144. package/build/interfaces/BaseCoreActionsInterface.js +2 -0
  145. package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -0
  146. package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -0
  147. package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -0
  148. package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -0
  149. package/build/interfaces/BaseCorePropsInterface.d.ts +37 -0
  150. package/build/interfaces/BaseCorePropsInterface.js +2 -0
  151. package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -0
  152. package/build/interfaces/BaseCoreValidatorsInterface.js +2 -0
  153. package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -0
  154. package/build/interfaces/BaseGatewayPropsInterface.js +2 -0
  155. package/build/interfaces/BuildConfigOptions.d.ts +6 -0
  156. package/build/interfaces/BuildConfigOptions.js +2 -0
  157. package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -0
  158. package/build/interfaces/BuildDefaultResolversOptions.js +2 -0
  159. package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -0
  160. package/build/interfaces/BuildListQueryArgsInterface.js +2 -0
  161. package/build/interfaces/CacheInterface.d.ts +15 -0
  162. package/build/interfaces/CacheInterface.js +2 -0
  163. package/build/interfaces/CommonValidatorArgs.d.ts +3 -0
  164. package/build/interfaces/CommonValidatorArgs.js +2 -0
  165. package/build/interfaces/CoreActionInterface.d.ts +16 -0
  166. package/build/interfaces/CoreActionInterface.js +2 -0
  167. package/build/interfaces/CreateUserTokenArgs.d.ts +11 -0
  168. package/build/interfaces/CreateUserTokenArgs.js +2 -0
  169. package/build/interfaces/CreateUserTokenResult.d.ts +4 -0
  170. package/build/interfaces/CreateUserTokenResult.js +2 -0
  171. package/build/interfaces/DatabaseOptions.d.ts +26 -0
  172. package/build/interfaces/DatabaseOptions.js +2 -0
  173. package/build/interfaces/DatabaseReadInterface.d.ts +14 -0
  174. package/build/interfaces/DatabaseReadInterface.js +2 -0
  175. package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -0
  176. package/build/interfaces/DbFieldAddDefaultInterface.js +2 -0
  177. package/build/interfaces/DownloadFileArgs.d.ts +6 -0
  178. package/build/interfaces/DownloadFileArgs.js +2 -0
  179. package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -0
  180. package/build/interfaces/ExtractCredentialsArgs.js +2 -0
  181. package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -0
  182. package/build/interfaces/JwtTokenSecretAndSettings.js +2 -0
  183. package/build/interfaces/ListArgsInterface.d.ts +7 -0
  184. package/build/interfaces/ListArgsInterface.js +2 -0
  185. package/build/interfaces/LoaderInterface.d.ts +7 -0
  186. package/build/interfaces/LoaderInterface.js +2 -0
  187. package/build/interfaces/PaginationInterface.d.ts +10 -0
  188. package/build/interfaces/PaginationInterface.js +2 -0
  189. package/build/interfaces/SortItemInterface.d.ts +4 -0
  190. package/build/interfaces/SortItemInterface.js +2 -0
  191. package/build/interfaces/UserTokenParams.d.ts +8 -0
  192. package/build/interfaces/UserTokenParams.js +2 -0
  193. package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -0
  194. package/build/interfaces/VerifyUserTokenArgs.js +2 -0
  195. package/build/interfaces/index.d.ts +28 -0
  196. package/build/interfaces/index.js +43 -0
  197. package/build/knexConfigBuilder.d.ts +1 -0
  198. package/build/knexConfigBuilder.js +91 -0
  199. package/build/passwordHelpers.d.ts +5 -0
  200. package/build/passwordHelpers.js +21 -0
  201. package/build/redisClient.d.ts +17 -0
  202. package/build/redisClient.js +100 -0
  203. package/build/resolversHelpers.d.ts +3 -0
  204. package/build/resolversHelpers.js +69 -0
  205. package/build/userToken.d.ts +8 -0
  206. package/build/userToken.js +92 -0
  207. package/build/utils.d.ts +2 -0
  208. package/build/utils.js +21 -0
  209. package/package.json +1 -1
@@ -0,0 +1,36 @@
1
+ import { DatabaseOptions, DatabaseReadInterface } from './interfaces';
2
+ declare class Database {
3
+ private trx;
4
+ private dbType;
5
+ private knexHandle;
6
+ private dbSchema;
7
+ private logger;
8
+ constructor(props: DatabaseOptions);
9
+ destroy(): void;
10
+ isDbError(ex: any): any[];
11
+ createTransaction(trx: any): Promise<unknown>;
12
+ getDbType(): string;
13
+ getSchema(): string;
14
+ getBuilder(arg: any): any;
15
+ raw(query: string, bindings?: any): any;
16
+ runRawQuery(query: string, bindings: any): Promise<any>;
17
+ startTransaction(): Promise<unknown>;
18
+ setTransaction(trx: any): void;
19
+ getTransaction(): any;
20
+ commitTransaction(): Promise<void>;
21
+ rollbackTransaction(): Promise<void>;
22
+ doCalculated(item: any, calculatedFields: any[]): void;
23
+ read(args: DatabaseReadInterface): Promise<any>;
24
+ createSortBy(sortBy: any[], defaultSortBy?: any): any[] | null;
25
+ createLimitOffset(page: number, pageSize: number): {
26
+ limit: number | undefined;
27
+ offset: number;
28
+ };
29
+ createLimitOffsetSortBy(params: any, defaultSortBy: any): {
30
+ limit: number | undefined;
31
+ offset: number;
32
+ sortBy: any[] | null;
33
+ };
34
+ static destroyHandle(props: DatabaseOptions): void;
35
+ }
36
+ export { Database };
@@ -0,0 +1,195 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Database = void 0;
7
+ const lodash_1 = require("lodash");
8
+ const knex_1 = __importDefault(require("knex"));
9
+ const utils_1 = require("@sdflc/utils");
10
+ const knexHandles = {};
11
+ const buildKey = (args) => {
12
+ const { connection, dbSchema } = args || {};
13
+ const { host, port, user, password, database } = connection || {};
14
+ const parts = [host, port, user, password, database, dbSchema];
15
+ return parts.join('|');
16
+ };
17
+ class Database {
18
+ trx = null;
19
+ dbType = '';
20
+ knexHandle = null;
21
+ dbSchema = '';
22
+ logger = new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
23
+ constructor(props) {
24
+ if (!props.knexConfig) {
25
+ throw new Error('The Database class expects to have `knexConfig` in its constructor props');
26
+ }
27
+ if (props.logger instanceof utils_1.Logger) {
28
+ this.logger = props.logger;
29
+ }
30
+ const cfgKey = buildKey({ connection: props.knexConfig.connection, dbSchema: props.dbSchema });
31
+ if (!knexHandles[cfgKey]) {
32
+ this.logger.debug(`Create a new KnexJs configuration`);
33
+ knexHandles[cfgKey] = (0, knex_1.default)(props.knexConfig);
34
+ }
35
+ else {
36
+ this.logger.debug(`Reusing existing KnexJs configuration`);
37
+ }
38
+ this.knexHandle = knexHandles[cfgKey];
39
+ this.dbType = props.dbType || props.knexConfig?.client || '';
40
+ this.dbSchema = props.dbSchema || '';
41
+ this.trx = props.trx;
42
+ }
43
+ destroy() {
44
+ if (this.knexHandle) {
45
+ this.knexHandle.destroy();
46
+ }
47
+ }
48
+ isDbError(ex) {
49
+ const { code } = ex || {};
50
+ switch (code) {
51
+ default:
52
+ return [false, ''];
53
+ case '23503':
54
+ // ex.detail: 'Key (message_id)=(78403d9e-4e95-4091-8136-ae99ddbfa3ca) is not present in table "message".',
55
+ // ex.constraint: 'message_recipient_status_message_id_foreign',
56
+ return [true, `Reference does not exist`];
57
+ case '23505':
58
+ return [true, `Such item already exists`];
59
+ }
60
+ }
61
+ createTransaction(trx) {
62
+ return new Promise((resolve) => {
63
+ return (trx ? trx : this.knexHandle).transaction(resolve);
64
+ });
65
+ }
66
+ getDbType() {
67
+ return this.dbType;
68
+ }
69
+ getSchema() {
70
+ return this.dbSchema;
71
+ }
72
+ getBuilder(arg) {
73
+ return this.trx ? this.trx(arg).withSchema(this.getSchema()) : this.knexHandle(arg).withSchema(this.getSchema());
74
+ }
75
+ raw(query, bindings) {
76
+ return this.knexHandle.raw(query, bindings);
77
+ }
78
+ async runRawQuery(query, bindings) {
79
+ const rows = await (this.trx ? this.trx : this.knexHandle).raw(query, bindings);
80
+ return rows;
81
+ }
82
+ async startTransaction() {
83
+ const trx = await this.createTransaction(this.trx);
84
+ this.trx = trx;
85
+ return trx;
86
+ }
87
+ setTransaction(trx) {
88
+ this.trx = trx;
89
+ }
90
+ getTransaction() {
91
+ return this.trx;
92
+ }
93
+ async commitTransaction() {
94
+ if (this.trx) {
95
+ await this.trx.commit();
96
+ this.trx = undefined;
97
+ }
98
+ }
99
+ async rollbackTransaction() {
100
+ if (this.trx) {
101
+ try {
102
+ await this.trx.rollback();
103
+ }
104
+ catch (ex) {
105
+ // Do nothing here
106
+ this.logger.error(`Failed to rollback a transaction:`, ex.message);
107
+ }
108
+ }
109
+ this.trx = undefined;
110
+ }
111
+ doCalculated(item, calculatedFields) {
112
+ calculatedFields.forEach((calcField) => {
113
+ if (calcField.name) {
114
+ item[calcField.name] = typeof calcField.value === 'function' ? calcField.value(item) : calcField.value;
115
+ }
116
+ });
117
+ }
118
+ async read(args) {
119
+ const { where, whereIn, extendQuery, table, selectFields, mapBy, calculated, buildKey } = args || {};
120
+ const query = this.getBuilder(table).select(Array.isArray(selectFields) && selectFields.length > 0 ? selectFields : '*');
121
+ if (where) {
122
+ query.where(where);
123
+ }
124
+ if (whereIn) {
125
+ query.whereIn(whereIn.field, whereIn.values);
126
+ }
127
+ if (typeof extendQuery === 'function') {
128
+ await extendQuery(query);
129
+ }
130
+ let dbResults = await query;
131
+ if (mapBy) {
132
+ const fnBuildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
133
+ dbResults = dbResults.reduce((acc, item) => {
134
+ const key = fnBuildKey(item[(0, lodash_1.camelCase)(mapBy)]);
135
+ if (Array.isArray(calculated) && calculated.length > 0) {
136
+ this.doCalculated(item, calculated);
137
+ }
138
+ acc[key] = item;
139
+ return acc;
140
+ }, {});
141
+ }
142
+ else {
143
+ if (Array.isArray(calculated) && calculated.length > 0) {
144
+ dbResults.forEach((row) => {
145
+ this.doCalculated(row, calculated);
146
+ });
147
+ }
148
+ }
149
+ return dbResults;
150
+ }
151
+ createSortBy(sortBy, defaultSortBy) {
152
+ if (!sortBy) {
153
+ if (Array.isArray(defaultSortBy)) {
154
+ return this.createSortBy(defaultSortBy);
155
+ }
156
+ else {
157
+ return null;
158
+ }
159
+ }
160
+ return sortBy.reduce((acc, item) => {
161
+ acc.push({
162
+ column: item.name,
163
+ order: item.order,
164
+ });
165
+ return acc;
166
+ }, []);
167
+ }
168
+ createLimitOffset(page, pageSize) {
169
+ const limit = isNaN(pageSize) || pageSize == null ? undefined : Number(pageSize);
170
+ const pageToUse = isNaN(page) || page < 1 ? 1 : Number(page);
171
+ return {
172
+ limit,
173
+ offset: limit ? (pageToUse - 1) * limit : 0,
174
+ };
175
+ }
176
+ createLimitOffsetSortBy(params, defaultSortBy) {
177
+ const { pagination, sortBy } = params || {};
178
+ const { page, pageSize } = pagination || {};
179
+ const { limit, offset } = this.createLimitOffset(page, pageSize);
180
+ const sortByToUse = this.createSortBy(sortBy, defaultSortBy);
181
+ return {
182
+ limit,
183
+ offset,
184
+ sortBy: sortByToUse,
185
+ };
186
+ }
187
+ static destroyHandle(props) {
188
+ const cfgKey = buildKey({ connection: props.knexConfig.connection, dbSchema: props.dbSchema });
189
+ if (knexHandles[cfgKey]) {
190
+ knexHandles[cfgKey].destroy();
191
+ delete knexHandles[cfgKey];
192
+ }
193
+ }
194
+ }
195
+ exports.Database = Database;
@@ -0,0 +1,49 @@
1
+ declare class DbMigratorAndSeeder {
2
+ private knexHandle;
3
+ private config;
4
+ private logger;
5
+ constructor(knexHandle: any, config?: any);
6
+ runMigrations(): Promise<void>;
7
+ /**
8
+ * Go from array of objects which contain keys of object names which have an array value of seeded objects
9
+ * to a single object with keys of object names and arrays of seeded objects
10
+ * ie from [{table1: [], table2: [] }, { table3: [] }]
11
+ * to { table1: [], table2: [], table3: []}
12
+ */
13
+ transformSeedsToObject(generatedSeedArrays: any[]): any;
14
+ /**
15
+ * Drop all tables, run migrations, then seed the tables
16
+ */
17
+ resetDb(): Promise<void>;
18
+ /**
19
+ * Selects all the tables in the schema and drops them.
20
+ * This approach does not work in case we have constraints.
21
+ * TODO: Remove all constraints and then drop tables.
22
+ */
23
+ wipeDb(): Promise<void>;
24
+ /**
25
+ * Removes all the records from provided list of tables
26
+ * @param tablesToClean array of table names
27
+ * @param knexHandle Knex handle object with configured connection to DB
28
+ * @param appSettings settings with db related information
29
+ * @returns promise
30
+ */
31
+ cleanDb(tablesToClean: any[]): Promise<unknown>;
32
+ /**
33
+ * Removes all the records from provided table.
34
+ * If appSettings has cleanTable object with table names then
35
+ * it is used to add extra filters to clear the table to avoid for example users data
36
+ * @param table Name of table from which to remove all the records
37
+ */
38
+ cleanDbTable(table: string): Promise<void>;
39
+ /**
40
+ * Returns a list of tables in db schedm specified in settings
41
+ * @returns arrays of table names in the provided db schema
42
+ */
43
+ listDbTables(): Promise<any>;
44
+ generateSeeds(dirname: string, seedFiles: any): Promise<any>;
45
+ removeSeedsFromDb(seedFiles: any): Promise<unknown>;
46
+ insertRecords(seedGroups: any): Promise<any>;
47
+ runSeeds(dirname: string, seedFiles: any): Promise<any>;
48
+ }
49
+ export { DbMigratorAndSeeder };
@@ -0,0 +1,212 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ var __importDefault = (this && this.__importDefault) || function (mod) {
36
+ return (mod && mod.__esModule) ? mod : { "default": mod };
37
+ };
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.DbMigratorAndSeeder = void 0;
40
+ const path_1 = __importDefault(require("path"));
41
+ const bluebird_1 = __importDefault(require("bluebird"));
42
+ const lodash_1 = require("lodash");
43
+ const utils_1 = require("@sdflc/utils");
44
+ const SQLS = {
45
+ pg: {
46
+ listTablesSchema: (appSettings) => `
47
+ SELECT TABLE_NAME as table_name
48
+ FROM INFORMATION_SCHEMA.TABLES
49
+ WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
50
+ `,
51
+ listTables: () => `
52
+ SELECT TABLE_NAME as table_name
53
+ FROM INFORMATION_SCHEMA.TABLES
54
+ `,
55
+ dropTablesSchema: (appSettings) => `
56
+ SELECT 'DROP TABLE IF EXISTS ${appSettings?.dbSchema}.' || TABLE_NAME || ';' as cmd
57
+ FROM INFORMATION_SCHEMA.TABLES
58
+ WHERE TABLE_SCHEMA = '${appSettings?.dbSchema}';
59
+ `,
60
+ dropTables: () => `
61
+ SELECT 'DROP TABLE IF EXISTS ' || TABLE_NAME || ';' as cmd
62
+ FROM INFORMATION_SCHEMA.TABLES';
63
+ `,
64
+ },
65
+ };
66
+ class DbMigratorAndSeeder {
67
+ knexHandle;
68
+ config;
69
+ logger;
70
+ constructor(knexHandle, config) {
71
+ this.knexHandle = knexHandle;
72
+ this.config = config;
73
+ this.logger = config?.logger instanceof utils_1.Logger ? config.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.LOG });
74
+ }
75
+ async runMigrations() {
76
+ const result = await this.knexHandle.migrate.latest();
77
+ if (this.config && this.config.runDbSeed && result[1].length > 0) {
78
+ await this.knexHandle.seed.run();
79
+ }
80
+ this.logger.log(`Ran ${result[1].length} migration(s)`);
81
+ const version = this.knexHandle.migrate.currentVersion();
82
+ this.logger.log(`Current version of database: ${version}`);
83
+ }
84
+ /**
85
+ * Go from array of objects which contain keys of object names which have an array value of seeded objects
86
+ * to a single object with keys of object names and arrays of seeded objects
87
+ * ie from [{table1: [], table2: [] }, { table3: [] }]
88
+ * to { table1: [], table2: [], table3: []}
89
+ */
90
+ transformSeedsToObject(generatedSeedArrays) {
91
+ return (0, lodash_1.reduce)(generatedSeedArrays, (acc, cur) => {
92
+ for (const key in cur) {
93
+ acc[key] = cur[key];
94
+ }
95
+ return acc;
96
+ }, {});
97
+ }
98
+ /**
99
+ * Drop all tables, run migrations, then seed the tables
100
+ */
101
+ async resetDb() {
102
+ try {
103
+ await this.wipeDb();
104
+ await this.runMigrations();
105
+ }
106
+ catch (ex) {
107
+ this.logger.error('An exception has occurred on resetting database: ', ex);
108
+ }
109
+ }
110
+ /**
111
+ * Selects all the tables in the schema and drops them.
112
+ * This approach does not work in case we have constraints.
113
+ * TODO: Remove all constraints and then drop tables.
114
+ */
115
+ async wipeDb() {
116
+ const query = this.config.dbSchema
117
+ ? SQLS[this.config.dbType].dropTablesSchema(this.config)
118
+ : SQLS[this.config.dbType].dropTables(this.config);
119
+ const result = await this.knexHandle.raw(query);
120
+ const commands = result.rows.map((r) => r.cmd);
121
+ bluebird_1.default.each(commands, (cmd) => this.knexHandle.raw(cmd));
122
+ }
123
+ /**
124
+ * Removes all the records from provided list of tables
125
+ * @param tablesToClean array of table names
126
+ * @param knexHandle Knex handle object with configured connection to DB
127
+ * @param appSettings settings with db related information
128
+ * @returns promise
129
+ */
130
+ async cleanDb(tablesToClean) {
131
+ for (let idx = 0, len = tablesToClean.length; idx < len; ++idx) {
132
+ await this.cleanDbTable(tablesToClean[idx]);
133
+ }
134
+ return new bluebird_1.default((resolve) => {
135
+ resolve(tablesToClean);
136
+ });
137
+ }
138
+ /**
139
+ * Removes all the records from provided table.
140
+ * If appSettings has cleanTable object with table names then
141
+ * it is used to add extra filters to clear the table to avoid for example users data
142
+ * @param table Name of table from which to remove all the records
143
+ */
144
+ async cleanDbTable(table) {
145
+ const query = this.knexHandle(table).withSchema(this.config.dbSchema).delete();
146
+ const cleanTableFilters = this.config?.cleanTableFilters?.[table];
147
+ if (cleanTableFilters != undefined) {
148
+ if (Array.isArray(cleanTableFilters.where)) {
149
+ cleanTableFilters.where.forEach((whereItem) => {
150
+ if (whereItem.fieldName) {
151
+ query.where(whereItem.fieldName, whereItem.operand, whereItem.value);
152
+ }
153
+ });
154
+ }
155
+ }
156
+ await query;
157
+ }
158
+ /**
159
+ * Returns a list of tables in db schedm specified in settings
160
+ * @returns arrays of table names in the provided db schema
161
+ */
162
+ async listDbTables() {
163
+ const skipTables = this.config?.skipTables ?? [];
164
+ const query = this.config.dbSchema
165
+ ? SQLS[this.config.dbType].listTablesSchema(this.config)
166
+ : SQLS[this.config.dbType].listTables(this.config);
167
+ const tables = await this.knexHandle.raw(query);
168
+ return (tables.rows || [])
169
+ .map((row) => row.table_name)
170
+ .filter((table) => skipTables.indexOf(table) === -1);
171
+ }
172
+ async generateSeeds(dirname, seedFiles) {
173
+ let seedGroups = [];
174
+ const seedFileNames = (0, lodash_1.flatten)(seedFiles);
175
+ const seedFnExports = await Promise.all((0, lodash_1.map)(seedFileNames, (filename) => Promise.resolve(`${path_1.default.join(dirname, `./objects/${filename}.js`)}`).then(s => __importStar(require(s)))));
176
+ const seedFns = (0, lodash_1.map)(seedFnExports, 'default');
177
+ const seedFnsMap = {};
178
+ for (let i = 0; i < seedFileNames.length; i++) {
179
+ const seedFileName = seedFileNames[i];
180
+ seedFnsMap[seedFileName] = seedFns[i];
181
+ }
182
+ seedFiles.reduce((seeds, fileGroup) => {
183
+ seedGroups = seedGroups.concat(fileGroup.reduce((group, filename) => {
184
+ const seedFn = seedFnsMap[filename];
185
+ const seed = seedFn(seeds);
186
+ const filenameCamel = (0, lodash_1.camelCase)(filename);
187
+ seeds[filenameCamel] = seed;
188
+ return Object.assign({ [filenameCamel]: seed }, group);
189
+ }, {}));
190
+ return seeds;
191
+ }, {});
192
+ return seedGroups;
193
+ }
194
+ removeSeedsFromDb(seedFiles) {
195
+ const tablesToReset = (0, lodash_1.flatten)(seedFiles).reverse();
196
+ this.logger.log('Deleting records from tables: ', tablesToReset);
197
+ return this.cleanDb(tablesToReset);
198
+ }
199
+ async insertRecords(seedGroups) {
200
+ this.logger.log('Inserting seeds records into tables...');
201
+ return seedGroups.reduce(async (previousGroup, group) => {
202
+ await previousGroup;
203
+ return Promise.all(Object.keys(group).map((table) => this.knexHandle(table).withSchema(this.config.dbSchema).insert(group[table])));
204
+ }, Promise.resolve());
205
+ }
206
+ async runSeeds(dirname, seedFiles) {
207
+ const groups = await this.generateSeeds(dirname, seedFiles);
208
+ await this.removeSeedsFromDb(seedFiles);
209
+ return await this.insertRecords(groups);
210
+ }
211
+ }
212
+ exports.DbMigratorAndSeeder = DbMigratorAndSeeder;
@@ -0,0 +1,41 @@
1
+ declare class RedisDataLoader {
2
+ private opt?;
3
+ private dataLoaderOpt?;
4
+ private keySpace;
5
+ private loader?;
6
+ private redisClient;
7
+ private static messages;
8
+ constructor(redisClient: any, keySpace?: string, userLoader?: any, opt?: any);
9
+ /**
10
+ * Adds a prefix to the passed key
11
+ * @param key Key value
12
+ * @returns Trasnformed key value that includes key space
13
+ */
14
+ makeKey(key: string): string;
15
+ /**
16
+ * Deserializes response from Readis
17
+ * @param str String to deserialize
18
+ * @returns
19
+ */
20
+ parse(str: any): Promise<any>;
21
+ /**
22
+ * Serializes passed value to a string
23
+ * @param value A value to be serialized
24
+ * @returns serialized value in the form of string
25
+ */
26
+ toString(value: any): Promise<string>;
27
+ rSetAndGet(key: string, rawValue: any): Promise<any>;
28
+ rMSetAndGet(keys: string[], rawValues: any[]): Promise<any[]>;
29
+ rGet(key: string): Promise<any>;
30
+ rMGet(keys: string[]): Promise<any[]>;
31
+ rSet(key: string, rawValue: any): Promise<any>;
32
+ rMSet(keys: string[], values: any[]): Promise<any>;
33
+ rDel(key: string): Promise<any>;
34
+ load(key: string): Promise<any>;
35
+ loadMany(keys: string[]): Promise<any>;
36
+ prime(key: string, value: any): Promise<any>;
37
+ clear(key: string): Promise<any>;
38
+ clearAllLocal(): Promise<any>;
39
+ clearLocal(key: string): Promise<any>;
40
+ }
41
+ export { RedisDataLoader };