rado 0.0.0 → 0.1.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/Column.d.ts +13 -7
- package/dist/Column.js +7 -4
- package/dist/Cursor.d.ts +6 -5
- package/dist/Cursor.js +6 -5
- package/dist/Driver.d.ts +30 -4
- package/dist/Driver.js +117 -18
- package/dist/Formatter.d.ts +8 -2
- package/dist/Formatter.js +60 -17
- package/dist/Ops.js +9 -10
- package/dist/Query.d.ts +40 -18
- package/dist/Query.js +15 -0
- package/dist/Sanitizer.d.ts +1 -1
- package/dist/Schema.d.ts +23 -0
- package/dist/Schema.js +60 -0
- package/dist/Statement.d.ts +5 -2
- package/dist/Statement.js +2 -2
- package/dist/Table.d.ts +8 -10
- package/dist/Table.js +34 -18
- package/dist/Target.d.ts +4 -4
- package/dist/driver/better-sqlite3.d.ts +10 -5
- package/dist/driver/better-sqlite3.js +26 -24
- package/dist/driver/sql.js.d.ts +10 -5
- package/dist/driver/sql.js.js +35 -32
- package/dist/driver/sqlite3.d.ts +10 -3
- package/dist/driver/sqlite3.js +51 -46
- package/dist/sqlite/SqliteFormatter.d.ts +1 -1
- package/dist/sqlite/SqliteFormatter.js +1 -1
- package/dist/sqlite/SqliteSchema.d.ts +24 -0
- package/dist/sqlite/SqliteSchema.js +38 -0
- package/package.json +1 -1
package/dist/Column.d.ts
CHANGED
|
@@ -1,28 +1,33 @@
|
|
|
1
|
+
import { Expr, ExprData } from './Expr';
|
|
1
2
|
export declare const enum ColumnType {
|
|
2
3
|
String = "String",
|
|
3
4
|
Integer = "Integer",
|
|
4
5
|
Number = "Number",
|
|
5
6
|
Boolean = "Boolean",
|
|
6
|
-
|
|
7
|
-
Array = "Array"
|
|
7
|
+
Json = "Json"
|
|
8
8
|
}
|
|
9
|
-
|
|
9
|
+
interface PartialColumnData {
|
|
10
10
|
type: ColumnType;
|
|
11
11
|
name?: string;
|
|
12
12
|
nullable?: boolean;
|
|
13
|
+
defaultValue?: any;
|
|
13
14
|
autoIncrement?: boolean;
|
|
14
15
|
primaryKey?: boolean;
|
|
15
16
|
unique?: boolean;
|
|
16
|
-
|
|
17
|
-
|
|
17
|
+
references?: ExprData;
|
|
18
|
+
}
|
|
19
|
+
export interface ColumnData extends PartialColumnData {
|
|
20
|
+
type: ColumnType;
|
|
21
|
+
name: string;
|
|
18
22
|
}
|
|
19
23
|
export declare class Column<T = any> {
|
|
20
|
-
data:
|
|
21
|
-
constructor(data:
|
|
24
|
+
data: PartialColumnData;
|
|
25
|
+
constructor(data: PartialColumnData);
|
|
22
26
|
name(name: string): Column<T>;
|
|
23
27
|
nullable(): Column<T | null>;
|
|
24
28
|
autoIncrement(): Column<T & Column.Optional>;
|
|
25
29
|
primaryKey(): Column<T & Column.Optional>;
|
|
30
|
+
references(column: Expr<T>): Column<any>;
|
|
26
31
|
unique(): Column<T>;
|
|
27
32
|
defaultValue(value: T): Column<T & Column.Optional>;
|
|
28
33
|
}
|
|
@@ -41,3 +46,4 @@ export declare const column: {
|
|
|
41
46
|
object<T_4 extends object = object>(): Column<T_4>;
|
|
42
47
|
array<T_5 = any>(): Column<T_5[]>;
|
|
43
48
|
};
|
|
49
|
+
export {};
|
package/dist/Column.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// src/Column.ts
|
|
2
|
+
import { ExprData } from "./Expr.js";
|
|
2
3
|
var ColumnType = /* @__PURE__ */ ((ColumnType2) => {
|
|
3
4
|
ColumnType2["String"] = "String";
|
|
4
5
|
ColumnType2["Integer"] = "Integer";
|
|
5
6
|
ColumnType2["Number"] = "Number";
|
|
6
7
|
ColumnType2["Boolean"] = "Boolean";
|
|
7
|
-
ColumnType2["
|
|
8
|
-
ColumnType2["Array"] = "Array";
|
|
8
|
+
ColumnType2["Json"] = "Json";
|
|
9
9
|
return ColumnType2;
|
|
10
10
|
})(ColumnType || {});
|
|
11
11
|
var Column = class {
|
|
@@ -24,6 +24,9 @@ var Column = class {
|
|
|
24
24
|
primaryKey() {
|
|
25
25
|
return new Column({ ...this.data, primaryKey: true });
|
|
26
26
|
}
|
|
27
|
+
references(column2) {
|
|
28
|
+
return new Column({ ...this.data, references: ExprData.create(column2) });
|
|
29
|
+
}
|
|
27
30
|
unique() {
|
|
28
31
|
return new Column({ ...this.data, unique: true });
|
|
29
32
|
}
|
|
@@ -45,10 +48,10 @@ var column = {
|
|
|
45
48
|
return new Column({ type: "Boolean" /* Boolean */ });
|
|
46
49
|
},
|
|
47
50
|
object() {
|
|
48
|
-
return new Column({ type: "
|
|
51
|
+
return new Column({ type: "Json" /* Json */ });
|
|
49
52
|
},
|
|
50
53
|
array() {
|
|
51
|
-
return new Column({ type: "
|
|
54
|
+
return new Column({ type: "Json" /* Json */ });
|
|
52
55
|
}
|
|
53
56
|
};
|
|
54
57
|
export {
|
package/dist/Cursor.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { EV, Expr } from './Expr';
|
|
2
2
|
import { OrderBy } from './OrderBy';
|
|
3
3
|
import { Query } from './Query';
|
|
4
|
+
import { Schema } from './Schema';
|
|
4
5
|
import { Selection } from './Selection';
|
|
5
|
-
import { Table
|
|
6
|
+
import { Table } from './Table';
|
|
6
7
|
import { Update as UpdateSet } from './Update';
|
|
7
8
|
export declare class Cursor<T> {
|
|
8
9
|
/** @internal */
|
|
@@ -39,13 +40,13 @@ export declare namespace Cursor {
|
|
|
39
40
|
returning<X extends Selection>(selection: X): InsertValuesReturning<Selection.Infer<X>>;
|
|
40
41
|
}
|
|
41
42
|
class Insert<T> {
|
|
42
|
-
protected into:
|
|
43
|
-
constructor(into:
|
|
43
|
+
protected into: Schema;
|
|
44
|
+
constructor(into: Schema);
|
|
44
45
|
values(...data: Array<Table.Insert<T>>): InsertValues;
|
|
45
46
|
}
|
|
46
47
|
class Create extends Cursor<void> {
|
|
47
|
-
protected table:
|
|
48
|
-
constructor(table:
|
|
48
|
+
protected table: Schema;
|
|
49
|
+
constructor(table: Schema);
|
|
49
50
|
}
|
|
50
51
|
class Batch<T = void> extends Cursor<T> {
|
|
51
52
|
protected queries: Array<Query>;
|
package/dist/Cursor.js
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import { Expr, ExprData } from "./Expr.js";
|
|
3
3
|
import { Functions } from "./Functions.js";
|
|
4
4
|
import { Query } from "./Query.js";
|
|
5
|
+
import { Schema } from "./Schema.js";
|
|
5
6
|
import { Target } from "./Target.js";
|
|
6
7
|
var Cursor = class {
|
|
7
8
|
constructor(query) {
|
|
@@ -75,7 +76,7 @@ var Cursor = class {
|
|
|
75
76
|
Cursor2.Insert = Insert;
|
|
76
77
|
class Create extends Cursor2 {
|
|
77
78
|
constructor(table) {
|
|
78
|
-
super(
|
|
79
|
+
super(Schema.create(table));
|
|
79
80
|
this.table = table;
|
|
80
81
|
}
|
|
81
82
|
}
|
|
@@ -97,7 +98,7 @@ var Cursor = class {
|
|
|
97
98
|
...query,
|
|
98
99
|
from: Target.Join(
|
|
99
100
|
query.from,
|
|
100
|
-
Target.Table(that.
|
|
101
|
+
Target.Table(that.schema()),
|
|
101
102
|
"left",
|
|
102
103
|
Expr.and(...on).expr
|
|
103
104
|
)
|
|
@@ -109,7 +110,7 @@ var Cursor = class {
|
|
|
109
110
|
...query,
|
|
110
111
|
from: Target.Join(
|
|
111
112
|
query.from,
|
|
112
|
-
Target.Table(that.
|
|
113
|
+
Target.Table(that.schema()),
|
|
113
114
|
"inner",
|
|
114
115
|
Expr.and(...on).expr
|
|
115
116
|
)
|
|
@@ -161,7 +162,7 @@ var Cursor = class {
|
|
|
161
162
|
...query,
|
|
162
163
|
from: Target.Join(
|
|
163
164
|
query.from,
|
|
164
|
-
Target.Table(that.
|
|
165
|
+
Target.Table(that.schema()),
|
|
165
166
|
"left",
|
|
166
167
|
Expr.and(...on).expr
|
|
167
168
|
)
|
|
@@ -173,7 +174,7 @@ var Cursor = class {
|
|
|
173
174
|
...query,
|
|
174
175
|
from: Target.Join(
|
|
175
176
|
query.from,
|
|
176
|
-
Target.Table(that.
|
|
177
|
+
Target.Table(that.schema()),
|
|
177
178
|
"inner",
|
|
178
179
|
Expr.and(...on).expr
|
|
179
180
|
)
|
package/dist/Driver.d.ts
CHANGED
|
@@ -1,10 +1,15 @@
|
|
|
1
1
|
import { Cursor } from './Cursor';
|
|
2
|
+
import { Formatter } from './Formatter';
|
|
2
3
|
import { Query } from './Query';
|
|
4
|
+
import { SchemaInstructions } from './Schema';
|
|
5
|
+
import { Statement } from './Statement';
|
|
6
|
+
import { Table } from './Table';
|
|
3
7
|
declare class Callable extends Function {
|
|
4
8
|
constructor(fn: Function);
|
|
5
9
|
}
|
|
6
10
|
declare abstract class DriverBase extends Callable {
|
|
7
|
-
|
|
11
|
+
formatter: Formatter;
|
|
12
|
+
constructor(formatter: Formatter);
|
|
8
13
|
all(strings: TemplateStringsArray, ...params: Array<any>): any;
|
|
9
14
|
get(strings: TemplateStringsArray, ...params: Array<any>): any;
|
|
10
15
|
executeTemplate(expectedReturn: Query.RawReturn, strings: TemplateStringsArray, ...params: Array<any>): any;
|
|
@@ -17,7 +22,14 @@ export declare namespace Driver {
|
|
|
17
22
|
}
|
|
18
23
|
export abstract class Sync extends DriverBase {
|
|
19
24
|
transactionId: number;
|
|
20
|
-
abstract
|
|
25
|
+
abstract rows(stmt: Statement.Compiled): Array<object>;
|
|
26
|
+
abstract values(stmt: Statement.Compiled): Array<Array<any>>;
|
|
27
|
+
abstract execute(stmt: Statement.Compiled): void;
|
|
28
|
+
abstract mutate(stmt: Statement.Compiled): {
|
|
29
|
+
rowsAffected: number;
|
|
30
|
+
};
|
|
31
|
+
abstract schemaInstructions(tableName: string): SchemaInstructions | undefined;
|
|
32
|
+
migrateSchema(...tables: Array<Table<any>>): unknown;
|
|
21
33
|
executeQuery<T>(query: Query<T>): T;
|
|
22
34
|
all<T>(strings: TemplateStringsArray, ...params: Array<any>): Array<T>;
|
|
23
35
|
get<T>(strings: TemplateStringsArray, ...params: Array<any>): T;
|
|
@@ -30,8 +42,15 @@ export declare namespace Driver {
|
|
|
30
42
|
}
|
|
31
43
|
export abstract class Async extends DriverBase {
|
|
32
44
|
transactionId: number;
|
|
33
|
-
abstract execute(query: Query): Promise<any>;
|
|
34
45
|
abstract isolate(): [connection: Async, release: () => Promise<void>];
|
|
46
|
+
abstract rows(stmt: Statement.Compiled): Promise<Array<object>>;
|
|
47
|
+
abstract values(stmt: Statement.Compiled): Promise<Array<Array<any>>>;
|
|
48
|
+
abstract execute(stmt: Statement.Compiled): Promise<void>;
|
|
49
|
+
abstract mutate(stmt: Statement.Compiled): Promise<{
|
|
50
|
+
rowsAffected: number;
|
|
51
|
+
}>;
|
|
52
|
+
abstract schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
53
|
+
migrateSchema(...tables: Array<Table<any>>): Promise<unknown>;
|
|
35
54
|
executeQuery<T>(query: Query<T>): Promise<T>;
|
|
36
55
|
all<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<Array<T>>;
|
|
37
56
|
get<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<T>;
|
|
@@ -41,7 +60,14 @@ export declare namespace Driver {
|
|
|
41
60
|
private sync;
|
|
42
61
|
lock: Promise<void> | undefined;
|
|
43
62
|
constructor(sync: Sync);
|
|
44
|
-
|
|
63
|
+
executeQuery<T>(query: Query<T>): Promise<T>;
|
|
64
|
+
rows(stmt: Statement.Compiled): Promise<Array<object>>;
|
|
65
|
+
values(stmt: Statement.Compiled): Promise<Array<Array<any>>>;
|
|
66
|
+
execute(stmt: Statement.Compiled): Promise<void>;
|
|
67
|
+
mutate(stmt: Statement.Compiled): Promise<{
|
|
68
|
+
rowsAffected: number;
|
|
69
|
+
}>;
|
|
70
|
+
schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
|
|
45
71
|
isolate(): [connection: Async, release: () => Promise<void>];
|
|
46
72
|
}
|
|
47
73
|
export {};
|
package/dist/Driver.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// src/Driver.ts
|
|
2
2
|
import { Cursor } from "./Cursor.js";
|
|
3
3
|
import { Query, QueryType } from "./Query.js";
|
|
4
|
+
import { Schema } from "./Schema.js";
|
|
4
5
|
var Callable = class extends Function {
|
|
5
6
|
constructor(fn) {
|
|
6
7
|
super();
|
|
@@ -12,13 +13,14 @@ var Callable = class extends Function {
|
|
|
12
13
|
}
|
|
13
14
|
};
|
|
14
15
|
var DriverBase = class extends Callable {
|
|
15
|
-
constructor() {
|
|
16
|
+
constructor(formatter) {
|
|
16
17
|
super((...args) => {
|
|
17
18
|
const [input, ...rest] = args;
|
|
18
19
|
if (input instanceof Cursor)
|
|
19
20
|
return this.executeQuery(input.query());
|
|
20
21
|
return this.executeTemplate(void 0, input, ...rest);
|
|
21
22
|
});
|
|
23
|
+
this.formatter = formatter;
|
|
22
24
|
}
|
|
23
25
|
all(strings, ...params) {
|
|
24
26
|
return this.executeTemplate("rows", strings, ...params);
|
|
@@ -36,16 +38,57 @@ var Driver;
|
|
|
36
38
|
((Driver2) => {
|
|
37
39
|
class Sync extends DriverBase {
|
|
38
40
|
transactionId = 0;
|
|
41
|
+
migrateSchema(...tables) {
|
|
42
|
+
const queries = [];
|
|
43
|
+
for (const table of Object.values(tables)) {
|
|
44
|
+
const schema = table.schema();
|
|
45
|
+
const localSchema = this.schemaInstructions(schema.name);
|
|
46
|
+
if (!localSchema) {
|
|
47
|
+
queries.push(...Schema.create(schema).queries);
|
|
48
|
+
} else {
|
|
49
|
+
const changes = Schema.upgrade(this.formatter, localSchema, schema);
|
|
50
|
+
if (changes.length)
|
|
51
|
+
queries.push(...changes);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return this.executeQuery(Query.Batch({ queries }));
|
|
55
|
+
}
|
|
39
56
|
executeQuery(query) {
|
|
40
57
|
switch (query.type) {
|
|
41
58
|
case QueryType.Batch:
|
|
42
59
|
let result;
|
|
43
60
|
const stmts = query.queries;
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
61
|
+
if (stmts.length === 0)
|
|
62
|
+
return void 0;
|
|
63
|
+
if (stmts.length === 1)
|
|
64
|
+
return this.executeQuery(stmts[0]);
|
|
65
|
+
return this.transaction((cnx) => {
|
|
66
|
+
for (const query2 of stmts)
|
|
67
|
+
result = cnx.executeQuery(query2);
|
|
68
|
+
return result;
|
|
69
|
+
});
|
|
47
70
|
default:
|
|
48
|
-
|
|
71
|
+
const stmt = this.formatter.compile(query);
|
|
72
|
+
if ("selection" in query) {
|
|
73
|
+
const res = this.values(stmt).map(
|
|
74
|
+
([item]) => JSON.parse(item).result
|
|
75
|
+
);
|
|
76
|
+
if (query.singleResult)
|
|
77
|
+
return res[0];
|
|
78
|
+
return res;
|
|
79
|
+
} else if (query.type === QueryType.Raw) {
|
|
80
|
+
switch (query.expectedReturn) {
|
|
81
|
+
case "row":
|
|
82
|
+
return this.rows(stmt)[0];
|
|
83
|
+
case "rows":
|
|
84
|
+
return this.rows(stmt);
|
|
85
|
+
default:
|
|
86
|
+
this.execute(stmt);
|
|
87
|
+
return void 0;
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
return this.mutate(stmt);
|
|
91
|
+
}
|
|
49
92
|
}
|
|
50
93
|
}
|
|
51
94
|
all(strings, ...params) {
|
|
@@ -56,17 +99,17 @@ var Driver;
|
|
|
56
99
|
}
|
|
57
100
|
transaction(run) {
|
|
58
101
|
const id = `t${this.transactionId++}`;
|
|
59
|
-
this.
|
|
102
|
+
this.executeQuery(
|
|
60
103
|
Query.Transaction({ op: Query.TransactionOperation.Begin, id })
|
|
61
104
|
);
|
|
62
105
|
try {
|
|
63
106
|
const res = run(this);
|
|
64
|
-
this.
|
|
107
|
+
this.executeQuery(
|
|
65
108
|
Query.Transaction({ op: Query.TransactionOperation.Commit, id })
|
|
66
109
|
);
|
|
67
110
|
return res;
|
|
68
111
|
} catch (e) {
|
|
69
|
-
this.
|
|
112
|
+
this.executeQuery(
|
|
70
113
|
Query.Transaction({ op: Query.TransactionOperation.Rollback, id })
|
|
71
114
|
);
|
|
72
115
|
throw e;
|
|
@@ -79,16 +122,57 @@ var Driver;
|
|
|
79
122
|
Driver2.Sync = Sync;
|
|
80
123
|
class Async extends DriverBase {
|
|
81
124
|
transactionId = 0;
|
|
125
|
+
async migrateSchema(...tables) {
|
|
126
|
+
const queries = [];
|
|
127
|
+
for (const table of Object.values(tables)) {
|
|
128
|
+
const schema = table.schema();
|
|
129
|
+
const localSchema = await this.schemaInstructions(schema.name);
|
|
130
|
+
if (!localSchema) {
|
|
131
|
+
queries.push(...Schema.create(schema).queries);
|
|
132
|
+
} else {
|
|
133
|
+
const changes = Schema.upgrade(this.formatter, localSchema, schema);
|
|
134
|
+
if (changes.length)
|
|
135
|
+
queries.push(...changes);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return this.executeQuery(Query.Batch({ queries }));
|
|
139
|
+
}
|
|
82
140
|
async executeQuery(query) {
|
|
83
141
|
switch (query.type) {
|
|
84
142
|
case QueryType.Batch:
|
|
85
143
|
let result;
|
|
86
144
|
const stmts = query.queries;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
145
|
+
if (stmts.length === 0)
|
|
146
|
+
return void 0;
|
|
147
|
+
if (stmts.length === 1)
|
|
148
|
+
return this.executeQuery(stmts[0]);
|
|
149
|
+
return this.transaction(async (cnx) => {
|
|
150
|
+
for (const query2 of stmts)
|
|
151
|
+
result = await cnx.executeQuery(query2);
|
|
152
|
+
return result;
|
|
153
|
+
});
|
|
90
154
|
default:
|
|
91
|
-
|
|
155
|
+
const stmt = this.formatter.compile(query);
|
|
156
|
+
if ("selection" in query) {
|
|
157
|
+
const res = (await this.values(stmt)).map(
|
|
158
|
+
([item]) => JSON.parse(item).result
|
|
159
|
+
);
|
|
160
|
+
if (query.singleResult)
|
|
161
|
+
return res[0];
|
|
162
|
+
return res;
|
|
163
|
+
} else if (query.type === QueryType.Raw) {
|
|
164
|
+
switch (query.expectedReturn) {
|
|
165
|
+
case "row":
|
|
166
|
+
return (await this.rows(stmt))[0];
|
|
167
|
+
case "rows":
|
|
168
|
+
return await this.rows(stmt);
|
|
169
|
+
default:
|
|
170
|
+
await this.execute(stmt);
|
|
171
|
+
return void 0;
|
|
172
|
+
}
|
|
173
|
+
} else {
|
|
174
|
+
return await this.mutate(stmt);
|
|
175
|
+
}
|
|
92
176
|
}
|
|
93
177
|
}
|
|
94
178
|
all(strings, ...params) {
|
|
@@ -100,17 +184,17 @@ var Driver;
|
|
|
100
184
|
async transaction(run) {
|
|
101
185
|
const id = `t${this.transactionId++}`;
|
|
102
186
|
const [connection, release] = this.isolate();
|
|
103
|
-
await connection.
|
|
187
|
+
await connection.executeQuery(
|
|
104
188
|
Query.Transaction({ op: Query.TransactionOperation.Begin, id })
|
|
105
189
|
);
|
|
106
190
|
try {
|
|
107
191
|
const res = await run(connection);
|
|
108
|
-
await connection.
|
|
192
|
+
await connection.executeQuery(
|
|
109
193
|
Query.Transaction({ op: Query.TransactionOperation.Commit, id })
|
|
110
194
|
);
|
|
111
195
|
return res;
|
|
112
196
|
} catch (e) {
|
|
113
|
-
await connection.
|
|
197
|
+
await connection.executeQuery(
|
|
114
198
|
Query.Transaction({ op: Query.TransactionOperation.Rollback, id })
|
|
115
199
|
);
|
|
116
200
|
throw e;
|
|
@@ -122,13 +206,28 @@ var Driver;
|
|
|
122
206
|
Driver2.Async = Async;
|
|
123
207
|
class SyncWrapper extends Async {
|
|
124
208
|
constructor(sync) {
|
|
125
|
-
super();
|
|
209
|
+
super(sync.formatter);
|
|
126
210
|
this.sync = sync;
|
|
127
211
|
}
|
|
128
212
|
lock;
|
|
129
|
-
async
|
|
213
|
+
async executeQuery(query) {
|
|
130
214
|
await this.lock;
|
|
131
|
-
return
|
|
215
|
+
return super.executeQuery(query);
|
|
216
|
+
}
|
|
217
|
+
async rows(stmt) {
|
|
218
|
+
return this.sync.rows(stmt);
|
|
219
|
+
}
|
|
220
|
+
async values(stmt) {
|
|
221
|
+
return this.sync.values(stmt);
|
|
222
|
+
}
|
|
223
|
+
async execute(stmt) {
|
|
224
|
+
return this.sync.execute(stmt);
|
|
225
|
+
}
|
|
226
|
+
async mutate(stmt) {
|
|
227
|
+
return this.sync.mutate(stmt);
|
|
228
|
+
}
|
|
229
|
+
async schemaInstructions(tableName) {
|
|
230
|
+
return this.sync.schemaInstructions(tableName);
|
|
132
231
|
}
|
|
133
232
|
isolate() {
|
|
134
233
|
const connection = new SyncWrapper(this.sync);
|
package/dist/Formatter.d.ts
CHANGED
|
@@ -7,27 +7,32 @@ import { Statement } from './Statement';
|
|
|
7
7
|
import { Target } from './Target';
|
|
8
8
|
export interface FormatContext {
|
|
9
9
|
nameResult?: string;
|
|
10
|
+
skipTableName?: boolean;
|
|
10
11
|
formatAsJson?: boolean;
|
|
11
12
|
formatSubject?: (subject: Statement) => Statement;
|
|
12
13
|
}
|
|
13
14
|
export declare abstract class Formatter implements Sanitizer {
|
|
14
15
|
constructor();
|
|
15
16
|
abstract escapeValue(value: any): string;
|
|
16
|
-
abstract
|
|
17
|
+
abstract escapeIdentifier(ident: string): string;
|
|
17
18
|
abstract formatSqlAccess(on: Statement, field: string): Statement;
|
|
18
19
|
abstract formatJsonAccess(on: Statement, field: string): Statement;
|
|
19
20
|
formatAccess(on: Statement, field: string, formatAsJson?: boolean): Statement;
|
|
20
|
-
compile<T>(query: Query<T>, formatInline?: boolean):
|
|
21
|
+
compile<T>(query: Query<T>, formatInline?: boolean): Statement.Compiled;
|
|
21
22
|
format<T>(query: Query<T>, ctx?: FormatContext): Statement;
|
|
22
23
|
formatSelect(query: Query.Select, ctx: FormatContext): Statement;
|
|
23
24
|
formatInsert(query: Query.Insert, ctx: FormatContext): Statement;
|
|
24
25
|
formatUpdate(query: Query.Update, ctx: FormatContext): Statement;
|
|
25
26
|
formatDelete(query: Query.Delete, ctx: FormatContext): Statement;
|
|
26
27
|
formatCreateTable(query: Query.CreateTable, ctx: FormatContext): Statement;
|
|
28
|
+
formatCreateIndex(query: Query.CreateIndex, ctx?: FormatContext): Statement;
|
|
29
|
+
formatDropIndex(query: Query.DropIndex, ctx: FormatContext): Statement;
|
|
30
|
+
formatAlterTable(query: Query.AlterTable, ctx: FormatContext): Statement;
|
|
27
31
|
formatTransaction({ op, id }: Query.Transaction, ctx: FormatContext): Statement;
|
|
28
32
|
formatBatch(queries: Query[], ctx: FormatContext): Statement;
|
|
29
33
|
formatRaw({ strings, params }: Query.Raw, ctx: FormatContext): Statement;
|
|
30
34
|
formatColumn(column: ColumnData): Statement;
|
|
35
|
+
formatContraintReference(reference: ExprData): Statement;
|
|
31
36
|
formatType(type: ColumnType): Statement;
|
|
32
37
|
formatInsertRow(columns: Record<string, ColumnData>, row: Record<string, any>): Statement;
|
|
33
38
|
formatColumnValue(column: ColumnData, columnValue: any): Statement;
|
|
@@ -44,6 +49,7 @@ export declare abstract class Formatter implements Sanitizer {
|
|
|
44
49
|
retrieveField(expr: ExprData, field: string): ExprData | undefined;
|
|
45
50
|
formatField(expr: ExprData, field: string, ctx: FormatContext): Statement;
|
|
46
51
|
formatString(input: string): Statement;
|
|
52
|
+
formatInlineValue(rawValue: any): Statement;
|
|
47
53
|
formatValue(rawValue: any, formatAsJson?: boolean): Statement;
|
|
48
54
|
formatExpr(expr: ExprData, ctx: FormatContext): Statement;
|
|
49
55
|
}
|
package/dist/Formatter.js
CHANGED
|
@@ -65,6 +65,12 @@ var Formatter = class {
|
|
|
65
65
|
return this.formatDelete(query, ctx);
|
|
66
66
|
case QueryType.CreateTable:
|
|
67
67
|
return this.formatCreateTable(query, ctx);
|
|
68
|
+
case QueryType.CreateIndex:
|
|
69
|
+
return this.formatCreateIndex(query, ctx);
|
|
70
|
+
case QueryType.DropIndex:
|
|
71
|
+
return this.formatDropIndex(query, ctx);
|
|
72
|
+
case QueryType.AlterTable:
|
|
73
|
+
return this.formatAlterTable(query, ctx);
|
|
68
74
|
case QueryType.Transaction:
|
|
69
75
|
return this.formatTransaction(query, ctx);
|
|
70
76
|
case QueryType.Batch:
|
|
@@ -114,6 +120,29 @@ var Formatter = class {
|
|
|
114
120
|
})
|
|
115
121
|
);
|
|
116
122
|
}
|
|
123
|
+
formatCreateIndex(query, ctx = {}) {
|
|
124
|
+
return raw("create index").addIf(query.ifNotExists, "if not exists").addIdentifier(query.index.name).add("on").addIdentifier(query.table.name).parenthesis(
|
|
125
|
+
separated(
|
|
126
|
+
query.index.on.map(
|
|
127
|
+
(expr) => this.formatExprValue(expr, { ...ctx, skipTableName: true })
|
|
128
|
+
)
|
|
129
|
+
)
|
|
130
|
+
).add(this.formatWhere(query.where, ctx));
|
|
131
|
+
}
|
|
132
|
+
formatDropIndex(query, ctx) {
|
|
133
|
+
return raw("drop index").addIf(query.ifExists, "if exists").addIdentifier(query.index.name);
|
|
134
|
+
}
|
|
135
|
+
formatAlterTable(query, ctx) {
|
|
136
|
+
let stmt = raw("alter table").addIdentifier(query.table.name);
|
|
137
|
+
if (query.addColumn) {
|
|
138
|
+
stmt = stmt.add("add column").add(this.formatColumn(query.addColumn));
|
|
139
|
+
} else if (query.dropColumn) {
|
|
140
|
+
stmt = stmt.add("drop column").addIdentifier(query.dropColumn);
|
|
141
|
+
} else if (query.alterColumn) {
|
|
142
|
+
throw new Error(`Not available in this formatter: alter column`);
|
|
143
|
+
}
|
|
144
|
+
return stmt;
|
|
145
|
+
}
|
|
117
146
|
formatTransaction({ op, id }, ctx) {
|
|
118
147
|
switch (op) {
|
|
119
148
|
case Query.TransactionOperation.Begin:
|
|
@@ -131,23 +160,32 @@ var Formatter = class {
|
|
|
131
160
|
);
|
|
132
161
|
}
|
|
133
162
|
formatRaw({ strings, params }, ctx) {
|
|
134
|
-
return Statement.
|
|
163
|
+
return Statement.tag(strings, ...params);
|
|
135
164
|
}
|
|
136
165
|
formatColumn(column) {
|
|
137
166
|
return identifier(column.name).add(this.formatType(column.type)).addIf(column.unique, "unique").addIf(column.autoIncrement, "autoincrement").addIf(column.primaryKey, "primary key").addIf(!column.nullable, "not null").addIf(
|
|
138
167
|
column.defaultValue !== void 0,
|
|
139
|
-
raw("default").
|
|
140
|
-
)
|
|
168
|
+
raw("default").add(this.formatInlineValue(column.defaultValue))
|
|
169
|
+
).addIf(column.references, () => {
|
|
170
|
+
return this.formatContraintReference(column.references);
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
formatContraintReference(reference) {
|
|
174
|
+
if (reference.type !== ExprType.Field || reference.expr.type !== ExprType.Row)
|
|
175
|
+
throw new Error("not supported");
|
|
176
|
+
const from = reference.expr.target;
|
|
177
|
+
return raw("references").addIdentifier(Target.source(from).name).parenthesis(identifier(reference.field));
|
|
141
178
|
}
|
|
142
179
|
formatType(type) {
|
|
143
180
|
switch (type) {
|
|
144
181
|
case ColumnType.String:
|
|
145
|
-
case ColumnType.Object:
|
|
146
|
-
case ColumnType.Array:
|
|
147
182
|
return raw("text");
|
|
183
|
+
case ColumnType.Json:
|
|
184
|
+
return raw("json");
|
|
148
185
|
case ColumnType.Number:
|
|
149
|
-
case ColumnType.Boolean:
|
|
150
186
|
return raw("numeric");
|
|
187
|
+
case ColumnType.Boolean:
|
|
188
|
+
return raw("boolean");
|
|
151
189
|
case ColumnType.Integer:
|
|
152
190
|
return raw("integer");
|
|
153
191
|
}
|
|
@@ -184,14 +222,10 @@ var Formatter = class {
|
|
|
184
222
|
if (typeof columnValue !== "boolean")
|
|
185
223
|
throw new TypeError(`Expected boolean for column ${column.name}`);
|
|
186
224
|
return value(columnValue);
|
|
187
|
-
case ColumnType.
|
|
225
|
+
case ColumnType.Json:
|
|
188
226
|
if (typeof columnValue !== "object")
|
|
189
227
|
throw new TypeError(`Expected object for column ${column.name}`);
|
|
190
228
|
return value(JSON.stringify(columnValue));
|
|
191
|
-
case ColumnType.Array:
|
|
192
|
-
if (!Array.isArray(columnValue))
|
|
193
|
-
throw new TypeError(`Expected array for column ${column.name}`);
|
|
194
|
-
return value(JSON.stringify(columnValue));
|
|
195
229
|
}
|
|
196
230
|
}
|
|
197
231
|
formatTarget(target, ctx) {
|
|
@@ -283,13 +317,10 @@ var Formatter = class {
|
|
|
283
317
|
case ExprType.Row:
|
|
284
318
|
switch (expr.target.type) {
|
|
285
319
|
case TargetType.Table:
|
|
286
|
-
const selection = identifier(
|
|
287
|
-
expr.target.table.alias || expr.target.table.name
|
|
288
|
-
).raw(".").identifier(field);
|
|
320
|
+
const selection = ctx.skipTableName ? identifier(field) : identifier(expr.target.table.alias || expr.target.table.name).raw(".").identifier(field);
|
|
289
321
|
const column = expr.target.table.columns[field];
|
|
290
322
|
switch (column?.type) {
|
|
291
|
-
case ColumnType.
|
|
292
|
-
case ColumnType.Object:
|
|
323
|
+
case ColumnType.Json:
|
|
293
324
|
return call("json", selection);
|
|
294
325
|
default:
|
|
295
326
|
return selection;
|
|
@@ -306,6 +337,18 @@ var Formatter = class {
|
|
|
306
337
|
formatString(input) {
|
|
307
338
|
return raw(this.escapeValue(String(input)));
|
|
308
339
|
}
|
|
340
|
+
formatInlineValue(rawValue) {
|
|
341
|
+
switch (true) {
|
|
342
|
+
case (rawValue === null || rawValue === void 0):
|
|
343
|
+
return raw("null");
|
|
344
|
+
case typeof rawValue === "boolean":
|
|
345
|
+
return rawValue ? raw("true") : raw("false");
|
|
346
|
+
case (typeof rawValue === "string" || typeof rawValue === "number"):
|
|
347
|
+
return this.formatString(rawValue);
|
|
348
|
+
default:
|
|
349
|
+
return this.formatString(JSON.stringify(rawValue));
|
|
350
|
+
}
|
|
351
|
+
}
|
|
309
352
|
formatValue(rawValue, formatAsJson) {
|
|
310
353
|
switch (true) {
|
|
311
354
|
case (rawValue === null || rawValue === void 0):
|
|
@@ -407,7 +450,7 @@ var Formatter = class {
|
|
|
407
450
|
return call(
|
|
408
451
|
"json_object",
|
|
409
452
|
...Object.entries(expr.fields).map(([key, value2]) => {
|
|
410
|
-
return this.formatString(key).raw(",").
|
|
453
|
+
return this.formatString(key).raw(", ").concat(this.formatExpr(value2, { ...ctx, formatAsJson: true }));
|
|
411
454
|
})
|
|
412
455
|
);
|
|
413
456
|
case ExprType.Filter: {
|
package/dist/Ops.js
CHANGED
|
@@ -2,38 +2,37 @@
|
|
|
2
2
|
import { Cursor } from "./Cursor.js";
|
|
3
3
|
import { ExprData } from "./Expr.js";
|
|
4
4
|
import { Query } from "./Query.js";
|
|
5
|
+
import { Schema } from "./Schema.js";
|
|
5
6
|
import { Target } from "./Target.js";
|
|
6
7
|
function selectAll(table) {
|
|
7
8
|
return new Cursor.SelectMultiple(
|
|
8
9
|
Query.Select({
|
|
9
|
-
from: Target.Table(table.
|
|
10
|
-
selection: ExprData.Row(Target.Table(table.
|
|
10
|
+
from: Target.Table(table.schema()),
|
|
11
|
+
selection: ExprData.Row(Target.Table(table.schema()))
|
|
11
12
|
})
|
|
12
13
|
);
|
|
13
14
|
}
|
|
14
15
|
function selectFirst(table) {
|
|
15
16
|
return new Cursor.SelectSingle(
|
|
16
17
|
Query.Select({
|
|
17
|
-
from: Target.Table(table.
|
|
18
|
-
selection: ExprData.Row(Target.Table(table.
|
|
18
|
+
from: Target.Table(table.schema()),
|
|
19
|
+
selection: ExprData.Row(Target.Table(table.schema())),
|
|
19
20
|
singleResult: true
|
|
20
21
|
})
|
|
21
22
|
);
|
|
22
23
|
}
|
|
23
24
|
function update(table) {
|
|
24
|
-
return new Cursor.Update(Query.Update({ table: table.
|
|
25
|
+
return new Cursor.Update(Query.Update({ table: table.schema() }));
|
|
25
26
|
}
|
|
26
27
|
function insertInto(table) {
|
|
27
|
-
return new Cursor.Insert(table.
|
|
28
|
+
return new Cursor.Insert(table.schema());
|
|
28
29
|
}
|
|
29
30
|
function deleteFrom(table) {
|
|
30
|
-
return new Cursor.Delete(Query.Delete({ table: table.
|
|
31
|
+
return new Cursor.Delete(Query.Delete({ table: table.schema() }));
|
|
31
32
|
}
|
|
32
33
|
function create(...tables) {
|
|
33
34
|
return new Cursor.Batch(
|
|
34
|
-
tables.
|
|
35
|
-
(table) => Query.CreateTable({ table: table.data(), ifNotExists: true })
|
|
36
|
-
)
|
|
35
|
+
tables.flatMap((table) => Schema.create(table.schema()).queries)
|
|
37
36
|
);
|
|
38
37
|
}
|
|
39
38
|
export {
|