Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ jobs:
strategy:
matrix:
postgres:
- '14.2'
- '16'
- "14.2"
- "16"
runs-on: ubuntu-latest
services:
postgres:
Expand Down Expand Up @@ -105,14 +105,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
Expand Down Expand Up @@ -191,14 +191,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
Expand Down Expand Up @@ -249,11 +249,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
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ test-mysql: install
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 ./...
cd internal/integration && yarn tsc
cd internal/integration && yarn test
# cd internal/integration && go test ./...

test-sqlite: install
rm -rf internal/integration/db
Expand All @@ -44,9 +44,9 @@ test-sqlite: install
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 ./...
cd internal/integration && yarn tsc
cd internal/integration && yarn test
# cd internal/integration && go test ./...

test: test-postgresql test-sqlite test-mysql

Expand Down
4 changes: 0 additions & 4 deletions generator/client/typescript/templates/[model]/[model].tstmpl
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,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 }},
Expand Down
26 changes: 18 additions & 8 deletions generator/client/typescript/templates/queryx/adapter.mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,46 @@ 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<R>(query: string, ...args: any[]) {
console.log(query, args);
let [rows] = await this.db.query<R & RowDataPacket[]>(query, args);
return rows;
}

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

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

async _exec(query: string, ...args: any[]) {
console.log(query, args);
let [res] = await this.db.execute<ResultSetHeader>(query, args);
return res;
}
Expand All @@ -50,8 +62,6 @@ export class Adapter {
}
}

export function rebind<T extends any[] = any[]>(query: string, args?: T) {}

export function adapterValue(type: string, value: any) {
switch (type) {
case "time":
Expand Down
25 changes: 20 additions & 5 deletions generator/client/typescript/templates/queryx/adapter.postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,35 @@ import { Config } from "./config";
types.setTypeParser(types.builtins.INT8, (val) => parseInt(val, 10));

export class Adapter {
private db: Pool;
public config: Config;
public pool: Pool;
public db: Pool;

constructor(config: Config) {
this.db = new Pool({
connectionString: config.url,
this.config = config;
}

connect() {
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<R extends QueryResultRow = any, I extends any[] = any[]>(
query: string,
args?: I
args?: I,
) {
let [query1, args1] = rebind<I>(query, args);
console.log(query1, args1);
return this.db.query<R, I>(query1, args1);
}

Expand Down
16 changes: 12 additions & 4 deletions generator/client/typescript/templates/queryx/adapter.sqlite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,37 @@ 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<R>(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<R>(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;
Expand Down
41 changes: 29 additions & 12 deletions generator/client/typescript/templates/queryx/client.tstmpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@

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";

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;
Expand Down Expand Up @@ -49,23 +62,26 @@ export class QXClient {
{{- end }}

raw(fragment: string, ...args: any[]) {
return new Clause(fragment, args)
return new Clause(fragment, args);
}

async tx() {
const tx = new Tx(this.config);
tx.adapter.db = await this.adapter.newClient();
await tx.adapter.beginTx();
return tx;
}

async transaction(fn: (t: Tx) => void) {
const tx = await this.tx()
async transaction(fn: (t: Tx) => Promise<void>) {
const tx = await this.tx();
try {
await fn(tx);
await tx.commit();
} catch (err) {
await tx.rollback();
throw err;
} finally {
tx.adapter.release();
}
}

Expand All @@ -74,7 +90,7 @@ export class QXClient {
}

or(...clauses: Clause[]) {
return clauses[0].or(...clauses.slice(1))
return clauses[0].or(...clauses.slice(1));
}
}

Expand All @@ -100,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;
};
19 changes: 18 additions & 1 deletion internal/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,8 @@ 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" });
console.log(user.createdAt, user.updatedAt);
expect(user.updatedAt > user.createdAt).toBe(true);
});

Expand Down Expand Up @@ -387,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",
Expand Down