Skip to content

Commit a23946f

Browse files
authored
tsc (#24)
* add tsc check in tests * some stuff * ts template fmt * fmt * transaction should use same connection * disable tsc check in github action * reuse pool connection when creating transaction * fix adapter in sqlite and mysql
1 parent de9e843 commit a23946f

File tree

8 files changed

+117
-54
lines changed

8 files changed

+117
-54
lines changed

.github/workflows/integration.yml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ jobs:
2727
strategy:
2828
matrix:
2929
postgres:
30-
- '14.2'
31-
- '16'
30+
- "14.2"
31+
- "16"
3232
runs-on: ubuntu-latest
3333
services:
3434
postgres:
@@ -105,14 +105,14 @@ jobs:
105105
run: |
106106
cd internal/integration
107107
yarn install
108-
# - name: yarn tsc
109-
# run: |
110-
# cd internal/integration
111-
# yarn tsc
112108
- name: yarn test
113109
run: |
114110
cd internal/integration
115111
yarn test
112+
# - name: yarn tsc
113+
# run: |
114+
# cd internal/integration
115+
# yarn tsc
116116
mysql-golang:
117117
needs: [build]
118118
runs-on: ubuntu-latest
@@ -191,14 +191,14 @@ jobs:
191191
run: |
192192
cd internal/integration
193193
yarn install
194-
# - name: yarn tsc
195-
# run: |
196-
# cd internal/integration
197-
# yarn tsc
198194
- name: yarn test
199195
run: |
200196
cd internal/integration
201197
yarn test
198+
# - name: yarn tsc
199+
# run: |
200+
# cd internal/integration
201+
# yarn tsc
202202
sqlite-golang:
203203
needs: [build]
204204
runs-on: ubuntu-latest
@@ -249,11 +249,11 @@ jobs:
249249
run: |
250250
cd internal/integration
251251
yarn install
252-
# - name: yarn tsc
253-
# run: |
254-
# cd internal/integration
255-
# yarn tsc
256252
- name: yarn test
257253
run: |
258254
cd internal/integration
259255
yarn test
256+
# - name: yarn tsc
257+
# run: |
258+
# cd internal/integration
259+
# yarn tsc

Makefile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ test-mysql: install
3333
cd internal/integration && queryx db:migrate --schema mysql.hcl
3434
cd internal/integration && queryx db:migrate --schema mysql.hcl
3535
cd internal/integration && queryx generate --schema mysql.hcl
36-
# cd internal/integration && yarn tsc
37-
# cd internal/integration && yarn test
38-
cd internal/integration && go test ./...
36+
cd internal/integration && yarn tsc
37+
cd internal/integration && yarn test
38+
# cd internal/integration && go test ./...
3939

4040
test-sqlite: install
4141
rm -rf internal/integration/db
@@ -44,9 +44,9 @@ test-sqlite: install
4444
cd internal/integration && queryx db:migrate --schema sqlite.hcl
4545
cd internal/integration && queryx db:migrate --schema sqlite.hcl
4646
cd internal/integration && queryx generate --schema sqlite.hcl
47-
# cd internal/integration && yarn tsc
48-
# cd internal/integration && yarn test
49-
cd internal/integration && go test ./...
47+
cd internal/integration && yarn tsc
48+
cd internal/integration && yarn test
49+
# cd internal/integration && go test ./...
5050

5151
test: test-postgresql test-sqlite test-mysql
5252

generator/client/typescript/templates/[model]/[model].tstmpl

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,11 +107,7 @@ export class {{ $.model.Name }} {
107107
toJSON() {
108108
return {
109109
{{- range $c := $.model.Columns }}
110-
{{- if eq $c.Type "bigint"}}
111-
{{ $c.Name | camel }}: Number(this.{{ $c.Name | camel }}),
112-
{{- else }}
113110
{{ $c.Name | camel }}: this.{{ $c.Name | camel }},
114-
{{- end }}
115111
{{- end }}
116112
{{- range $b := $.model.BelongsTo }}
117113
{{ $b.Name | camel }}: this.{{ $b.Name | camel }},

generator/client/typescript/templates/queryx/adapter.mysql.ts

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,34 +5,46 @@ import { parse } from "date-fns";
55
import { Config } from "./config";
66

77
export class Adapter {
8+
public config: Config;
9+
public pool: mysql.Pool;
810
public db: mysql.Pool;
911

1012
constructor(config: Config) {
11-
this.db = mysql.createPool({
12-
uri: config.url,
13+
this.config = config;
14+
}
15+
16+
connect() {
17+
const pool = mysql.createPool({
18+
uri: this.config.url,
1319
});
20+
this.pool = pool;
21+
this.db = pool;
22+
}
23+
24+
newClient() {
25+
return this.pool.getConnection();
26+
}
27+
28+
release() {
29+
this.db.release();
1430
}
1531

1632
async query<R>(query: string, ...args: any[]) {
17-
console.log(query, args);
1833
let [rows] = await this.db.query<R & RowDataPacket[]>(query, args);
1934
return rows;
2035
}
2136

2237
async queryOne<R>(query: string, ...args: any[]) {
23-
console.log(query, args);
2438
let [rows] = await this.db.query<R & RowDataPacket[]>(query, args);
2539
return rows[0] || null;
2640
}
2741

2842
async exec(query: string, ...args: any[]) {
29-
console.log(query, args);
3043
let [res] = await this.db.execute<ResultSetHeader>(query, args);
3144
return res.affectedRows;
3245
}
3346

3447
async _exec(query: string, ...args: any[]) {
35-
console.log(query, args);
3648
let [res] = await this.db.execute<ResultSetHeader>(query, args);
3749
return res;
3850
}
@@ -50,8 +62,6 @@ export class Adapter {
5062
}
5163
}
5264

53-
export function rebind<T extends any[] = any[]>(query: string, args?: T) {}
54-
5565
export function adapterValue(type: string, value: any) {
5666
switch (type) {
5767
case "time":

generator/client/typescript/templates/queryx/adapter.postgresql.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@ import { Config } from "./config";
77
types.setTypeParser(types.builtins.INT8, (val) => parseInt(val, 10));
88

99
export class Adapter {
10-
private db: Pool;
10+
public config: Config;
11+
public pool: Pool;
12+
public db: Pool;
1113

1214
constructor(config: Config) {
13-
this.db = new Pool({
14-
connectionString: config.url,
15+
this.config = config;
16+
}
17+
18+
connect() {
19+
const pool = new Pool({
20+
connectionString: this.config.url,
1521
});
22+
this.pool = pool;
23+
this.db = pool;
24+
}
25+
26+
newClient() {
27+
return this.pool.connect();
28+
}
29+
30+
release() {
31+
this.db.release();
1632
}
1733

1834
private _query<R extends QueryResultRow = any, I extends any[] = any[]>(
1935
query: string,
20-
args?: I
36+
args?: I,
2137
) {
2238
let [query1, args1] = rebind<I>(query, args);
23-
console.log(query1, args1);
2439
return this.db.query<R, I>(query1, args1);
2540
}
2641

generator/client/typescript/templates/queryx/adapter.sqlite.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,37 @@ import { format, parse } from "date-fns";
55
import { Config } from "./config";
66

77
export class Adapter {
8+
public config: Config;
89
private db;
910

1011
constructor(config: Config) {
11-
this.db = new Database(config.url);
12+
this.config = config;
1213
}
1314

15+
connect() {
16+
this.db = new Database(this.config.url);
17+
}
18+
19+
newClient() {
20+
return new Database(this.config.url);
21+
}
22+
23+
release() {}
24+
1425
query<R>(query: string, ...args: any[]): R[] {
1526
let [query1, args1] = rebind(query, args);
16-
console.log(query1, args1);
1727
let stmt = this.db.prepare(query1);
1828
return stmt.all(...args1) as R[];
1929
}
2030

2131
queryOne<R>(query: string, ...args: any[]): R {
2232
let [query1, args1] = rebind(query, args);
23-
console.log(query1, args1);
2433
let stmt = this.db.prepare(query1);
2534
return stmt.get(...args1) as R;
2635
}
2736

2837
async exec(query: string, ...args: any[]) {
2938
let [query1, args1] = rebind(query, args);
30-
console.log(query1, args1);
3139
let stmt = this.db.prepare(query1);
3240
let res = stmt.run(...args1);
3341
return res.changes;

generator/client/typescript/templates/queryx/client.tstmpl

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,34 @@
22

33
import { Env, Config, newConfig, getenv } from "./config";
44
{{- range $m := $.client.Models }}
5-
import { {{ $m.Name }}Query } from "../{{ $m.Name | snake }}"
5+
import { {{ $m.Name }}Query } from "../{{ $m.Name | snake }}";
66
{{- end }}
7-
import { Table, BigIntColumn, IntegerColumn, FloatColumn, BooleanColumn, StringColumn, TextColumn, DateColumn, TimeColumn, DatetimeColumn, UUIDColumn, JSONColumn } from "./table";
7+
import {
8+
Table,
9+
BigIntColumn,
10+
IntegerColumn,
11+
FloatColumn,
12+
BooleanColumn,
13+
StringColumn,
14+
TextColumn,
15+
DateColumn,
16+
TimeColumn,
17+
DatetimeColumn,
18+
UUIDColumn,
19+
JSONColumn,
20+
} from "./table";
821
import { Adapter } from "./adapter";
922
import { Clause } from "./clause";
1023

1124
export class QXClient {
1225
public config: Config;
1326
public adapter: Adapter;
14-
{{- range $m := $.client.Models }}
15-
public {{ $m.Name | camel }}: Table;
16-
{{- range $c := .Columns }}
17-
public {{ $m.Name | camel }}{{ $c.Name | pascal }}: {{goType .Type}}Column
18-
{{- end}}
19-
{{- end}}
27+
{{- range $m := $.client.Models }}
28+
public {{ $m.Name | camel }}: Table;
29+
{{- range $c := .Columns }}
30+
public {{ $m.Name | camel }}{{ $c.Name | pascal }}: {{ goType .Type }}Column;
31+
{{- end}}
32+
{{- end}}
2033

2134
constructor(config: Config) {
2235
this.config = config;
@@ -49,23 +62,26 @@ export class QXClient {
4962
{{- end }}
5063

5164
raw(fragment: string, ...args: any[]) {
52-
return new Clause(fragment, args)
65+
return new Clause(fragment, args);
5366
}
5467

5568
async tx() {
5669
const tx = new Tx(this.config);
70+
tx.adapter.db = await this.adapter.newClient();
5771
await tx.adapter.beginTx();
5872
return tx;
5973
}
6074

61-
async transaction(fn: (t: Tx) => void) {
62-
const tx = await this.tx()
75+
async transaction(fn: (t: Tx) => Promise<void>) {
76+
const tx = await this.tx();
6377
try {
6478
await fn(tx);
6579
await tx.commit();
6680
} catch (err) {
6781
await tx.rollback();
6882
throw err;
83+
} finally {
84+
tx.adapter.release();
6985
}
7086
}
7187

@@ -74,7 +90,7 @@ export class QXClient {
7490
}
7591

7692
or(...clauses: Clause[]) {
77-
return clauses[0].or(...clauses.slice(1))
93+
return clauses[0].or(...clauses.slice(1));
7894
}
7995
}
8096

@@ -100,5 +116,6 @@ export const newClient = () => {
100116
export const newClientWithEnv = (env: Env) => {
101117
let config = newConfig(env);
102118
let client = new QXClient(config);
119+
client.adapter.connect();
103120
return client;
104121
};

internal/integration/client.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ test("timestamps", async () => {
139139
expect(user.updatedAt).not.toBeNull();
140140
expect(user.createdAt).toEqual(user.updatedAt);
141141

142+
await new Promise((resolve) => setTimeout(resolve, 10));
142143
await user.update({ name: "new name" });
143-
console.log(user.createdAt, user.updatedAt);
144144
expect(user.updatedAt > user.createdAt).toBe(true);
145145
});
146146

@@ -387,6 +387,23 @@ test("transactionBlock", async () => {
387387
expect(total).toEqual(2);
388388
});
389389

390+
test("transaction with pool connection", async () => {
391+
await c.queryTag().deleteAll();
392+
393+
let tx = await c.tx();
394+
let tag1 = await tx.queryTag().create({ name: "tag1" });
395+
396+
await Promise.all([
397+
tx.queryOne("select 1"),
398+
tag1.update({ name: "tag1-updated" }),
399+
]);
400+
401+
await tx.commit();
402+
403+
let tags = await c.query<{ name: string }>("select name from tags");
404+
expect(tags).toEqual([{ name: "tag1-updated" }]);
405+
});
406+
390407
test("changeJSON", async () => {
391408
let userChange = new UserChange({
392409
name: "user name",

0 commit comments

Comments
 (0)