@technicity/data-service-generator 0.6.1 → 0.7.0
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.
- package/CHANGELOG.md +166 -0
- package/dist/generation/generate.js +4 -4
- package/dist/runtime/RuntimeMySQL.js +7 -6
- package/dist/runtime/lib/mysql.d.ts +7 -0
- package/dist/runtime/lib/mysql.js +36 -0
- package/package.json +3 -3
- package/dist/mysql.d.ts +0 -3
- package/dist/mysql.js +0 -35
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
# 0.7.0 [2022.01.17]
|
|
2
|
+
|
|
3
|
+
- Add support for MySQL 8
|
|
4
|
+
|
|
5
|
+
# 0.5.12 [2021.11.18]
|
|
6
|
+
|
|
7
|
+
- Add `$queryRaw`
|
|
8
|
+
|
|
9
|
+
```ts
|
|
10
|
+
await sdk.$queryRaw("SELECT * FROM Foo WHERE id = ?", [2]);
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Use the `?` character as a placeholder for values that need to be escaped.
|
|
14
|
+
|
|
15
|
+
# 0.5.12 [2021.11.18]
|
|
16
|
+
|
|
17
|
+
- Add support for nested creates. All of the specified creates are wrapped in a transaction.
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
await sdk.postFoo({
|
|
21
|
+
foo: "blah",
|
|
22
|
+
barList: {
|
|
23
|
+
$create: [
|
|
24
|
+
{
|
|
25
|
+
note: "blah",
|
|
26
|
+
bazList: { $create: [{ baz: "asdf" }, { baz: "zzz" }] },
|
|
27
|
+
},
|
|
28
|
+
{ note: "blah 2" },
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
In the above example, 5 records are created: 1 `Foo` record, 2 `Bar` records, and 2 `Baz` records.
|
|
35
|
+
|
|
36
|
+
# 0.5.7 [2021.10.12]
|
|
37
|
+
|
|
38
|
+
- Add `$nlike` operator. Equivalent to SQL's `NOT LIKE` operator.
|
|
39
|
+
|
|
40
|
+
```ts
|
|
41
|
+
await sdk.getFooList({
|
|
42
|
+
$where: { bar: { $nlike: "%baz%" } },
|
|
43
|
+
});
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
# 0.5.5 [2021.10.12]
|
|
47
|
+
|
|
48
|
+
- Add limit + offset pagination
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
await sdk.getFooListPaginated({
|
|
52
|
+
$paginate: { limit: 5, offset: 2 },
|
|
53
|
+
});
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
# 0.5.0 [2021.10.08]
|
|
57
|
+
|
|
58
|
+
- Add support for middleware
|
|
59
|
+
|
|
60
|
+
Generated SDKs now have a `$use` method, which takes a single argument: your middleware function.
|
|
61
|
+
|
|
62
|
+
Example middleware that logs the amount of time an operation took:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
sdk.$use(async (params, next) => {
|
|
66
|
+
const start = process.hrtime();
|
|
67
|
+
const result = await next(params);
|
|
68
|
+
const end = process.hrtime(start);
|
|
69
|
+
console.log(`${params.resource}.${params.action} took ${end[1] / 1000000}ms`);
|
|
70
|
+
return result;
|
|
71
|
+
});
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Migrating from 0.4.x
|
|
75
|
+
|
|
76
|
+
1. `npm install -S @technicity/data-service-generator@0.5.0`
|
|
77
|
+
2. Regenerate SDK
|
|
78
|
+
|
|
79
|
+
# 0.4.0 [2021.09.17]
|
|
80
|
+
|
|
81
|
+
- Implement operations on update for string, number fields
|
|
82
|
+
|
|
83
|
+
## string
|
|
84
|
+
|
|
85
|
+
### `$prepend`
|
|
86
|
+
|
|
87
|
+
Prepend a string to the current value. If the current value is `null`, the value is not updated.
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
await sdk.patchFoo(
|
|
91
|
+
{ id: 1 },
|
|
92
|
+
{ bar: { $prepend: `BAZ_` }
|
|
93
|
+
);
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### `$append`
|
|
97
|
+
|
|
98
|
+
Append a string to the current value. If the current value is `null`, the value is not updated.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
await sdk.patchFoo(
|
|
102
|
+
{ id: 1 },
|
|
103
|
+
{ bar: { $append: `_BAZ` }
|
|
104
|
+
);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## number
|
|
108
|
+
|
|
109
|
+
### `$increment`
|
|
110
|
+
|
|
111
|
+
Increment the current value. If the current value is `null`, the value is not updated.
|
|
112
|
+
|
|
113
|
+
```ts
|
|
114
|
+
await sdk.patchBar(
|
|
115
|
+
{ id: 1 },
|
|
116
|
+
{ foo: { $increment: 5 }
|
|
117
|
+
);
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### `$decrement`
|
|
121
|
+
|
|
122
|
+
Decrement the current value. If the current value is `null`, the value is not updated.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
await sdk.patchBar(
|
|
126
|
+
{ id: 1 },
|
|
127
|
+
{ foo: { $decrement: 5 }
|
|
128
|
+
);
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
## Migrating from 0.3.x
|
|
132
|
+
|
|
133
|
+
1. `npm install -S @technicity/data-service-generator@0.4.0`
|
|
134
|
+
2. Regenerate SDK
|
|
135
|
+
|
|
136
|
+
# 0.3.1 [2021.09.09]
|
|
137
|
+
|
|
138
|
+
- [MSSQL] Allow passing custom `typeCast`
|
|
139
|
+
|
|
140
|
+
# 0.3.0 [2021.09.08]
|
|
141
|
+
|
|
142
|
+
- For the generated SDK, instead of specifying a `dialect`, specify a `runtime`. This reduces the need to regenerate SDKs, since updates and bugfixes to the runtime side can be obtained solely by updating the `@technicity/data-service-generator` version, instead of both updating the `@technicity/data-service-generator` version and regenerating the SDK. It also allows for custom runtimes that live outside this package. `@technicity/data-service-generator` ships with 3 runtimes: `RuntimeMySQL`, `RuntimeMSSQL`, `RuntimeKSQL`.
|
|
143
|
+
- Add KSQL support
|
|
144
|
+
- `driverOpts` -> `clientOpts`
|
|
145
|
+
- Add `otherOpts` to allow passing extra runtime-specific options
|
|
146
|
+
|
|
147
|
+
## Migrating from 0.2.x
|
|
148
|
+
|
|
149
|
+
1. `npm install -S @technicity/data-service-generator@0.3.0`
|
|
150
|
+
2. Regenerate SDK
|
|
151
|
+
3. Update SDK instantiation:
|
|
152
|
+
|
|
153
|
+
```diff
|
|
154
|
+
+ import { RuntimeMySQL } from "@technicity/data-service-generator/dist/runtime/RuntimeMySQL";
|
|
155
|
+
|
|
156
|
+
const sdk = new SDK({
|
|
157
|
+
- dialect: "mysql",
|
|
158
|
+
+ runtime: RuntimeMySQL,
|
|
159
|
+
- driverOpts: {
|
|
160
|
+
+ clientOpts: {
|
|
161
|
+
database: "MY_DATABASE",
|
|
162
|
+
user: "MY_USER",
|
|
163
|
+
password: "MY_PASSWORD",
|
|
164
|
+
},
|
|
165
|
+
});
|
|
166
|
+
```
|
|
@@ -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
|
|
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.
|
|
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);
|
|
@@ -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
|
|
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
|
-
(
|
|
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 (
|
|
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 (
|
|
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,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.
|
|
3
|
+
"version": "0.7.0",
|
|
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": {
|
package/dist/mysql.d.ts
DELETED
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;
|