rado 0.2.23 → 0.2.25
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/define/CTE.d.ts +2 -0
- package/dist/define/CTE.js +51 -0
- package/dist/define/Expr.d.ts +5 -4
- package/dist/define/Fields.d.ts +2 -2
- package/dist/define/Functions.js +4 -2
- package/dist/define/Ops.d.ts +11 -7
- package/dist/define/Ops.js +11 -6
- package/dist/define/Query.d.ts +24 -95
- package/dist/define/Query.js +28 -310
- package/dist/define/Selection.d.ts +2 -2
- package/dist/define/Table.d.ts +9 -7
- package/dist/define/Table.js +41 -3
- package/dist/define/Target.d.ts +8 -1
- package/dist/define/Target.js +9 -0
- package/dist/define/query/Batch.d.ts +7 -0
- package/dist/define/query/Batch.js +18 -0
- package/dist/define/query/CreateTable.d.ts +6 -0
- package/dist/define/query/CreateTable.js +12 -0
- package/dist/define/query/Delete.d.ts +11 -0
- package/dist/define/query/Delete.js +19 -0
- package/dist/define/query/Insert.d.ts +19 -0
- package/dist/define/query/Insert.js +36 -0
- package/dist/define/query/Select.d.ts +41 -0
- package/dist/define/query/Select.js +190 -0
- package/dist/define/query/TableSelect.d.ts +21 -0
- package/dist/define/query/TableSelect.js +71 -0
- package/dist/define/query/Union.d.ts +10 -0
- package/dist/define/query/Union.js +46 -0
- package/dist/define/query/Update.d.ts +12 -0
- package/dist/define/query/Update.js +19 -0
- package/dist/driver/bun-sqlite.js +3 -2
- package/dist/driver/sql.js.d.ts +2 -2
- package/dist/driver/sql.js.js +2 -2
- package/dist/lib/Driver.d.ts +3 -16
- package/dist/lib/Driver.js +2 -30
- package/dist/lib/Formatter.d.ts +1 -0
- package/dist/lib/Formatter.js +42 -14
- package/dist/sqlite/SqliteSchema.js +3 -3
- package/package.json +1 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
// src/define/CTE.ts
|
|
2
|
+
import { Expr, ExprData, ExprType } from "./Expr.js";
|
|
3
|
+
import { Query, QueryData } from "./Query.js";
|
|
4
|
+
import { virtualTable } from "./Table.js";
|
|
5
|
+
import { Target, TargetType } from "./Target.js";
|
|
6
|
+
var { keys, create, fromEntries } = Object;
|
|
7
|
+
function columnsOf(expr) {
|
|
8
|
+
switch (expr.type) {
|
|
9
|
+
case ExprType.Record:
|
|
10
|
+
return keys(expr.fields);
|
|
11
|
+
case ExprType.Row:
|
|
12
|
+
switch (expr.target.type) {
|
|
13
|
+
case TargetType.Table:
|
|
14
|
+
return keys(expr.target.table.columns);
|
|
15
|
+
default:
|
|
16
|
+
throw new Error("Could not retrieve CTE columns");
|
|
17
|
+
}
|
|
18
|
+
default:
|
|
19
|
+
throw new Error("Could not retrieve CTE columns");
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
function makeRecursiveUnion(initial, createUnion, operator) {
|
|
23
|
+
let name;
|
|
24
|
+
const alias = new Proxy(create(null), {
|
|
25
|
+
get(_, key) {
|
|
26
|
+
name = key;
|
|
27
|
+
return virtualTable(name);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
const right = createUnion(alias)[Query.Data];
|
|
31
|
+
if (!name)
|
|
32
|
+
throw new TypeError("No CTE name provided");
|
|
33
|
+
const cte = alias[name];
|
|
34
|
+
const cols = columnsOf(initial.selection);
|
|
35
|
+
const selection = new ExprData.Record(
|
|
36
|
+
fromEntries(cols.map((col) => [col, cte[col][Expr.Data]]))
|
|
37
|
+
);
|
|
38
|
+
const union = new QueryData.Union({
|
|
39
|
+
a: initial,
|
|
40
|
+
operator,
|
|
41
|
+
b: right
|
|
42
|
+
});
|
|
43
|
+
const from = new Target.CTE(name, union);
|
|
44
|
+
return new QueryData.Select({
|
|
45
|
+
selection,
|
|
46
|
+
from
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
export {
|
|
50
|
+
makeRecursiveUnion
|
|
51
|
+
};
|
package/dist/define/Expr.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Fields } from './Fields';
|
|
2
2
|
import { OrderBy } from './OrderBy';
|
|
3
3
|
import { ParamData } from './Param';
|
|
4
|
-
import {
|
|
4
|
+
import { QueryData } from './Query';
|
|
5
5
|
import { Selection } from './Selection';
|
|
6
6
|
import { Target } from './Target';
|
|
7
|
+
import { SelectMultiple, SelectSingle } from './query/Select';
|
|
7
8
|
declare const DATA: unique symbol;
|
|
8
9
|
declare const TO_EXPR: unique symbol;
|
|
9
10
|
declare const IS_EXPR: unique symbol;
|
|
@@ -127,13 +128,13 @@ export declare class Expr<T> {
|
|
|
127
128
|
not(): Expr<boolean>;
|
|
128
129
|
or(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
|
|
129
130
|
and(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
|
|
130
|
-
is(that: EV<T> |
|
|
131
|
+
is(that: EV<T> | SelectSingle<T>): Expr<boolean>;
|
|
131
132
|
isConstant(value: T): boolean;
|
|
132
133
|
isNot(that: EV<T>): Expr<boolean>;
|
|
133
134
|
isNull(): Expr<boolean>;
|
|
134
135
|
isNotNull(): Expr<boolean>;
|
|
135
|
-
isIn(that: EV<Array<T>> |
|
|
136
|
-
isNotIn(that: EV<Array<T>> |
|
|
136
|
+
isIn(that: EV<Array<T>> | SelectMultiple<T>): Expr<boolean>;
|
|
137
|
+
isNotIn(that: EV<Array<T>> | SelectMultiple<T>): Expr<boolean>;
|
|
137
138
|
isGreater(that: EV<any>): Expr<boolean>;
|
|
138
139
|
isGreaterOrEqual(that: EV<any>): Expr<boolean>;
|
|
139
140
|
isLess(that: EV<any>): Expr<boolean>;
|
package/dist/define/Fields.d.ts
CHANGED
|
@@ -4,9 +4,9 @@ type ObjectUnion<T> = {
|
|
|
4
4
|
};
|
|
5
5
|
type RecordField<T> = Expr<T> & FieldsOf<ObjectUnion<T>>;
|
|
6
6
|
type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [number | string | boolean] ? Expr<T> : [T] extends [Record<string, any> | null] ? RecordField<T> : Expr<T>;
|
|
7
|
-
type FieldsOf<Row> = Row extends Record<string, any> ? {
|
|
7
|
+
type FieldsOf<Row> = [Row] extends [Record<string, any>] ? {
|
|
8
8
|
[K in keyof Row]-?: Field<Row[K]>;
|
|
9
9
|
} : never;
|
|
10
10
|
type IsStrictlyAny<T> = (T extends never ? true : false) extends false ? false : true;
|
|
11
|
-
export type Fields<T> = IsStrictlyAny<T> extends true ? any : T extends object ? FieldsOf<T> : Field<T>;
|
|
11
|
+
export type Fields<T> = IsStrictlyAny<T> extends true ? any : [T] extends [object] ? FieldsOf<T> : Field<T>;
|
|
12
12
|
export {};
|
package/dist/define/Functions.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
// src/define/Functions.ts
|
|
2
2
|
import { Expr, ExprData } from "./Expr.js";
|
|
3
|
-
function get(
|
|
4
|
-
|
|
3
|
+
function get(target, method) {
|
|
4
|
+
if (method in target)
|
|
5
|
+
return target[method];
|
|
6
|
+
return target[method] = (...args) => {
|
|
5
7
|
return new Expr(new ExprData.Call(method, args.map(ExprData.create)));
|
|
6
8
|
};
|
|
7
9
|
}
|
package/dist/define/Ops.d.ts
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
|
-
import { Query } from './Query';
|
|
2
1
|
import { Selection } from './Selection';
|
|
3
2
|
import { Table } from './Table';
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export declare function
|
|
3
|
+
import { Batch } from './query/Batch';
|
|
4
|
+
import { Delete } from './query/Delete';
|
|
5
|
+
import { Insert } from './query/Insert';
|
|
6
|
+
import { SelectMultiple } from './query/Select';
|
|
7
|
+
import { Update } from './query/Update';
|
|
8
|
+
export declare function select<X extends Selection>(selection: X): SelectMultiple<Selection.Infer<X>>;
|
|
9
|
+
export declare function from<Row>(source: Table<Row> | SelectMultiple<Row>): SelectMultiple<Row>;
|
|
10
|
+
export declare function update<Row>(table: Table<Row>): Update<Row>;
|
|
11
|
+
export declare function insertInto<Row>(table: Table<Row>): Insert<Row>;
|
|
12
|
+
export declare function deleteFrom<Row>(table: Table<Row>): Delete;
|
|
13
|
+
export declare function create(...tables: Array<Table<any>>): Batch;
|
package/dist/define/Ops.js
CHANGED
|
@@ -4,8 +4,13 @@ import { Query, QueryData } from "./Query.js";
|
|
|
4
4
|
import { Schema } from "./Schema.js";
|
|
5
5
|
import { Table } from "./Table.js";
|
|
6
6
|
import { Target } from "./Target.js";
|
|
7
|
+
import { Batch } from "./query/Batch.js";
|
|
8
|
+
import { Delete } from "./query/Delete.js";
|
|
9
|
+
import { Insert } from "./query/Insert.js";
|
|
10
|
+
import { SelectMultiple } from "./query/Select.js";
|
|
11
|
+
import { Update } from "./query/Update.js";
|
|
7
12
|
function select(selection) {
|
|
8
|
-
return new
|
|
13
|
+
return new SelectMultiple(
|
|
9
14
|
new QueryData.Select({
|
|
10
15
|
selection: ExprData.create(selection)
|
|
11
16
|
})
|
|
@@ -13,7 +18,7 @@ function select(selection) {
|
|
|
13
18
|
}
|
|
14
19
|
function from(source) {
|
|
15
20
|
const target = Query.isQuery(source) ? new Target.Query(source[Query.Data]) : new Target.Table(source[Table.Data]);
|
|
16
|
-
return new
|
|
21
|
+
return new SelectMultiple(
|
|
17
22
|
new QueryData.Select({
|
|
18
23
|
from: target,
|
|
19
24
|
selection: new ExprData.Row(target)
|
|
@@ -21,16 +26,16 @@ function from(source) {
|
|
|
21
26
|
);
|
|
22
27
|
}
|
|
23
28
|
function update(table) {
|
|
24
|
-
return new
|
|
29
|
+
return new Update(new QueryData.Update({ table: table[Table.Data] }));
|
|
25
30
|
}
|
|
26
31
|
function insertInto(table) {
|
|
27
|
-
return new
|
|
32
|
+
return new Insert(table[Table.Data]);
|
|
28
33
|
}
|
|
29
34
|
function deleteFrom(table) {
|
|
30
|
-
return new
|
|
35
|
+
return new Delete(new QueryData.Delete({ table: table[Table.Data] }));
|
|
31
36
|
}
|
|
32
37
|
function create(...tables) {
|
|
33
|
-
return new
|
|
38
|
+
return new Batch(
|
|
34
39
|
tables.flatMap((table) => Schema.create(table[Table.Data]).queries)
|
|
35
40
|
);
|
|
36
41
|
}
|
package/dist/define/Query.d.ts
CHANGED
|
@@ -1,16 +1,17 @@
|
|
|
1
1
|
import { Driver } from '../lib/Driver';
|
|
2
2
|
import { CompileOptions } from '../lib/Formatter';
|
|
3
3
|
import { ColumnData } from './Column';
|
|
4
|
-
import { EV,
|
|
4
|
+
import { EV, ExprData } from './Expr';
|
|
5
5
|
import { IndexData } from './Index';
|
|
6
6
|
import { OrderBy } from './OrderBy';
|
|
7
7
|
import { Selection } from './Selection';
|
|
8
|
-
import {
|
|
8
|
+
import { TableData } from './Table';
|
|
9
9
|
import { Target } from './Target';
|
|
10
10
|
declare const DATA: unique symbol;
|
|
11
11
|
export declare enum QueryType {
|
|
12
12
|
Insert = "QueryData.Insert",
|
|
13
13
|
Select = "QueryData.Select",
|
|
14
|
+
Union = "QueryData.Union",
|
|
14
15
|
Update = "QueryData.Update",
|
|
15
16
|
Delete = "QueryData.Delete",
|
|
16
17
|
CreateTable = "QueryData.CreateTable",
|
|
@@ -22,7 +23,7 @@ export declare enum QueryType {
|
|
|
22
23
|
Transaction = "QueryData.Transaction",
|
|
23
24
|
Raw = "QueryData.Raw"
|
|
24
25
|
}
|
|
25
|
-
export type QueryData = QueryData.Insert | QueryData.Select | QueryData.Update | QueryData.Delete | QueryData.CreateTable | QueryData.AlterTable | QueryData.DropTable | QueryData.CreateIndex | QueryData.DropIndex | QueryData.Batch | QueryData.Transaction | QueryData.Raw;
|
|
26
|
+
export type QueryData = QueryData.Insert | QueryData.Select | QueryData.Union | QueryData.Update | QueryData.Delete | QueryData.CreateTable | QueryData.AlterTable | QueryData.DropTable | QueryData.CreateIndex | QueryData.DropIndex | QueryData.Batch | QueryData.Transaction | QueryData.Raw;
|
|
26
27
|
export declare namespace QueryData {
|
|
27
28
|
abstract class Data<T> {
|
|
28
29
|
abstract type: QueryType;
|
|
@@ -35,12 +36,29 @@ export declare namespace QueryData {
|
|
|
35
36
|
selection?: ExprData;
|
|
36
37
|
singleResult?: boolean;
|
|
37
38
|
validate?: boolean;
|
|
38
|
-
constructor(data: Omit<T, 'type'>);
|
|
39
|
+
constructor(data: Omit<T, 'type' | 'with'>);
|
|
40
|
+
with(data: Partial<T>): any;
|
|
41
|
+
}
|
|
42
|
+
export enum UnionOperation {
|
|
43
|
+
Union = "Union",
|
|
44
|
+
UnionAll = "UnionAll",
|
|
45
|
+
Intersect = "Intersect",
|
|
46
|
+
Except = "Except"
|
|
47
|
+
}
|
|
48
|
+
export class Union extends Data<Union> {
|
|
49
|
+
type: QueryType.Union;
|
|
50
|
+
a: Select | Union;
|
|
51
|
+
operator: UnionOperation;
|
|
52
|
+
b: Select | Union;
|
|
39
53
|
}
|
|
40
54
|
export class Select extends Data<Select> {
|
|
41
55
|
type: QueryType.Select;
|
|
42
56
|
selection: ExprData;
|
|
43
57
|
from?: Target;
|
|
58
|
+
union?: Select;
|
|
59
|
+
unionAll?: Select;
|
|
60
|
+
intersect?: Select;
|
|
61
|
+
except?: Select;
|
|
44
62
|
}
|
|
45
63
|
export class Insert extends Data<Insert> {
|
|
46
64
|
type: QueryType.Insert;
|
|
@@ -113,7 +131,7 @@ export declare namespace QueryData {
|
|
|
113
131
|
expectedReturn?: 'row' | 'rows';
|
|
114
132
|
strings: ReadonlyArray<string>;
|
|
115
133
|
params: Array<any>;
|
|
116
|
-
constructor(data: Omit<Raw, 'type'>);
|
|
134
|
+
constructor(data: Omit<Raw, 'type' | 'with'>);
|
|
117
135
|
}
|
|
118
136
|
export {};
|
|
119
137
|
}
|
|
@@ -121,104 +139,15 @@ export declare class Query<T> {
|
|
|
121
139
|
[Selection.CursorType]: () => T;
|
|
122
140
|
[DATA]: QueryData;
|
|
123
141
|
constructor(query: QueryData);
|
|
124
|
-
static all(strings: TemplateStringsArray, ...params: Array<any>): Query<any>;
|
|
125
142
|
next<T>(cursor: Query<T>): Query<T>;
|
|
126
143
|
on(driver: Driver.Sync): T;
|
|
127
144
|
on(driver: Driver.Async): Promise<T>;
|
|
128
145
|
toSql(driver: Driver, options?: CompileOptions): string;
|
|
129
146
|
toJSON(): QueryData;
|
|
130
|
-
|
|
131
|
-
export declare namespace Query {
|
|
132
|
-
class Delete extends Query<{
|
|
133
|
-
rowsAffected: number;
|
|
134
|
-
}> {
|
|
135
|
-
[DATA]: QueryData.Delete;
|
|
136
|
-
where(...where: Array<EV<boolean>>): Delete;
|
|
137
|
-
take(limit: number | undefined): Delete;
|
|
138
|
-
skip(offset: number | undefined): Delete;
|
|
139
|
-
}
|
|
140
|
-
class Update<Definition> extends Query<{
|
|
141
|
-
rowsAffected: number;
|
|
142
|
-
}> {
|
|
143
|
-
[DATA]: QueryData.Update;
|
|
144
|
-
set(set: Table.Update<Definition>): Update<Definition>;
|
|
145
|
-
where(...where: Array<EV<boolean>>): Update<Definition>;
|
|
146
|
-
take(limit: number | undefined): Update<Definition>;
|
|
147
|
-
skip(offset: number | undefined): Update<Definition>;
|
|
148
|
-
}
|
|
149
|
-
class InsertValuesReturning<T> extends Query<T> {
|
|
150
|
-
}
|
|
151
|
-
class Inserted extends Query<{
|
|
152
|
-
rowsAffected: number;
|
|
153
|
-
}> {
|
|
154
|
-
[DATA]: QueryData.Insert;
|
|
155
|
-
constructor(query: QueryData.Insert);
|
|
156
|
-
returning<X extends Selection>(selection: X): InsertValuesReturning<Selection.Infer<X>>;
|
|
157
|
-
}
|
|
158
|
-
class Insert<Definition> {
|
|
159
|
-
protected into: TableData;
|
|
160
|
-
constructor(into: TableData);
|
|
161
|
-
selection(query: Query.SelectMultiple<Table.Select<Definition>>): Inserted;
|
|
162
|
-
values(...data: Array<Table.Insert<Definition>>): Inserted;
|
|
163
|
-
}
|
|
164
|
-
class CreateTable extends Query<void> {
|
|
165
|
-
protected table: TableData;
|
|
166
|
-
constructor(table: TableData);
|
|
167
|
-
}
|
|
168
|
-
class Batch<T = void> extends Query<T> {
|
|
169
|
-
protected queries: Array<QueryData>;
|
|
170
|
-
[DATA]: QueryData.Batch;
|
|
171
|
-
constructor(queries: Array<QueryData>);
|
|
172
|
-
next<T>(cursor: Query<T>): Query<T>;
|
|
173
|
-
}
|
|
174
|
-
class SelectMultiple<Row> extends Query<Array<Row>> {
|
|
175
|
-
[DATA]: QueryData.Select;
|
|
176
|
-
constructor(query: QueryData.Select);
|
|
177
|
-
leftJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
178
|
-
innerJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
179
|
-
select<X extends Selection>(selection: X): SelectMultiple<Selection.Infer<X>>;
|
|
180
|
-
count(): SelectSingle<number>;
|
|
181
|
-
orderBy(...orderBy: Array<Expr<any> | OrderBy>): SelectMultiple<Row>;
|
|
182
|
-
groupBy(...groupBy: Array<Expr<any>>): SelectMultiple<Row>;
|
|
183
|
-
maybeFirst(): SelectSingle<Row | undefined>;
|
|
184
|
-
first(): SelectSingle<Row>;
|
|
185
|
-
where(...where: Array<EV<boolean>>): SelectMultiple<Row>;
|
|
186
|
-
take(limit: number | undefined): SelectMultiple<Row>;
|
|
187
|
-
skip(offset: number | undefined): SelectMultiple<Row>;
|
|
188
|
-
[Expr.ToExpr](): Expr<Row>;
|
|
189
|
-
}
|
|
190
|
-
class TableSelect<Definition> extends SelectMultiple<Table.Select<Definition>> {
|
|
191
|
-
protected table: TableData;
|
|
192
|
-
[DATA]: QueryData.Select;
|
|
193
|
-
constructor(table: TableData, conditions?: Array<EV<boolean>>);
|
|
194
|
-
as(alias: string): Table<Definition>;
|
|
195
|
-
create(): CreateTable;
|
|
196
|
-
insertSelect(query: SelectMultiple<Table.Insert<Definition>>): Inserted;
|
|
197
|
-
insertOne(record: Table.Insert<Definition>): Query<Table.Select<Definition>>;
|
|
198
|
-
insertAll(data: Array<Table.Insert<Definition>>): Inserted;
|
|
199
|
-
set(data: Table.Update<Definition>): Update<Definition>;
|
|
200
|
-
delete(): Delete;
|
|
201
|
-
get(name: string): Expr<any>;
|
|
202
|
-
}
|
|
203
|
-
class SelectSingle<Row> extends Query<Row> {
|
|
204
|
-
[DATA]: QueryData.Select;
|
|
205
|
-
constructor(query: QueryData.Select);
|
|
206
|
-
leftJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectSingle<Row>;
|
|
207
|
-
innerJoin<C>(that: Table<C> | TableSelect<C>, ...on: Array<EV<boolean>>): SelectSingle<Row>;
|
|
208
|
-
select<X extends Selection>(selection: X): SelectSingle<Selection.Infer<X>>;
|
|
209
|
-
orderBy(...orderBy: Array<OrderBy>): SelectSingle<Row>;
|
|
210
|
-
groupBy(...groupBy: Array<Expr<any>>): SelectSingle<Row>;
|
|
211
|
-
where(...where: Array<EV<boolean>>): SelectSingle<Row>;
|
|
212
|
-
take(limit: number | undefined): SelectSingle<Row>;
|
|
213
|
-
skip(offset: number | undefined): SelectSingle<Row>;
|
|
214
|
-
all(): SelectMultiple<Row>;
|
|
215
|
-
[Expr.ToExpr](): Expr<Row>;
|
|
216
|
-
}
|
|
147
|
+
protected addWhere(where: Array<EV<boolean>>): this[typeof DATA];
|
|
217
148
|
}
|
|
218
149
|
export declare namespace Query {
|
|
219
150
|
const Data: typeof DATA;
|
|
220
151
|
function isQuery<T>(input: any): input is Query<T>;
|
|
221
|
-
function isSingle<T>(input: any): input is Query.SelectSingle<T>;
|
|
222
|
-
function isMultiple<T>(input: any): input is Query.SelectMultiple<T>;
|
|
223
152
|
}
|
|
224
153
|
export {};
|