@sdflc/backend-helpers 1.0.105 → 1.0.106

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