rado 0.3.1 → 0.3.2
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 +275 -275
- package/dist/define/Column.d.ts +14 -1
- package/dist/define/Column.js +25 -1
- package/dist/define/Cursor.d.ts +85 -0
- package/dist/define/Cursor.js +256 -0
- package/dist/define/Update.d.ts +4 -0
- package/dist/define/Update.js +0 -0
- package/dist/lib/Column.d.ts +57 -0
- package/dist/lib/Column.js +68 -0
- package/dist/lib/Cursor.d.ts +85 -0
- package/dist/lib/Cursor.js +256 -0
- package/dist/lib/Expr.d.ts +154 -0
- package/dist/lib/Expr.js +284 -0
- package/dist/lib/Fields.d.ts +13 -0
- package/dist/lib/Fields.js +0 -0
- package/dist/lib/Formatter.d.ts +1 -1
- package/dist/lib/Formatter.js +19 -4
- package/dist/lib/Functions.d.ts +5 -0
- package/dist/lib/Functions.js +11 -0
- package/dist/lib/Id.d.ts +3 -0
- package/dist/lib/Id.js +0 -0
- package/dist/lib/Index.d.ts +17 -0
- package/dist/lib/Index.js +20 -0
- package/dist/lib/Ops.d.ts +7 -0
- package/dist/lib/Ops.js +36 -0
- package/dist/lib/OrderBy.d.ts +9 -0
- package/dist/lib/OrderBy.js +9 -0
- package/dist/lib/Param.d.ts +20 -0
- package/dist/lib/Param.js +27 -0
- package/dist/lib/Query.d.ts +109 -0
- package/dist/lib/Query.js +74 -0
- package/dist/lib/Schema.d.ts +18 -0
- package/dist/lib/Schema.js +67 -0
- package/dist/lib/Selection.d.ts +20 -0
- package/dist/lib/Selection.js +7 -0
- package/dist/lib/Table.d.ts +62 -0
- package/dist/lib/Table.js +130 -0
- package/dist/lib/Target.d.ts +38 -0
- package/dist/lib/Target.js +42 -0
- package/dist/lib/Update.d.ts +4 -0
- package/dist/lib/Update.js +0 -0
- package/package.json +74 -74
package/dist/lib/Formatter.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ export declare abstract class Formatter implements Sanitizer {
|
|
|
62
62
|
formatBatch(ctx: FormatContext, { queries }: QueryData.Batch): Statement;
|
|
63
63
|
formatRaw(ctx: FormatContext, { strings, params }: QueryData.Raw): Statement;
|
|
64
64
|
formatColumn(ctx: FormatContext, column: ColumnData): Statement;
|
|
65
|
-
formatConstraintReference(ctx: FormatContext,
|
|
65
|
+
formatConstraintReference(ctx: FormatContext, { references, onDelete, onUpdate }: ColumnData): Statement;
|
|
66
66
|
formatType(ctx: FormatContext, type: ColumnType): Statement;
|
|
67
67
|
formatInsertRow(ctx: FormatContext, columns: Record<string, ColumnData>, row: Record<string, any>): Statement;
|
|
68
68
|
formatColumnValue(ctx: FormatContext, column: ColumnData, columnValue: any): Statement;
|
package/dist/lib/Formatter.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ColumnType } from "../define/Column.js";
|
|
1
|
+
import { Action, ColumnType } from "../define/Column.js";
|
|
2
2
|
import { BinOpType, Expr, ExprData, ExprType, UnOpType } from "../define/Expr.js";
|
|
3
3
|
import { OrderDirection } from "../define/OrderBy.js";
|
|
4
4
|
import { ParamType } from "../define/Param.js";
|
|
@@ -36,6 +36,13 @@ const unions = {
|
|
|
36
36
|
[QueryData.UnionOperation.Intersect]: "INTERSECT",
|
|
37
37
|
[QueryData.UnionOperation.Except]: "EXCEPT"
|
|
38
38
|
};
|
|
39
|
+
const actions = {
|
|
40
|
+
[Action.NoAction]: "NO ACTION",
|
|
41
|
+
[Action.Restrict]: "RESTRICT",
|
|
42
|
+
[Action.Cascade]: "CASCADE",
|
|
43
|
+
[Action.SetNull]: "SET NULL",
|
|
44
|
+
[Action.SetDefault]: "SET DEFAULT"
|
|
45
|
+
};
|
|
39
46
|
function formatAsResultObject(stmt, mkSubject) {
|
|
40
47
|
stmt.raw("json_object('result', ");
|
|
41
48
|
mkSubject();
|
|
@@ -318,15 +325,23 @@ class Formatter {
|
|
|
318
325
|
}
|
|
319
326
|
}
|
|
320
327
|
if (column.references)
|
|
321
|
-
this.formatConstraintReference(ctx, column
|
|
328
|
+
this.formatConstraintReference(ctx, column);
|
|
322
329
|
return stmt;
|
|
323
330
|
}
|
|
324
|
-
formatConstraintReference(ctx,
|
|
331
|
+
formatConstraintReference(ctx, { references, onDelete, onUpdate }) {
|
|
325
332
|
const { stmt } = ctx;
|
|
333
|
+
if (!references)
|
|
334
|
+
return stmt;
|
|
335
|
+
const reference = references();
|
|
326
336
|
if (reference.type !== ExprType.Field || reference.expr.type !== ExprType.Row)
|
|
327
337
|
throw new Error("not supported");
|
|
328
338
|
const from = reference.expr.target;
|
|
329
|
-
|
|
339
|
+
stmt.add("REFERENCES").addIdentifier(Target.source(from).name).openParenthesis().identifier(reference.field).closeParenthesis();
|
|
340
|
+
if (onDelete && actions[onDelete])
|
|
341
|
+
stmt.add("ON DELETE").add(actions[onDelete]);
|
|
342
|
+
if (onUpdate && actions[onUpdate])
|
|
343
|
+
stmt.add("ON UPDATE").add(actions[onUpdate]);
|
|
344
|
+
return stmt;
|
|
330
345
|
}
|
|
331
346
|
formatType(ctx, type) {
|
|
332
347
|
const { stmt } = ctx;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// src/lib/Functions.ts
|
|
2
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
3
|
+
function get(_, method) {
|
|
4
|
+
return (...args) => {
|
|
5
|
+
return new Expr(ExprData.Call(method, args.map(ExprData.create)));
|
|
6
|
+
};
|
|
7
|
+
}
|
|
8
|
+
var Functions = new Proxy({}, { get });
|
|
9
|
+
export {
|
|
10
|
+
Functions
|
|
11
|
+
};
|
package/dist/lib/Id.d.ts
ADDED
package/dist/lib/Id.js
ADDED
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Expr, ExprData } from './Expr';
|
|
2
|
+
interface PartialIndexData {
|
|
3
|
+
on: Array<ExprData>;
|
|
4
|
+
unique?: boolean;
|
|
5
|
+
where?: ExprData;
|
|
6
|
+
}
|
|
7
|
+
export interface IndexData extends PartialIndexData {
|
|
8
|
+
name: string;
|
|
9
|
+
}
|
|
10
|
+
export declare class Index {
|
|
11
|
+
data: PartialIndexData;
|
|
12
|
+
constructor(data: PartialIndexData);
|
|
13
|
+
unique(): Index;
|
|
14
|
+
where(where: Expr<boolean>): Index;
|
|
15
|
+
}
|
|
16
|
+
export declare function index(...on: Array<Expr<any>>): Index;
|
|
17
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// src/lib/Index.ts
|
|
2
|
+
import { ExprData } from "./Expr.js";
|
|
3
|
+
var Index = class {
|
|
4
|
+
constructor(data) {
|
|
5
|
+
this.data = data;
|
|
6
|
+
}
|
|
7
|
+
unique() {
|
|
8
|
+
return new Index({ ...this.data, unique: true });
|
|
9
|
+
}
|
|
10
|
+
where(where) {
|
|
11
|
+
return new Index({ ...this.data, where: ExprData.create(where) });
|
|
12
|
+
}
|
|
13
|
+
};
|
|
14
|
+
function index(...on) {
|
|
15
|
+
return new Index({ on: on.map(ExprData.create) });
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
Index,
|
|
19
|
+
index
|
|
20
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { Cursor } from './Cursor';
|
|
2
|
+
import { Table } from './Table';
|
|
3
|
+
export declare function from<Row>(source: Table<Row> | Cursor.SelectMultiple<Row>): Cursor.SelectMultiple<Row>;
|
|
4
|
+
export declare function update<Row>(table: Table<Row>): Cursor.Update<Row>;
|
|
5
|
+
export declare function insertInto<Row>(table: Table<Row>): Cursor.Insert<Row>;
|
|
6
|
+
export declare function deleteFrom<Row>(table: Table<Row>): Cursor.Delete;
|
|
7
|
+
export declare function create(...tables: Array<Table<any>>): Cursor.Batch;
|
package/dist/lib/Ops.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
// src/lib/Ops.ts
|
|
2
|
+
import { Cursor } from "./Cursor.js";
|
|
3
|
+
import { ExprData } from "./Expr.js";
|
|
4
|
+
import { Query } from "./Query.js";
|
|
5
|
+
import { Schema } from "./Schema.js";
|
|
6
|
+
import { Target } from "./Target.js";
|
|
7
|
+
function from(source) {
|
|
8
|
+
const target = "schema" in source ? Target.Table(source.schema()) : Target.Query(source.query());
|
|
9
|
+
return new Cursor.SelectMultiple(
|
|
10
|
+
Query.Select({
|
|
11
|
+
from: target,
|
|
12
|
+
selection: ExprData.Row(target)
|
|
13
|
+
})
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
function update(table) {
|
|
17
|
+
return new Cursor.Update(Query.Update({ table: table.schema() }));
|
|
18
|
+
}
|
|
19
|
+
function insertInto(table) {
|
|
20
|
+
return new Cursor.Insert(table.schema());
|
|
21
|
+
}
|
|
22
|
+
function deleteFrom(table) {
|
|
23
|
+
return new Cursor.Delete(Query.Delete({ table: table.schema() }));
|
|
24
|
+
}
|
|
25
|
+
function create(...tables) {
|
|
26
|
+
return new Cursor.Batch(
|
|
27
|
+
tables.flatMap((table) => Schema.create(table.schema()).queries)
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
export {
|
|
31
|
+
create,
|
|
32
|
+
deleteFrom,
|
|
33
|
+
from,
|
|
34
|
+
insertInto,
|
|
35
|
+
update
|
|
36
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { Expr } from './Expr';
|
|
2
|
+
export declare enum ParamType {
|
|
3
|
+
Value = "Value",
|
|
4
|
+
Named = "Named"
|
|
5
|
+
}
|
|
6
|
+
export type ParamData = {
|
|
7
|
+
type: ParamType.Value;
|
|
8
|
+
value: any;
|
|
9
|
+
} | {
|
|
10
|
+
type: ParamType.Named;
|
|
11
|
+
name: string;
|
|
12
|
+
};
|
|
13
|
+
export declare const ParamData: {
|
|
14
|
+
Value(value: any): ParamData;
|
|
15
|
+
Named(name: string): ParamData;
|
|
16
|
+
};
|
|
17
|
+
export type Params<T> = {
|
|
18
|
+
[K in keyof T]: Expr<T[K]>;
|
|
19
|
+
};
|
|
20
|
+
export declare function createParams<T>(): Params<T>;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
// src/lib/Param.ts
|
|
2
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
3
|
+
var ParamType = /* @__PURE__ */ ((ParamType2) => {
|
|
4
|
+
ParamType2["Value"] = "Value";
|
|
5
|
+
ParamType2["Named"] = "Named";
|
|
6
|
+
return ParamType2;
|
|
7
|
+
})(ParamType || {});
|
|
8
|
+
var ParamData = {
|
|
9
|
+
Value(value) {
|
|
10
|
+
return { type: "Value" /* Value */, value };
|
|
11
|
+
},
|
|
12
|
+
Named(name) {
|
|
13
|
+
return { type: "Named" /* Named */, name };
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
function createParams() {
|
|
17
|
+
return new Proxy({}, {
|
|
18
|
+
get(target, prop) {
|
|
19
|
+
return new Expr(ExprData.Param(ParamData.Named(prop)));
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
ParamData,
|
|
25
|
+
ParamType,
|
|
26
|
+
createParams
|
|
27
|
+
};
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { ColumnData } from './Column';
|
|
2
|
+
import { ExprData } from './Expr';
|
|
3
|
+
import { IndexData } from './Index';
|
|
4
|
+
import { OrderBy } from './OrderBy';
|
|
5
|
+
import { Schema } from './Schema';
|
|
6
|
+
import { Target } from './Target';
|
|
7
|
+
export declare enum QueryType {
|
|
8
|
+
Insert = "Insert",
|
|
9
|
+
Select = "Select",
|
|
10
|
+
Update = "Update",
|
|
11
|
+
Delete = "Delete",
|
|
12
|
+
CreateTable = "CreateTable",
|
|
13
|
+
CreateIndex = "CreateIndex",
|
|
14
|
+
DropIndex = "DropIndex",
|
|
15
|
+
AlterTable = "AlterTable",
|
|
16
|
+
Batch = "Batch",
|
|
17
|
+
Transaction = "Transaction",
|
|
18
|
+
Raw = "Raw"
|
|
19
|
+
}
|
|
20
|
+
export type Query<T = any> = Query.Insert | Query.Select | Query.Update | Query.Delete | Query.CreateTable | Query.CreateIndex | Query.DropIndex | Query.AlterTable | Query.Batch | Query.Transaction | Query.Raw;
|
|
21
|
+
export declare namespace Query {
|
|
22
|
+
interface QueryBase {
|
|
23
|
+
limit?: number;
|
|
24
|
+
offset?: number;
|
|
25
|
+
type: QueryType;
|
|
26
|
+
where?: ExprData;
|
|
27
|
+
orderBy?: Array<OrderBy>;
|
|
28
|
+
groupBy?: Array<ExprData>;
|
|
29
|
+
having?: ExprData;
|
|
30
|
+
selection?: ExprData;
|
|
31
|
+
singleResult?: boolean;
|
|
32
|
+
validate?: boolean;
|
|
33
|
+
}
|
|
34
|
+
interface Insert extends QueryBase {
|
|
35
|
+
type: QueryType.Insert;
|
|
36
|
+
into: Schema;
|
|
37
|
+
data: Array<any>;
|
|
38
|
+
}
|
|
39
|
+
function Insert(insert: Omit<Insert, 'type'>): Query.Insert;
|
|
40
|
+
interface Select extends QueryBase {
|
|
41
|
+
type: QueryType.Select;
|
|
42
|
+
selection: ExprData;
|
|
43
|
+
from: Target;
|
|
44
|
+
}
|
|
45
|
+
function Select(select: Omit<Select, 'type'>): Query.Select;
|
|
46
|
+
interface Update extends QueryBase {
|
|
47
|
+
type: QueryType.Update;
|
|
48
|
+
table: Schema;
|
|
49
|
+
set?: Record<string, any>;
|
|
50
|
+
}
|
|
51
|
+
function Update(update: Omit<Update, 'type'>): Query.Update;
|
|
52
|
+
interface Delete extends QueryBase {
|
|
53
|
+
type: QueryType.Delete;
|
|
54
|
+
table: Schema;
|
|
55
|
+
}
|
|
56
|
+
function Delete(del: Omit<Delete, 'type'>): Query.Delete;
|
|
57
|
+
interface CreateTable extends QueryBase {
|
|
58
|
+
type: QueryType.CreateTable;
|
|
59
|
+
table: Schema;
|
|
60
|
+
ifNotExists?: boolean;
|
|
61
|
+
}
|
|
62
|
+
function CreateTable(create: Omit<CreateTable, 'type'>): Query.CreateTable;
|
|
63
|
+
interface CreateIndex extends QueryBase {
|
|
64
|
+
type: QueryType.CreateIndex;
|
|
65
|
+
table: Schema;
|
|
66
|
+
index: IndexData;
|
|
67
|
+
ifNotExists?: boolean;
|
|
68
|
+
}
|
|
69
|
+
function CreateIndex(create: Omit<CreateIndex, 'type'>): Query.CreateIndex;
|
|
70
|
+
interface DropIndex extends QueryBase {
|
|
71
|
+
type: QueryType.DropIndex;
|
|
72
|
+
table: Schema;
|
|
73
|
+
name: string;
|
|
74
|
+
ifExists?: boolean;
|
|
75
|
+
}
|
|
76
|
+
function DropIndex(drop: Omit<DropIndex, 'type'>): Query.DropIndex;
|
|
77
|
+
interface Batch extends QueryBase {
|
|
78
|
+
type: QueryType.Batch;
|
|
79
|
+
queries: Array<Query<any>>;
|
|
80
|
+
}
|
|
81
|
+
function Batch(batch: Omit<Batch, 'type'>): Query.Batch;
|
|
82
|
+
enum TransactionOperation {
|
|
83
|
+
Begin = "Begin",
|
|
84
|
+
Commit = "Commit",
|
|
85
|
+
Rollback = "Rollback"
|
|
86
|
+
}
|
|
87
|
+
interface Transaction extends QueryBase {
|
|
88
|
+
type: QueryType.Transaction;
|
|
89
|
+
id: string;
|
|
90
|
+
op: TransactionOperation;
|
|
91
|
+
}
|
|
92
|
+
function Transaction(transaction: Omit<Transaction, 'type'>): Query.Transaction;
|
|
93
|
+
type RawReturn = 'row' | 'rows' | undefined;
|
|
94
|
+
interface Raw extends QueryBase {
|
|
95
|
+
type: QueryType.Raw;
|
|
96
|
+
expectedReturn?: 'row' | 'rows';
|
|
97
|
+
strings: ReadonlyArray<string>;
|
|
98
|
+
params: Array<any>;
|
|
99
|
+
}
|
|
100
|
+
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
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/lib/Query.ts
|
|
2
|
+
var QueryType = /* @__PURE__ */ ((QueryType2) => {
|
|
3
|
+
QueryType2["Insert"] = "Insert";
|
|
4
|
+
QueryType2["Select"] = "Select";
|
|
5
|
+
QueryType2["Update"] = "Update";
|
|
6
|
+
QueryType2["Delete"] = "Delete";
|
|
7
|
+
QueryType2["CreateTable"] = "CreateTable";
|
|
8
|
+
QueryType2["CreateIndex"] = "CreateIndex";
|
|
9
|
+
QueryType2["DropIndex"] = "DropIndex";
|
|
10
|
+
QueryType2["AlterTable"] = "AlterTable";
|
|
11
|
+
QueryType2["Batch"] = "Batch";
|
|
12
|
+
QueryType2["Transaction"] = "Transaction";
|
|
13
|
+
QueryType2["Raw"] = "Raw";
|
|
14
|
+
return QueryType2;
|
|
15
|
+
})(QueryType || {});
|
|
16
|
+
var Query;
|
|
17
|
+
((Query2) => {
|
|
18
|
+
function Insert(insert) {
|
|
19
|
+
return { type: "Insert" /* Insert */, ...insert };
|
|
20
|
+
}
|
|
21
|
+
Query2.Insert = Insert;
|
|
22
|
+
function Select(select) {
|
|
23
|
+
return { type: "Select" /* Select */, ...select };
|
|
24
|
+
}
|
|
25
|
+
Query2.Select = Select;
|
|
26
|
+
function Update(update) {
|
|
27
|
+
return { type: "Update" /* Update */, ...update };
|
|
28
|
+
}
|
|
29
|
+
Query2.Update = Update;
|
|
30
|
+
function Delete(del) {
|
|
31
|
+
return { type: "Delete" /* Delete */, ...del };
|
|
32
|
+
}
|
|
33
|
+
Query2.Delete = Delete;
|
|
34
|
+
function CreateTable(create) {
|
|
35
|
+
return { type: "CreateTable" /* CreateTable */, ...create };
|
|
36
|
+
}
|
|
37
|
+
Query2.CreateTable = CreateTable;
|
|
38
|
+
function CreateIndex(create) {
|
|
39
|
+
return { type: "CreateIndex" /* CreateIndex */, ...create };
|
|
40
|
+
}
|
|
41
|
+
Query2.CreateIndex = CreateIndex;
|
|
42
|
+
function DropIndex(drop) {
|
|
43
|
+
return { type: "DropIndex" /* DropIndex */, ...drop };
|
|
44
|
+
}
|
|
45
|
+
Query2.DropIndex = DropIndex;
|
|
46
|
+
function Batch(batch) {
|
|
47
|
+
return { type: "Batch" /* Batch */, ...batch };
|
|
48
|
+
}
|
|
49
|
+
Query2.Batch = Batch;
|
|
50
|
+
let TransactionOperation;
|
|
51
|
+
((TransactionOperation2) => {
|
|
52
|
+
TransactionOperation2["Begin"] = "Begin";
|
|
53
|
+
TransactionOperation2["Commit"] = "Commit";
|
|
54
|
+
TransactionOperation2["Rollback"] = "Rollback";
|
|
55
|
+
})(TransactionOperation = Query2.TransactionOperation || (Query2.TransactionOperation = {}));
|
|
56
|
+
function Transaction(transaction) {
|
|
57
|
+
return { type: "Transaction" /* Transaction */, ...transaction };
|
|
58
|
+
}
|
|
59
|
+
Query2.Transaction = Transaction;
|
|
60
|
+
function Raw(raw) {
|
|
61
|
+
if (raw.strings.some((chunk) => chunk.includes("?")))
|
|
62
|
+
throw new TypeError("SQL injection hazard");
|
|
63
|
+
return { type: "Raw" /* Raw */, ...raw };
|
|
64
|
+
}
|
|
65
|
+
Query2.Raw = Raw;
|
|
66
|
+
function AlterTable(alter) {
|
|
67
|
+
return { type: "AlterTable" /* AlterTable */, ...alter };
|
|
68
|
+
}
|
|
69
|
+
Query2.AlterTable = AlterTable;
|
|
70
|
+
})(Query || (Query = {}));
|
|
71
|
+
export {
|
|
72
|
+
Query,
|
|
73
|
+
QueryType
|
|
74
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { ColumnData } from './Column';
|
|
2
|
+
import { Formatter } from './Formatter';
|
|
3
|
+
import { IndexData } from './Index';
|
|
4
|
+
import { Query } from './Query';
|
|
5
|
+
export interface Schema {
|
|
6
|
+
name: string;
|
|
7
|
+
alias?: string;
|
|
8
|
+
columns: Record<string, ColumnData>;
|
|
9
|
+
indexes: Record<string, IndexData>;
|
|
10
|
+
}
|
|
11
|
+
export interface SchemaInstructions {
|
|
12
|
+
columns: Record<string, string>;
|
|
13
|
+
indexes: Record<string, string>;
|
|
14
|
+
}
|
|
15
|
+
export declare namespace Schema {
|
|
16
|
+
function create(schema: Schema): Query.Batch;
|
|
17
|
+
function upgrade(formatter: Formatter, local: SchemaInstructions, schema: Schema): Array<Query>;
|
|
18
|
+
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// src/lib/Schema.ts
|
|
2
|
+
import { Query } from "./Query.js";
|
|
3
|
+
import { Statement } from "./Statement.js";
|
|
4
|
+
var Schema;
|
|
5
|
+
((Schema2) => {
|
|
6
|
+
function create(schema) {
|
|
7
|
+
const queries = [];
|
|
8
|
+
queries.push(Query.CreateTable({ table: schema, ifNotExists: true }));
|
|
9
|
+
for (const index of Object.values(schema.indexes))
|
|
10
|
+
queries.push(Query.CreateIndex({ table: schema, index, ifNotExists: true }));
|
|
11
|
+
return Query.Batch({ queries });
|
|
12
|
+
}
|
|
13
|
+
Schema2.create = create;
|
|
14
|
+
function removeLeadingWhitespace(str) {
|
|
15
|
+
return str.replace(/\n\s+/g, "\n");
|
|
16
|
+
}
|
|
17
|
+
function upgrade(formatter, local, schema) {
|
|
18
|
+
const columnNames = /* @__PURE__ */ new Set([
|
|
19
|
+
...Object.keys(local.columns),
|
|
20
|
+
...Object.keys(schema.columns)
|
|
21
|
+
]);
|
|
22
|
+
const res = [];
|
|
23
|
+
for (const columnName of columnNames) {
|
|
24
|
+
const localInstruction = local.columns[columnName];
|
|
25
|
+
const schemaCol = schema.columns[columnName];
|
|
26
|
+
if (!localInstruction) {
|
|
27
|
+
res.push(Query.AlterTable({ table: schema, addColumn: schemaCol }));
|
|
28
|
+
} else if (!schemaCol) {
|
|
29
|
+
res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
|
|
30
|
+
} else {
|
|
31
|
+
const { sql: instruction } = formatter.formatColumn(
|
|
32
|
+
{ stmt: new Statement(formatter) },
|
|
33
|
+
{ ...schemaCol, references: void 0 }
|
|
34
|
+
);
|
|
35
|
+
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
36
|
+
res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const indexNames = /* @__PURE__ */ new Set([
|
|
41
|
+
...Object.keys(local.indexes),
|
|
42
|
+
...Object.keys(schema.indexes)
|
|
43
|
+
]);
|
|
44
|
+
for (const indexName of indexNames) {
|
|
45
|
+
const localInstruction = local.indexes[indexName];
|
|
46
|
+
const schemaIndex = schema.indexes[indexName];
|
|
47
|
+
if (!localInstruction) {
|
|
48
|
+
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
49
|
+
} else if (!schemaIndex) {
|
|
50
|
+
res.unshift(Query.DropIndex({ table: schema, name: indexName }));
|
|
51
|
+
} else {
|
|
52
|
+
const { sql: instruction } = formatter.compile(
|
|
53
|
+
Query.CreateIndex({ table: schema, index: schemaIndex })
|
|
54
|
+
);
|
|
55
|
+
if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
|
|
56
|
+
res.unshift(Query.DropIndex({ table: schema, name: indexName }));
|
|
57
|
+
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return res;
|
|
62
|
+
}
|
|
63
|
+
Schema2.upgrade = upgrade;
|
|
64
|
+
})(Schema || (Schema = {}));
|
|
65
|
+
export {
|
|
66
|
+
Schema
|
|
67
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Cursor } from './Cursor';
|
|
2
|
+
import type { Expr } from './Expr';
|
|
3
|
+
type SelectionBase = (() => any) | Expr<any> | Cursor.SelectMultiple<any> | Cursor.SelectSingle<any>;
|
|
4
|
+
interface SelectionRecord extends Record<string, Selection> {
|
|
5
|
+
}
|
|
6
|
+
export type Selection = SelectionBase | SelectionRecord;
|
|
7
|
+
export declare namespace Selection {
|
|
8
|
+
const __tableType: unique symbol;
|
|
9
|
+
const __cursorType: unique symbol;
|
|
10
|
+
type Infer<T> = T extends {
|
|
11
|
+
[__tableType](): infer K;
|
|
12
|
+
} ? K : T extends {
|
|
13
|
+
[__cursorType](): infer K;
|
|
14
|
+
} ? K : T extends Expr<infer K> ? K : T extends Record<string, Selection> ? {
|
|
15
|
+
[K in keyof T as T[K] extends () => any ? never : K]: Infer<T[K]>;
|
|
16
|
+
} : T extends () => any ? never : T;
|
|
17
|
+
type With<A, B> = Expr<Combine<A, B>>;
|
|
18
|
+
type Combine<A, B> = Omit<A, keyof Infer<B>> & Infer<B>;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Column, PrimaryKey } from './Column';
|
|
2
|
+
import { Cursor } from './Cursor';
|
|
3
|
+
import { EV, Expr } from './Expr';
|
|
4
|
+
import { Fields } from './Fields';
|
|
5
|
+
import { Index } from './Index';
|
|
6
|
+
import { Schema } from './Schema';
|
|
7
|
+
import { Selection } from './Selection';
|
|
8
|
+
import { Update } from './Update';
|
|
9
|
+
export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>> {
|
|
10
|
+
[Selection.__tableType](): Table.Normalize<T>;
|
|
11
|
+
constructor(schema: Schema);
|
|
12
|
+
fetch(id: EV<T extends {
|
|
13
|
+
id: Column.IsPrimary<infer V, any>;
|
|
14
|
+
} ? V : never>): Cursor.SelectSingle<Table.Normalize<T> | undefined>;
|
|
15
|
+
insertOne(record: Table.Insert<T>): Cursor<Table.Normalize<T>>;
|
|
16
|
+
insertAll(data: Array<Table.Insert<T>>): Cursor.InsertValues;
|
|
17
|
+
set(data: Update<Table.Normalize<T>>): Cursor.Update<Table.Normalize<T>>;
|
|
18
|
+
delete(): Cursor.Delete;
|
|
19
|
+
createTable(): Cursor.CreateTable;
|
|
20
|
+
as(alias: string): this;
|
|
21
|
+
alias(): Record<string, this>;
|
|
22
|
+
get(name: string): Expr<any>;
|
|
23
|
+
toExpr(): Expr<Table.Normalize<T>>;
|
|
24
|
+
schema(): Schema;
|
|
25
|
+
}
|
|
26
|
+
export declare namespace Table {
|
|
27
|
+
type Intersection<A, B> = A & B extends infer U ? {
|
|
28
|
+
[P in keyof U]: EV<U[P]>;
|
|
29
|
+
} : never;
|
|
30
|
+
type OptionalKeys<T> = {
|
|
31
|
+
[K in keyof T]: null extends T[K] ? K : T[K] extends Column.IsPrimary<any, any> ? K : T[K] extends Column.IsOptional<any> ? K : never;
|
|
32
|
+
}[keyof T];
|
|
33
|
+
type RequiredKeys<T> = {
|
|
34
|
+
[K in keyof T]: null extends T[K] ? never : T[K] extends Column.IsPrimary<any, any> ? never : T[K] extends Column.IsOptional<any> ? never : K;
|
|
35
|
+
}[keyof T];
|
|
36
|
+
type Optionals<T> = {
|
|
37
|
+
[K in keyof T]?: T[K] extends Column.IsOptional<infer V> ? V : T[K] extends Column.IsPrimary<infer V, infer K> ? PrimaryKey<V, K> : T[K];
|
|
38
|
+
};
|
|
39
|
+
export type Insert<T> = Intersection<Optionals<Pick<T, OptionalKeys<T>>>, Pick<T, RequiredKeys<T>>>;
|
|
40
|
+
export type Normalize<T> = {
|
|
41
|
+
[K in keyof T]: T[K] extends Column.IsOptional<infer V> ? V : T[K] extends Column.IsPrimary<infer V, infer K> ? PrimaryKey<V, K> : T[K];
|
|
42
|
+
};
|
|
43
|
+
export type Infer<T> = T extends Table<infer U> ? Normalize<U> : never;
|
|
44
|
+
export {};
|
|
45
|
+
}
|
|
46
|
+
export type TableOptions<T, R> = {
|
|
47
|
+
name: string;
|
|
48
|
+
alias?: string;
|
|
49
|
+
columns: {
|
|
50
|
+
[K in keyof T]: Column<T[K]>;
|
|
51
|
+
};
|
|
52
|
+
indexes?: (this: Fields<T>) => Record<string, Index>;
|
|
53
|
+
};
|
|
54
|
+
export declare function table<T extends {}, R>(options: TableOptions<T, R>): Table<T> & Fields<T>;
|
|
55
|
+
export declare namespace table {
|
|
56
|
+
export type infer<T> = Table.Infer<T>;
|
|
57
|
+
type Extensions<T> = {
|
|
58
|
+
[key: string]: (this: T, ...args: any[]) => any;
|
|
59
|
+
};
|
|
60
|
+
export function extend<T extends Table<any>, E extends Extensions<T>>(target: T, extensions: E): T & E;
|
|
61
|
+
export {};
|
|
62
|
+
}
|