rado 0.2.21 → 0.2.22
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/define/Column.js +4 -4
- package/dist/define/Expr.d.ts +64 -79
- package/dist/define/Expr.js +117 -76
- package/dist/define/Functions.js +1 -1
- package/dist/define/Ops.d.ts +7 -7
- package/dist/define/Ops.js +11 -12
- package/dist/define/Param.d.ts +15 -13
- package/dist/define/Param.js +18 -9
- package/dist/define/Query.d.ts +150 -50
- package/dist/define/Query.js +400 -53
- package/dist/define/Schema.d.ts +5 -4
- package/dist/define/Schema.js +40 -28
- package/dist/define/Selection.d.ts +2 -2
- package/dist/define/Sql.d.ts +6 -0
- package/dist/define/Sql.js +22 -0
- package/dist/define/Table.d.ts +6 -5
- package/dist/define/Table.js +52 -42
- package/dist/define/Target.d.ts +21 -21
- package/dist/define/Target.js +31 -14
- package/dist/driver/bun-sqlite.js +3 -4
- package/dist/driver/sqlite3.d.ts +2 -2
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/lib/Driver.d.ts +23 -25
- package/dist/lib/Driver.js +50 -32
- package/dist/lib/Formatter.d.ts +32 -21
- package/dist/lib/Formatter.js +229 -196
- package/dist/lib/Statement.js +1 -1
- package/dist/sqlite/SqliteFormatter.d.ts +1 -1
- package/dist/sqlite/SqliteFormatter.js +15 -19
- package/dist/sqlite/SqliteFunctions.d.ts +3 -3
- package/dist/sqlite/SqliteSchema.d.ts +3 -3
- package/dist/sqlite/SqliteSchema.js +3 -3
- package/package.json +4 -4
- package/dist/define/Cursor.d.ts +0 -108
- package/dist/define/Cursor.js +0 -338
package/dist/define/Table.d.ts
CHANGED
|
@@ -1,26 +1,27 @@
|
|
|
1
1
|
import { Column, ColumnData, OptionalColumn, PrimaryColumn } from './Column';
|
|
2
|
-
import { Cursor } from './Cursor';
|
|
3
2
|
import { EV } from './Expr';
|
|
4
3
|
import { Index, IndexData } from './Index';
|
|
4
|
+
import { Query } from './Query';
|
|
5
5
|
import { Selection } from './Selection';
|
|
6
6
|
declare const DATA: unique symbol;
|
|
7
7
|
declare const META: unique symbol;
|
|
8
8
|
interface TableDefinition {
|
|
9
9
|
}
|
|
10
|
-
export
|
|
10
|
+
export declare class TableData {
|
|
11
11
|
name: string;
|
|
12
12
|
alias?: string;
|
|
13
13
|
definition: TableDefinition;
|
|
14
14
|
columns: Record<string, ColumnData>;
|
|
15
|
-
meta()
|
|
15
|
+
meta: () => {
|
|
16
16
|
indexes: Record<string, IndexData>;
|
|
17
17
|
};
|
|
18
|
+
constructor(data: TableData);
|
|
18
19
|
}
|
|
19
20
|
export interface TableInstance<Definition> {
|
|
20
21
|
(conditions: {
|
|
21
22
|
[K in keyof Definition]?: Definition[K] extends Column<infer V> ? EV<V> : never;
|
|
22
|
-
}):
|
|
23
|
-
(...conditions: Array<EV<boolean>>):
|
|
23
|
+
}): Query.TableSelect<Definition>;
|
|
24
|
+
(...conditions: Array<EV<boolean>>): Query.TableSelect<Definition>;
|
|
24
25
|
}
|
|
25
26
|
export declare class TableInstance<Definition> {
|
|
26
27
|
[Selection.TableType](): Table.Select<Definition>;
|
package/dist/define/Table.js
CHANGED
|
@@ -3,28 +3,33 @@ import {
|
|
|
3
3
|
Column,
|
|
4
4
|
ColumnType
|
|
5
5
|
} from "./Column.js";
|
|
6
|
-
import { Cursor } from "./Cursor.js";
|
|
7
6
|
import { BinOpType, Expr, ExprData } from "./Expr.js";
|
|
7
|
+
import { Query } from "./Query.js";
|
|
8
8
|
import { Target } from "./Target.js";
|
|
9
|
-
var DATA = Symbol("Table.Data");
|
|
10
|
-
var META = Symbol("Table.Meta");
|
|
11
9
|
var {
|
|
10
|
+
assign,
|
|
12
11
|
keys,
|
|
13
12
|
entries,
|
|
14
13
|
fromEntries,
|
|
15
14
|
getOwnPropertyDescriptors,
|
|
16
15
|
getPrototypeOf,
|
|
17
16
|
setPrototypeOf,
|
|
18
|
-
defineProperty
|
|
19
|
-
getOwnPropertyNames
|
|
17
|
+
defineProperty
|
|
20
18
|
} = Object;
|
|
19
|
+
var DATA = Symbol("Table.Data");
|
|
20
|
+
var META = Symbol("Table.Meta");
|
|
21
|
+
var TableData = class {
|
|
22
|
+
constructor(data) {
|
|
23
|
+
assign(this, data);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
21
26
|
var Table;
|
|
22
27
|
((Table2) => {
|
|
23
28
|
Table2.Data = DATA;
|
|
24
29
|
Table2.Meta = META;
|
|
25
30
|
})(Table || (Table = {}));
|
|
26
31
|
function createTable(data) {
|
|
27
|
-
const target = Target.Table(data);
|
|
32
|
+
const target = new Target.Table(data);
|
|
28
33
|
const call = {
|
|
29
34
|
[data.name]: function(...args) {
|
|
30
35
|
const isConditionalRecord = args.length === 1 && !Expr.isExpr(args[0]);
|
|
@@ -33,21 +38,21 @@ function createTable(data) {
|
|
|
33
38
|
if (!column)
|
|
34
39
|
throw new Error(`Column ${key} not found`);
|
|
35
40
|
return new Expr(
|
|
36
|
-
ExprData.BinOp(
|
|
41
|
+
new ExprData.BinOp(
|
|
37
42
|
BinOpType.Equals,
|
|
38
|
-
ExprData.Field(ExprData.Row(target), key),
|
|
43
|
+
new ExprData.Field(new ExprData.Row(target), key),
|
|
39
44
|
ExprData.create(value)
|
|
40
45
|
)
|
|
41
46
|
);
|
|
42
47
|
}) : args;
|
|
43
|
-
return new
|
|
48
|
+
return new Query.TableSelect(data, conditions);
|
|
44
49
|
}
|
|
45
50
|
}[data.name];
|
|
46
51
|
const cols = keys(data.columns);
|
|
47
|
-
const row = ExprData.Row(target);
|
|
52
|
+
const row = new ExprData.Row(target);
|
|
48
53
|
const expressions = fromEntries(
|
|
49
54
|
cols.map((name) => {
|
|
50
|
-
let expr = new Expr(ExprData.Field(row, name));
|
|
55
|
+
let expr = new Expr(new ExprData.Field(row, name));
|
|
51
56
|
if (data.columns[name].type === ColumnType.Json)
|
|
52
57
|
expr = expr.dynamic();
|
|
53
58
|
return [name, expr];
|
|
@@ -71,39 +76,43 @@ function table(define) {
|
|
|
71
76
|
const target = define[name];
|
|
72
77
|
const definition = "prototype" in target ? new target() : target;
|
|
73
78
|
const columns = definition;
|
|
74
|
-
const res = createTable(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
79
|
+
const res = createTable(
|
|
80
|
+
new TableData({
|
|
81
|
+
name,
|
|
82
|
+
definition,
|
|
83
|
+
columns: fromEntries(
|
|
84
|
+
entries(getOwnPropertyDescriptors(columns)).map(
|
|
85
|
+
([name2, descriptor]) => {
|
|
86
|
+
const column = columns[name2];
|
|
87
|
+
const data = column[Column.Data];
|
|
88
|
+
if (!data.type)
|
|
89
|
+
throw new Error(`Column ${name2} has no type`);
|
|
90
|
+
return [
|
|
91
|
+
name2,
|
|
92
|
+
{
|
|
93
|
+
...data,
|
|
94
|
+
type: data.type,
|
|
95
|
+
name: data.name || name2,
|
|
96
|
+
enumerable: descriptor.enumerable
|
|
97
|
+
}
|
|
98
|
+
];
|
|
90
99
|
}
|
|
91
|
-
];
|
|
92
|
-
})
|
|
93
|
-
),
|
|
94
|
-
meta() {
|
|
95
|
-
const createMeta = res[table.meta];
|
|
96
|
-
const meta = createMeta ? createMeta.apply(res) : {};
|
|
97
|
-
return {
|
|
98
|
-
indexes: fromEntries(
|
|
99
|
-
entries(meta?.indexes || {}).map(([key, index]) => {
|
|
100
|
-
const indexName = `${name}.${key}`;
|
|
101
|
-
return [indexName, { name: indexName, ...index.data }];
|
|
102
|
-
})
|
|
103
100
|
)
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
101
|
+
),
|
|
102
|
+
meta() {
|
|
103
|
+
const createMeta = res[table.meta];
|
|
104
|
+
const meta = createMeta ? createMeta.apply(res) : {};
|
|
105
|
+
return {
|
|
106
|
+
indexes: fromEntries(
|
|
107
|
+
entries(meta?.indexes || {}).map(([key, index]) => {
|
|
108
|
+
const indexName = `${name}.${key}`;
|
|
109
|
+
return [indexName, { name: indexName, ...index.data }];
|
|
110
|
+
})
|
|
111
|
+
)
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
);
|
|
107
116
|
return res;
|
|
108
117
|
}
|
|
109
118
|
((table2) => {
|
|
@@ -111,6 +120,7 @@ function table(define) {
|
|
|
111
120
|
})(table || (table = {}));
|
|
112
121
|
export {
|
|
113
122
|
Table,
|
|
123
|
+
TableData,
|
|
114
124
|
createTable,
|
|
115
125
|
table
|
|
116
126
|
};
|
package/dist/define/Target.d.ts
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import { ExprData } from './Expr';
|
|
2
|
-
import {
|
|
3
|
-
import { TableData } from './Table';
|
|
1
|
+
import type { ExprData } from './Expr';
|
|
2
|
+
import type { QueryData } from './Query';
|
|
3
|
+
import type { TableData } from './Table';
|
|
4
4
|
export declare enum TargetType {
|
|
5
|
-
Expr = "Expr",
|
|
6
|
-
Query = "Query",
|
|
7
|
-
Table = "Table",
|
|
8
|
-
Join = "Join"
|
|
5
|
+
Expr = "Target.Expr",
|
|
6
|
+
Query = "Target.Query",
|
|
7
|
+
Table = "Target.Table",
|
|
8
|
+
Join = "Target.Join"
|
|
9
9
|
}
|
|
10
10
|
export type Target = Target.Expr | Target.Query | Target.Table | Target.Join;
|
|
11
11
|
export declare namespace Target {
|
|
12
|
-
|
|
13
|
-
type: TargetType.Expr;
|
|
12
|
+
class Expr {
|
|
14
13
|
expr: ExprData;
|
|
15
|
-
alias?: string;
|
|
14
|
+
alias?: string | undefined;
|
|
15
|
+
type: TargetType.Expr;
|
|
16
|
+
constructor(expr: ExprData, alias?: string | undefined);
|
|
16
17
|
}
|
|
17
|
-
|
|
18
|
-
interface Query {
|
|
19
|
-
type: TargetType.Query;
|
|
18
|
+
class Query {
|
|
20
19
|
query: QueryData;
|
|
21
|
-
alias?: string;
|
|
20
|
+
alias?: string | undefined;
|
|
21
|
+
type: TargetType.Query;
|
|
22
|
+
constructor(query: QueryData, alias?: string | undefined);
|
|
22
23
|
}
|
|
23
|
-
|
|
24
|
-
interface Table {
|
|
25
|
-
type: TargetType.Table;
|
|
24
|
+
class Table {
|
|
26
25
|
table: TableData;
|
|
26
|
+
type: TargetType.Table;
|
|
27
|
+
constructor(table: TableData);
|
|
27
28
|
}
|
|
28
|
-
|
|
29
|
-
interface Join {
|
|
30
|
-
type: TargetType.Join;
|
|
29
|
+
class Join {
|
|
31
30
|
left: Target;
|
|
32
31
|
right: Target;
|
|
33
32
|
joinType: 'left' | 'inner';
|
|
34
33
|
on: ExprData;
|
|
34
|
+
type: TargetType.Join;
|
|
35
|
+
constructor(left: Target, right: Target, joinType: 'left' | 'inner', on: ExprData);
|
|
35
36
|
}
|
|
36
|
-
function Join(left: Target, right: Target, joinType: 'left' | 'inner', on: ExprData): Join;
|
|
37
37
|
function source(from: Target): TableData | undefined;
|
|
38
38
|
}
|
package/dist/define/Target.js
CHANGED
|
@@ -1,34 +1,51 @@
|
|
|
1
1
|
// src/define/Target.ts
|
|
2
2
|
var TargetType = /* @__PURE__ */ ((TargetType2) => {
|
|
3
|
-
TargetType2["Expr"] = "Expr";
|
|
4
|
-
TargetType2["Query"] = "Query";
|
|
5
|
-
TargetType2["Table"] = "Table";
|
|
6
|
-
TargetType2["Join"] = "Join";
|
|
3
|
+
TargetType2["Expr"] = "Target.Expr";
|
|
4
|
+
TargetType2["Query"] = "Target.Query";
|
|
5
|
+
TargetType2["Table"] = "Target.Table";
|
|
6
|
+
TargetType2["Join"] = "Target.Join";
|
|
7
7
|
return TargetType2;
|
|
8
8
|
})(TargetType || {});
|
|
9
9
|
var Target;
|
|
10
10
|
((Target2) => {
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
class Expr {
|
|
12
|
+
constructor(expr, alias) {
|
|
13
|
+
this.expr = expr;
|
|
14
|
+
this.alias = alias;
|
|
15
|
+
}
|
|
16
|
+
type = "Target.Expr" /* Expr */;
|
|
13
17
|
}
|
|
14
18
|
Target2.Expr = Expr;
|
|
15
|
-
|
|
16
|
-
|
|
19
|
+
class Query {
|
|
20
|
+
constructor(query, alias) {
|
|
21
|
+
this.query = query;
|
|
22
|
+
this.alias = alias;
|
|
23
|
+
}
|
|
24
|
+
type = "Target.Query" /* Query */;
|
|
17
25
|
}
|
|
18
26
|
Target2.Query = Query;
|
|
19
|
-
|
|
20
|
-
|
|
27
|
+
class Table {
|
|
28
|
+
constructor(table) {
|
|
29
|
+
this.table = table;
|
|
30
|
+
}
|
|
31
|
+
type = "Target.Table" /* Table */;
|
|
21
32
|
}
|
|
22
33
|
Target2.Table = Table;
|
|
23
|
-
|
|
24
|
-
|
|
34
|
+
class Join {
|
|
35
|
+
constructor(left, right, joinType, on) {
|
|
36
|
+
this.left = left;
|
|
37
|
+
this.right = right;
|
|
38
|
+
this.joinType = joinType;
|
|
39
|
+
this.on = on;
|
|
40
|
+
}
|
|
41
|
+
type = "Target.Join" /* Join */;
|
|
25
42
|
}
|
|
26
43
|
Target2.Join = Join;
|
|
27
44
|
function source(from) {
|
|
28
45
|
switch (from.type) {
|
|
29
|
-
case "Table" /* Table */:
|
|
46
|
+
case "Target.Table" /* Table */:
|
|
30
47
|
return from.table;
|
|
31
|
-
case "Join" /* Join */:
|
|
48
|
+
case "Target.Join" /* Join */:
|
|
32
49
|
return source(from.left);
|
|
33
50
|
default:
|
|
34
51
|
return void 0;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// src/driver/bun-sqlite.ts
|
|
2
|
-
import {
|
|
3
|
-
import { Query } from "../define/Query.js";
|
|
2
|
+
import { Query, QueryData } from "../define/Query.js";
|
|
4
3
|
import { Driver } from "../lib/Driver.js";
|
|
5
4
|
import { SqlError } from "../lib/SqlError.js";
|
|
6
5
|
import { SqliteFormatter } from "../sqlite/SqliteFormatter.js";
|
|
@@ -35,8 +34,8 @@ var BunSqliteDriver = class extends Driver.Sync {
|
|
|
35
34
|
this.tableData = this.prepare(SqliteSchema.tableData);
|
|
36
35
|
this.indexData = this.prepare(SqliteSchema.indexData);
|
|
37
36
|
this.lastChanges = this.prepare(() => {
|
|
38
|
-
return new
|
|
39
|
-
|
|
37
|
+
return new Query.SelectSingle(
|
|
38
|
+
new QueryData.Raw({
|
|
40
39
|
expectedReturn: "row",
|
|
41
40
|
strings: ["SELECT changes() as rowsAffected"],
|
|
42
41
|
params: []
|
package/dist/driver/sqlite3.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Database } from 'sqlite3';
|
|
2
|
-
import {
|
|
2
|
+
import { QueryData } from '../define/Query';
|
|
3
3
|
import { SchemaInstructions } from '../define/Schema';
|
|
4
4
|
import { Driver } from '../lib/Driver';
|
|
5
5
|
import { Statement } from '../lib/Statement';
|
|
@@ -10,7 +10,7 @@ export declare class Sqlite3Driver extends Driver.Async {
|
|
|
10
10
|
tableData: (tableName: string) => Promise<Array<SqliteSchema.Column>>;
|
|
11
11
|
indexData: (tableName: string) => Promise<Array<SqliteSchema.Index>>;
|
|
12
12
|
constructor(db: Database);
|
|
13
|
-
executeQuery
|
|
13
|
+
executeQuery(query: QueryData, stmt?: Driver.Async.PreparedStatement, params?: any[] | undefined): Promise<unknown>;
|
|
14
14
|
prepareStatement(stmt: Statement): Driver.Async.PreparedStatement;
|
|
15
15
|
schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
16
16
|
isolate(): [connection: Driver.Async, release: () => Promise<void>];
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
export * from './define/Column';
|
|
2
|
-
export * from './define/Cursor';
|
|
3
2
|
export * from './define/Expr';
|
|
4
3
|
export type { Fields } from './define/Fields';
|
|
5
4
|
export * from './define/Functions';
|
|
@@ -11,6 +10,7 @@ export * from './define/Param';
|
|
|
11
10
|
export * from './define/Query';
|
|
12
11
|
export * from './define/Schema';
|
|
13
12
|
export type { Selection } from './define/Selection';
|
|
13
|
+
export * from './define/Sql';
|
|
14
14
|
export * from './define/Table';
|
|
15
15
|
export * from './define/Target';
|
|
16
16
|
export type { Driver } from './lib/Driver';
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
2
|
export * from "./define/Column.js";
|
|
3
|
-
export * from "./define/Cursor.js";
|
|
4
3
|
export * from "./define/Expr.js";
|
|
5
4
|
export * from "./define/Functions.js";
|
|
6
5
|
export * from "./define/Index.js";
|
|
@@ -9,6 +8,7 @@ export * from "./define/OrderBy.js";
|
|
|
9
8
|
export * from "./define/Param.js";
|
|
10
9
|
export * from "./define/Query.js";
|
|
11
10
|
export * from "./define/Schema.js";
|
|
11
|
+
export * from "./define/Sql.js";
|
|
12
12
|
export * from "./define/Table.js";
|
|
13
13
|
export * from "./define/Target.js";
|
|
14
14
|
export * from "./lib/SqlError.js";
|
package/dist/lib/Driver.d.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { Cursor } from '../define/Cursor';
|
|
2
1
|
import { Expr } from '../define/Expr';
|
|
3
|
-
import { Query } from '../define/Query';
|
|
2
|
+
import { Query, QueryData } from '../define/Query';
|
|
4
3
|
import { SchemaInstructions } from '../define/Schema';
|
|
5
4
|
import { Table } from '../define/Table';
|
|
6
5
|
import { Callable } from '../util/Callable';
|
|
@@ -9,12 +8,11 @@ import { Statement } from './Statement';
|
|
|
9
8
|
declare abstract class DriverBase extends Callable {
|
|
10
9
|
formatter: Formatter;
|
|
11
10
|
constructor(formatter: Formatter);
|
|
12
|
-
compile<T extends Array<Expr<any>>, R>(create: (...params: T) =>
|
|
11
|
+
compile<T extends Array<Expr<any>>, R>(create: (...params: T) => Query<R>): [QueryData, Statement];
|
|
13
12
|
all(...args: Array<any>): any;
|
|
14
13
|
get(...args: Array<any>): any;
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
abstract executeQuery(query: Query<any>): any;
|
|
14
|
+
executeTemplate(expectedReturn: QueryData.RawReturn, strings: TemplateStringsArray, ...params: Array<any>): any;
|
|
15
|
+
abstract executeQuery(query: QueryData): any;
|
|
18
16
|
}
|
|
19
17
|
type ParamTypes<Params extends [...any[]]> = {
|
|
20
18
|
[K in keyof Params]: Params[K] extends Expr<infer T> ? T : never;
|
|
@@ -22,8 +20,8 @@ type ParamTypes<Params extends [...any[]]> = {
|
|
|
22
20
|
length: Params['length'];
|
|
23
21
|
};
|
|
24
22
|
interface SyncDriver {
|
|
25
|
-
<T>(query:
|
|
26
|
-
<T>(...queries: Array<
|
|
23
|
+
<T>(query: Query<T>): T;
|
|
24
|
+
<T>(...queries: Array<Query<any>>): T;
|
|
27
25
|
(strings: TemplateStringsArray, ...values: any[]): any;
|
|
28
26
|
}
|
|
29
27
|
declare abstract class SyncDriver extends DriverBase {
|
|
@@ -31,16 +29,16 @@ declare abstract class SyncDriver extends DriverBase {
|
|
|
31
29
|
constructor(formatter: Formatter);
|
|
32
30
|
abstract prepareStatement(stmt: Statement, discardAfter: boolean): SyncPreparedStatement;
|
|
33
31
|
abstract schemaInstructions(tableName: string): SchemaInstructions | undefined;
|
|
34
|
-
prepare<T extends Array<Expr<any>>, R>(create: (...params: T) =>
|
|
32
|
+
prepare<T extends Array<Expr<any>>, R>(create: (...params: T) => Query<R>): (...params: ParamTypes<T>) => R;
|
|
35
33
|
migrateSchema(...tables: Array<Table<any>>): unknown;
|
|
36
|
-
executeQuery
|
|
37
|
-
all<T>(cursor:
|
|
38
|
-
all<T>(cursor:
|
|
34
|
+
executeQuery(query: QueryData, stmt?: SyncPreparedStatement, params?: Array<any>): unknown;
|
|
35
|
+
all<T>(cursor: Query.SelectSingle<T>): Array<T>;
|
|
36
|
+
all<T>(cursor: Query<T>): T;
|
|
39
37
|
all<T>(strings: TemplateStringsArray, ...params: Array<any>): Array<T>;
|
|
40
|
-
get<T>(cursor:
|
|
41
|
-
get<T>(cursor:
|
|
38
|
+
get<T>(cursor: Query.SelectMultiple<T>): T | null;
|
|
39
|
+
get<T>(cursor: Query<T>): T;
|
|
42
40
|
get<T>(strings: TemplateStringsArray, ...params: Array<any>): T;
|
|
43
|
-
iterate<T>(cursor:
|
|
41
|
+
iterate<T>(cursor: Query.SelectMultiple<T>): Iterable<T>;
|
|
44
42
|
transaction<T>(run: (query: SyncDriver) => T): T;
|
|
45
43
|
toAsync(): SyncWrapper;
|
|
46
44
|
}
|
|
@@ -54,8 +52,8 @@ interface SyncPreparedStatement {
|
|
|
54
52
|
execute(params?: Array<any>): void;
|
|
55
53
|
}
|
|
56
54
|
interface AsyncDriver {
|
|
57
|
-
<T>(query:
|
|
58
|
-
<T>(...queries: Array<
|
|
55
|
+
<T>(query: Query<T>): Promise<T>;
|
|
56
|
+
<T>(...queries: Array<Query<any>>): Promise<T>;
|
|
59
57
|
(strings: TemplateStringsArray, ...values: any[]): Promise<any>;
|
|
60
58
|
}
|
|
61
59
|
declare abstract class AsyncDriver extends DriverBase {
|
|
@@ -64,23 +62,23 @@ declare abstract class AsyncDriver extends DriverBase {
|
|
|
64
62
|
abstract isolate(): [connection: AsyncDriver, release: () => Promise<void>];
|
|
65
63
|
abstract prepareStatement(stmt: Statement, discardAfter: boolean): AsyncPreparedStatement;
|
|
66
64
|
abstract schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
67
|
-
prepare<T extends Array<Expr<any>>, R>(create: (...params: T) =>
|
|
65
|
+
prepare<T extends Array<Expr<any>>, R>(create: (...params: T) => Query<R>): (...params: ParamTypes<T>) => Promise<R>;
|
|
68
66
|
migrateSchema(...tables: Array<Table<any>>): Promise<unknown>;
|
|
69
|
-
executeQuery
|
|
70
|
-
all<T>(cursor:
|
|
71
|
-
all<T>(cursor:
|
|
67
|
+
executeQuery(query: QueryData, stmt?: AsyncPreparedStatement, params?: Array<any>): Promise<unknown>;
|
|
68
|
+
all<T>(cursor: Query.SelectSingle<T>): Promise<Array<T>>;
|
|
69
|
+
all<T>(cursor: Query<T>): Promise<T>;
|
|
72
70
|
all<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<Array<T>>;
|
|
73
|
-
get<T>(cursor:
|
|
74
|
-
get<T>(cursor:
|
|
71
|
+
get<T>(cursor: Query.SelectMultiple<T>): Promise<T | null>;
|
|
72
|
+
get<T>(cursor: Query<T>): Promise<T>;
|
|
75
73
|
get<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<T>;
|
|
76
|
-
iterate<T>(cursor:
|
|
74
|
+
iterate<T>(cursor: Query.SelectMultiple<T>): AsyncIterable<T>;
|
|
77
75
|
transaction<T>(run: (query: AsyncDriver) => T): Promise<T>;
|
|
78
76
|
}
|
|
79
77
|
declare class SyncWrapper extends AsyncDriver {
|
|
80
78
|
private sync;
|
|
81
79
|
lock: Promise<void> | undefined;
|
|
82
80
|
constructor(sync: SyncDriver);
|
|
83
|
-
executeQuery
|
|
81
|
+
executeQuery(query: QueryData, stmt?: AsyncPreparedStatement, params?: Array<any>): Promise<unknown>;
|
|
84
82
|
prepareStatement(stmt: Statement, discardAfter: boolean): AsyncPreparedStatement;
|
|
85
83
|
schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
86
84
|
isolate(): [connection: AsyncDriver, release: () => Promise<void>];
|
package/dist/lib/Driver.js
CHANGED
|
@@ -1,8 +1,7 @@
|
|
|
1
1
|
// src/lib/Driver.ts
|
|
2
|
-
import { Cursor } from "../define/Cursor.js";
|
|
3
2
|
import { Expr, ExprData } from "../define/Expr.js";
|
|
4
3
|
import { ParamData } from "../define/Param.js";
|
|
5
|
-
import { Query, QueryType } from "../define/Query.js";
|
|
4
|
+
import { Query, QueryData, QueryType } from "../define/Query.js";
|
|
6
5
|
import { Schema } from "../define/Schema.js";
|
|
7
6
|
import { Table } from "../define/Table.js";
|
|
8
7
|
import { Callable } from "../util/Callable.js";
|
|
@@ -13,13 +12,13 @@ var DriverBase = class extends Callable {
|
|
|
13
12
|
constructor(formatter) {
|
|
14
13
|
super((...args) => {
|
|
15
14
|
const [input, ...rest] = args;
|
|
16
|
-
if (input
|
|
17
|
-
return this.executeQuery(input[
|
|
15
|
+
if (Query.isQuery(input) && rest.length === 0)
|
|
16
|
+
return this.executeQuery(input[Query.Data]);
|
|
18
17
|
if (isTemplateStringsArray(input))
|
|
19
18
|
return this.executeTemplate(void 0, input, ...rest);
|
|
20
19
|
return this.executeQuery(
|
|
21
|
-
|
|
22
|
-
queries: args.filter(
|
|
20
|
+
new QueryData.Batch({
|
|
21
|
+
queries: args.filter(Query.isQuery).map((arg) => arg[Query.Data])
|
|
23
22
|
})
|
|
24
23
|
);
|
|
25
24
|
});
|
|
@@ -29,33 +28,32 @@ var DriverBase = class extends Callable {
|
|
|
29
28
|
const { length } = create;
|
|
30
29
|
const paramNames = Array.from({ length }, (_, i) => `p${i}`);
|
|
31
30
|
const params = paramNames.map(
|
|
32
|
-
(name) => new Expr(ExprData.Param(ParamData.Named(name)))
|
|
31
|
+
(name) => new Expr(new ExprData.Param(new ParamData.Named(name)))
|
|
33
32
|
);
|
|
34
33
|
const cursor = create(...params);
|
|
35
|
-
const query = cursor[
|
|
34
|
+
const query = cursor[Query.Data];
|
|
36
35
|
return [query, this.formatter.compile(query)];
|
|
37
36
|
}
|
|
38
37
|
all(...args) {
|
|
39
38
|
const [input, ...rest] = args;
|
|
40
|
-
if (input
|
|
41
|
-
return this.executeQuery(input.all()[
|
|
42
|
-
if (input
|
|
43
|
-
return this.executeQuery(input[
|
|
39
|
+
if (Query.isSingle(input))
|
|
40
|
+
return this.executeQuery(input.all()[Query.Data]);
|
|
41
|
+
if (Query.isQuery(input))
|
|
42
|
+
return this.executeQuery(input[Query.Data]);
|
|
44
43
|
return this.executeTemplate("rows", input, ...rest);
|
|
45
44
|
}
|
|
46
45
|
get(...args) {
|
|
47
46
|
const [input, ...rest] = args;
|
|
48
|
-
if (input
|
|
49
|
-
return this.executeQuery(input.maybeFirst()[
|
|
50
|
-
if (input
|
|
51
|
-
return this.executeQuery(input[
|
|
47
|
+
if (Query.isMultiple(input))
|
|
48
|
+
return this.executeQuery(input.maybeFirst()[Query.Data]);
|
|
49
|
+
if (Query.isQuery(input))
|
|
50
|
+
return this.executeQuery(input[Query.Data]);
|
|
52
51
|
return this.executeTemplate("row", input, ...rest);
|
|
53
52
|
}
|
|
54
|
-
sql(strings, ...params) {
|
|
55
|
-
return new Cursor(Query.Raw({ strings, params }));
|
|
56
|
-
}
|
|
57
53
|
executeTemplate(expectedReturn, strings, ...params) {
|
|
58
|
-
return this.executeQuery(
|
|
54
|
+
return this.executeQuery(
|
|
55
|
+
new QueryData.Raw({ strings, params, expectedReturn })
|
|
56
|
+
);
|
|
59
57
|
}
|
|
60
58
|
};
|
|
61
59
|
var SyncDriver = class extends DriverBase {
|
|
@@ -71,7 +69,11 @@ var SyncDriver = class extends DriverBase {
|
|
|
71
69
|
const namedParams = Object.fromEntries(
|
|
72
70
|
params.map((value, i) => [`p${i}`, value])
|
|
73
71
|
);
|
|
74
|
-
return this.executeQuery(
|
|
72
|
+
return this.executeQuery(
|
|
73
|
+
query,
|
|
74
|
+
prepared,
|
|
75
|
+
compiled.params(namedParams)
|
|
76
|
+
);
|
|
75
77
|
};
|
|
76
78
|
}
|
|
77
79
|
migrateSchema(...tables) {
|
|
@@ -87,7 +89,7 @@ var SyncDriver = class extends DriverBase {
|
|
|
87
89
|
queries.push(...changes);
|
|
88
90
|
}
|
|
89
91
|
}
|
|
90
|
-
return this.executeQuery(
|
|
92
|
+
return this.executeQuery(new QueryData.Batch({ queries }));
|
|
91
93
|
}
|
|
92
94
|
executeQuery(query, stmt, params) {
|
|
93
95
|
switch (query.type) {
|
|
@@ -139,7 +141,7 @@ var SyncDriver = class extends DriverBase {
|
|
|
139
141
|
}
|
|
140
142
|
*iterate(cursor) {
|
|
141
143
|
const stmt = this.prepareStatement(
|
|
142
|
-
this.formatter.compile(cursor[
|
|
144
|
+
this.formatter.compile(cursor[Query.Data]),
|
|
143
145
|
true
|
|
144
146
|
);
|
|
145
147
|
for (const row of stmt.iterate()) {
|
|
@@ -149,17 +151,23 @@ var SyncDriver = class extends DriverBase {
|
|
|
149
151
|
transaction(run) {
|
|
150
152
|
const id = `t${this.transactionId++}`;
|
|
151
153
|
this.executeQuery(
|
|
152
|
-
|
|
154
|
+
new QueryData.Transaction({ op: QueryData.TransactionOperation.Begin, id })
|
|
153
155
|
);
|
|
154
156
|
try {
|
|
155
157
|
const res = run(this);
|
|
156
158
|
this.executeQuery(
|
|
157
|
-
|
|
159
|
+
new QueryData.Transaction({
|
|
160
|
+
op: QueryData.TransactionOperation.Commit,
|
|
161
|
+
id
|
|
162
|
+
})
|
|
158
163
|
);
|
|
159
164
|
return res;
|
|
160
165
|
} catch (e) {
|
|
161
166
|
this.executeQuery(
|
|
162
|
-
|
|
167
|
+
new QueryData.Transaction({
|
|
168
|
+
op: QueryData.TransactionOperation.Rollback,
|
|
169
|
+
id
|
|
170
|
+
})
|
|
163
171
|
);
|
|
164
172
|
throw e;
|
|
165
173
|
}
|
|
@@ -181,7 +189,11 @@ var AsyncDriver = class extends DriverBase {
|
|
|
181
189
|
const namedParams = Object.fromEntries(
|
|
182
190
|
params.map((value, i) => [`p${i}`, value])
|
|
183
191
|
);
|
|
184
|
-
return this.executeQuery(
|
|
192
|
+
return this.executeQuery(
|
|
193
|
+
query,
|
|
194
|
+
prepared,
|
|
195
|
+
compiled.params(namedParams)
|
|
196
|
+
);
|
|
185
197
|
};
|
|
186
198
|
}
|
|
187
199
|
async migrateSchema(...tables) {
|
|
@@ -197,7 +209,7 @@ var AsyncDriver = class extends DriverBase {
|
|
|
197
209
|
queries.push(...changes);
|
|
198
210
|
}
|
|
199
211
|
}
|
|
200
|
-
return this.executeQuery(
|
|
212
|
+
return this.executeQuery(new QueryData.Batch({ queries }));
|
|
201
213
|
}
|
|
202
214
|
async executeQuery(query, stmt, params) {
|
|
203
215
|
switch (query.type) {
|
|
@@ -251,7 +263,7 @@ var AsyncDriver = class extends DriverBase {
|
|
|
251
263
|
}
|
|
252
264
|
async *iterate(cursor) {
|
|
253
265
|
const stmt = this.prepareStatement(
|
|
254
|
-
this.formatter.compile(cursor[
|
|
266
|
+
this.formatter.compile(cursor[Query.Data]),
|
|
255
267
|
true
|
|
256
268
|
);
|
|
257
269
|
for await (const row of stmt.iterate()) {
|
|
@@ -262,17 +274,23 @@ var AsyncDriver = class extends DriverBase {
|
|
|
262
274
|
const id = `t${this.transactionId++}`;
|
|
263
275
|
const [connection, release] = this.isolate();
|
|
264
276
|
await connection.executeQuery(
|
|
265
|
-
|
|
277
|
+
new QueryData.Transaction({ op: QueryData.TransactionOperation.Begin, id })
|
|
266
278
|
);
|
|
267
279
|
try {
|
|
268
280
|
const res = await run(connection);
|
|
269
281
|
await connection.executeQuery(
|
|
270
|
-
|
|
282
|
+
new QueryData.Transaction({
|
|
283
|
+
op: QueryData.TransactionOperation.Commit,
|
|
284
|
+
id
|
|
285
|
+
})
|
|
271
286
|
);
|
|
272
287
|
return res;
|
|
273
288
|
} catch (e) {
|
|
274
289
|
await connection.executeQuery(
|
|
275
|
-
|
|
290
|
+
new QueryData.Transaction({
|
|
291
|
+
op: QueryData.TransactionOperation.Rollback,
|
|
292
|
+
id
|
|
293
|
+
})
|
|
276
294
|
);
|
|
277
295
|
throw e;
|
|
278
296
|
} finally {
|