From e0a7e1a5178612403d8615071714eaf2c00ea3f9 Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Fri, 11 Aug 2023 10:01:48 +0800 Subject: [PATCH 1/8] add tsc check in tests --- .github/workflows/integration.yml | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index a9f9a50a..2c1164ab 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -97,14 +97,14 @@ jobs: run: | cd internal/integration yarn install - # - name: yarn tsc - # run: | - # cd internal/integration - # yarn tsc - name: yarn test run: | cd internal/integration yarn test + - name: yarn tsc + run: | + cd internal/integration + yarn tsc mysql-golang: needs: [build] runs-on: ubuntu-latest @@ -183,14 +183,14 @@ jobs: run: | cd internal/integration yarn install - # - name: yarn tsc - # run: | - # cd internal/integration - # yarn tsc - name: yarn test run: | cd internal/integration yarn test + - name: yarn tsc + run: | + cd internal/integration + yarn tsc sqlite-golang: needs: [build] runs-on: ubuntu-latest @@ -241,11 +241,11 @@ jobs: run: | cd internal/integration yarn install - # - name: yarn tsc - # run: | - # cd internal/integration - # yarn tsc - name: yarn test run: | cd internal/integration yarn test + - name: yarn tsc + run: | + cd internal/integration + yarn tsc From de03ecb796076fcc73b503cbf0ca4853f8e8ee70 Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Sun, 13 Aug 2023 10:26:27 +0800 Subject: [PATCH 2/8] some stuff --- Makefile | 32 +++++++++---------- .../templates/[model]/[model].tstmpl | 10 +++--- .../templates/queryx/adapter.mysql.ts | 6 ---- .../templates/queryx/adapter.postgresql.ts | 1 - .../templates/queryx/adapter.sqlite.ts | 3 -- internal/integration/client.test.ts | 1 - internal/integration/mysql.hcl | 6 ++-- internal/integration/postgresql.hcl | 6 ++-- internal/integration/sqlite.hcl | 6 ++-- 9 files changed, 30 insertions(+), 41 deletions(-) diff --git a/Makefile b/Makefile index 8b40e601..c6a9542e 100644 --- a/Makefile +++ b/Makefile @@ -16,34 +16,34 @@ clean: test-postgresql: install rm -rf internal/integration/db - cd internal/integration && QUERYX_ENV=test queryx db:drop --schema postgresql.hcl - cd internal/integration && QUERYX_ENV=test queryx db:create --schema postgresql.hcl - cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema postgresql.hcl - cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema postgresql.hcl - cd internal/integration && QUERYX_ENV=test queryx generate --schema postgresql.hcl + cd internal/integration && queryx db:drop --schema postgresql.hcl + cd internal/integration && queryx db:create --schema postgresql.hcl + cd internal/integration && queryx db:migrate --schema postgresql.hcl + cd internal/integration && queryx db:migrate --schema postgresql.hcl + cd internal/integration && queryx generate --schema postgresql.hcl cd internal/integration && yarn tsc cd internal/integration && yarn test # cd internal/integration && go test ./... - # cd internal/integration && QUERYX_ENV=test queryx db:drop --schema postgresql.hcl + # cd internal/integration && queryx db:drop --schema postgresql.hcl test-mysql: install rm -rf internal/integration/db - cd internal/integration && QUERYX_ENV=test queryx db:drop --schema mysql.hcl - cd internal/integration && QUERYX_ENV=test queryx db:create --schema mysql.hcl - cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema mysql.hcl - cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema mysql.hcl - cd internal/integration && QUERYX_ENV=test queryx generate --schema mysql.hcl + cd internal/integration && queryx db:drop --schema mysql.hcl + cd internal/integration && queryx db:create --schema mysql.hcl + cd internal/integration && queryx db:migrate --schema mysql.hcl + cd internal/integration && queryx db:migrate --schema mysql.hcl + cd internal/integration && queryx generate --schema mysql.hcl cd internal/integration && yarn tsc cd internal/integration && yarn test # cd internal/integration && go test ./... test-sqlite: install rm -rf internal/integration/db - cd internal/integration && QUERYX_ENV=test queryx db:drop --schema sqlite.hcl - cd internal/integration && QUERYX_ENV=test queryx db:create --schema sqlite.hcl - cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema sqlite.hcl - cd internal/integration && QUERYX_ENV=test queryx db:migrate --schema sqlite.hcl - cd internal/integration && QUERYX_ENV=test queryx generate --schema sqlite.hcl + cd internal/integration && queryx db:drop --schema sqlite.hcl + cd internal/integration && queryx db:create --schema sqlite.hcl + cd internal/integration && queryx db:migrate --schema sqlite.hcl + cd internal/integration && queryx db:migrate --schema sqlite.hcl + cd internal/integration && queryx generate --schema sqlite.hcl cd internal/integration && yarn tsc cd internal/integration && yarn test # cd internal/integration && go test ./... diff --git a/generator/client/typescript/templates/[model]/[model].tstmpl b/generator/client/typescript/templates/[model]/[model].tstmpl index 168f52c8..a8b1a5de 100644 --- a/generator/client/typescript/templates/[model]/[model].tstmpl +++ b/generator/client/typescript/templates/[model]/[model].tstmpl @@ -16,7 +16,11 @@ export interface {{ $.model.Name }}Row {} export class {{ $.model.Name }} { {{- range $c := $.model.Columns }} - public {{ $c.Name | camel }}: {{ tsType $c.Type }}{{ if $c.Null }} | null{{ end }} = null; + {{- if $c.Null }} + public {{ $c.Name | camel }}?: {{ tsType $c.Type }} | null; + {{- else }} + public {{ $c.Name | camel }}?: {{ tsType $c.Type }}; + {{- end }} {{- end }} {{- range $b := $.model.BelongsTo }} public {{ $b.Name | camel }}: {{ $b.ModelName }} | null; @@ -107,11 +111,7 @@ export class {{ $.model.Name }} { toJSON() { return { {{- range $c := $.model.Columns }} - {{- if eq $c.Type "bigint"}} - {{ $c.Name | camel }}: Number(this.{{ $c.Name | camel }}), - {{- else }} {{ $c.Name | camel }}: this.{{ $c.Name | camel }}, - {{- end }} {{- end }} {{- range $b := $.model.BelongsTo }} {{ $b.Name | camel }}: this.{{ $b.Name | camel }}, diff --git a/generator/client/typescript/templates/queryx/adapter.mysql.ts b/generator/client/typescript/templates/queryx/adapter.mysql.ts index caf31ad1..d6508b43 100644 --- a/generator/client/typescript/templates/queryx/adapter.mysql.ts +++ b/generator/client/typescript/templates/queryx/adapter.mysql.ts @@ -14,25 +14,21 @@ export class Adapter { } async query(query: string, ...args: any[]) { - console.log(query, args); let [rows] = await this.db.query(query, args); return rows; } async queryOne(query: string, ...args: any[]) { - console.log(query, args); let [rows] = await this.db.query(query, args); return rows[0] || null; } async exec(query: string, ...args: any[]) { - console.log(query, args); let [res] = await this.db.execute(query, args); return res.affectedRows; } async _exec(query: string, ...args: any[]) { - console.log(query, args); let [res] = await this.db.execute(query, args); return res; } @@ -50,8 +46,6 @@ export class Adapter { } } -export function rebind(query: string, args?: T) {} - export function adapterValue(type: string, value: any) { switch (type) { case "time": diff --git a/generator/client/typescript/templates/queryx/adapter.postgresql.ts b/generator/client/typescript/templates/queryx/adapter.postgresql.ts index 72a4aa33..6aeddf89 100644 --- a/generator/client/typescript/templates/queryx/adapter.postgresql.ts +++ b/generator/client/typescript/templates/queryx/adapter.postgresql.ts @@ -20,7 +20,6 @@ export class Adapter { args?: I ) { let [query1, args1] = rebind(query, args); - console.log(query1, args1); return this.db.query(query1, args1); } diff --git a/generator/client/typescript/templates/queryx/adapter.sqlite.ts b/generator/client/typescript/templates/queryx/adapter.sqlite.ts index cf43ea53..258d8af6 100644 --- a/generator/client/typescript/templates/queryx/adapter.sqlite.ts +++ b/generator/client/typescript/templates/queryx/adapter.sqlite.ts @@ -13,21 +13,18 @@ export class Adapter { query(query: string, ...args: any[]): R[] { let [query1, args1] = rebind(query, args); - console.log(query1, args1); let stmt = this.db.prepare(query1); return stmt.all(...args1) as R[]; } queryOne(query: string, ...args: any[]): R { let [query1, args1] = rebind(query, args); - console.log(query1, args1); let stmt = this.db.prepare(query1); return stmt.get(...args1) as R; } async exec(query: string, ...args: any[]) { let [query1, args1] = rebind(query, args); - console.log(query1, args1); let stmt = this.db.prepare(query1); let res = stmt.run(...args1); return res.changes; diff --git a/internal/integration/client.test.ts b/internal/integration/client.test.ts index b6a4a56f..0326837a 100644 --- a/internal/integration/client.test.ts +++ b/internal/integration/client.test.ts @@ -137,7 +137,6 @@ test("timestamps", async () => { expect(user.createdAt).toEqual(user.updatedAt); await user.update({ name: "new name" }); - console.log(user.createdAt, user.updatedAt); expect(user.updatedAt > user.createdAt).toBe(true); }); diff --git a/internal/integration/mysql.hcl b/internal/integration/mysql.hcl index a329c30e..eb6d30e5 100644 --- a/internal/integration/mysql.hcl +++ b/internal/integration/mysql.hcl @@ -9,9 +9,9 @@ database "db" { url = env("DATABASE_URL") } - generator "client-golang" { - test = true - } + // generator "client-golang" { + // test = true + // } generator "client-typescript" { test = true } diff --git a/internal/integration/postgresql.hcl b/internal/integration/postgresql.hcl index 80bda1c8..d059f2e4 100644 --- a/internal/integration/postgresql.hcl +++ b/internal/integration/postgresql.hcl @@ -9,9 +9,9 @@ database "db" { url = env("DATABASE_URL") } - generator "client-golang" { - test = true - } + // generator "client-golang" { + // test = true + // } generator "client-typescript" { test = true } diff --git a/internal/integration/sqlite.hcl b/internal/integration/sqlite.hcl index c056094a..eb9e5bd1 100644 --- a/internal/integration/sqlite.hcl +++ b/internal/integration/sqlite.hcl @@ -9,9 +9,9 @@ database "db" { url = env("DATABASE_URL") } - generator "client-golang" { - test = true - } + // generator "client-golang" { + // test = true + // } generator "client-typescript" { test = true } From bb2836f486eeddb46f6a4e41baebd223298a7aff Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Mon, 16 Oct 2023 14:19:27 +0800 Subject: [PATCH 3/8] ts template fmt --- .../typescript/templates/queryx/client.tstmpl | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/generator/client/typescript/templates/queryx/client.tstmpl b/generator/client/typescript/templates/queryx/client.tstmpl index b7c752da..5c755186 100644 --- a/generator/client/typescript/templates/queryx/client.tstmpl +++ b/generator/client/typescript/templates/queryx/client.tstmpl @@ -11,12 +11,12 @@ import { Clause } from "./clause"; export class QXClient { public config: Config; public adapter: Adapter; - {{- range $m := $.client.Models }} - public {{ $m.Name | camel }}: Table; - {{- range $c := .Columns }} - public {{ $m.Name | camel }}{{ $c.Name | pascal }}: {{goType .Type}}Column - {{- end}} - {{- end}} + {{- range $m := $.client.Models }} + public {{ $m.Name | camel }}: Table; + {{- range $c := .Columns }} + public {{ $m.Name | camel }}{{ $c.Name | pascal }}: {{ goType .Type }}Column + {{- end}} + {{- end}} constructor(config: Config) { this.config = config; @@ -58,7 +58,7 @@ export class QXClient { return tx; } - async transaction(fn: (t: Tx) => void) { + async transaction(fn: (t: Tx) => Promise) { const tx = await this.tx() try { await fn(tx); @@ -74,7 +74,7 @@ export class QXClient { } or(...clauses: Clause[]) { - return clauses[0].or(...clauses.slice(1)) + return clauses[0].or(...clauses.slice(1)); } } From 095f244da0295c8ede2d131cc283cd8286cbde8b Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Wed, 18 Oct 2023 10:33:14 +0800 Subject: [PATCH 4/8] fmt --- .../typescript/templates/queryx/client.tstmpl | 19 ++++++++++++++++--- internal/integration/mysql.hcl | 6 +++--- internal/integration/postgresql.hcl | 6 +++--- internal/integration/sqlite.hcl | 6 +++--- 4 files changed, 25 insertions(+), 12 deletions(-) diff --git a/generator/client/typescript/templates/queryx/client.tstmpl b/generator/client/typescript/templates/queryx/client.tstmpl index 5c755186..cbf886a8 100644 --- a/generator/client/typescript/templates/queryx/client.tstmpl +++ b/generator/client/typescript/templates/queryx/client.tstmpl @@ -2,9 +2,22 @@ import { Env, Config, newConfig, getenv } from "./config"; {{- range $m := $.client.Models }} -import { {{ $m.Name }}Query } from "../{{ $m.Name | snake }}" +import { {{ $m.Name }}Query } from "../{{ $m.Name | snake }}"; {{- end }} -import { Table, BigIntColumn, IntegerColumn, FloatColumn, BooleanColumn, StringColumn, TextColumn, DateColumn, TimeColumn, DatetimeColumn, UUIDColumn, JSONColumn } from "./table"; +import { + Table, + BigIntColumn, + IntegerColumn, + FloatColumn, + BooleanColumn, + StringColumn, + TextColumn, + DateColumn, + TimeColumn, + DatetimeColumn, + UUIDColumn, + JSONColumn +} from "./table"; import { Adapter } from "./adapter"; import { Clause } from "./clause"; @@ -14,7 +27,7 @@ export class QXClient { {{- range $m := $.client.Models }} public {{ $m.Name | camel }}: Table; {{- range $c := .Columns }} - public {{ $m.Name | camel }}{{ $c.Name | pascal }}: {{ goType .Type }}Column + public {{ $m.Name | camel }}{{ $c.Name | pascal }}: {{ goType .Type }}Column; {{- end}} {{- end}} diff --git a/internal/integration/mysql.hcl b/internal/integration/mysql.hcl index 6887d1c2..fbe07a64 100644 --- a/internal/integration/mysql.hcl +++ b/internal/integration/mysql.hcl @@ -9,9 +9,9 @@ database "db" { url = env("DATABASE_URL") } - // generator "client-golang" { - // test = true - // } + generator "client-golang" { + test = true + } generator "client-typescript" { test = true } diff --git a/internal/integration/postgresql.hcl b/internal/integration/postgresql.hcl index 71f6c439..17ea6a7e 100644 --- a/internal/integration/postgresql.hcl +++ b/internal/integration/postgresql.hcl @@ -9,9 +9,9 @@ database "db" { url = env("DATABASE_URL") } - // generator "client-golang" { - // test = true - // } + generator "client-golang" { + test = true + } generator "client-typescript" { test = true } diff --git a/internal/integration/sqlite.hcl b/internal/integration/sqlite.hcl index 8384efbc..0a266f2d 100644 --- a/internal/integration/sqlite.hcl +++ b/internal/integration/sqlite.hcl @@ -9,9 +9,9 @@ database "db" { url = env("DATABASE_URL") } - // generator "client-golang" { - // test = true - // } + generator "client-golang" { + test = true + } generator "client-typescript" { test = true } From 75aa02f901a286afb4e2f722f4163116263dcc6a Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Wed, 17 Jan 2024 23:41:03 +0800 Subject: [PATCH 5/8] transaction should use same connection --- .../templates/queryx/adapter.postgresql.ts | 6 ++++-- .../typescript/templates/queryx/client.tstmpl | 4 ++-- internal/integration/client.test.ts | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/generator/client/typescript/templates/queryx/adapter.postgresql.ts b/generator/client/typescript/templates/queryx/adapter.postgresql.ts index 6aeddf89..db1292a9 100644 --- a/generator/client/typescript/templates/queryx/adapter.postgresql.ts +++ b/generator/client/typescript/templates/queryx/adapter.postgresql.ts @@ -10,14 +10,16 @@ export class Adapter { private db: Pool; constructor(config: Config) { - this.db = new Pool({ + const pool = new Pool({ connectionString: config.url, }); + + this.db = pool; } private _query( query: string, - args?: I + args?: I, ) { let [query1, args1] = rebind(query, args); return this.db.query(query1, args1); diff --git a/generator/client/typescript/templates/queryx/client.tstmpl b/generator/client/typescript/templates/queryx/client.tstmpl index cbf886a8..a372c81c 100644 --- a/generator/client/typescript/templates/queryx/client.tstmpl +++ b/generator/client/typescript/templates/queryx/client.tstmpl @@ -62,7 +62,7 @@ export class QXClient { {{- end }} raw(fragment: string, ...args: any[]) { - return new Clause(fragment, args) + return new Clause(fragment, args); } async tx() { @@ -72,7 +72,7 @@ export class QXClient { } async transaction(fn: (t: Tx) => Promise) { - const tx = await this.tx() + const tx = await this.tx(); try { await fn(tx); await tx.commit(); diff --git a/internal/integration/client.test.ts b/internal/integration/client.test.ts index 4e2aeac6..0f79614b 100644 --- a/internal/integration/client.test.ts +++ b/internal/integration/client.test.ts @@ -139,6 +139,7 @@ test("timestamps", async () => { expect(user.updatedAt).not.toBeNull(); expect(user.createdAt).toEqual(user.updatedAt); + await new Promise((resolve) => setTimeout(resolve, 10)); await user.update({ name: "new name" }); expect(user.updatedAt > user.createdAt).toBe(true); }); @@ -386,6 +387,23 @@ test("transactionBlock", async () => { expect(total).toEqual(2); }); +test("transaction with pool connection", async () => { + await c.queryTag().deleteAll(); + + let tx = await c.tx(); + let tag1 = await tx.queryTag().create({ name: "tag1" }); + + await Promise.all([ + tx.queryOne("select 1"), + tag1.update({ name: "tag1-updated" }), + ]); + + await tx.commit(); + + let tags = await c.query<{ name: string }>("select name from tags"); + expect(tags).toEqual([{ name: "tag1-updated" }]); +}); + test("changeJSON", async () => { let userChange = new UserChange({ name: "user name", From 36553eb17358e031a9c23830d590d80c53ab9267 Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Wed, 17 Jan 2024 23:42:17 +0800 Subject: [PATCH 6/8] disable tsc check in github action --- .github/workflows/integration.yml | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/.github/workflows/integration.yml b/.github/workflows/integration.yml index 348d3b6c..437d4107 100644 --- a/.github/workflows/integration.yml +++ b/.github/workflows/integration.yml @@ -27,8 +27,8 @@ jobs: strategy: matrix: postgres: - - '14.2' - - '16' + - "14.2" + - "16" runs-on: ubuntu-latest services: postgres: @@ -109,10 +109,10 @@ jobs: run: | cd internal/integration yarn test - - name: yarn tsc - run: | - cd internal/integration - yarn tsc + # - name: yarn tsc + # run: | + # cd internal/integration + # yarn tsc mysql-golang: needs: [build] runs-on: ubuntu-latest @@ -195,10 +195,10 @@ jobs: run: | cd internal/integration yarn test - - name: yarn tsc - run: | - cd internal/integration - yarn tsc + # - name: yarn tsc + # run: | + # cd internal/integration + # yarn tsc sqlite-golang: needs: [build] runs-on: ubuntu-latest @@ -253,7 +253,7 @@ jobs: run: | cd internal/integration yarn test - - name: yarn tsc - run: | - cd internal/integration - yarn tsc + # - name: yarn tsc + # run: | + # cd internal/integration + # yarn tsc From 6ec5a511ad3acad0bde85bfd36fbd22ba8413a51 Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Thu, 18 Jan 2024 00:06:41 +0800 Subject: [PATCH 7/8] reuse pool connection when creating transaction --- .../typescript/templates/queryx/adapter.postgresql.ts | 10 +++++++--- .../client/typescript/templates/queryx/client.tstmpl | 6 +++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/generator/client/typescript/templates/queryx/adapter.postgresql.ts b/generator/client/typescript/templates/queryx/adapter.postgresql.ts index db1292a9..376c2c1e 100644 --- a/generator/client/typescript/templates/queryx/adapter.postgresql.ts +++ b/generator/client/typescript/templates/queryx/adapter.postgresql.ts @@ -7,13 +7,17 @@ import { Config } from "./config"; types.setTypeParser(types.builtins.INT8, (val) => parseInt(val, 10)); export class Adapter { - private db: Pool; + public config: Config; + public db: Pool; constructor(config: Config) { + this.config = config; + } + + connect() { const pool = new Pool({ - connectionString: config.url, + connectionString: this.config.url, }); - this.db = pool; } diff --git a/generator/client/typescript/templates/queryx/client.tstmpl b/generator/client/typescript/templates/queryx/client.tstmpl index a372c81c..3d704f29 100644 --- a/generator/client/typescript/templates/queryx/client.tstmpl +++ b/generator/client/typescript/templates/queryx/client.tstmpl @@ -16,7 +16,7 @@ import { TimeColumn, DatetimeColumn, UUIDColumn, - JSONColumn + JSONColumn, } from "./table"; import { Adapter } from "./adapter"; import { Clause } from "./clause"; @@ -67,6 +67,7 @@ export class QXClient { async tx() { const tx = new Tx(this.config); + tx.adapter.db = await this.adapter.db.connect(); await tx.adapter.beginTx(); return tx; } @@ -79,6 +80,8 @@ export class QXClient { } catch (err) { await tx.rollback(); throw err; + } finally { + tx.adapter.db.release(); } } @@ -113,5 +116,6 @@ export const newClient = () => { export const newClientWithEnv = (env: Env) => { let config = newConfig(env); let client = new QXClient(config); + client.adapter.connect(); return client; }; From a333c6b6603933e484ee9b9c75eb50c96e2b9640 Mon Sep 17 00:00:00 2001 From: Wang Zuo Date: Thu, 18 Jan 2024 00:13:58 +0800 Subject: [PATCH 8/8] fix adapter in sqlite and mysql --- .../templates/[model]/[model].tstmpl | 6 +----- .../templates/queryx/adapter.mysql.ts | 20 +++++++++++++++++-- .../templates/queryx/adapter.postgresql.ts | 10 ++++++++++ .../templates/queryx/adapter.sqlite.ts | 13 +++++++++++- .../typescript/templates/queryx/client.tstmpl | 4 ++-- 5 files changed, 43 insertions(+), 10 deletions(-) diff --git a/generator/client/typescript/templates/[model]/[model].tstmpl b/generator/client/typescript/templates/[model]/[model].tstmpl index a8b1a5de..acb5b03e 100644 --- a/generator/client/typescript/templates/[model]/[model].tstmpl +++ b/generator/client/typescript/templates/[model]/[model].tstmpl @@ -16,11 +16,7 @@ export interface {{ $.model.Name }}Row {} export class {{ $.model.Name }} { {{- range $c := $.model.Columns }} - {{- if $c.Null }} - public {{ $c.Name | camel }}?: {{ tsType $c.Type }} | null; - {{- else }} - public {{ $c.Name | camel }}?: {{ tsType $c.Type }}; - {{- end }} + public {{ $c.Name | camel }}: {{ tsType $c.Type }}{{ if $c.Null }} | null{{ end }} = null; {{- end }} {{- range $b := $.model.BelongsTo }} public {{ $b.Name | camel }}: {{ $b.ModelName }} | null; diff --git a/generator/client/typescript/templates/queryx/adapter.mysql.ts b/generator/client/typescript/templates/queryx/adapter.mysql.ts index d6508b43..22c21bfe 100644 --- a/generator/client/typescript/templates/queryx/adapter.mysql.ts +++ b/generator/client/typescript/templates/queryx/adapter.mysql.ts @@ -5,12 +5,28 @@ import { parse } from "date-fns"; import { Config } from "./config"; export class Adapter { + public config: Config; + public pool: mysql.Pool; public db: mysql.Pool; constructor(config: Config) { - this.db = mysql.createPool({ - uri: config.url, + this.config = config; + } + + connect() { + const pool = mysql.createPool({ + uri: this.config.url, }); + this.pool = pool; + this.db = pool; + } + + newClient() { + return this.pool.getConnection(); + } + + release() { + this.db.release(); } async query(query: string, ...args: any[]) { diff --git a/generator/client/typescript/templates/queryx/adapter.postgresql.ts b/generator/client/typescript/templates/queryx/adapter.postgresql.ts index 376c2c1e..d6010b81 100644 --- a/generator/client/typescript/templates/queryx/adapter.postgresql.ts +++ b/generator/client/typescript/templates/queryx/adapter.postgresql.ts @@ -8,6 +8,7 @@ types.setTypeParser(types.builtins.INT8, (val) => parseInt(val, 10)); export class Adapter { public config: Config; + public pool: Pool; public db: Pool; constructor(config: Config) { @@ -18,9 +19,18 @@ export class Adapter { const pool = new Pool({ connectionString: this.config.url, }); + this.pool = pool; this.db = pool; } + newClient() { + return this.pool.connect(); + } + + release() { + this.db.release(); + } + private _query( query: string, args?: I, diff --git a/generator/client/typescript/templates/queryx/adapter.sqlite.ts b/generator/client/typescript/templates/queryx/adapter.sqlite.ts index 258d8af6..3552a9ea 100644 --- a/generator/client/typescript/templates/queryx/adapter.sqlite.ts +++ b/generator/client/typescript/templates/queryx/adapter.sqlite.ts @@ -5,12 +5,23 @@ import { format, parse } from "date-fns"; import { Config } from "./config"; export class Adapter { + public config: Config; private db; constructor(config: Config) { - this.db = new Database(config.url); + this.config = config; } + connect() { + this.db = new Database(this.config.url); + } + + newClient() { + return new Database(this.config.url); + } + + release() {} + query(query: string, ...args: any[]): R[] { let [query1, args1] = rebind(query, args); let stmt = this.db.prepare(query1); diff --git a/generator/client/typescript/templates/queryx/client.tstmpl b/generator/client/typescript/templates/queryx/client.tstmpl index 3d704f29..044477fa 100644 --- a/generator/client/typescript/templates/queryx/client.tstmpl +++ b/generator/client/typescript/templates/queryx/client.tstmpl @@ -67,7 +67,7 @@ export class QXClient { async tx() { const tx = new Tx(this.config); - tx.adapter.db = await this.adapter.db.connect(); + tx.adapter.db = await this.adapter.newClient(); await tx.adapter.beginTx(); return tx; } @@ -81,7 +81,7 @@ export class QXClient { await tx.rollback(); throw err; } finally { - tx.adapter.db.release(); + tx.adapter.release(); } }