@sdflc/backend-helpers 1.0.77 → 1.0.79

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