rado 1.0.0-preview.5 → 1.0.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/dist/core/Builder.d.ts +2 -2
- package/dist/core/expr/Conditions.d.ts +2 -2
- package/dist/core/query/Delete.d.ts +7 -5
- package/dist/core/query/Delete.js +6 -4
- package/dist/core/query/Select.d.ts +2 -2
- package/dist/core/query/Update.d.ts +1 -1
- package/dist/core/query/Update.js +6 -2
- package/dist/driver/mysql2.js +9 -1
- package/dist/driver/pg.js +3 -0
- package/dist/driver/pglite.js +3 -1
- package/dist/mysql/transactions.d.ts +3 -0
- package/dist/mysql/transactions.js +20 -0
- package/dist/postgres/transactions.d.ts +2 -0
- package/dist/postgres/transactions.js +12 -0
- package/package.json +2 -2
package/dist/core/Builder.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type HasQuery, type HasSql, type HasTarget, internalData } from './Internal.js';
|
|
2
2
|
import type { IsPostgres, QueryMeta } from './MetaData.js';
|
|
3
3
|
import type { QueryData } from './Query.js';
|
|
4
4
|
import { type SelectionInput } from './Selection.js';
|
|
@@ -18,7 +18,7 @@ declare class BuilderBase<Meta extends QueryMeta> {
|
|
|
18
18
|
selectDistinctOn<Input extends SelectionInput>(this: Builder<IsPostgres>, columns: Array<HasSql>, selection: Input): WithSelection<Input, Meta>;
|
|
19
19
|
update<Definition extends TableDefinition>(table: Table<Definition>): UpdateTable<Definition, Meta>;
|
|
20
20
|
insert<Definition extends TableDefinition>(into: Table<Definition>): InsertInto<Definition, Meta>;
|
|
21
|
-
delete(from:
|
|
21
|
+
delete<Definition extends TableDefinition>(from: Table<Definition>): DeleteFrom<Definition, Meta>;
|
|
22
22
|
}
|
|
23
23
|
export type CTE<Input = unknown> = Input & HasTarget & HasQuery;
|
|
24
24
|
export declare class Builder<Meta extends QueryMeta> extends BuilderBase<Meta> {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type HasSql } from '../Internal.js';
|
|
2
2
|
import { type Sql } from '../Sql.js';
|
|
3
|
-
import type {
|
|
3
|
+
import type { Select } from '../query/Select.js';
|
|
4
4
|
import { type Input } from './Input.js';
|
|
5
5
|
export declare const eq: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
|
|
6
6
|
export declare const ne: <T>(left: Input<T>, right: Input<T>) => Sql<boolean>;
|
|
@@ -29,4 +29,4 @@ export declare function desc<T>(input: HasSql<T>): Sql;
|
|
|
29
29
|
export declare function distinct<T>(input: HasSql<T>): Sql;
|
|
30
30
|
export declare function when<In, Out>(compare: Input<In>, ...cases: Array<[condition: Input<In>, result: Input<Out>] | Input<Out>>): Sql<Out>;
|
|
31
31
|
export declare function when<Out>(...cases: Array<[condition: Input<boolean>, result: Input<Out>] | Input<Out>>): Sql<Out>;
|
|
32
|
-
export declare function exists<T>(query:
|
|
32
|
+
export declare function exists<T>(query: Select<T>): Sql<boolean>;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import type { QueryMeta } from '../MetaData.js';
|
|
1
|
+
import { type HasSql, type HasTable, internalData, internalQuery, internalSelection } from '../Internal.js';
|
|
2
|
+
import type { IsPostgres, IsSqlite, QueryMeta } from '../MetaData.js';
|
|
3
3
|
import { Query, type QueryData } from '../Query.js';
|
|
4
4
|
import { type Selection, type SelectionInput, type SelectionRow } from '../Selection.js';
|
|
5
5
|
import { type Sql } from '../Sql.js';
|
|
6
|
+
import type { TableDefinition, TableRow } from '../Table.js';
|
|
6
7
|
export interface DeleteData<Meta extends QueryMeta = QueryMeta> extends QueryData<Meta> {
|
|
7
8
|
from: HasTable;
|
|
8
9
|
where?: HasSql;
|
|
@@ -14,7 +15,8 @@ export declare class Delete<Result, Meta extends QueryMeta = QueryMeta> extends
|
|
|
14
15
|
constructor(data: DeleteData<Meta>);
|
|
15
16
|
get [internalQuery](): Sql;
|
|
16
17
|
}
|
|
17
|
-
export declare class DeleteFrom<Meta extends QueryMeta> extends Delete<void, Meta> {
|
|
18
|
-
where(
|
|
19
|
-
returning<
|
|
18
|
+
export declare class DeleteFrom<Definition extends TableDefinition, Meta extends QueryMeta> extends Delete<void, Meta> {
|
|
19
|
+
where(...where: Array<HasSql<boolean> | undefined>): DeleteFrom<Definition, Meta>;
|
|
20
|
+
returning(this: DeleteFrom<Definition, IsPostgres | IsSqlite>): Delete<TableRow<Definition>, Meta>;
|
|
21
|
+
returning<Input extends SelectionInput>(this: DeleteFrom<Definition, IsPostgres | IsSqlite>, returning: Input): Delete<SelectionRow<Input>, Meta>;
|
|
20
22
|
}
|
|
@@ -10,6 +10,7 @@ import {
|
|
|
10
10
|
selection
|
|
11
11
|
} from "../Selection.js";
|
|
12
12
|
import { sql } from "../Sql.js";
|
|
13
|
+
import { and } from "../expr/Conditions.js";
|
|
13
14
|
var Delete = class extends Query {
|
|
14
15
|
[internalData];
|
|
15
16
|
constructor(data) {
|
|
@@ -23,13 +24,14 @@ var Delete = class extends Query {
|
|
|
23
24
|
}
|
|
24
25
|
};
|
|
25
26
|
var DeleteFrom = class _DeleteFrom extends Delete {
|
|
26
|
-
where(
|
|
27
|
-
return new _DeleteFrom({ ...getData(this), where:
|
|
27
|
+
where(...where) {
|
|
28
|
+
return new _DeleteFrom({ ...getData(this), where: and(...where) });
|
|
28
29
|
}
|
|
29
30
|
returning(returning) {
|
|
31
|
+
const data = getData(this);
|
|
30
32
|
return new Delete({
|
|
31
|
-
...
|
|
32
|
-
returning: selection(returning)
|
|
33
|
+
...data,
|
|
34
|
+
returning: selection(returning ?? data.from)
|
|
33
35
|
});
|
|
34
36
|
}
|
|
35
37
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { type HasSelection, type HasSql, type HasTable, type HasTarget, internalData, internalQuery, internalSelection } from '../Internal.js';
|
|
2
2
|
import type { QueryMeta } from '../MetaData.js';
|
|
3
3
|
import type { Query } from '../Query.js';
|
|
4
4
|
import { type IsNullable, type MakeNullable, type Selection, type SelectionRecord, type SelectionRow } from '../Selection.js';
|
|
@@ -35,7 +35,7 @@ export declare class Select<Input, Meta extends QueryMeta = QueryMeta> extends U
|
|
|
35
35
|
rightJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
|
|
36
36
|
innerJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
|
|
37
37
|
fullJoin(right: HasTable, on: HasSql<boolean>): Select<Input, Meta>;
|
|
38
|
-
where(...where: Array<HasSql<boolean
|
|
38
|
+
where(...where: Array<HasSql<boolean> | undefined>): Select<Input, Meta>;
|
|
39
39
|
groupBy(...exprs: Array<HasSql>): Select<Input, Meta>;
|
|
40
40
|
having(having: HasSql<boolean>): Select<Input, Meta>;
|
|
41
41
|
orderBy(...exprs: Array<HasSql>): Select<Input, Meta>;
|
|
@@ -18,7 +18,7 @@ export declare class Update<Result, Meta extends QueryMeta = QueryMeta> extends
|
|
|
18
18
|
}
|
|
19
19
|
export declare class UpdateTable<Definition extends TableDefinition, Meta extends QueryMeta> extends Update<void, Meta> {
|
|
20
20
|
set(values: TableUpdate<Definition>): UpdateTable<Definition, Meta>;
|
|
21
|
-
where(where: HasSql<boolean>): UpdateTable<Definition, Meta>;
|
|
21
|
+
where(...where: Array<HasSql<boolean> | undefined>): UpdateTable<Definition, Meta>;
|
|
22
22
|
returning(this: UpdateTable<Definition, IsPostgres | IsSqlite>): Update<TableRow<Definition>, Meta>;
|
|
23
23
|
returning<Input extends SelectionInput>(this: UpdateTable<Definition, IsPostgres | IsSqlite>, returning?: Input): Update<SelectionRow<Input>, Meta>;
|
|
24
24
|
}
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
selection
|
|
12
12
|
} from "../Selection.js";
|
|
13
13
|
import { sql } from "../Sql.js";
|
|
14
|
+
import { and } from "../expr/Conditions.js";
|
|
14
15
|
import { input } from "../expr/Input.js";
|
|
15
16
|
var Update = class extends Query {
|
|
16
17
|
[internalData];
|
|
@@ -39,8 +40,11 @@ var UpdateTable = class _UpdateTable extends Update {
|
|
|
39
40
|
);
|
|
40
41
|
return new _UpdateTable({ ...getData(this), set });
|
|
41
42
|
}
|
|
42
|
-
where(where) {
|
|
43
|
-
return new _UpdateTable({
|
|
43
|
+
where(...where) {
|
|
44
|
+
return new _UpdateTable({
|
|
45
|
+
...getData(this),
|
|
46
|
+
where: and(...where)
|
|
47
|
+
});
|
|
44
48
|
}
|
|
45
49
|
returning(returning) {
|
|
46
50
|
const data = getData(this);
|
package/dist/driver/mysql2.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { AsyncDatabase } from "../core/Database.js";
|
|
3
3
|
import { mysqlDialect } from "../mysql/dialect.js";
|
|
4
4
|
import { mysqlDiff } from "../mysql/diff.js";
|
|
5
|
+
import { setTransaction, startTransaction } from "../mysql/transactions.js";
|
|
5
6
|
var PreparedStatement = class {
|
|
6
7
|
constructor(client, sql, name) {
|
|
7
8
|
this.client = client;
|
|
@@ -54,7 +55,14 @@ var Mysql2Driver = class _Mysql2Driver {
|
|
|
54
55
|
const client = isPool(this.client) ? await this.client.getConnection() : this.client;
|
|
55
56
|
const driver = new _Mysql2Driver(client, this.depth + 1);
|
|
56
57
|
try {
|
|
57
|
-
await client.query(
|
|
58
|
+
await client.query(
|
|
59
|
+
this.depth > 0 ? `savepoint d${this.depth}` : startTransaction(options)
|
|
60
|
+
);
|
|
61
|
+
if (this.depth === 0) {
|
|
62
|
+
const setOptions = setTransaction(options);
|
|
63
|
+
if (setOptions)
|
|
64
|
+
await client.query(setOptions);
|
|
65
|
+
}
|
|
58
66
|
const result = await run(driver);
|
|
59
67
|
await client.query(
|
|
60
68
|
this.depth > 0 ? `release savepoint d${this.depth}` : "commit"
|
package/dist/driver/pg.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { AsyncDatabase } from "../core/Database.js";
|
|
3
3
|
import { postgresDialect } from "../postgres/dialect.js";
|
|
4
4
|
import { postgresDiff } from "../postgres/diff.js";
|
|
5
|
+
import { setTransaction } from "../postgres/transactions.js";
|
|
5
6
|
var PreparedStatement = class {
|
|
6
7
|
constructor(client, sql, name) {
|
|
7
8
|
this.client = client;
|
|
@@ -69,6 +70,8 @@ var PgDriver = class _PgDriver {
|
|
|
69
70
|
const client = "totalCount" in this.client ? await this.client.connect() : this.client;
|
|
70
71
|
try {
|
|
71
72
|
await client.query(this.depth > 0 ? `savepoint d${this.depth}` : "begin");
|
|
73
|
+
if (this.depth === 0)
|
|
74
|
+
await client.query(setTransaction(options));
|
|
72
75
|
const result = await run(new _PgDriver(client, this.depth + 1));
|
|
73
76
|
await client.query(
|
|
74
77
|
this.depth > 0 ? `release savepoint d${this.depth}` : "commit"
|
package/dist/driver/pglite.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { AsyncDatabase } from "../index.js";
|
|
3
3
|
import { postgresDialect } from "../postgres/dialect.js";
|
|
4
4
|
import { postgresDiff } from "../postgres/diff.js";
|
|
5
|
+
import { setTransaction } from "../postgres/transactions.js";
|
|
5
6
|
var PreparedStatement = class {
|
|
6
7
|
constructor(client, sql) {
|
|
7
8
|
this.client = client;
|
|
@@ -59,7 +60,8 @@ var PGliteDriver = class _PGliteDriver {
|
|
|
59
60
|
}
|
|
60
61
|
async transaction(run, options) {
|
|
61
62
|
if (this.depth === 0 && "transaction" in this.client)
|
|
62
|
-
return this.client.transaction((tx) => {
|
|
63
|
+
return this.client.transaction(async (tx) => {
|
|
64
|
+
await tx.query(setTransaction(options));
|
|
63
65
|
return run(new _PGliteDriver(tx, this.depth + 1));
|
|
64
66
|
});
|
|
65
67
|
await this.exec(`savepoint d${this.depth}`);
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/mysql/transactions.ts
|
|
2
|
+
function startTransaction(options) {
|
|
3
|
+
return [
|
|
4
|
+
"start transaction",
|
|
5
|
+
options.withConsistentSnapshot && "with consistent snapshot",
|
|
6
|
+
options.accessMode
|
|
7
|
+
].filter(Boolean).join(" ");
|
|
8
|
+
}
|
|
9
|
+
function setTransaction(options) {
|
|
10
|
+
if (!options.isolationLevel)
|
|
11
|
+
return;
|
|
12
|
+
return [
|
|
13
|
+
"set transaction",
|
|
14
|
+
options.isolationLevel && `isolation level ${options.isolationLevel}`
|
|
15
|
+
].filter(Boolean).join(" ");
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
setTransaction,
|
|
19
|
+
startTransaction
|
|
20
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/postgres/transactions.ts
|
|
2
|
+
function setTransaction(options) {
|
|
3
|
+
return [
|
|
4
|
+
"set transaction",
|
|
5
|
+
options.isolationLevel && `isolation level ${options.isolationLevel}`,
|
|
6
|
+
options.accessMode,
|
|
7
|
+
options.deferrable ? "deferrable" : "not deferrable"
|
|
8
|
+
].filter(Boolean).join(" ");
|
|
9
|
+
}
|
|
10
|
+
export {
|
|
11
|
+
setTransaction
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rado",
|
|
3
|
-
"version": "1.0.0
|
|
3
|
+
"version": "1.0.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"scripts": {
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
"better-sqlite3": "^11.0.0",
|
|
40
40
|
"esbuild": "^0.18.11",
|
|
41
41
|
"glob": "^8.0.3",
|
|
42
|
-
"madge": "^
|
|
42
|
+
"madge": "^8.0.0",
|
|
43
43
|
"mysql2": "^3.9.7",
|
|
44
44
|
"pg": "^8.11.5",
|
|
45
45
|
"rimraf": "^4.1.2",
|