@technicity/data-service-generator 0.6.0 → 0.7.2

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.
@@ -15,7 +15,7 @@ const TSqlString = require("tsqlstring");
15
15
  const json_schema_to_typescript_1 = require("json-schema-to-typescript");
16
16
  const getDuplicates_1 = require("../lib/getDuplicates");
17
17
  const isNotNullOrUndefined_1 = require("../lib/isNotNullOrUndefined");
18
- const mysql = require("../mysql");
18
+ const MySQL_1 = require("../runtime/lib/MySQL");
19
19
  // json-schema-to-typescript inlines everything. We don't want that,
20
20
  // so use `tsType`, and add imports manually.
21
21
  // https://github.com/bcherny/json-schema-to-typescript#custom-schema-properties
@@ -138,14 +138,14 @@ exports.generate = generate;
138
138
  function init(input) {
139
139
  const { database, user, password, host, port, server } = input;
140
140
  if (dialect === "mysql") {
141
- mysql.init({
141
+ let mysql = new MySQL_1.MySQL({
142
142
  user,
143
143
  password,
144
144
  host,
145
145
  port,
146
146
  database,
147
147
  });
148
- query = mysql.query;
148
+ query = mysql.query.bind(mysql);
149
149
  }
150
150
  if (dialect === "mssql" || dialect === "ksql") {
151
151
  const pool = new mssql.ConnectionPool({
@@ -1240,7 +1240,7 @@ const getRelationsOneToMany = _.memoize(async function getRelationsOneToMany(tab
1240
1240
  nullable: false,
1241
1241
  };
1242
1242
  })));
1243
- return _.sortBy((x) => x.referencedTable, xs);
1243
+ return _.sortBy((x) => x.referencedKey, _.sortBy((x) => x.referencedTable, xs));
1244
1244
  });
1245
1245
  async function getPrimaryColumn(table) {
1246
1246
  const tableMeta = await getTableMeta(table);
@@ -5,6 +5,8 @@ export declare class RuntimeKSQL implements IRuntime {
5
5
  #private;
6
6
  constructor(clientOpts: ConstructorParameters<typeof KSQL>[0], otherOpts: {
7
7
  getBaseTableName: TGetTableName;
8
+ getMaterializedViewName: TGetTableName;
9
+ doNotUseMaterializedViews?: boolean;
8
10
  _dbCall: TDbCall;
9
11
  }, artifacts: IArtifacts);
10
12
  resolve(input: TResolveParams): Promise<any>;
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _RuntimeKSQL_ksql, _RuntimeKSQL_middlewareHandler, _RuntimeKSQL_dbCall, _RuntimeKSQL_formatQuery, _RuntimeKSQL_getBaseTableName;
13
+ var _RuntimeKSQL_ksql, _RuntimeKSQL_middlewareHandler, _RuntimeKSQL_dbCall, _RuntimeKSQL_formatQuery, _RuntimeKSQL_getBaseTableName, _RuntimeKSQL_getMaterializedViewName, _RuntimeKSQL_doNotUseMaterializedViews;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.RuntimeKSQL = void 0;
16
16
  const SqlString = require("sqlstring");
@@ -30,14 +30,20 @@ class RuntimeKSQL {
30
30
  _RuntimeKSQL_dbCall.set(this, void 0);
31
31
  _RuntimeKSQL_formatQuery.set(this, void 0);
32
32
  _RuntimeKSQL_getBaseTableName.set(this, void 0);
33
+ _RuntimeKSQL_getMaterializedViewName.set(this, void 0);
34
+ _RuntimeKSQL_doNotUseMaterializedViews.set(this, void 0);
33
35
  __classPrivateFieldSet(this, _RuntimeKSQL_ksql, new ksql_1.KSQL(clientOpts ?? {}), "f");
34
36
  __classPrivateFieldSet(this, _RuntimeKSQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
35
- __classPrivateFieldSet(this, _RuntimeKSQL_dbCall, otherOpts._dbCall ?? __classPrivateFieldGet(this, _RuntimeKSQL_ksql, "f").streamQuery.bind(__classPrivateFieldGet(this, _RuntimeKSQL_ksql, "f")), "f");
37
+ __classPrivateFieldSet(this, _RuntimeKSQL_dbCall, typeof otherOpts._dbCall === "function"
38
+ ? (q) => otherOpts._dbCall(q).then(mapResult)
39
+ : (q) => __classPrivateFieldGet(this, _RuntimeKSQL_ksql, "f").streamQuery(q).then(mapResult), "f");
36
40
  __classPrivateFieldSet(this, _RuntimeKSQL_getBaseTableName, otherOpts.getBaseTableName, "f");
41
+ __classPrivateFieldSet(this, _RuntimeKSQL_getMaterializedViewName, otherOpts.getMaterializedViewName, "f");
42
+ __classPrivateFieldSet(this, _RuntimeKSQL_doNotUseMaterializedViews, otherOpts.doNotUseMaterializedViews ?? false, "f");
37
43
  __classPrivateFieldSet(this, _RuntimeKSQL_formatQuery, SqlString.format.bind(SqlString), "f");
38
44
  }
39
45
  async resolve(input) {
40
- return resolve(input, __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_formatQuery, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_middlewareHandler, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_getBaseTableName, "f"));
46
+ return resolve(input, __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_formatQuery, "f").bind(this), __classPrivateFieldGet(this, _RuntimeKSQL_middlewareHandler, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_getBaseTableName, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_getMaterializedViewName, "f"), __classPrivateFieldGet(this, _RuntimeKSQL_doNotUseMaterializedViews, "f"));
41
47
  }
42
48
  async $queryRaw(sql, values) {
43
49
  return __classPrivateFieldGet(this, _RuntimeKSQL_dbCall, "f").call(this, SqlString.format(sql, values ?? []));
@@ -53,8 +59,8 @@ class RuntimeKSQL {
53
59
  }
54
60
  }
55
61
  exports.RuntimeKSQL = RuntimeKSQL;
56
- _RuntimeKSQL_ksql = new WeakMap(), _RuntimeKSQL_middlewareHandler = new WeakMap(), _RuntimeKSQL_dbCall = new WeakMap(), _RuntimeKSQL_formatQuery = new WeakMap(), _RuntimeKSQL_getBaseTableName = new WeakMap();
57
- async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTableName) {
62
+ _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();
63
+ async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
58
64
  // https://github.com/prisma/prisma/blob/822198e5ba21535364d20c86901b8c3778ebf6a3/packages/client/src/runtime/getPrismaClient.ts#L1087
59
65
  let index = -1;
60
66
  if (middlewareHandler.length() > 0) {
@@ -66,21 +72,21 @@ async function resolve(input, dbCall, formatQuery, middlewareHandler, getBaseTab
66
72
  return nextMiddleware(paramsMaybeMutated, consumer);
67
73
  }
68
74
  const paramsChanged = { ...input, ...params };
69
- return _resolve(paramsChanged, dbCall, formatQuery, getBaseTableName);
75
+ return _resolve(paramsChanged, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
70
76
  };
71
77
  return resource.runInAsyncScope(() => consumer(params));
72
78
  }
73
- return _resolve(input, dbCall, formatQuery, getBaseTableName);
79
+ return _resolve(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
74
80
  }
75
- function _resolve(input, dbCall, formatQuery, getBaseTableName) {
81
+ function _resolve(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
76
82
  if (input.action === "findUnique") {
77
- return getData(input, dbCall, formatQuery, getBaseTableName);
83
+ return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
78
84
  }
79
85
  if (input.action === "findMany") {
80
- return getData(input, dbCall, formatQuery, getBaseTableName);
86
+ return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
81
87
  }
82
88
  if (input.action === "findManyPaginated") {
83
- return getData(input, dbCall, formatQuery, getBaseTableName);
89
+ return getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
84
90
  }
85
91
  if (input.action === "create") {
86
92
  throw new Error("Not implemented.");
@@ -99,34 +105,46 @@ function _resolve(input, dbCall, formatQuery, getBaseTableName) {
99
105
  }
100
106
  throw new Error("Invalid action: " + input.action);
101
107
  }
102
- async function getData(input, dbCall, formatQuery, getBaseTableName) {
108
+ async function getData(input, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
103
109
  const { action } = input;
104
110
  const grabMany = action === "findMany" ||
105
111
  action === "findManyPaginated" ||
106
112
  action === "updateMany" ||
107
113
  action === "deleteMany";
108
- const data = await _getData(input, grabMany, dbCall, formatQuery, getBaseTableName);
114
+ const data = await _getData(input, grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews);
109
115
  if (data == null && !grabMany) {
110
116
  throw new SDKNotFoundError_1.SDKNotFoundError();
111
117
  }
112
- // We only need to remove extra keys if input.fields isn't
113
- // specified, since otherwise there wouldn't be extra keys
114
- const shouldRemoveExtraKeys = input.fields != null;
118
+ const scalarFields = input.artifacts[input.resource].scalarFields;
115
119
  if (action === "findManyPaginated" && data?.results != null) {
116
- (0, shared_1.postProcess)(data.results, input.fields ?? [], shouldRemoveExtraKeys);
120
+ (0, shared_1.postProcess)(data.results, input.fields ?? scalarFields, true);
117
121
  }
118
122
  else {
119
- (0, shared_1.postProcess)(data, input.fields ?? [], shouldRemoveExtraKeys);
123
+ (0, shared_1.postProcess)(data, input.fields ?? scalarFields, true);
120
124
  }
121
125
  return data;
122
126
  }
123
- async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName) {
127
+ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
124
128
  const { resource: table, artifacts, args, action } = input;
125
129
  const tableArtifacts = artifacts[table];
130
+ const isInputFieldAnArray = Array.isArray(input.fields);
126
131
  let fields = input.fields;
127
132
  if (!Array.isArray(fields)) {
128
133
  fields = tableArtifacts.scalarFields;
129
134
  }
135
+ let useMaterializedView = !doNotUseMaterializedViews &&
136
+ // If fields isn't an array, we're only getting scalar fields,
137
+ // so no point using the materialized view.
138
+ isInputFieldAnArray &&
139
+ // We're getting relation fields
140
+ fields.some((x) => typeof x === "object") &&
141
+ Object.values(tableArtifacts.relationFields).filter((x) => x.type === "one-to-many__many-to-one" && x.kind === "many-to-one").length > 0 &&
142
+ !Object.values(tableArtifacts.relationFields).some((x) => x.type === "one-to-many__many-to-one" &&
143
+ x.kind === "many-to-one" &&
144
+ x.nullable === true);
145
+ const getTableName = useMaterializedView
146
+ ? getMaterializedViewName
147
+ : getBaseTableName;
130
148
  let where = undefined;
131
149
  if (args?.$where != null) {
132
150
  let argsMapped = args;
@@ -136,7 +154,7 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
136
154
  }
137
155
  const whereResult = (0, stringifyWhere_1.stringifyWhere)({
138
156
  where: argsMapped.$where,
139
- table: escapeId(getBaseTableName(table)),
157
+ table: escapeId(getTableName(table)),
140
158
  dialect: "mysql",
141
159
  args: argsMapped,
142
160
  });
@@ -161,15 +179,19 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
161
179
  }
162
180
  }
163
181
  else {
164
- relationFields.push(x);
165
182
  const relationField = tableArtifacts.relationFields?.[x.name];
166
- if (relationField != null) {
167
- if (relationField.type === "one-to-many__many-to-one" &&
168
- !columns.includes(relationField.relation.foreignKey)) {
183
+ if (relationField == null) {
184
+ throw new Error(`Invalid relation field: ${x.name}`);
185
+ }
186
+ if (relationField.type === "one-to-many__many-to-one") {
187
+ relationFields.push(x);
188
+ if (!columns.includes(relationField.relation.foreignKey)) {
169
189
  columns.push(relationField.relation.foreignKey);
170
190
  }
171
- else if (relationField.type === "many-to-many" &&
172
- !columns.includes(relationField.relations[0].referencedKey)) {
191
+ }
192
+ else if (relationField.type === "many-to-many") {
193
+ relationFields.push(x);
194
+ if (!columns.includes(relationField.relations[0].referencedKey)) {
173
195
  columns.push(relationField.relations[0].referencedKey);
174
196
  }
175
197
  }
@@ -181,7 +203,25 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
181
203
  columns.push(tableArtifacts.primaryKey);
182
204
  }
183
205
  }
184
- let s = formatQuery(`SELECT ?? FROM ??`, [columns, getBaseTableName(table)]);
206
+ if (useMaterializedView) {
207
+ relationFields = relationFields.filter((x) => {
208
+ if (typeof x === "string") {
209
+ return true;
210
+ }
211
+ const relationField = tableArtifacts.relationFields?.[x.name];
212
+ if (relationField == null) {
213
+ throw new Error(`Invalid relation field: ${x.name}`);
214
+ }
215
+ // Since we're going to use the materialized view,
216
+ // filter out non-nullable many-to-one relations.
217
+ return !(relationField.type === "one-to-many__many-to-one" &&
218
+ relationField.kind === "many-to-one" &&
219
+ relationField.nullable === false);
220
+ });
221
+ }
222
+ let s = useMaterializedView
223
+ ? formatQuery(`SELECT * FROM ??`, [getTableName(table)])
224
+ : formatQuery(`SELECT ?? FROM ??`, [columns, getTableName(table)]);
185
225
  if (where) {
186
226
  s += ` WHERE ${where}`;
187
227
  }
@@ -215,7 +255,7 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
215
255
  result = _.orderBy(result, orderBy.map((x) => x.column), orderBy.map((x) => x.direction));
216
256
  if (action === "findManyPaginated") {
217
257
  result = paginate(result, args, tableArtifacts.primaryKey, action);
218
- return Promise.all(result.results.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName).then((xx) => mergeSubResults(x, xx)))).then((xx) => {
258
+ return Promise.all(result.results.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(x, xx)))).then((xx) => {
219
259
  result.results = xx;
220
260
  return result;
221
261
  });
@@ -224,12 +264,12 @@ async function _getData(input, grabMany, dbCall, formatQuery, getBaseTableName)
224
264
  if (typeof limit === "number") {
225
265
  result = result.slice(0, limit);
226
266
  }
227
- return Promise.all(result.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName).then((xx) => mergeSubResults(x, xx))));
267
+ return Promise.all(result.map((x) => resolveDependentFields(table, x, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(x, xx))));
228
268
  }
229
- return resolveDependentFields(table, result, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName).then((xx) => mergeSubResults(result, xx));
269
+ return resolveDependentFields(table, result, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => mergeSubResults(result, xx));
230
270
  });
231
271
  }
232
- async function resolveDependentFields(table, parentData, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName) {
272
+ async function resolveDependentFields(table, parentData, mappedFields, relationFields, artifacts, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews) {
233
273
  const tableArtifacts = artifacts[table];
234
274
  return Promise.all([]
235
275
  .concat(mappedFields.map((x) => {
@@ -305,7 +345,7 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
305
345
  fields: x.fields,
306
346
  artifacts,
307
347
  action: relationField.grabMany ? "findMany" : "findUnique",
308
- }, relationField.grabMany, dbCall, formatQuery, getBaseTableName).then((xx) => ({ [x.as ?? x.name]: xx }));
348
+ }, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
309
349
  });
310
350
  }
311
351
  if (relationField.type === "one-to-many__many-to-one") {
@@ -328,7 +368,7 @@ async function resolveDependentFields(table, parentData, mappedFields, relationF
328
368
  fields: x.fields,
329
369
  artifacts,
330
370
  action: relationField.grabMany ? "findMany" : "findUnique",
331
- }, relationField.grabMany, dbCall, formatQuery, getBaseTableName).then((xx) => ({ [x.as ?? x.name]: xx }));
371
+ }, relationField.grabMany, dbCall, formatQuery, getBaseTableName, getMaterializedViewName, doNotUseMaterializedViews).then((xx) => ({ [x.as ?? x.name]: xx }));
332
372
  }
333
373
  throw new Error(`Invalid relationField.type`);
334
374
  })));
@@ -386,3 +426,13 @@ function paginate(data, args, primaryKey, action) {
386
426
  }
387
427
  throw new Error(`Invalid pagination action: ${action}`);
388
428
  }
429
+ function mapResult(data) {
430
+ return data.map((x) => {
431
+ for (let k of Object.keys(x)) {
432
+ if (typeof x[k] === "string" && (k === "uuid" || k.endsWith("Uuid"))) {
433
+ x[k] = x[k].toLocaleLowerCase();
434
+ }
435
+ }
436
+ return x;
437
+ });
438
+ }
@@ -10,15 +10,16 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _RuntimeMySQL_dialect, _RuntimeMySQL_middlewareHandler;
13
+ var _RuntimeMySQL_dialect, _RuntimeMySQL_mysqlClient, _RuntimeMySQL_middlewareHandler;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.RuntimeMySQL = void 0;
16
16
  const SqlString = require("sqlstring");
17
- const mysql_1 = require("../mysql");
17
+ const MySQL_1 = require("./lib/MySQL");
18
18
  const shared_1 = require("./lib/shared");
19
19
  class RuntimeMySQL {
20
20
  constructor(clientOpts, otherOpts, artifacts) {
21
21
  _RuntimeMySQL_dialect.set(this, "mysql");
22
+ _RuntimeMySQL_mysqlClient.set(this, void 0);
22
23
  _RuntimeMySQL_middlewareHandler.set(this, void 0);
23
24
  __classPrivateFieldSet(this, _RuntimeMySQL_middlewareHandler, new shared_1.MiddlewareHandler(), "f");
24
25
  if (otherOpts.supplementClientOpts) {
@@ -60,7 +61,7 @@ class RuntimeMySQL {
60
61
  ...clientOpts,
61
62
  };
62
63
  }
63
- (0, mysql_1.init)(clientOpts);
64
+ __classPrivateFieldSet(this, _RuntimeMySQL_mysqlClient, new MySQL_1.MySQL(clientOpts), "f");
64
65
  }
65
66
  async resolve(input) {
66
67
  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 ?? {});
@@ -78,14 +79,14 @@ class RuntimeMySQL {
78
79
  return (0, shared_1._prepareWhere)(artifacts, table, data, this.dbCall.bind(this), this.formatQuery.bind(this));
79
80
  }
80
81
  dbCall(q) {
81
- return (0, mysql_1.query)(q);
82
+ return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").query(q);
82
83
  }
83
84
  formatQuery(q, values) {
84
85
  return SqlString.format(q, values);
85
86
  }
86
87
  beginTransaction() {
87
- return (0, mysql_1.beginTransaction)();
88
+ return __classPrivateFieldGet(this, _RuntimeMySQL_mysqlClient, "f").beginTransaction();
88
89
  }
89
90
  }
90
91
  exports.RuntimeMySQL = RuntimeMySQL;
91
- _RuntimeMySQL_dialect = new WeakMap(), _RuntimeMySQL_middlewareHandler = new WeakMap();
92
+ _RuntimeMySQL_dialect = new WeakMap(), _RuntimeMySQL_mysqlClient = new WeakMap(), _RuntimeMySQL_middlewareHandler = new WeakMap();
@@ -0,0 +1,7 @@
1
+ export declare class MySQL {
2
+ pool: any;
3
+ constructor(opts: any);
4
+ query(...args: any[]): any;
5
+ beginTransaction(): Promise<any>;
6
+ getConnection(): Promise<any>;
7
+ }
@@ -0,0 +1,36 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.MySQL = void 0;
4
+ const mysql = require("mysql");
5
+ const P = require("bluebird");
6
+ P.promisifyAll(require("mysql/lib/Connection").prototype);
7
+ P.promisifyAll(require("mysql/lib/Pool").prototype);
8
+ class MySQL {
9
+ constructor(opts) {
10
+ this.pool = mysql.createPool(opts);
11
+ }
12
+ // http://bluebirdjs.com/docs/api/promise.using.html
13
+ query(...args) {
14
+ return P.using(this.getConnection(), (connection) => connection.queryAsync(...args));
15
+ }
16
+ async beginTransaction() {
17
+ return P.using(this.getConnection(), async (connection) => {
18
+ async function handleError(err) {
19
+ await connection.rollbackAsync();
20
+ throw err;
21
+ }
22
+ await connection.beginTransactionAsync();
23
+ return {
24
+ commit: () => connection.commitAsync().catch(handleError),
25
+ dbCall: (q) => connection.queryAsync(q).catch(handleError),
26
+ };
27
+ });
28
+ }
29
+ // http://bluebirdjs.com/docs/api/disposer.html
30
+ async getConnection() {
31
+ return this.pool
32
+ .getConnectionAsync()
33
+ .disposer((connection) => connection.release());
34
+ }
35
+ }
36
+ exports.MySQL = MySQL;
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@technicity/data-service-generator",
3
- "version": "0.6.0",
3
+ "version": "0.7.2",
4
4
  "main": "./dist/index.js",
5
5
  "files": [
6
6
  "dist"
7
7
  ],
8
8
  "scripts": {
9
9
  "compile": "rm -rf dist && tsc",
10
- "create-database": "env-cmd node ./test/mysql/create-database.js && env-cmd node ./test/mssql/create-database.js",
11
- "generate": "npm run compile && npm run create-database && env-cmd node ./test/mysql/generate.js && env-cmd node ./test/mssql/generate.js",
10
+ "create-database": "env-cmd node ./test/mysql/create-database.js && env-cmd node ./test/mysql8/create-database.js && env-cmd node ./test/mssql/create-database.js",
11
+ "generate": "npm run compile && npm run create-database && env-cmd node ./test/mysql/generate.js && env-cmd node ./test/mysql8/generate.js && env-cmd node ./test/mssql/generate.js",
12
12
  "test": "npm run generate && env-cmd mocha ./test/addNullFallbacks.test.js && env-cmd mocha ./test/stringifyWhere.test.js && env-cmd mocha ./test/test.js"
13
13
  },
14
14
  "dependencies": {
@@ -17,7 +17,7 @@
17
17
  "fs-extra": "10.0.0",
18
18
  "graphql": "15.8.0",
19
19
  "graphql-relay": "^0.9.0",
20
- "join-monster": "apalm/join-monster#340bcad96da4c268e874d07552d564c98ecf39ea",
20
+ "join-monster": "git+https://github.com/apalm/join-monster.git#340bcad96da4c268e874d07552d564c98ecf39ea",
21
21
  "json-schema-to-typescript": "10.1.5",
22
22
  "lodash": "^4.17.20",
23
23
  "mssql": "^6.3.1",
package/dist/mysql.d.ts DELETED
@@ -1,3 +0,0 @@
1
- export function init(opts: any): void;
2
- export function query(...args: any[]): any;
3
- export function beginTransaction(): Promise<any>;
package/dist/mysql.js DELETED
@@ -1,35 +0,0 @@
1
- "use strict";
2
- const mysql = require("mysql");
3
- const Promise = require("bluebird");
4
- Promise.promisifyAll(require("mysql/lib/Connection").prototype);
5
- Promise.promisifyAll(require("mysql/lib/Pool").prototype);
6
- let pool;
7
- function init(opts) {
8
- pool = mysql.createPool(opts);
9
- }
10
- // http://bluebirdjs.com/docs/api/disposer.html
11
- async function getConnection() {
12
- return pool
13
- .getConnectionAsync()
14
- .disposer((connection) => connection.release());
15
- }
16
- // http://bluebirdjs.com/docs/api/promise.using.html
17
- function query(...args) {
18
- return Promise.using(getConnection(), (connection) => connection.queryAsync(...args));
19
- }
20
- async function beginTransaction() {
21
- return Promise.using(getConnection(), async (connection) => {
22
- async function handleError(err) {
23
- await connection.rollbackAsync();
24
- throw err;
25
- }
26
- await connection.beginTransactionAsync();
27
- return {
28
- commit: () => connection.commitAsync().catch(handleError),
29
- dbCall: (q) => connection.queryAsync(q).catch(handleError),
30
- };
31
- });
32
- }
33
- module.exports.init = init;
34
- module.exports.query = query;
35
- module.exports.beginTransaction = beginTransaction;