rado 0.1.64 → 0.2.1
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/README.md +269 -0
- package/dist/define/Column.d.ts +19 -19
- package/dist/define/Column.js +17 -5
- package/dist/define/Cursor.d.ts +48 -37
- package/dist/define/Cursor.js +109 -42
- package/dist/define/Expr.d.ts +2 -1
- package/dist/define/Expr.js +4 -3
- package/dist/define/Fields.d.ts +2 -2
- package/dist/define/Ops.js +6 -5
- package/dist/define/Query.d.ts +33 -18
- package/dist/define/Query.js +10 -5
- package/dist/define/Schema.d.ts +3 -10
- package/dist/define/Schema.js +51 -6
- package/dist/define/Selection.d.ts +3 -3
- package/dist/define/Table.d.ts +59 -51
- package/dist/define/Table.js +148 -103
- package/dist/define/Target.d.ts +4 -4
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/lib/Callable.d.ts +3 -0
- package/dist/lib/Callable.js +14 -0
- package/dist/lib/Driver.js +5 -4
- package/dist/lib/Formatter.d.ts +3 -0
- package/dist/lib/Formatter.js +58 -20
- package/package.json +1 -1
- package/dist/define/Update.d.ts +0 -4
- package/dist/define/Update.js +0 -0
package/dist/define/Cursor.js
CHANGED
|
@@ -4,6 +4,7 @@ import { Functions } from "./Functions.js";
|
|
|
4
4
|
import { Query } from "./Query.js";
|
|
5
5
|
import { Schema } from "./Schema.js";
|
|
6
6
|
import { Selection } from "./Selection.js";
|
|
7
|
+
import { createTable, table } from "./Table.js";
|
|
7
8
|
import { Target } from "./Target.js";
|
|
8
9
|
var Cursor = class {
|
|
9
10
|
[Selection.__cursorType]() {
|
|
@@ -21,11 +22,18 @@ var Cursor = class {
|
|
|
21
22
|
query() {
|
|
22
23
|
throw new Error("Not implemented");
|
|
23
24
|
}
|
|
25
|
+
next(cursor) {
|
|
26
|
+
return new Cursor(
|
|
27
|
+
Query.Batch({
|
|
28
|
+
queries: [this.query(), cursor.query()]
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
}
|
|
24
32
|
on(driver) {
|
|
25
33
|
return driver.executeQuery(this.query());
|
|
26
34
|
}
|
|
27
|
-
|
|
28
|
-
return driver.formatter.compile(this.query(), options);
|
|
35
|
+
toSql(driver, options = { forceInline: true }) {
|
|
36
|
+
return driver.formatter.compile(this.query(), options).sql;
|
|
29
37
|
}
|
|
30
38
|
toJSON() {
|
|
31
39
|
return this.query();
|
|
@@ -77,7 +85,7 @@ function addWhere(query, where) {
|
|
|
77
85
|
class InsertValuesReturning extends Cursor2 {
|
|
78
86
|
}
|
|
79
87
|
Cursor2.InsertValuesReturning = InsertValuesReturning;
|
|
80
|
-
class
|
|
88
|
+
class Inserted extends Cursor2 {
|
|
81
89
|
query() {
|
|
82
90
|
return super.query();
|
|
83
91
|
}
|
|
@@ -87,20 +95,25 @@ function addWhere(query, where) {
|
|
|
87
95
|
);
|
|
88
96
|
}
|
|
89
97
|
}
|
|
90
|
-
Cursor2.
|
|
98
|
+
Cursor2.Inserted = Inserted;
|
|
91
99
|
class Insert {
|
|
92
100
|
constructor(into) {
|
|
93
101
|
this.into = into;
|
|
94
102
|
}
|
|
103
|
+
selection(query) {
|
|
104
|
+
return new Inserted(
|
|
105
|
+
Query.Insert({ into: this.into, select: query.query() })
|
|
106
|
+
);
|
|
107
|
+
}
|
|
95
108
|
values(...data) {
|
|
96
|
-
return new
|
|
109
|
+
return new Inserted(Query.Insert({ into: this.into, data }));
|
|
97
110
|
}
|
|
98
111
|
}
|
|
99
112
|
Cursor2.Insert = Insert;
|
|
100
113
|
class CreateTable extends Cursor2 {
|
|
101
|
-
constructor(
|
|
102
|
-
super(Schema.create(
|
|
103
|
-
this.table =
|
|
114
|
+
constructor(table2) {
|
|
115
|
+
super(Schema.create(table2));
|
|
116
|
+
this.table = table2;
|
|
104
117
|
}
|
|
105
118
|
}
|
|
106
119
|
Cursor2.CreateTable = CreateTable;
|
|
@@ -109,38 +122,52 @@ function addWhere(query, where) {
|
|
|
109
122
|
super(Query.Batch({ queries }));
|
|
110
123
|
this.queries = queries;
|
|
111
124
|
}
|
|
125
|
+
query() {
|
|
126
|
+
return super.query();
|
|
127
|
+
}
|
|
128
|
+
next(cursor) {
|
|
129
|
+
return new Cursor2(
|
|
130
|
+
Query.Batch({
|
|
131
|
+
queries: [...this.query().queries, cursor.query()]
|
|
132
|
+
})
|
|
133
|
+
);
|
|
134
|
+
}
|
|
112
135
|
}
|
|
113
136
|
Cursor2.Batch = Batch;
|
|
137
|
+
function joinTarget(joinType, query, to, on) {
|
|
138
|
+
const toQuery = to instanceof Cursor2 ? to.query() : void 0;
|
|
139
|
+
const target = toQuery ? toQuery.from : Target.Table(to[table.data]);
|
|
140
|
+
if (!query.from || !target)
|
|
141
|
+
throw new Error("No from clause");
|
|
142
|
+
const conditions = [...on];
|
|
143
|
+
if (toQuery) {
|
|
144
|
+
const where = toQuery.where;
|
|
145
|
+
if (where)
|
|
146
|
+
conditions.push(new Expr(where));
|
|
147
|
+
}
|
|
148
|
+
return Target.Join(
|
|
149
|
+
query.from,
|
|
150
|
+
target,
|
|
151
|
+
joinType,
|
|
152
|
+
Expr.and(...conditions).expr
|
|
153
|
+
);
|
|
154
|
+
}
|
|
114
155
|
class SelectMultiple extends Cursor2 {
|
|
115
156
|
query() {
|
|
116
157
|
return super.query();
|
|
117
158
|
}
|
|
118
159
|
leftJoin(that, ...on) {
|
|
119
160
|
const query = this.query();
|
|
120
|
-
if (!query.from)
|
|
121
|
-
throw new Error("No from clause");
|
|
122
161
|
return new SelectMultiple({
|
|
123
162
|
...query,
|
|
124
|
-
from:
|
|
125
|
-
query.from,
|
|
126
|
-
Target.Table(that.schema()),
|
|
127
|
-
"left",
|
|
128
|
-
Expr.and(...on).expr
|
|
129
|
-
)
|
|
163
|
+
from: joinTarget("left", query, that, on)
|
|
130
164
|
});
|
|
131
165
|
}
|
|
132
166
|
innerJoin(that, ...on) {
|
|
133
167
|
const query = this.query();
|
|
134
|
-
if (!query.from)
|
|
135
|
-
throw new Error("No from clause");
|
|
136
168
|
return new SelectMultiple({
|
|
137
169
|
...query,
|
|
138
|
-
from:
|
|
139
|
-
query.from,
|
|
140
|
-
Target.Table(that.schema()),
|
|
141
|
-
"inner",
|
|
142
|
-
Expr.and(...on).expr
|
|
143
|
-
)
|
|
170
|
+
from: joinTarget("inner", query, that, on)
|
|
144
171
|
});
|
|
145
172
|
}
|
|
146
173
|
select(selection) {
|
|
@@ -189,41 +216,81 @@ function addWhere(query, where) {
|
|
|
189
216
|
skip(offset) {
|
|
190
217
|
return new SelectMultiple({ ...this.query(), offset });
|
|
191
218
|
}
|
|
192
|
-
toExpr() {
|
|
219
|
+
[Expr.toExpr]() {
|
|
193
220
|
return new Expr(ExprData.Query(this.query()));
|
|
194
221
|
}
|
|
195
222
|
}
|
|
196
223
|
Cursor2.SelectMultiple = SelectMultiple;
|
|
224
|
+
class TableSelect extends SelectMultiple {
|
|
225
|
+
constructor(table2, conditions = []) {
|
|
226
|
+
const target = Target.Table(table2);
|
|
227
|
+
super(
|
|
228
|
+
Query.Select({
|
|
229
|
+
from: target,
|
|
230
|
+
selection: ExprData.Row(target),
|
|
231
|
+
where: Expr.and(...conditions).expr
|
|
232
|
+
})
|
|
233
|
+
);
|
|
234
|
+
this.table = table2;
|
|
235
|
+
}
|
|
236
|
+
as(alias) {
|
|
237
|
+
return createTable({ ...this.table, alias });
|
|
238
|
+
}
|
|
239
|
+
create() {
|
|
240
|
+
return new Cursor2.CreateTable(this.table);
|
|
241
|
+
}
|
|
242
|
+
insertSelect(query) {
|
|
243
|
+
return new Cursor2.Inserted(
|
|
244
|
+
Query.Insert({ into: this.table, select: query.query() })
|
|
245
|
+
);
|
|
246
|
+
}
|
|
247
|
+
insertOne(record) {
|
|
248
|
+
return new Cursor2(
|
|
249
|
+
Query.Insert({
|
|
250
|
+
into: this.table,
|
|
251
|
+
data: [record],
|
|
252
|
+
selection: ExprData.Row(Target.Table(this.table)),
|
|
253
|
+
singleResult: true
|
|
254
|
+
})
|
|
255
|
+
);
|
|
256
|
+
}
|
|
257
|
+
insertAll(data) {
|
|
258
|
+
return new Cursor2.Insert(this.table).values(...data);
|
|
259
|
+
}
|
|
260
|
+
set(data) {
|
|
261
|
+
return new Cursor2.Update(
|
|
262
|
+
Query.Update({
|
|
263
|
+
table: this.table,
|
|
264
|
+
where: this.query().where
|
|
265
|
+
})
|
|
266
|
+
).set(data);
|
|
267
|
+
}
|
|
268
|
+
delete() {
|
|
269
|
+
return new Cursor2.Delete(
|
|
270
|
+
Query.Delete({
|
|
271
|
+
table: this.table,
|
|
272
|
+
where: this.query().where
|
|
273
|
+
})
|
|
274
|
+
);
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
Cursor2.TableSelect = TableSelect;
|
|
197
278
|
class SelectSingle extends Cursor2 {
|
|
198
279
|
query() {
|
|
199
280
|
return super.query();
|
|
200
281
|
}
|
|
201
282
|
leftJoin(that, ...on) {
|
|
202
283
|
const query = this.query();
|
|
203
|
-
if (!query.from)
|
|
204
|
-
throw new Error("No from clause");
|
|
205
284
|
return new SelectSingle({
|
|
206
285
|
...query,
|
|
207
|
-
from:
|
|
208
|
-
query.from,
|
|
209
|
-
Target.Table(that.schema()),
|
|
210
|
-
"left",
|
|
211
|
-
Expr.and(...on).expr
|
|
212
|
-
)
|
|
286
|
+
from: joinTarget("left", query, that, on)
|
|
213
287
|
});
|
|
214
288
|
}
|
|
215
289
|
innerJoin(that, ...on) {
|
|
216
290
|
const query = this.query();
|
|
217
|
-
if (!query.from)
|
|
218
|
-
throw new Error("No from clause");
|
|
219
291
|
return new SelectSingle({
|
|
220
292
|
...query,
|
|
221
|
-
from:
|
|
222
|
-
query.from,
|
|
223
|
-
Target.Table(that.schema()),
|
|
224
|
-
"inner",
|
|
225
|
-
Expr.and(...on).expr
|
|
226
|
-
)
|
|
293
|
+
from: joinTarget("inner", query, that, on)
|
|
227
294
|
});
|
|
228
295
|
}
|
|
229
296
|
select(selection) {
|
|
@@ -256,7 +323,7 @@ function addWhere(query, where) {
|
|
|
256
323
|
all() {
|
|
257
324
|
return new SelectMultiple({ ...this.query(), singleResult: false });
|
|
258
325
|
}
|
|
259
|
-
toExpr() {
|
|
326
|
+
[Expr.toExpr]() {
|
|
260
327
|
return new Expr(ExprData.Query(this.query()));
|
|
261
328
|
}
|
|
262
329
|
}
|
package/dist/define/Expr.d.ts
CHANGED
|
@@ -128,7 +128,8 @@ export declare const ExprData: {
|
|
|
128
128
|
export type EV<T> = Expr<T> | T;
|
|
129
129
|
export declare class Expr<T> {
|
|
130
130
|
expr: ExprData;
|
|
131
|
-
static NULL:
|
|
131
|
+
static NULL: Expr<null>;
|
|
132
|
+
static toExpr: symbol;
|
|
132
133
|
static value<T>(value: T): Expr<T>;
|
|
133
134
|
static create<T>(input: EV<T>): Expr<T>;
|
|
134
135
|
static and(...conditions: Array<EV<boolean>>): Expr<boolean>;
|
package/dist/define/Expr.js
CHANGED
|
@@ -91,8 +91,8 @@ var ExprData = {
|
|
|
91
91
|
create(input) {
|
|
92
92
|
if (input === null || input === void 0)
|
|
93
93
|
return ExprData.Param(ParamData.Value(null));
|
|
94
|
-
if (input && typeof input
|
|
95
|
-
input = input.toExpr();
|
|
94
|
+
if (input && typeof input[Expr.toExpr] === "function")
|
|
95
|
+
input = input[Expr.toExpr]();
|
|
96
96
|
if (input instanceof Expr)
|
|
97
97
|
return input.expr;
|
|
98
98
|
if (input && typeof input === "object" && !Array.isArray(input))
|
|
@@ -277,7 +277,8 @@ var _Expr = class {
|
|
|
277
277
|
}
|
|
278
278
|
};
|
|
279
279
|
var Expr = _Expr;
|
|
280
|
-
__publicField(Expr, "NULL",
|
|
280
|
+
__publicField(Expr, "NULL", _Expr.create(null));
|
|
281
|
+
__publicField(Expr, "toExpr", Symbol("toExpr"));
|
|
281
282
|
export {
|
|
282
283
|
BinOpType,
|
|
283
284
|
Expr,
|
package/dist/define/Fields.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { OptionalColumn, PrimaryColumn, PrimaryKey } from './Column';
|
|
2
2
|
import { Expr } from './Expr';
|
|
3
3
|
type ObjectUnion<T> = {
|
|
4
4
|
[K in T extends infer P ? keyof P : never]: T extends infer P ? K extends keyof P ? P[K] : never : never;
|
|
5
5
|
};
|
|
6
6
|
type RecordField<T> = Expr<T> & FieldsOf<ObjectUnion<T>>;
|
|
7
|
-
type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [
|
|
7
|
+
type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [PrimaryColumn<infer V, infer K>] ? Expr<PrimaryKey<V, K>> : [T] extends [OptionalColumn<infer V>] ? Field<V> : [T] extends [number | string | boolean] ? Expr<T> : [T] extends [Record<string, any> | null] ? RecordField<T> : Expr<T>;
|
|
8
8
|
type FieldsOf<Row> = Row extends Record<string, any> ? {
|
|
9
9
|
[K in keyof Row]-?: Field<Row[K]>;
|
|
10
10
|
} : never;
|
package/dist/define/Ops.js
CHANGED
|
@@ -3,6 +3,7 @@ import { Cursor } from "./Cursor.js";
|
|
|
3
3
|
import { ExprData } from "./Expr.js";
|
|
4
4
|
import { Query } from "./Query.js";
|
|
5
5
|
import { Schema } from "./Schema.js";
|
|
6
|
+
import { table as createTable } from "./Table.js";
|
|
6
7
|
import { Target } from "./Target.js";
|
|
7
8
|
function select(selection) {
|
|
8
9
|
return new Cursor.SelectMultiple(
|
|
@@ -12,7 +13,7 @@ function select(selection) {
|
|
|
12
13
|
);
|
|
13
14
|
}
|
|
14
15
|
function from(source) {
|
|
15
|
-
const target =
|
|
16
|
+
const target = source instanceof Cursor ? Target.Query(source.query()) : Target.Table(source[createTable.data]);
|
|
16
17
|
return new Cursor.SelectMultiple(
|
|
17
18
|
Query.Select({
|
|
18
19
|
from: target,
|
|
@@ -21,17 +22,17 @@ function from(source) {
|
|
|
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[createTable.data] }));
|
|
25
26
|
}
|
|
26
27
|
function insertInto(table) {
|
|
27
|
-
return new Cursor.Insert(table.
|
|
28
|
+
return new Cursor.Insert(table[createTable.data]);
|
|
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[createTable.data] }));
|
|
31
32
|
}
|
|
32
33
|
function create(...tables) {
|
|
33
34
|
return new Cursor.Batch(
|
|
34
|
-
tables.flatMap((table) => Schema.create(table.
|
|
35
|
+
tables.flatMap((table) => Schema.create(table[createTable.data]).queries)
|
|
35
36
|
);
|
|
36
37
|
}
|
|
37
38
|
export {
|
package/dist/define/Query.d.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { ColumnData } from './Column';
|
|
|
2
2
|
import { ExprData } from './Expr';
|
|
3
3
|
import { IndexData } from './Index';
|
|
4
4
|
import { OrderBy } from './OrderBy';
|
|
5
|
-
import {
|
|
5
|
+
import { TableData } from './Table';
|
|
6
6
|
import { Target } from './Target';
|
|
7
7
|
export declare enum QueryType {
|
|
8
8
|
Insert = "Insert",
|
|
@@ -10,14 +10,15 @@ export declare enum QueryType {
|
|
|
10
10
|
Update = "Update",
|
|
11
11
|
Delete = "Delete",
|
|
12
12
|
CreateTable = "CreateTable",
|
|
13
|
+
AlterTable = "AlterTable",
|
|
14
|
+
DropTable = "DropTable",
|
|
13
15
|
CreateIndex = "CreateIndex",
|
|
14
16
|
DropIndex = "DropIndex",
|
|
15
|
-
AlterTable = "AlterTable",
|
|
16
17
|
Batch = "Batch",
|
|
17
18
|
Transaction = "Transaction",
|
|
18
19
|
Raw = "Raw"
|
|
19
20
|
}
|
|
20
|
-
export type Query<T = any> = Query.Insert | Query.Select | Query.Update | Query.Delete | Query.CreateTable | Query.
|
|
21
|
+
export type Query<T = any> = Query.Insert | Query.Select | Query.Update | Query.Delete | Query.CreateTable | Query.AlterTable | Query.DropTable | Query.CreateIndex | Query.DropIndex | Query.Batch | Query.Transaction | Query.Raw;
|
|
21
22
|
export declare namespace Query {
|
|
22
23
|
interface QueryBase {
|
|
23
24
|
limit?: number;
|
|
@@ -33,8 +34,9 @@ export declare namespace Query {
|
|
|
33
34
|
}
|
|
34
35
|
interface Insert extends QueryBase {
|
|
35
36
|
type: QueryType.Insert;
|
|
36
|
-
into:
|
|
37
|
-
data
|
|
37
|
+
into: TableData;
|
|
38
|
+
data?: Array<any>;
|
|
39
|
+
select?: Query.Select;
|
|
38
40
|
}
|
|
39
41
|
function Insert(insert: Omit<Insert, 'type'>): Query.Insert;
|
|
40
42
|
interface Select extends QueryBase {
|
|
@@ -45,31 +47,52 @@ export declare namespace Query {
|
|
|
45
47
|
function Select(select: Omit<Select, 'type'>): Query.Select;
|
|
46
48
|
interface Update extends QueryBase {
|
|
47
49
|
type: QueryType.Update;
|
|
48
|
-
table:
|
|
50
|
+
table: TableData;
|
|
49
51
|
set?: Record<string, any>;
|
|
50
52
|
}
|
|
51
53
|
function Update(update: Omit<Update, 'type'>): Query.Update;
|
|
52
54
|
interface Delete extends QueryBase {
|
|
53
55
|
type: QueryType.Delete;
|
|
54
|
-
table:
|
|
56
|
+
table: TableData;
|
|
55
57
|
}
|
|
56
58
|
function Delete(del: Omit<Delete, 'type'>): Query.Delete;
|
|
57
59
|
interface CreateTable extends QueryBase {
|
|
58
60
|
type: QueryType.CreateTable;
|
|
59
|
-
table:
|
|
61
|
+
table: TableData;
|
|
60
62
|
ifNotExists?: boolean;
|
|
61
63
|
}
|
|
62
64
|
function CreateTable(create: Omit<CreateTable, 'type'>): Query.CreateTable;
|
|
65
|
+
interface AlterTable extends QueryBase {
|
|
66
|
+
type: QueryType.AlterTable;
|
|
67
|
+
table: TableData;
|
|
68
|
+
alterColumn?: ColumnData;
|
|
69
|
+
renameColumn?: {
|
|
70
|
+
from: string;
|
|
71
|
+
to: string;
|
|
72
|
+
};
|
|
73
|
+
addColumn?: ColumnData;
|
|
74
|
+
dropColumn?: string;
|
|
75
|
+
renameTable?: {
|
|
76
|
+
from: string;
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
function AlterTable(alter: Omit<AlterTable, 'type'>): Query.AlterTable;
|
|
80
|
+
interface DropTable extends QueryBase {
|
|
81
|
+
type: QueryType.DropTable;
|
|
82
|
+
table: TableData;
|
|
83
|
+
ifExists?: boolean;
|
|
84
|
+
}
|
|
85
|
+
function DropTable(drop: Omit<DropTable, 'type'>): Query.DropTable;
|
|
63
86
|
interface CreateIndex extends QueryBase {
|
|
64
87
|
type: QueryType.CreateIndex;
|
|
65
|
-
table:
|
|
88
|
+
table: TableData;
|
|
66
89
|
index: IndexData;
|
|
67
90
|
ifNotExists?: boolean;
|
|
68
91
|
}
|
|
69
92
|
function CreateIndex(create: Omit<CreateIndex, 'type'>): Query.CreateIndex;
|
|
70
93
|
interface DropIndex extends QueryBase {
|
|
71
94
|
type: QueryType.DropIndex;
|
|
72
|
-
table:
|
|
95
|
+
table: TableData;
|
|
73
96
|
name: string;
|
|
74
97
|
ifExists?: boolean;
|
|
75
98
|
}
|
|
@@ -98,12 +121,4 @@ export declare namespace Query {
|
|
|
98
121
|
params: Array<any>;
|
|
99
122
|
}
|
|
100
123
|
function Raw(raw: Omit<Raw, 'type'>): Query.Raw;
|
|
101
|
-
interface AlterTable extends QueryBase {
|
|
102
|
-
type: QueryType.AlterTable;
|
|
103
|
-
table: Schema;
|
|
104
|
-
alterColumn?: ColumnData;
|
|
105
|
-
addColumn?: ColumnData;
|
|
106
|
-
dropColumn?: string;
|
|
107
|
-
}
|
|
108
|
-
function AlterTable(alter: Omit<AlterTable, 'type'>): Query.AlterTable;
|
|
109
124
|
}
|
package/dist/define/Query.js
CHANGED
|
@@ -5,9 +5,10 @@ var QueryType = /* @__PURE__ */ ((QueryType2) => {
|
|
|
5
5
|
QueryType2["Update"] = "Update";
|
|
6
6
|
QueryType2["Delete"] = "Delete";
|
|
7
7
|
QueryType2["CreateTable"] = "CreateTable";
|
|
8
|
+
QueryType2["AlterTable"] = "AlterTable";
|
|
9
|
+
QueryType2["DropTable"] = "DropTable";
|
|
8
10
|
QueryType2["CreateIndex"] = "CreateIndex";
|
|
9
11
|
QueryType2["DropIndex"] = "DropIndex";
|
|
10
|
-
QueryType2["AlterTable"] = "AlterTable";
|
|
11
12
|
QueryType2["Batch"] = "Batch";
|
|
12
13
|
QueryType2["Transaction"] = "Transaction";
|
|
13
14
|
QueryType2["Raw"] = "Raw";
|
|
@@ -35,6 +36,14 @@ var Query;
|
|
|
35
36
|
return { type: "CreateTable" /* CreateTable */, ...create };
|
|
36
37
|
}
|
|
37
38
|
Query2.CreateTable = CreateTable;
|
|
39
|
+
function AlterTable(alter) {
|
|
40
|
+
return { type: "AlterTable" /* AlterTable */, ...alter };
|
|
41
|
+
}
|
|
42
|
+
Query2.AlterTable = AlterTable;
|
|
43
|
+
function DropTable(drop) {
|
|
44
|
+
return { type: "DropTable" /* DropTable */, ...drop };
|
|
45
|
+
}
|
|
46
|
+
Query2.DropTable = DropTable;
|
|
38
47
|
function CreateIndex(create) {
|
|
39
48
|
return { type: "CreateIndex" /* CreateIndex */, ...create };
|
|
40
49
|
}
|
|
@@ -63,10 +72,6 @@ var Query;
|
|
|
63
72
|
return { type: "Raw" /* Raw */, ...raw };
|
|
64
73
|
}
|
|
65
74
|
Query2.Raw = Raw;
|
|
66
|
-
function AlterTable(alter) {
|
|
67
|
-
return { type: "AlterTable" /* AlterTable */, ...alter };
|
|
68
|
-
}
|
|
69
|
-
Query2.AlterTable = AlterTable;
|
|
70
75
|
})(Query || (Query = {}));
|
|
71
76
|
export {
|
|
72
77
|
Query,
|
package/dist/define/Schema.d.ts
CHANGED
|
@@ -1,18 +1,11 @@
|
|
|
1
1
|
import { Formatter } from '../lib/Formatter';
|
|
2
|
-
import { ColumnData } from './Column';
|
|
3
|
-
import { IndexData } from './Index';
|
|
4
2
|
import { Query } from './Query';
|
|
5
|
-
|
|
6
|
-
name: string;
|
|
7
|
-
alias?: string;
|
|
8
|
-
columns: Record<string, ColumnData>;
|
|
9
|
-
indexes: Record<string, IndexData>;
|
|
10
|
-
}
|
|
3
|
+
import { TableData } from './Table';
|
|
11
4
|
export interface SchemaInstructions {
|
|
12
5
|
columns: Record<string, string>;
|
|
13
6
|
indexes: Record<string, string>;
|
|
14
7
|
}
|
|
15
8
|
export declare namespace Schema {
|
|
16
|
-
function create(schema:
|
|
17
|
-
function upgrade(formatter: Formatter, local: SchemaInstructions, schema:
|
|
9
|
+
function create(schema: TableData): Query.Batch;
|
|
10
|
+
function upgrade(formatter: Formatter, local: SchemaInstructions, schema: TableData): Array<Query>;
|
|
18
11
|
}
|
package/dist/define/Schema.js
CHANGED
|
@@ -1,25 +1,63 @@
|
|
|
1
1
|
// src/define/Schema.ts
|
|
2
2
|
import { Statement } from "../lib/Statement.js";
|
|
3
|
-
import {
|
|
3
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
4
|
+
import { Query, QueryType } from "./Query.js";
|
|
5
|
+
import { Target } from "./Target.js";
|
|
4
6
|
var Schema;
|
|
5
7
|
((Schema2) => {
|
|
6
8
|
function create(schema) {
|
|
7
9
|
const queries = [];
|
|
8
10
|
queries.push(Query.CreateTable({ table: schema, ifNotExists: true }));
|
|
9
|
-
|
|
10
|
-
|
|
11
|
+
const meta = schema.meta();
|
|
12
|
+
if (meta.indexes)
|
|
13
|
+
for (const index of Object.values(meta.indexes))
|
|
14
|
+
queries.push(
|
|
15
|
+
Query.CreateIndex({ table: schema, index, ifNotExists: true })
|
|
16
|
+
);
|
|
11
17
|
return Query.Batch({ queries });
|
|
12
18
|
}
|
|
13
19
|
Schema2.create = create;
|
|
14
20
|
function removeLeadingWhitespace(str) {
|
|
15
21
|
return str.replace(/\n\s+/g, "\n");
|
|
16
22
|
}
|
|
23
|
+
function recreateTable(table, addedColumns) {
|
|
24
|
+
const queries = [];
|
|
25
|
+
const tempTable = { ...table, name: `$$new_${table.name}` };
|
|
26
|
+
queries.push(Query.CreateTable({ table: tempTable }));
|
|
27
|
+
queries.push(
|
|
28
|
+
Query.Insert({
|
|
29
|
+
into: tempTable,
|
|
30
|
+
select: Query.Select({
|
|
31
|
+
from: Target.Table(table),
|
|
32
|
+
selection: ExprData.Record(
|
|
33
|
+
Object.fromEntries(
|
|
34
|
+
Object.entries(table.columns).map(([key, column]) => [
|
|
35
|
+
key,
|
|
36
|
+
addedColumns.has(key) ? (typeof column.defaultValue === "function" ? column.defaultValue() : column.defaultValue) || Expr.NULL.expr : ExprData.Field(ExprData.Row(Target.Table(table)), key)
|
|
37
|
+
])
|
|
38
|
+
)
|
|
39
|
+
)
|
|
40
|
+
})
|
|
41
|
+
})
|
|
42
|
+
);
|
|
43
|
+
queries.push(Query.DropTable({ table, ifExists: true }));
|
|
44
|
+
queries.push(
|
|
45
|
+
Query.AlterTable({
|
|
46
|
+
table,
|
|
47
|
+
renameTable: { from: tempTable.name }
|
|
48
|
+
})
|
|
49
|
+
);
|
|
50
|
+
for (const index of Object.values(table.meta().indexes))
|
|
51
|
+
queries.push(Query.CreateIndex({ table, index }));
|
|
52
|
+
return queries;
|
|
53
|
+
}
|
|
17
54
|
function upgrade(formatter, local, schema) {
|
|
18
55
|
const columnNames = /* @__PURE__ */ new Set([
|
|
19
56
|
...Object.keys(local.columns),
|
|
20
57
|
...Object.keys(schema.columns)
|
|
21
58
|
]);
|
|
22
59
|
const res = [];
|
|
60
|
+
let recreate = false;
|
|
23
61
|
for (const columnName of columnNames) {
|
|
24
62
|
const localInstruction = local.columns[columnName];
|
|
25
63
|
const schemaCol = schema.columns[columnName];
|
|
@@ -33,17 +71,24 @@ var Schema;
|
|
|
33
71
|
{ ...schemaCol, references: void 0 }
|
|
34
72
|
);
|
|
35
73
|
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
36
|
-
|
|
74
|
+
recreate = true;
|
|
37
75
|
}
|
|
38
76
|
}
|
|
39
77
|
}
|
|
78
|
+
if (recreate) {
|
|
79
|
+
const added = res.filter(
|
|
80
|
+
(query) => query.type === QueryType.AlterTable && Boolean(query.addColumn)
|
|
81
|
+
).map((query) => query.addColumn.name);
|
|
82
|
+
return recreateTable(schema, new Set(added));
|
|
83
|
+
}
|
|
84
|
+
const meta = schema.meta();
|
|
40
85
|
const indexNames = /* @__PURE__ */ new Set([
|
|
41
86
|
...Object.keys(local.indexes),
|
|
42
|
-
...Object.keys(
|
|
87
|
+
...Object.keys(meta.indexes)
|
|
43
88
|
]);
|
|
44
89
|
for (const indexName of indexNames) {
|
|
45
90
|
const localInstruction = local.indexes[indexName];
|
|
46
|
-
const schemaIndex =
|
|
91
|
+
const schemaIndex = meta.indexes[indexName];
|
|
47
92
|
if (!localInstruction) {
|
|
48
93
|
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
49
94
|
} else if (!schemaIndex) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Cursor } from './Cursor';
|
|
2
2
|
import type { Expr } from './Expr';
|
|
3
|
-
type SelectionBase = (() => any) | Expr<any> | Cursor.SelectMultiple<any> | Cursor.SelectSingle<any>;
|
|
3
|
+
type SelectionBase = unknown | (() => any) | Expr<any> | Cursor.SelectMultiple<any> | Cursor.SelectSingle<any>;
|
|
4
4
|
interface SelectionRecord extends Record<string, Selection> {
|
|
5
5
|
}
|
|
6
6
|
export type Selection = SelectionBase | SelectionRecord;
|
|
@@ -12,8 +12,8 @@ export declare namespace Selection {
|
|
|
12
12
|
} ? K : T extends {
|
|
13
13
|
[__cursorType](): infer K;
|
|
14
14
|
} ? K : T extends Expr<infer K> ? K : T extends Record<string, Selection> ? {
|
|
15
|
-
[K in keyof T
|
|
16
|
-
} : T extends () => any ? never : T;
|
|
15
|
+
[K in keyof T]: Infer<T[K]>;
|
|
16
|
+
} : T extends () => any ? never : unknown extends T ? never : T;
|
|
17
17
|
type With<A, B> = Expr<Combine<A, B>>;
|
|
18
18
|
type Combine<A, B> = Omit<A, keyof Infer<B>> & Infer<B>;
|
|
19
19
|
}
|