rado 0.1.60 → 0.1.62
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/CPU.20230126.092143.12244.0.001.cpuprofile +1 -0
- package/dist/Column.d.ts +54 -0
- package/dist/Column.js +63 -0
- package/dist/Cursor.d.ts +77 -0
- package/dist/Cursor.js +221 -0
- package/dist/Driver.d.ts +89 -0
- package/dist/Driver.js +262 -0
- package/dist/Expr.d.ts +149 -0
- package/dist/Expr.js +284 -0
- package/dist/Fields.d.ts +11 -0
- package/dist/Fields.js +0 -0
- package/dist/Formatter.d.ts +55 -0
- package/dist/Formatter.js +496 -0
- package/dist/Functions.d.ts +7 -0
- package/dist/Functions.js +11 -0
- package/dist/Id.d.ts +3 -0
- package/dist/Id.js +0 -0
- package/dist/Ops.d.ts +8 -0
- package/dist/Ops.js +45 -0
- package/dist/OrderBy.d.ts +9 -0
- package/dist/OrderBy.js +9 -0
- package/dist/Param.d.ts +15 -0
- package/dist/Param.js +18 -0
- package/dist/Query.d.ts +107 -0
- package/dist/Query.js +74 -0
- package/dist/Sanitizer.d.ts +4 -0
- package/dist/Sanitizer.js +0 -0
- package/dist/Schema.d.ts +23 -0
- package/dist/Schema.js +60 -0
- package/dist/Selection.d.ts +20 -0
- package/dist/Selection.js +7 -0
- package/dist/Statement.d.ts +57 -0
- package/dist/Statement.js +184 -0
- package/dist/Table.d.ts +56 -0
- package/dist/Table.js +104 -0
- package/dist/Target.d.ts +30 -0
- package/dist/Target.js +37 -0
- package/dist/Update.d.ts +4 -0
- package/dist/Update.js +0 -0
- package/dist/define/Column.d.ts +58 -58
- package/dist/define/Cursor.d.ts +87 -87
- package/dist/define/Expr.d.ts +172 -154
- package/dist/define/Expr.js +31 -31
- package/dist/define/Fields.d.ts +13 -13
- package/dist/define/Functions.d.ts +5 -5
- package/dist/define/Id.d.ts +3 -3
- package/dist/define/Index.d.ts +17 -17
- package/dist/define/Ops.d.ts +7 -7
- package/dist/define/OrderBy.d.ts +9 -9
- package/dist/define/Param.d.ts +15 -15
- package/dist/define/Query.d.ts +109 -109
- package/dist/define/Schema.d.ts +18 -18
- package/dist/define/Selection.d.ts +20 -20
- package/dist/define/Table.d.ts +62 -62
- package/dist/define/Target.d.ts +38 -38
- package/dist/define/Update.d.ts +4 -4
- package/dist/driver/better-sqlite3.d.ts +15 -15
- package/dist/driver/bun-sqlite.d.ts +18 -18
- package/dist/driver/sql.js.d.ts +15 -15
- package/dist/driver/sqlite3.d.ts +18 -18
- package/dist/index.d.ts +21 -21
- package/dist/lib/Column.d.ts +57 -0
- package/dist/lib/Column.js +63 -0
- package/dist/lib/Cursor.d.ts +79 -0
- package/dist/lib/Cursor.js +231 -0
- package/dist/lib/Driver.d.ts +110 -110
- 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 +81 -80
- package/dist/lib/Formatter.js +59 -54
- 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 +8 -0
- package/dist/lib/Ops.js +45 -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/Sanitizer.d.ts +5 -5
- 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/SqlError.d.ts +3 -3
- package/dist/lib/Statement.d.ts +33 -33
- package/dist/lib/Table.d.ts +62 -0
- package/dist/lib/Table.js +130 -0
- package/dist/lib/Target.d.ts +30 -0
- package/dist/lib/Target.js +37 -0
- package/dist/lib/Update.d.ts +4 -0
- package/dist/lib/Update.js +0 -0
- package/dist/sqlite/SqliteFormatter.d.ts +11 -11
- package/dist/sqlite/SqliteFunctions.d.ts +165 -165
- package/dist/sqlite/SqliteSchema.d.ts +25 -25
- package/dist/sqlite/index.d.ts +2 -0
- package/dist/sqlite/index.js +3 -0
- package/dist/sqlite.d.ts +2 -2
- package/dist/src/driver/better-sqlite3.js +49 -0
- package/dist/src/driver/sql.js.js +66 -0
- package/dist/src/driver/sqlite3.js +87 -0
- package/dist/src/index.js +24 -0
- package/dist/src/lib/Column.js +63 -0
- package/dist/src/lib/Cursor.js +231 -0
- package/dist/src/lib/Driver.js +316 -0
- package/dist/src/lib/Expr.js +284 -0
- package/dist/src/lib/Fields.js +0 -0
- package/dist/src/lib/Formatter.js +625 -0
- package/dist/src/lib/Functions.js +11 -0
- package/dist/src/lib/Id.js +0 -0
- package/dist/src/lib/Index.js +20 -0
- package/dist/src/lib/Ops.js +45 -0
- package/dist/src/lib/OrderBy.js +9 -0
- package/dist/src/lib/Param.js +27 -0
- package/dist/src/lib/Query.js +74 -0
- package/dist/src/lib/Sanitizer.js +0 -0
- package/dist/src/lib/Schema.js +67 -0
- package/dist/src/lib/Selection.js +7 -0
- package/dist/src/lib/Statement.js +103 -0
- package/dist/src/lib/Table.js +130 -0
- package/dist/src/lib/Target.js +37 -0
- package/dist/src/lib/Update.js +0 -0
- package/dist/src/sqlite/SqliteFormatter.js +67 -0
- package/dist/src/sqlite/SqliteFunctions.js +146 -0
- package/dist/src/sqlite/SqliteSchema.js +47 -0
- package/dist/src/sqlite.js +3 -0
- package/dist/test/DbSuite.js +28 -0
- package/dist/test/TSTestTypes.js +6 -0
- package/dist/test/TestBasic.js +172 -0
- package/dist/test/TestExtend.js +68 -0
- package/dist/test/TestFts5.js +34 -0
- package/dist/test/TestFunctions.js +28 -0
- package/dist/test/TestJoins.js +34 -0
- package/dist/test/TestJson.js +33 -0
- package/dist/test/TestPrepare.js +27 -0
- package/dist/test/TestRawSql.js +31 -0
- package/dist/test/TestSchema.js +69 -0
- package/dist/test/TestSubQueries.js +79 -0
- package/dist/test/TestTransaction.js +54 -0
- package/dist/test/TestUpdate.js +95 -0
- package/dist/test/index.js +14 -0
- package/package.json +60 -60
package/dist/Query.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { ColumnData } from './Column';
|
|
2
|
+
import { ExprData } from './Expr';
|
|
3
|
+
import { OrderBy } from './OrderBy';
|
|
4
|
+
import { Index, Schema } from './Schema';
|
|
5
|
+
import { Target } from './Target';
|
|
6
|
+
export declare enum QueryType {
|
|
7
|
+
Insert = "Insert",
|
|
8
|
+
Select = "Select",
|
|
9
|
+
Update = "Update",
|
|
10
|
+
Delete = "Delete",
|
|
11
|
+
CreateTable = "CreateTable",
|
|
12
|
+
CreateIndex = "CreateIndex",
|
|
13
|
+
DropIndex = "DropIndex",
|
|
14
|
+
AlterTable = "AlterTable",
|
|
15
|
+
Batch = "Batch",
|
|
16
|
+
Transaction = "Transaction",
|
|
17
|
+
Raw = "Raw"
|
|
18
|
+
}
|
|
19
|
+
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;
|
|
20
|
+
export declare namespace Query {
|
|
21
|
+
interface QueryBase {
|
|
22
|
+
limit?: number;
|
|
23
|
+
offset?: number;
|
|
24
|
+
type: QueryType;
|
|
25
|
+
where?: ExprData;
|
|
26
|
+
orderBy?: Array<OrderBy>;
|
|
27
|
+
groupBy?: Array<ExprData>;
|
|
28
|
+
having?: ExprData;
|
|
29
|
+
selection?: ExprData;
|
|
30
|
+
singleResult?: boolean;
|
|
31
|
+
}
|
|
32
|
+
interface Insert extends QueryBase {
|
|
33
|
+
type: QueryType.Insert;
|
|
34
|
+
into: Schema;
|
|
35
|
+
data: Array<any>;
|
|
36
|
+
}
|
|
37
|
+
function Insert(insert: Omit<Insert, 'type'>): Query.Insert;
|
|
38
|
+
interface Select extends QueryBase {
|
|
39
|
+
type: QueryType.Select;
|
|
40
|
+
selection: ExprData;
|
|
41
|
+
from: Target;
|
|
42
|
+
}
|
|
43
|
+
function Select(select: Omit<Select, 'type'>): Query.Select;
|
|
44
|
+
interface Update extends QueryBase {
|
|
45
|
+
type: QueryType.Update;
|
|
46
|
+
table: Schema;
|
|
47
|
+
set?: Record<string, any>;
|
|
48
|
+
}
|
|
49
|
+
function Update(update: Omit<Update, 'type'>): Query.Update;
|
|
50
|
+
interface Delete extends QueryBase {
|
|
51
|
+
type: QueryType.Delete;
|
|
52
|
+
table: Schema;
|
|
53
|
+
}
|
|
54
|
+
function Delete(del: Omit<Delete, 'type'>): Query.Delete;
|
|
55
|
+
interface CreateTable extends QueryBase {
|
|
56
|
+
type: QueryType.CreateTable;
|
|
57
|
+
table: Schema;
|
|
58
|
+
ifNotExists?: boolean;
|
|
59
|
+
}
|
|
60
|
+
function CreateTable(create: Omit<CreateTable, 'type'>): Query.CreateTable;
|
|
61
|
+
interface CreateIndex extends QueryBase {
|
|
62
|
+
type: QueryType.CreateIndex;
|
|
63
|
+
table: Schema;
|
|
64
|
+
index: Index;
|
|
65
|
+
ifNotExists?: boolean;
|
|
66
|
+
}
|
|
67
|
+
function CreateIndex(create: Omit<CreateIndex, 'type'>): Query.CreateIndex;
|
|
68
|
+
interface DropIndex extends QueryBase {
|
|
69
|
+
type: QueryType.DropIndex;
|
|
70
|
+
table: Schema;
|
|
71
|
+
name: string;
|
|
72
|
+
ifExists?: boolean;
|
|
73
|
+
}
|
|
74
|
+
function DropIndex(drop: Omit<DropIndex, 'type'>): Query.DropIndex;
|
|
75
|
+
interface Batch extends QueryBase {
|
|
76
|
+
type: QueryType.Batch;
|
|
77
|
+
queries: Array<Query<any>>;
|
|
78
|
+
}
|
|
79
|
+
function Batch(batch: Omit<Batch, 'type'>): Query.Batch;
|
|
80
|
+
enum TransactionOperation {
|
|
81
|
+
Begin = "Begin",
|
|
82
|
+
Commit = "Commit",
|
|
83
|
+
Rollback = "Rollback"
|
|
84
|
+
}
|
|
85
|
+
interface Transaction extends QueryBase {
|
|
86
|
+
type: QueryType.Transaction;
|
|
87
|
+
id: string;
|
|
88
|
+
op: TransactionOperation;
|
|
89
|
+
}
|
|
90
|
+
function Transaction(transaction: Omit<Transaction, 'type'>): Query.Transaction;
|
|
91
|
+
type RawReturn = 'row' | 'rows' | undefined;
|
|
92
|
+
interface Raw extends QueryBase {
|
|
93
|
+
type: QueryType.Raw;
|
|
94
|
+
expectedReturn?: 'row' | 'rows';
|
|
95
|
+
strings: ReadonlyArray<string>;
|
|
96
|
+
params: Array<any>;
|
|
97
|
+
}
|
|
98
|
+
function Raw(raw: Omit<Raw, 'type'>): Query.Raw;
|
|
99
|
+
interface AlterTable extends QueryBase {
|
|
100
|
+
type: QueryType.AlterTable;
|
|
101
|
+
table: Schema;
|
|
102
|
+
alterColumn?: ColumnData;
|
|
103
|
+
addColumn?: ColumnData;
|
|
104
|
+
dropColumn?: string;
|
|
105
|
+
}
|
|
106
|
+
function AlterTable(alter: Omit<AlterTable, 'type'>): Query.AlterTable;
|
|
107
|
+
}
|
package/dist/Query.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// src/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
|
+
};
|
|
File without changes
|
package/dist/Schema.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { ColumnData } from './Column';
|
|
2
|
+
import { ExprData } from './Expr';
|
|
3
|
+
import { Formatter } from './Formatter';
|
|
4
|
+
import { Query } from './Query';
|
|
5
|
+
export interface Index {
|
|
6
|
+
name: string;
|
|
7
|
+
on: Array<ExprData>;
|
|
8
|
+
where?: ExprData;
|
|
9
|
+
}
|
|
10
|
+
export interface Schema {
|
|
11
|
+
name: string;
|
|
12
|
+
alias?: string;
|
|
13
|
+
columns: Record<string, ColumnData>;
|
|
14
|
+
indexes: Record<string, Index>;
|
|
15
|
+
}
|
|
16
|
+
export interface SchemaInstructions {
|
|
17
|
+
columns: Record<string, string>;
|
|
18
|
+
indexes: Record<string, string>;
|
|
19
|
+
}
|
|
20
|
+
export declare namespace Schema {
|
|
21
|
+
function create(schema: Schema): Query.Batch;
|
|
22
|
+
function upgrade(formatter: Formatter, local: SchemaInstructions, schema: Schema): Array<Query>;
|
|
23
|
+
}
|
package/dist/Schema.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
// src/Schema.ts
|
|
2
|
+
import { Query } from "./Query.js";
|
|
3
|
+
var Schema;
|
|
4
|
+
((Schema2) => {
|
|
5
|
+
function create(schema) {
|
|
6
|
+
const queries = [];
|
|
7
|
+
queries.push(Query.CreateTable({ table: schema, ifNotExists: true }));
|
|
8
|
+
for (const index of Object.values(schema.indexes))
|
|
9
|
+
queries.push(Query.CreateIndex({ table: schema, index, ifNotExists: true }));
|
|
10
|
+
return Query.Batch({ queries });
|
|
11
|
+
}
|
|
12
|
+
Schema2.create = create;
|
|
13
|
+
function upgrade(formatter, local, schema) {
|
|
14
|
+
const columnNames = /* @__PURE__ */ new Set([
|
|
15
|
+
...Object.keys(local.columns),
|
|
16
|
+
...Object.keys(schema.columns)
|
|
17
|
+
]);
|
|
18
|
+
const res = [];
|
|
19
|
+
for (const columnName of columnNames) {
|
|
20
|
+
const localInstruction = local.columns[columnName];
|
|
21
|
+
const schemaCol = schema.columns[columnName];
|
|
22
|
+
if (!localInstruction) {
|
|
23
|
+
res.push(Query.AlterTable({ table: schema, addColumn: schemaCol }));
|
|
24
|
+
} else if (!schemaCol) {
|
|
25
|
+
res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
|
|
26
|
+
} else {
|
|
27
|
+
const [instruction] = formatter.formatColumn({ ...schemaCol, references: void 0 }).compile(formatter);
|
|
28
|
+
if (localInstruction !== instruction) {
|
|
29
|
+
res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
const indexNames = /* @__PURE__ */ new Set([
|
|
34
|
+
...Object.keys(local.indexes),
|
|
35
|
+
...Object.keys(schema.indexes)
|
|
36
|
+
]);
|
|
37
|
+
for (const indexName of indexNames) {
|
|
38
|
+
const localInstruction = local.indexes[indexName];
|
|
39
|
+
const schemaIndex = schema.indexes[indexName];
|
|
40
|
+
if (!localInstruction) {
|
|
41
|
+
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
42
|
+
} else if (!schemaIndex) {
|
|
43
|
+
res.push(Query.DropIndex({ table: schema, name: indexName }));
|
|
44
|
+
} else {
|
|
45
|
+
const [instruction] = formatter.formatCreateIndex(
|
|
46
|
+
Query.CreateIndex({ table: schema, index: schemaIndex })
|
|
47
|
+
).compile(formatter);
|
|
48
|
+
if (localInstruction !== instruction) {
|
|
49
|
+
res.push(Query.DropIndex({ table: schema, name: indexName }));
|
|
50
|
+
res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return res;
|
|
55
|
+
}
|
|
56
|
+
Schema2.upgrade = upgrade;
|
|
57
|
+
})(Schema || (Schema = {}));
|
|
58
|
+
export {
|
|
59
|
+
Schema
|
|
60
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Cursor } from './Cursor';
|
|
2
|
+
import type { Expr } from './Expr';
|
|
3
|
+
type SelectionBase = 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]: Infer<T[K]>;
|
|
16
|
+
} : 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,57 @@
|
|
|
1
|
+
import { Sanitizer } from './Sanitizer';
|
|
2
|
+
declare enum TokenType {
|
|
3
|
+
Raw = "Raw",
|
|
4
|
+
Identifier = "Identifier",
|
|
5
|
+
Value = "Value",
|
|
6
|
+
Indent = "Indent",
|
|
7
|
+
Dedent = "Dedent"
|
|
8
|
+
}
|
|
9
|
+
declare class Token {
|
|
10
|
+
type: TokenType;
|
|
11
|
+
data: any;
|
|
12
|
+
constructor(type: TokenType, data: any);
|
|
13
|
+
static Raw(data: string): Token;
|
|
14
|
+
static Indent(): Token;
|
|
15
|
+
static Dedent(): Token;
|
|
16
|
+
static Identifier(data: string): Token;
|
|
17
|
+
static Value(data: any): Token;
|
|
18
|
+
}
|
|
19
|
+
export declare class Statement {
|
|
20
|
+
tokens: Array<Token>;
|
|
21
|
+
constructor(tokens: Array<Token>);
|
|
22
|
+
concat(...tokens: Array<Token | Statement>): Statement;
|
|
23
|
+
static create(from: string | Statement): Statement;
|
|
24
|
+
static tag(strings: ReadonlyArray<string>, ...params: Array<any>): Statement;
|
|
25
|
+
space(): Statement;
|
|
26
|
+
call(method: string, ...args: Array<Statement>): Statement;
|
|
27
|
+
addCall(method: string, ...args: Array<Statement>): Statement;
|
|
28
|
+
add(addition: undefined | string | Statement): Statement;
|
|
29
|
+
addLine(addition: undefined | string | Statement): Statement;
|
|
30
|
+
addIf(condition: any, addition: string | Statement | (() => string | Statement)): Statement;
|
|
31
|
+
indent(): Statement;
|
|
32
|
+
dedent(): Statement;
|
|
33
|
+
newline(): Statement;
|
|
34
|
+
identifier(name: string): Statement;
|
|
35
|
+
addIdentifier(name: string): Statement;
|
|
36
|
+
value(value: any): Statement;
|
|
37
|
+
addValue(value: any): Statement;
|
|
38
|
+
raw(query: string): Statement;
|
|
39
|
+
parenthesis(inner: string | Statement): Statement;
|
|
40
|
+
addParenthesis(stmnt: string | Statement): Statement;
|
|
41
|
+
separated(input: Array<Statement>, separator?: string): Statement;
|
|
42
|
+
addSeparated(input: Array<Statement>, separator?: string): Statement;
|
|
43
|
+
isEmpty(): boolean;
|
|
44
|
+
compile(sanitizer: Sanitizer, formatInline?: boolean): Statement.Compiled;
|
|
45
|
+
}
|
|
46
|
+
export declare namespace Statement {
|
|
47
|
+
type Compiled = [sql: string, params: Array<any>];
|
|
48
|
+
}
|
|
49
|
+
export declare function newline(): Statement;
|
|
50
|
+
export declare function raw(raw: string): Statement;
|
|
51
|
+
export declare function identifier(name: string): Statement;
|
|
52
|
+
export declare function value(value: any): Statement;
|
|
53
|
+
export declare function empty(): Statement;
|
|
54
|
+
export declare function parenthesis(stmnt: Statement): Statement;
|
|
55
|
+
export declare function call(method: string, ...args: Array<Statement>): Statement;
|
|
56
|
+
export declare function separated(input: Array<Statement>, separator?: string): Statement;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// src/Statement.ts
|
|
2
|
+
var Token = class {
|
|
3
|
+
constructor(type, data) {
|
|
4
|
+
this.type = type;
|
|
5
|
+
this.data = data;
|
|
6
|
+
}
|
|
7
|
+
static Raw(data) {
|
|
8
|
+
return new Token("Raw" /* Raw */, data);
|
|
9
|
+
}
|
|
10
|
+
static Indent() {
|
|
11
|
+
return new Token("Indent" /* Indent */, null);
|
|
12
|
+
}
|
|
13
|
+
static Dedent() {
|
|
14
|
+
return new Token("Dedent" /* Dedent */, null);
|
|
15
|
+
}
|
|
16
|
+
static Identifier(data) {
|
|
17
|
+
return new Token("Identifier" /* Identifier */, data);
|
|
18
|
+
}
|
|
19
|
+
static Value(data) {
|
|
20
|
+
return new Token("Value" /* Value */, data);
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
var SEPARATE = ",";
|
|
24
|
+
var WHITESPACE = " ";
|
|
25
|
+
var NEWLINE = "\n";
|
|
26
|
+
var Statement = class {
|
|
27
|
+
constructor(tokens) {
|
|
28
|
+
this.tokens = tokens;
|
|
29
|
+
}
|
|
30
|
+
concat(...tokens) {
|
|
31
|
+
return new Statement(
|
|
32
|
+
this.tokens.concat(
|
|
33
|
+
...tokens.flatMap((t) => t instanceof Statement ? t.tokens : [t])
|
|
34
|
+
)
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
static create(from) {
|
|
38
|
+
return typeof from === "string" ? raw(from) : from;
|
|
39
|
+
}
|
|
40
|
+
static tag(strings, ...params) {
|
|
41
|
+
return new Statement(
|
|
42
|
+
strings.flatMap((s, i) => [Token.Raw(s), Token.Value(params[i])]).slice(0, -1)
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
space() {
|
|
46
|
+
return this.concat(Token.Raw(WHITESPACE));
|
|
47
|
+
}
|
|
48
|
+
call(method, ...args) {
|
|
49
|
+
return this.identifier(method).parenthesis(separated(args));
|
|
50
|
+
}
|
|
51
|
+
addCall(method, ...args) {
|
|
52
|
+
return this.space().call(method, ...args);
|
|
53
|
+
}
|
|
54
|
+
add(addition) {
|
|
55
|
+
if (!addition)
|
|
56
|
+
return this;
|
|
57
|
+
if (addition instanceof Statement && addition.isEmpty())
|
|
58
|
+
return this;
|
|
59
|
+
return this.space().concat(Statement.create(addition));
|
|
60
|
+
}
|
|
61
|
+
addLine(addition) {
|
|
62
|
+
if (!addition)
|
|
63
|
+
return this;
|
|
64
|
+
if (addition instanceof Statement && addition.isEmpty())
|
|
65
|
+
return this;
|
|
66
|
+
return this.newline().concat(Statement.create(addition));
|
|
67
|
+
}
|
|
68
|
+
addIf(condition, addition) {
|
|
69
|
+
if (!condition)
|
|
70
|
+
return this;
|
|
71
|
+
return this.add(typeof addition === "function" ? addition() : addition);
|
|
72
|
+
}
|
|
73
|
+
indent() {
|
|
74
|
+
return this.concat(Token.Indent());
|
|
75
|
+
}
|
|
76
|
+
dedent() {
|
|
77
|
+
return this.concat(Token.Dedent());
|
|
78
|
+
}
|
|
79
|
+
newline() {
|
|
80
|
+
return this.raw(NEWLINE);
|
|
81
|
+
}
|
|
82
|
+
identifier(name) {
|
|
83
|
+
return this.concat(Token.Identifier(name));
|
|
84
|
+
}
|
|
85
|
+
addIdentifier(name) {
|
|
86
|
+
return this.space().identifier(name);
|
|
87
|
+
}
|
|
88
|
+
value(value2) {
|
|
89
|
+
return this.concat(Token.Value(value2));
|
|
90
|
+
}
|
|
91
|
+
addValue(value2) {
|
|
92
|
+
return this.space().value(value2);
|
|
93
|
+
}
|
|
94
|
+
raw(query) {
|
|
95
|
+
if (!query)
|
|
96
|
+
return this;
|
|
97
|
+
return this.concat(Token.Raw(query));
|
|
98
|
+
}
|
|
99
|
+
parenthesis(inner) {
|
|
100
|
+
return this.raw("(").indent().newline().concat(Statement.create(inner)).dedent().newline().raw(")");
|
|
101
|
+
}
|
|
102
|
+
addParenthesis(stmnt) {
|
|
103
|
+
return this.space().parenthesis(stmnt);
|
|
104
|
+
}
|
|
105
|
+
separated(input, separator = SEPARATE) {
|
|
106
|
+
return this.concat(
|
|
107
|
+
...input.flatMap(
|
|
108
|
+
(stmt, i) => i === 0 ? stmt.tokens : [Token.Raw(separator), Token.Raw(NEWLINE), ...stmt.tokens]
|
|
109
|
+
)
|
|
110
|
+
);
|
|
111
|
+
}
|
|
112
|
+
addSeparated(input, separator = SEPARATE) {
|
|
113
|
+
return this.space().separated(input, separator);
|
|
114
|
+
}
|
|
115
|
+
isEmpty() {
|
|
116
|
+
return this.tokens.length === 0 || this.tokens.length === 1 && this.tokens[0].type === "Raw" /* Raw */ && this.tokens[0].data === "";
|
|
117
|
+
}
|
|
118
|
+
compile(sanitizer, formatInline = false) {
|
|
119
|
+
let sql = "", params = [], indent = "";
|
|
120
|
+
for (const token of this.tokens) {
|
|
121
|
+
switch (token.type) {
|
|
122
|
+
case "Raw" /* Raw */:
|
|
123
|
+
if (token.data === NEWLINE)
|
|
124
|
+
sql += NEWLINE + indent;
|
|
125
|
+
else
|
|
126
|
+
sql += token.data;
|
|
127
|
+
break;
|
|
128
|
+
case "Identifier" /* Identifier */:
|
|
129
|
+
sql += sanitizer.escapeIdentifier(token.data);
|
|
130
|
+
break;
|
|
131
|
+
case "Value" /* Value */:
|
|
132
|
+
if (formatInline) {
|
|
133
|
+
sql += sanitizer.escapeValue(token.data);
|
|
134
|
+
} else {
|
|
135
|
+
sql += "?";
|
|
136
|
+
params.push(token.data === void 0 ? null : token.data);
|
|
137
|
+
}
|
|
138
|
+
break;
|
|
139
|
+
case "Indent" /* Indent */:
|
|
140
|
+
indent += " ";
|
|
141
|
+
break;
|
|
142
|
+
case "Dedent" /* Dedent */:
|
|
143
|
+
indent = indent.slice(0, -2);
|
|
144
|
+
break;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return [sql, params];
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
function newline() {
|
|
151
|
+
return new Statement([Token.Raw(NEWLINE)]);
|
|
152
|
+
}
|
|
153
|
+
function raw(raw2) {
|
|
154
|
+
return new Statement([Token.Raw(raw2)]);
|
|
155
|
+
}
|
|
156
|
+
function identifier(name) {
|
|
157
|
+
return new Statement([Token.Identifier(name)]);
|
|
158
|
+
}
|
|
159
|
+
function value(value2) {
|
|
160
|
+
return new Statement([Token.Value(value2)]);
|
|
161
|
+
}
|
|
162
|
+
function empty() {
|
|
163
|
+
return new Statement([]);
|
|
164
|
+
}
|
|
165
|
+
function parenthesis(stmnt) {
|
|
166
|
+
return empty().parenthesis(stmnt);
|
|
167
|
+
}
|
|
168
|
+
function call(method, ...args) {
|
|
169
|
+
return identifier(method).parenthesis(separated(args));
|
|
170
|
+
}
|
|
171
|
+
function separated(input, separator = SEPARATE) {
|
|
172
|
+
return empty().separated(input, separator);
|
|
173
|
+
}
|
|
174
|
+
export {
|
|
175
|
+
Statement,
|
|
176
|
+
call,
|
|
177
|
+
empty,
|
|
178
|
+
identifier,
|
|
179
|
+
newline,
|
|
180
|
+
parenthesis,
|
|
181
|
+
raw,
|
|
182
|
+
separated,
|
|
183
|
+
value
|
|
184
|
+
};
|
package/dist/Table.d.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Column } from './Column';
|
|
2
|
+
import { Cursor } from './Cursor';
|
|
3
|
+
import { Expr } from './Expr';
|
|
4
|
+
import { Fields } from './Fields';
|
|
5
|
+
import { Schema } from './Schema';
|
|
6
|
+
import { Selection } from './Selection';
|
|
7
|
+
import { Update } from './Update';
|
|
8
|
+
export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>> {
|
|
9
|
+
[Selection.__tableType](): Table.Normalize<T>;
|
|
10
|
+
constructor(schema: Schema);
|
|
11
|
+
insertOne(record: Table.Insert<T>): Cursor.Batch<Table.Normalize<T>>;
|
|
12
|
+
insertAll(data: Array<Table.Insert<T>>): Cursor.InsertValues;
|
|
13
|
+
set(data: Update<Table.Normalize<T>>): Cursor.Update<Table.Normalize<T>>;
|
|
14
|
+
createTable(): Cursor.Create;
|
|
15
|
+
as(alias: string): Table<T> & Fields<T>;
|
|
16
|
+
get(name: string): Expr<any>;
|
|
17
|
+
toExpr(): Expr<Table.Normalize<T>>;
|
|
18
|
+
schema(): Schema;
|
|
19
|
+
}
|
|
20
|
+
export declare namespace Table {
|
|
21
|
+
type Intersection<A, B> = A & B extends infer U ? {
|
|
22
|
+
[P in keyof U]: U[P];
|
|
23
|
+
} : never;
|
|
24
|
+
type OptionalKeys<T> = {
|
|
25
|
+
[K in keyof T]: null extends T[K] ? K : T[K] extends Column.Primary<any, any> ? K : T[K] extends Column.Optional<any> ? K : never;
|
|
26
|
+
}[keyof T];
|
|
27
|
+
type RequiredKeys<T> = {
|
|
28
|
+
[K in keyof T]: null extends T[K] ? never : T[K] extends Column.Primary<any, any> ? never : T[K] extends Column.Optional<any> ? never : K;
|
|
29
|
+
}[keyof T];
|
|
30
|
+
type Optionals<T> = {
|
|
31
|
+
[K in keyof T]?: T[K] extends Column.Optional<infer V> ? V : T[K];
|
|
32
|
+
};
|
|
33
|
+
export type Insert<T> = Intersection<Optionals<Pick<T, OptionalKeys<T>>>, Pick<T, RequiredKeys<T>>>;
|
|
34
|
+
export type Normalize<T> = {
|
|
35
|
+
[K in keyof T]: T[K] extends Column.Optional<infer V> ? V : T[K] extends Column.Primary<infer K, infer V> ? string extends K ? V : V & {
|
|
36
|
+
[Column.isPrimary]: K;
|
|
37
|
+
} : T[K];
|
|
38
|
+
};
|
|
39
|
+
export type Infer<T> = T extends Table<infer U> ? Normalize<U> : never;
|
|
40
|
+
export {};
|
|
41
|
+
}
|
|
42
|
+
export interface TableOptions<T> {
|
|
43
|
+
name: string;
|
|
44
|
+
alias?: string;
|
|
45
|
+
columns: {
|
|
46
|
+
[K in keyof T]: Column<T[K]>;
|
|
47
|
+
};
|
|
48
|
+
indexes?: (this: Fields<T>) => Record<string, {
|
|
49
|
+
on: Array<Expr<any>>;
|
|
50
|
+
where?: Expr<boolean>;
|
|
51
|
+
}>;
|
|
52
|
+
}
|
|
53
|
+
export declare function table<T extends {}>(options: TableOptions<T>): Table<T> & Fields<T>;
|
|
54
|
+
export declare namespace table {
|
|
55
|
+
type infer<T> = Table.Infer<T>;
|
|
56
|
+
}
|