@technicity/data-service-generator 0.11.4 → 0.11.5

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.
@@ -5,6 +5,9 @@ export interface IRuntime {
5
5
  $whereNeedsProcessing(where: any): boolean;
6
6
  $prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<any>;
7
7
  $shutdown(): Promise<void>;
8
+ $startTransaction(input?: {
9
+ isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
10
+ }): Promise<TBeginTransactionResult>;
8
11
  }
9
12
  export declare type TResolveParams = {
10
13
  resource: string;
@@ -15,6 +18,7 @@ export declare type TResolveParams = {
15
18
  fields?: IField[] | undefined;
16
19
  context?: TContext | undefined;
17
20
  skipCache?: boolean;
21
+ dbCall?: TDbCall;
18
22
  };
19
23
  export declare type TContext = {
20
24
  [k: string]: any;
@@ -23,10 +27,12 @@ export declare type TMiddleware = (params: TResolveParams, next: (params: TResol
23
27
  export declare type IDialect = "mysql" | "mssql" | "ksql";
24
28
  export declare type TDbCall = (q: string) => Promise<any>;
25
29
  export declare type TFormatQuery = (q: string, values: any[]) => string;
26
- export declare type TBeginTransaction = () => Promise<{
30
+ export declare type TBeginTransaction = () => Promise<TBeginTransactionResult>;
31
+ declare type TBeginTransactionResult = {
27
32
  dbCall: (q: string) => Promise<any>;
28
33
  commit: () => Promise<void>;
29
- }>;
34
+ rollback: () => Promise<void>;
35
+ };
30
36
  export declare type ISupplementClientOpts = boolean;
31
37
  export declare type IOrderBy = {
32
38
  column: string;
@@ -15,5 +15,12 @@ export declare class RuntimeKSQL implements IRuntime {
15
15
  $whereNeedsProcessing(where: any): boolean;
16
16
  $prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
17
17
  $shutdown(): Promise<void>;
18
+ $startTransaction(input?: {
19
+ isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
20
+ }): Promise<{
21
+ dbCall: (q: string) => Promise<string>;
22
+ commit: () => Promise<void>;
23
+ rollback: () => Promise<void>;
24
+ }>;
18
25
  }
19
26
  export {};
@@ -60,6 +60,14 @@ class RuntimeKSQL {
60
60
  async $shutdown() {
61
61
  // Nothing to do here, I think
62
62
  }
63
+ async $startTransaction(input) {
64
+ throw new Error("Not implemented.");
65
+ return {
66
+ dbCall: async (q) => "",
67
+ commit: async () => { },
68
+ rollback: async () => { }
69
+ };
70
+ }
63
71
  }
64
72
  exports.RuntimeKSQL = RuntimeKSQL;
65
73
  _RuntimeKSQL_ksql = new WeakMap(), _RuntimeKSQL_middlewareHandler = new WeakMap(), _RuntimeKSQL_dbCall = new WeakMap(), _RuntimeKSQL_formatQuery = new WeakMap(), _RuntimeKSQL_getBaseTableName = new WeakMap(), _RuntimeKSQL_getMaterializedViewName = new WeakMap(), _RuntimeKSQL_doNotUseMaterializedViews = new WeakMap();
@@ -151,8 +159,7 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName,
151
159
  let where = undefined;
152
160
  if (args?.$where != null) {
153
161
  let argsMapped = args;
154
- if (typeof argsMapped.$where === "object" &&
155
- argsMapped.$where[table] != null) {
162
+ if (typeof argsMapped.$where === "object" && argsMapped.$where[table] != null) {
156
163
  argsMapped = _.cloneDeep(argsMapped);
157
164
  argsMapped.$where = argsMapped.$where[table];
158
165
  }
@@ -160,7 +167,7 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName,
160
167
  where: argsMapped.$where,
161
168
  table: escapeId(getTableName(table)),
162
169
  dialect: "mysql",
163
- args: argsMapped,
170
+ args: argsMapped
164
171
  });
165
172
  if (whereResult) {
166
173
  where = whereResult;
@@ -288,10 +295,10 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
288
295
  mappedField.name,
289
296
  getBaseTableName(mappedField.referencedTable),
290
297
  mappedField.referencedKey,
291
- parentData?.[mappedField.foreignKey],
298
+ parentData?.[mappedField.foreignKey]
292
299
  ]);
293
300
  return dbCall(s).then((xs) => ({
294
- [x]: xs?.[0]?.[mappedField.name] ?? null,
301
+ [x]: xs?.[0]?.[mappedField.name] ?? null
295
302
  }));
296
303
  }))
297
304
  .concat(relationFields.map((x) => {
@@ -316,8 +323,8 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
316
323
  whereJunction = {
317
324
  $and: [
318
325
  { [relationField.relations[0].foreignKey]: junctionKeyValue },
319
- whereJunction,
320
- ],
326
+ whereJunction
327
+ ]
321
328
  };
322
329
  const key = relationField.relations[1].foreignKey;
323
330
  const s = formatQuery(`SELECT ?? FROM ?? WHERE ${(0, stringifyWhere_1.stringifyWhere)({
@@ -325,20 +332,20 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
325
332
  table: escapeId(getBaseTableName(relationField.junctionTable)),
326
333
  dialect: "mysql",
327
334
  // Since we're paginating, empty is fine
328
- args: {},
335
+ args: {}
329
336
  })};`, [key, getBaseTableName(relationField.junctionTable)]);
330
337
  return dbCall(s)
331
338
  .then((xs) => xs.map((x) => x?.[key]))
332
339
  .then((keys) => {
333
340
  const whereDest = {
334
- [relationField.relations[1].referencedKey]: { $in: keys },
341
+ [relationField.relations[1].referencedKey]: { $in: keys }
335
342
  };
336
343
  const argsMapped = x.args == null ? {} : _.cloneDeep(x.args);
337
344
  if (typeof argsMapped?.$where === "object" &&
338
345
  argsMapped.$where != null &&
339
346
  argsMapped.$where[relationField.table] != null) {
340
347
  argsMapped.$where = {
341
- $and: [whereDest, argsMapped.$where[relationField.table]],
348
+ $and: [whereDest, argsMapped.$where[relationField.table]]
342
349
  };
343
350
  }
344
351
  else if (argsMapped.$where != null) {
@@ -352,7 +359,7 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
352
359
  args: argsMapped,
353
360
  fields: x.fields,
354
361
  artifacts,
355
- action: relationField.grabMany ? "findMany" : "findUnique",
362
+ action: relationField.grabMany ? "findMany" : "findUnique"
356
363
  }, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
357
364
  });
358
365
  }
@@ -375,7 +382,7 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
375
382
  args: argsMapped,
376
383
  fields: x.fields,
377
384
  artifacts,
378
- action: relationField.grabMany ? "findMany" : "findUnique",
385
+ action: relationField.grabMany ? "findMany" : "findUnique"
379
386
  }, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
380
387
  }
381
388
  throw new Error(`Invalid relationField.type`);
@@ -396,7 +403,7 @@ function paginate(data, args, primaryKey, action) {
396
403
  data = data.slice(offset, offset + limit);
397
404
  return {
398
405
  results: data,
399
- paginationInfo: { totalCount },
406
+ paginationInfo: { totalCount }
400
407
  };
401
408
  }
402
409
  if (typeof args?.$paginate?.first === "number" ||
@@ -427,8 +434,8 @@ function paginate(data, args, primaryKey, action) {
427
434
  ...connection.pageInfo,
428
435
  startCursor,
429
436
  endCursor,
430
- totalCount,
431
- },
437
+ totalCount
438
+ }
432
439
  };
433
440
  }
434
441
  }
@@ -13,7 +13,13 @@ export declare class RuntimeMSSQL implements IRuntime {
13
13
  $whereNeedsProcessing(where: any): boolean;
14
14
  $prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
15
15
  $shutdown(): Promise<void>;
16
+ $startTransaction(input?: {
17
+ isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
18
+ }): Promise<{
19
+ commit: () => Promise<void>;
20
+ rollback: () => Promise<void>;
21
+ dbCall: (q: string) => Promise<any>;
22
+ }>;
16
23
  private dbCall;
17
24
  private formatQuery;
18
- private beginTransaction;
19
25
  }
@@ -27,7 +27,7 @@ class RuntimeMSSQL {
27
27
  __classPrivateFieldSet(this, _RuntimeMSSQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
28
28
  }
29
29
  async resolve(input) {
30
- return (0, shared_1.resolve)(input, this.dbCall.bind(this), this.formatQuery.bind(this), this.beginTransaction.bind(this), __classPrivateFieldGet(this, _RuntimeMSSQL_dialect, "f"), __classPrivateFieldGet(this, _RuntimeMSSQL_middlewareHandler, "f"), input.context ?? {});
30
+ return (0, shared_1.resolve)(input, input.dbCall ?? this.dbCall.bind(this), this.formatQuery.bind(this), this.$startTransaction.bind(this), __classPrivateFieldGet(this, _RuntimeMSSQL_dialect, "f"), __classPrivateFieldGet(this, _RuntimeMSSQL_middlewareHandler, "f"), input.context ?? {});
31
31
  }
32
32
  async $queryRaw(sql, values) {
33
33
  return this.dbCall(this.formatQuery(sql, values ?? []));
@@ -44,15 +44,15 @@ class RuntimeMSSQL {
44
44
  async $shutdown() {
45
45
  await __classPrivateFieldGet(this, _RuntimeMSSQL_mssqlClient, "f").closePool();
46
46
  }
47
+ async $startTransaction(input) {
48
+ return __classPrivateFieldGet(this, _RuntimeMSSQL_mssqlClient, "f").beginTransaction(input?.isolationLevel);
49
+ }
47
50
  async dbCall(q) {
48
51
  return __classPrivateFieldGet(this, _RuntimeMSSQL_mssqlClient, "f").dbCall(q);
49
52
  }
50
53
  formatQuery(q, values) {
51
54
  return __classPrivateFieldGet(this, _RuntimeMSSQL_mssqlClient, "f").formatQuery(q, values);
52
55
  }
53
- beginTransaction() {
54
- return __classPrivateFieldGet(this, _RuntimeMSSQL_mssqlClient, "f").beginTransaction();
55
- }
56
56
  }
57
57
  exports.RuntimeMSSQL = RuntimeMSSQL;
58
58
  _RuntimeMSSQL_dialect = new WeakMap(), _RuntimeMSSQL_mssqlClient = new WeakMap(), _RuntimeMSSQL_middlewareHandler = new WeakMap();
@@ -14,7 +14,9 @@ export declare class RuntimeMySQL implements IRuntime {
14
14
  $whereNeedsProcessing(where: any): boolean;
15
15
  $prepareWhere(artifacts: IArtifacts, table: string, data: any): Promise<{}>;
16
16
  $shutdown(): Promise<void>;
17
+ $startTransaction(input?: {
18
+ isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE";
19
+ }): Promise<any>;
17
20
  private dbCall;
18
21
  private formatQuery;
19
- private beginTransaction;
20
22
  }
@@ -70,7 +70,7 @@ class RuntimeMySQL {
70
70
  __classPrivateFieldSet(this, _RuntimeMySQL_mysqlClient, new MySQL_1.MySQL(clientOpts), "f");
71
71
  }
72
72
  async resolve(input) {
73
- return (0, shared_1.resolve)(input, this.dbCall.bind(this), this.formatQuery.bind(this), this.beginTransaction.bind(this), __classPrivateFieldGet(this, _RuntimeMySQL_dialect, "f"), __classPrivateFieldGet(this, _RuntimeMySQL_middlewareHandler, "f"), input.context ?? {}, __classPrivateFieldGet(this, _RuntimeMySQL_clientCache, "f"));
73
+ return (0, shared_1.resolve)(input, input.dbCall ?? this.dbCall.bind(this), this.formatQuery.bind(this), this.$startTransaction.bind(this), __classPrivateFieldGet(this, _RuntimeMySQL_dialect, "f"), __classPrivateFieldGet(this, _RuntimeMySQL_middlewareHandler, "f"), input.context ?? {}, __classPrivateFieldGet(this, _RuntimeMySQL_clientCache, "f"));
74
74
  }
75
75
  async $queryRaw(sql, values) {
76
76
  return this.dbCall(this.formatQuery(sql, values ?? []));
@@ -90,15 +90,15 @@ class RuntimeMySQL {
90
90
  }
91
91
  await __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").endPool();
92
92
  }
93
+ async $startTransaction(input) {
94
+ return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").beginTransaction(input?.isolationLevel);
95
+ }
93
96
  dbCall(q) {
94
97
  return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").query(q);
95
98
  }
96
99
  formatQuery(q, values) {
97
100
  return SqlString.format(q, values);
98
101
  }
99
- beginTransaction() {
100
- return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").beginTransaction();
101
- }
102
102
  }
103
103
  exports.RuntimeMySQL = RuntimeMySQL;
104
104
  _RuntimeMySQL_dialect = new WeakMap(), _RuntimeMySQL_mysqlClient = new WeakMap(), _RuntimeMySQL_clientCache = new WeakMap(), _RuntimeMySQL_middlewareHandler = new WeakMap();
@@ -5,8 +5,9 @@ export declare class MSSQL {
5
5
  dbCall(q: string): Promise<any>;
6
6
  private mapResult;
7
7
  formatQuery(q: string, values: any[]): any;
8
- beginTransaction(): Promise<{
8
+ beginTransaction(isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE"): Promise<{
9
9
  commit: () => Promise<void>;
10
+ rollback: () => Promise<void>;
10
11
  dbCall: (q: string) => Promise<any>;
11
12
  }>;
12
13
  closePool(): Promise<void>;
@@ -42,7 +42,7 @@ class MSSQL {
42
42
  formatQuery(q, values) {
43
43
  return TSqlString.format(q, values);
44
44
  }
45
- async beginTransaction() {
45
+ async beginTransaction(isolationLevel) {
46
46
  const transaction = new mssql.Transaction(__classPrivateFieldGet(this, _MSSQL_pool, "f"));
47
47
  let rolledBack = false;
48
48
  transaction.on("rollback", (aborted) => {
@@ -56,13 +56,18 @@ class MSSQL {
56
56
  throw err;
57
57
  }
58
58
  const mapResult = this.mapResult.bind(this);
59
- await transaction.begin();
59
+ let isolationLevelNumber;
60
+ if (isolationLevel) {
61
+ isolationLevelNumber = mssql.ISOLATION_LEVEL[isolationLevel.replace(" ", "_")];
62
+ if (isolationLevelNumber == null) {
63
+ throw new Error(`Invalid isolationLevel: ${isolationLevel}`);
64
+ }
65
+ }
66
+ await transaction.begin(isolationLevelNumber);
60
67
  return {
61
68
  commit: () => transaction.commit().catch(handleError),
62
- dbCall: (q) => new mssql.Request(transaction)
63
- .query(q)
64
- .then(mapResult)
65
- .catch(handleError),
69
+ rollback: () => transaction.rollback(),
70
+ dbCall: (q) => new mssql.Request(transaction).query(q).then(mapResult).catch(handleError)
66
71
  };
67
72
  }
68
73
  async closePool() {
@@ -2,7 +2,7 @@ export declare class MySQL {
2
2
  pool: any;
3
3
  constructor(opts: any);
4
4
  query(...args: any[]): any;
5
- beginTransaction(): Promise<any>;
5
+ beginTransaction(isolationLevel?: "READ UNCOMMITTED" | "READ COMMITTED" | "REPEATABLE READ" | "SERIALIZABLE"): Promise<any>;
6
6
  getConnection(): Promise<any>;
7
7
  endPool(): Promise<void>;
8
8
  }
@@ -13,16 +13,29 @@ class MySQL {
13
13
  query(...args) {
14
14
  return P.using(this.getConnection(), (connection) => connection.queryAsync(...args));
15
15
  }
16
- async beginTransaction() {
16
+ async beginTransaction(isolationLevel) {
17
17
  return P.using(this.getConnection(), async (connection) => {
18
18
  async function handleError(err) {
19
19
  await connection.rollbackAsync();
20
20
  throw err;
21
21
  }
22
+ // https://dev.mysql.com/doc/refman/8.0/en/set-transaction.html#set-transaction-isolation-level
23
+ // Without any SESSION or GLOBAL keyword:
24
+ // The statement applies only to the next single transaction performed within the session.
25
+ if (isolationLevel != null) {
26
+ if (isolationLevel !== "READ UNCOMMITTED" &&
27
+ isolationLevel !== "READ COMMITTED" &&
28
+ isolationLevel !== "REPEATABLE READ" &&
29
+ isolationLevel !== "SERIALIZABLE") {
30
+ throw new Error(`Invalid isolationLevel: ${isolationLevel}`);
31
+ }
32
+ await connection.queryAsync(`SET TRANSACTION ISOLATION LEVEL ${isolationLevel};`);
33
+ }
22
34
  await connection.beginTransactionAsync();
23
35
  return {
24
36
  commit: () => connection.commitAsync().catch(handleError),
25
- dbCall: (q) => connection.queryAsync(q).catch(handleError),
37
+ rollback: () => connection.rollbackAsync(),
38
+ dbCall: (q) => connection.queryAsync(q).catch(handleError)
26
39
  };
27
40
  });
28
41
  }
@@ -96,7 +96,7 @@ function _stringifyWhere(where, table, escapeId, escape, result) {
96
96
  }
97
97
  const operator = keys[0];
98
98
  const operand = v[operator];
99
- if (operator === "$ne") {
99
+ if (operator === "$neq") {
100
100
  return `${table}.${escapeId(k)} ${getOperatorNeq(v)} ${printValue(operand)}`;
101
101
  }
102
102
  if (operator === "$gt") {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@technicity/data-service-generator",
3
- "version": "0.11.4",
3
+ "version": "0.11.5",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist"