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
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// src/lib/Cursor.ts
|
|
2
|
+
import { Expr, ExprData } from "./Expr.js";
|
|
3
|
+
import { Functions } from "./Functions.js";
|
|
4
|
+
import { Query } from "./Query.js";
|
|
5
|
+
import { Schema } from "./Schema.js";
|
|
6
|
+
import { Selection } from "./Selection.js";
|
|
7
|
+
import { Target } from "./Target.js";
|
|
8
|
+
var Cursor = class {
|
|
9
|
+
[Selection.__cursorType]() {
|
|
10
|
+
throw "assert";
|
|
11
|
+
}
|
|
12
|
+
constructor(query) {
|
|
13
|
+
Object.defineProperty(this, "query", {
|
|
14
|
+
enumerable: false,
|
|
15
|
+
value: () => query
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
static all(strings, ...params) {
|
|
19
|
+
return new Cursor(Query.Raw({ expectedReturn: "rows", strings, params }));
|
|
20
|
+
}
|
|
21
|
+
query() {
|
|
22
|
+
throw new Error("Not implemented");
|
|
23
|
+
}
|
|
24
|
+
on(driver) {
|
|
25
|
+
return driver.executeQuery(this.query());
|
|
26
|
+
}
|
|
27
|
+
toJSON() {
|
|
28
|
+
return this.query();
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
function addWhere(query, where) {
|
|
32
|
+
const conditions = where.slice();
|
|
33
|
+
if (query.where)
|
|
34
|
+
conditions.push(query.where);
|
|
35
|
+
return {
|
|
36
|
+
...query,
|
|
37
|
+
where: Expr.and(...conditions).expr
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
((Cursor2) => {
|
|
41
|
+
class Delete extends Cursor2 {
|
|
42
|
+
query() {
|
|
43
|
+
return super.query();
|
|
44
|
+
}
|
|
45
|
+
where(...where) {
|
|
46
|
+
return new Delete(addWhere(this.query(), where));
|
|
47
|
+
}
|
|
48
|
+
take(limit) {
|
|
49
|
+
return new Delete({ ...this.query(), limit });
|
|
50
|
+
}
|
|
51
|
+
skip(offset) {
|
|
52
|
+
return new Delete({ ...this.query(), offset });
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
Cursor2.Delete = Delete;
|
|
56
|
+
class Update extends Cursor2 {
|
|
57
|
+
query() {
|
|
58
|
+
return super.query();
|
|
59
|
+
}
|
|
60
|
+
set(set) {
|
|
61
|
+
return new Update({ ...this.query(), set });
|
|
62
|
+
}
|
|
63
|
+
where(...where) {
|
|
64
|
+
return new Update(addWhere(this.query(), where));
|
|
65
|
+
}
|
|
66
|
+
take(limit) {
|
|
67
|
+
return new Update({ ...this.query(), limit });
|
|
68
|
+
}
|
|
69
|
+
skip(offset) {
|
|
70
|
+
return new Update({ ...this.query(), offset });
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
Cursor2.Update = Update;
|
|
74
|
+
class InsertValuesReturning extends Cursor2 {
|
|
75
|
+
}
|
|
76
|
+
Cursor2.InsertValuesReturning = InsertValuesReturning;
|
|
77
|
+
class InsertValues extends Cursor2 {
|
|
78
|
+
query() {
|
|
79
|
+
return super.query();
|
|
80
|
+
}
|
|
81
|
+
returning(selection) {
|
|
82
|
+
return new InsertValuesReturning(
|
|
83
|
+
Query.Insert({ ...this.query(), selection: ExprData.create(selection) })
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
Cursor2.InsertValues = InsertValues;
|
|
88
|
+
class Insert {
|
|
89
|
+
constructor(into) {
|
|
90
|
+
this.into = into;
|
|
91
|
+
}
|
|
92
|
+
values(...data) {
|
|
93
|
+
return new InsertValues(Query.Insert({ into: this.into, data }));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
Cursor2.Insert = Insert;
|
|
97
|
+
class CreateTable extends Cursor2 {
|
|
98
|
+
constructor(table) {
|
|
99
|
+
super(Schema.create(table));
|
|
100
|
+
this.table = table;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
Cursor2.CreateTable = CreateTable;
|
|
104
|
+
class Batch extends Cursor2 {
|
|
105
|
+
constructor(queries) {
|
|
106
|
+
super(Query.Batch({ queries }));
|
|
107
|
+
this.queries = queries;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
Cursor2.Batch = Batch;
|
|
111
|
+
class SelectMultiple extends Cursor2 {
|
|
112
|
+
query() {
|
|
113
|
+
return super.query();
|
|
114
|
+
}
|
|
115
|
+
leftJoin(that, ...on) {
|
|
116
|
+
const query = this.query();
|
|
117
|
+
return new SelectMultiple({
|
|
118
|
+
...query,
|
|
119
|
+
from: Target.Join(
|
|
120
|
+
query.from,
|
|
121
|
+
Target.Table(that.schema()),
|
|
122
|
+
"left",
|
|
123
|
+
Expr.and(...on).expr
|
|
124
|
+
)
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
innerJoin(that, ...on) {
|
|
128
|
+
const query = this.query();
|
|
129
|
+
return new SelectMultiple({
|
|
130
|
+
...query,
|
|
131
|
+
from: Target.Join(
|
|
132
|
+
query.from,
|
|
133
|
+
Target.Table(that.schema()),
|
|
134
|
+
"inner",
|
|
135
|
+
Expr.and(...on).expr
|
|
136
|
+
)
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
select(selection) {
|
|
140
|
+
return new SelectMultiple({
|
|
141
|
+
...this.query(),
|
|
142
|
+
selection: ExprData.create(selection)
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
count() {
|
|
146
|
+
return new SelectSingle({
|
|
147
|
+
...this.query(),
|
|
148
|
+
selection: Functions.count().expr,
|
|
149
|
+
singleResult: true
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
orderBy(...orderBy) {
|
|
153
|
+
return new SelectMultiple({
|
|
154
|
+
...this.query(),
|
|
155
|
+
orderBy: orderBy.map((e) => {
|
|
156
|
+
return e instanceof Expr ? e.asc() : e;
|
|
157
|
+
})
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
groupBy(...groupBy) {
|
|
161
|
+
return new SelectMultiple({
|
|
162
|
+
...this.query(),
|
|
163
|
+
groupBy: groupBy.map(ExprData.create)
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
first() {
|
|
167
|
+
return new SelectSingle({ ...this.query(), singleResult: true });
|
|
168
|
+
}
|
|
169
|
+
sure() {
|
|
170
|
+
return new SelectSingle({
|
|
171
|
+
...this.query(),
|
|
172
|
+
singleResult: true,
|
|
173
|
+
validate: true
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
where(...where) {
|
|
177
|
+
return new SelectMultiple(addWhere(this.query(), where));
|
|
178
|
+
}
|
|
179
|
+
take(limit) {
|
|
180
|
+
return new SelectMultiple({ ...this.query(), limit });
|
|
181
|
+
}
|
|
182
|
+
skip(offset) {
|
|
183
|
+
return new SelectMultiple({ ...this.query(), offset });
|
|
184
|
+
}
|
|
185
|
+
toExpr() {
|
|
186
|
+
return new Expr(ExprData.Query(this.query()));
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
Cursor2.SelectMultiple = SelectMultiple;
|
|
190
|
+
class SelectSingle extends Cursor2 {
|
|
191
|
+
query() {
|
|
192
|
+
return super.query();
|
|
193
|
+
}
|
|
194
|
+
leftJoin(that, ...on) {
|
|
195
|
+
const query = this.query();
|
|
196
|
+
return new SelectSingle({
|
|
197
|
+
...query,
|
|
198
|
+
from: Target.Join(
|
|
199
|
+
query.from,
|
|
200
|
+
Target.Table(that.schema()),
|
|
201
|
+
"left",
|
|
202
|
+
Expr.and(...on).expr
|
|
203
|
+
)
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
innerJoin(that, ...on) {
|
|
207
|
+
const query = this.query();
|
|
208
|
+
return new SelectSingle({
|
|
209
|
+
...query,
|
|
210
|
+
from: Target.Join(
|
|
211
|
+
query.from,
|
|
212
|
+
Target.Table(that.schema()),
|
|
213
|
+
"inner",
|
|
214
|
+
Expr.and(...on).expr
|
|
215
|
+
)
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
select(selection) {
|
|
219
|
+
return new SelectSingle({
|
|
220
|
+
...this.query(),
|
|
221
|
+
selection: ExprData.create(selection)
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
orderBy(...orderBy) {
|
|
225
|
+
return new SelectSingle({
|
|
226
|
+
...this.query(),
|
|
227
|
+
orderBy
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
groupBy(...groupBy) {
|
|
231
|
+
return new SelectSingle({
|
|
232
|
+
...this.query(),
|
|
233
|
+
groupBy: groupBy.map(ExprData.create)
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
where(...where) {
|
|
237
|
+
return new SelectSingle(addWhere(this.query(), where));
|
|
238
|
+
}
|
|
239
|
+
take(limit) {
|
|
240
|
+
return new SelectSingle({ ...this.query(), limit });
|
|
241
|
+
}
|
|
242
|
+
skip(offset) {
|
|
243
|
+
return new SelectSingle({ ...this.query(), offset });
|
|
244
|
+
}
|
|
245
|
+
all() {
|
|
246
|
+
return new SelectMultiple({ ...this.query(), singleResult: false });
|
|
247
|
+
}
|
|
248
|
+
toExpr() {
|
|
249
|
+
return new Expr(ExprData.Query(this.query()));
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
Cursor2.SelectSingle = SelectSingle;
|
|
253
|
+
})(Cursor || (Cursor = {}));
|
|
254
|
+
export {
|
|
255
|
+
Cursor
|
|
256
|
+
};
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
import { Cursor } from './Cursor';
|
|
2
|
+
import { Fields } from './Fields';
|
|
3
|
+
import { OrderBy } from './OrderBy';
|
|
4
|
+
import { ParamData } from './Param';
|
|
5
|
+
import { Query } from './Query';
|
|
6
|
+
import { Selection } from './Selection';
|
|
7
|
+
import { Target } from './Target';
|
|
8
|
+
export declare enum UnOp {
|
|
9
|
+
Not = "Not",
|
|
10
|
+
IsNull = "IsNull"
|
|
11
|
+
}
|
|
12
|
+
export declare enum BinOp {
|
|
13
|
+
Add = "Add",
|
|
14
|
+
Subt = "Subt",
|
|
15
|
+
Mult = "Mult",
|
|
16
|
+
Mod = "Mod",
|
|
17
|
+
Div = "Div",
|
|
18
|
+
Greater = "Greater",
|
|
19
|
+
GreaterOrEqual = "GreaterOrEqual",
|
|
20
|
+
Less = "Less",
|
|
21
|
+
LessOrEqual = "LessOrEqual",
|
|
22
|
+
Equals = "Equals",
|
|
23
|
+
NotEquals = "NotEquals",
|
|
24
|
+
And = "And",
|
|
25
|
+
Or = "Or",
|
|
26
|
+
Like = "Like",
|
|
27
|
+
Glob = "Glob",
|
|
28
|
+
Match = "Match",
|
|
29
|
+
In = "In",
|
|
30
|
+
NotIn = "NotIn",
|
|
31
|
+
Concat = "Concat"
|
|
32
|
+
}
|
|
33
|
+
export declare enum ExprType {
|
|
34
|
+
UnOp = "UnOp",
|
|
35
|
+
BinOp = "BinOp",
|
|
36
|
+
Field = "Field",
|
|
37
|
+
Param = "Param",
|
|
38
|
+
Call = "Call",
|
|
39
|
+
Query = "Query",
|
|
40
|
+
Record = "Record",
|
|
41
|
+
Row = "Row",
|
|
42
|
+
Map = "Map",
|
|
43
|
+
Filter = "Filter",
|
|
44
|
+
Merge = "Merge",
|
|
45
|
+
Case = "Case"
|
|
46
|
+
}
|
|
47
|
+
export type ExprData = {
|
|
48
|
+
type: ExprType.UnOp;
|
|
49
|
+
op: UnOp;
|
|
50
|
+
expr: ExprData;
|
|
51
|
+
} | {
|
|
52
|
+
type: ExprType.BinOp;
|
|
53
|
+
op: BinOp;
|
|
54
|
+
a: ExprData;
|
|
55
|
+
b: ExprData;
|
|
56
|
+
} | {
|
|
57
|
+
type: ExprType.Field;
|
|
58
|
+
expr: ExprData;
|
|
59
|
+
field: string;
|
|
60
|
+
} | {
|
|
61
|
+
type: ExprType.Param;
|
|
62
|
+
param: ParamData;
|
|
63
|
+
} | {
|
|
64
|
+
type: ExprType.Call;
|
|
65
|
+
method: string;
|
|
66
|
+
params: Array<ExprData>;
|
|
67
|
+
} | {
|
|
68
|
+
type: ExprType.Query;
|
|
69
|
+
query: Query.Select;
|
|
70
|
+
} | {
|
|
71
|
+
type: ExprType.Record;
|
|
72
|
+
fields: Record<string, ExprData>;
|
|
73
|
+
} | {
|
|
74
|
+
type: ExprType.Merge;
|
|
75
|
+
a: ExprData;
|
|
76
|
+
b: ExprData;
|
|
77
|
+
} | {
|
|
78
|
+
type: ExprType.Row;
|
|
79
|
+
target: Target;
|
|
80
|
+
} | {
|
|
81
|
+
type: ExprType.Map;
|
|
82
|
+
target: Target;
|
|
83
|
+
result: ExprData;
|
|
84
|
+
} | {
|
|
85
|
+
type: ExprType.Filter;
|
|
86
|
+
target: Target;
|
|
87
|
+
condition: ExprData;
|
|
88
|
+
} | {
|
|
89
|
+
type: ExprType.Case;
|
|
90
|
+
expr: ExprData;
|
|
91
|
+
cases: Record<string, ExprData>;
|
|
92
|
+
defaultCase?: ExprData;
|
|
93
|
+
};
|
|
94
|
+
export declare const ExprData: {
|
|
95
|
+
UnOp(op: UnOp, expr: ExprData): ExprData;
|
|
96
|
+
BinOp(op: BinOp, a: ExprData, b: ExprData): ExprData;
|
|
97
|
+
Field(expr: ExprData, field: string): ExprData;
|
|
98
|
+
Param(param: ParamData): ExprData;
|
|
99
|
+
Call(method: string, params: Array<ExprData>): ExprData;
|
|
100
|
+
Query(query: Query.Select): ExprData;
|
|
101
|
+
Record(fields: Record<string, ExprData>): ExprData;
|
|
102
|
+
Merge(a: ExprData, b: ExprData): ExprData;
|
|
103
|
+
Row(target: Target): ExprData;
|
|
104
|
+
Map(target: Target, result: ExprData): ExprData;
|
|
105
|
+
Filter(target: Target, condition: ExprData): ExprData;
|
|
106
|
+
Case(expr: ExprData, cases: Record<string, ExprData>, defaultCase?: ExprData): ExprData;
|
|
107
|
+
create(input: any): ExprData;
|
|
108
|
+
};
|
|
109
|
+
/** Expression or value of type T */
|
|
110
|
+
export type EV<T> = Expr<T> | T;
|
|
111
|
+
export declare class Expr<T> {
|
|
112
|
+
expr: ExprData;
|
|
113
|
+
static NULL: ExprData;
|
|
114
|
+
static value<T>(value: T): Expr<T>;
|
|
115
|
+
static create<T>(input: EV<T>): Expr<T>;
|
|
116
|
+
static and(...conditions: Array<EV<boolean>>): Expr<boolean>;
|
|
117
|
+
static or(...conditions: Array<EV<boolean>>): Expr<boolean>;
|
|
118
|
+
constructor(expr: ExprData);
|
|
119
|
+
asc(): OrderBy;
|
|
120
|
+
desc(): OrderBy;
|
|
121
|
+
not(): Expr<boolean>;
|
|
122
|
+
or(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
|
|
123
|
+
and(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
|
|
124
|
+
isNull(): Expr<boolean>;
|
|
125
|
+
isNotNull(): Expr<boolean>;
|
|
126
|
+
isNot(that: EV<T>): Expr<boolean>;
|
|
127
|
+
is(that: EV<T> | Cursor.SelectSingle<T>): Expr<boolean>;
|
|
128
|
+
in(that: EV<Array<T>> | Cursor.SelectMultiple<T>): Expr<boolean>;
|
|
129
|
+
notIn(that: EV<Array<T>> | Cursor.SelectMultiple<T>): Expr<boolean>;
|
|
130
|
+
add(this: Expr<number>, that: EV<number>): Expr<number>;
|
|
131
|
+
substract(this: Expr<number>, that: EV<number>): Expr<number>;
|
|
132
|
+
multiply(this: Expr<number>, that: EV<number>): Expr<number>;
|
|
133
|
+
remainder(this: Expr<number>, that: EV<number>): Expr<number>;
|
|
134
|
+
divide(this: Expr<number>, that: EV<number>): Expr<number>;
|
|
135
|
+
greater(that: EV<any>): Expr<boolean>;
|
|
136
|
+
greaterOrEqual(that: EV<any>): Expr<boolean>;
|
|
137
|
+
less(that: EV<any>): Expr<boolean>;
|
|
138
|
+
lessOrEqual(that: EV<any>): Expr<boolean>;
|
|
139
|
+
concat(this: Expr<string>, that: EV<string>): Expr<string>;
|
|
140
|
+
like(this: Expr<string>, that: EV<string>): Expr<boolean>;
|
|
141
|
+
glob(this: Expr<string>, that: EV<string>): Expr<boolean>;
|
|
142
|
+
match(this: Expr<string>, that: EV<string>): Expr<boolean>;
|
|
143
|
+
with<X extends Selection>(that: X): Selection.With<T, X>;
|
|
144
|
+
at<T>(this: Expr<Array<T>>, index: number): Expr<T | null>;
|
|
145
|
+
filter<T>(this: Expr<Array<T>>, fn: (cursor: Fields<T>) => Expr<boolean>): Expr<Array<T>>;
|
|
146
|
+
map<T, X extends Selection>(this: Expr<Array<T>>, fn: (cursor: Fields<T>) => X): Expr<Array<Selection.Infer<X>>>;
|
|
147
|
+
sure(): Expr<NonNullable<T>>;
|
|
148
|
+
get<T>(name: string): Expr<T>;
|
|
149
|
+
}
|
|
150
|
+
export declare namespace Expr {
|
|
151
|
+
type Record<T> = Expr<T> & {
|
|
152
|
+
[K in keyof T]: Expr<T[K]>;
|
|
153
|
+
};
|
|
154
|
+
}
|
package/dist/lib/Expr.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __publicField = (obj, key, value) => {
|
|
4
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
5
|
+
return value;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
// src/lib/Expr.ts
|
|
9
|
+
import { OrderDirection } from "./OrderBy.js";
|
|
10
|
+
import { ParamData, ParamType } from "./Param.js";
|
|
11
|
+
import { Target } from "./Target.js";
|
|
12
|
+
var UnOp = /* @__PURE__ */ ((UnOp2) => {
|
|
13
|
+
UnOp2["Not"] = "Not";
|
|
14
|
+
UnOp2["IsNull"] = "IsNull";
|
|
15
|
+
return UnOp2;
|
|
16
|
+
})(UnOp || {});
|
|
17
|
+
var BinOp = /* @__PURE__ */ ((BinOp2) => {
|
|
18
|
+
BinOp2["Add"] = "Add";
|
|
19
|
+
BinOp2["Subt"] = "Subt";
|
|
20
|
+
BinOp2["Mult"] = "Mult";
|
|
21
|
+
BinOp2["Mod"] = "Mod";
|
|
22
|
+
BinOp2["Div"] = "Div";
|
|
23
|
+
BinOp2["Greater"] = "Greater";
|
|
24
|
+
BinOp2["GreaterOrEqual"] = "GreaterOrEqual";
|
|
25
|
+
BinOp2["Less"] = "Less";
|
|
26
|
+
BinOp2["LessOrEqual"] = "LessOrEqual";
|
|
27
|
+
BinOp2["Equals"] = "Equals";
|
|
28
|
+
BinOp2["NotEquals"] = "NotEquals";
|
|
29
|
+
BinOp2["And"] = "And";
|
|
30
|
+
BinOp2["Or"] = "Or";
|
|
31
|
+
BinOp2["Like"] = "Like";
|
|
32
|
+
BinOp2["Glob"] = "Glob";
|
|
33
|
+
BinOp2["Match"] = "Match";
|
|
34
|
+
BinOp2["In"] = "In";
|
|
35
|
+
BinOp2["NotIn"] = "NotIn";
|
|
36
|
+
BinOp2["Concat"] = "Concat";
|
|
37
|
+
return BinOp2;
|
|
38
|
+
})(BinOp || {});
|
|
39
|
+
var ExprType = /* @__PURE__ */ ((ExprType2) => {
|
|
40
|
+
ExprType2["UnOp"] = "UnOp";
|
|
41
|
+
ExprType2["BinOp"] = "BinOp";
|
|
42
|
+
ExprType2["Field"] = "Field";
|
|
43
|
+
ExprType2["Param"] = "Param";
|
|
44
|
+
ExprType2["Call"] = "Call";
|
|
45
|
+
ExprType2["Query"] = "Query";
|
|
46
|
+
ExprType2["Record"] = "Record";
|
|
47
|
+
ExprType2["Row"] = "Row";
|
|
48
|
+
ExprType2["Map"] = "Map";
|
|
49
|
+
ExprType2["Filter"] = "Filter";
|
|
50
|
+
ExprType2["Merge"] = "Merge";
|
|
51
|
+
ExprType2["Case"] = "Case";
|
|
52
|
+
return ExprType2;
|
|
53
|
+
})(ExprType || {});
|
|
54
|
+
var ExprData = {
|
|
55
|
+
UnOp(op, expr) {
|
|
56
|
+
return { type: "UnOp" /* UnOp */, op, expr };
|
|
57
|
+
},
|
|
58
|
+
BinOp(op, a, b) {
|
|
59
|
+
return { type: "BinOp" /* BinOp */, op, a, b };
|
|
60
|
+
},
|
|
61
|
+
Field(expr, field) {
|
|
62
|
+
return { type: "Field" /* Field */, expr, field };
|
|
63
|
+
},
|
|
64
|
+
Param(param) {
|
|
65
|
+
return { type: "Param" /* Param */, param };
|
|
66
|
+
},
|
|
67
|
+
Call(method, params) {
|
|
68
|
+
return { type: "Call" /* Call */, method, params };
|
|
69
|
+
},
|
|
70
|
+
Query(query) {
|
|
71
|
+
return { type: "Query" /* Query */, query };
|
|
72
|
+
},
|
|
73
|
+
Record(fields) {
|
|
74
|
+
return { type: "Record" /* Record */, fields };
|
|
75
|
+
},
|
|
76
|
+
Merge(a, b) {
|
|
77
|
+
return { type: "Merge" /* Merge */, a, b };
|
|
78
|
+
},
|
|
79
|
+
Row(target) {
|
|
80
|
+
return { type: "Row" /* Row */, target };
|
|
81
|
+
},
|
|
82
|
+
Map(target, result) {
|
|
83
|
+
return { type: "Map" /* Map */, target, result };
|
|
84
|
+
},
|
|
85
|
+
Filter(target, condition) {
|
|
86
|
+
return { type: "Filter" /* Filter */, target, condition };
|
|
87
|
+
},
|
|
88
|
+
Case(expr, cases, defaultCase) {
|
|
89
|
+
return { type: "Case" /* Case */, expr, cases, defaultCase };
|
|
90
|
+
},
|
|
91
|
+
create(input) {
|
|
92
|
+
if (input === null || input === void 0)
|
|
93
|
+
return ExprData.Param(ParamData.Value(null));
|
|
94
|
+
if (input && typeof input === "object" && typeof input.toExpr === "function")
|
|
95
|
+
input = input.toExpr();
|
|
96
|
+
if (input instanceof Expr)
|
|
97
|
+
return input.expr;
|
|
98
|
+
if (input && typeof input === "object" && !Array.isArray(input))
|
|
99
|
+
return ExprData.Record(
|
|
100
|
+
Object.fromEntries(
|
|
101
|
+
Object.entries(input).map(([key, value]) => [
|
|
102
|
+
key,
|
|
103
|
+
ExprData.create(value)
|
|
104
|
+
])
|
|
105
|
+
)
|
|
106
|
+
);
|
|
107
|
+
return ExprData.Param(ParamData.Value(input));
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
var toExpr = ExprData.create;
|
|
111
|
+
function isConstant(e, value) {
|
|
112
|
+
switch (e.type) {
|
|
113
|
+
case "Param" /* Param */:
|
|
114
|
+
switch (e.param.type) {
|
|
115
|
+
case ParamType.Value:
|
|
116
|
+
return e.param.value == value;
|
|
117
|
+
default:
|
|
118
|
+
return false;
|
|
119
|
+
}
|
|
120
|
+
default:
|
|
121
|
+
return false;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
var _Expr = class {
|
|
125
|
+
constructor(expr) {
|
|
126
|
+
this.expr = expr;
|
|
127
|
+
return new Proxy(this, {
|
|
128
|
+
get(target, key) {
|
|
129
|
+
return key in target ? target[key] : target.get(key);
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
static value(value) {
|
|
134
|
+
return new _Expr(ExprData.Param(ParamData.Value(value)));
|
|
135
|
+
}
|
|
136
|
+
static create(input) {
|
|
137
|
+
if (input instanceof _Expr)
|
|
138
|
+
return input;
|
|
139
|
+
return new _Expr(ExprData.create(input));
|
|
140
|
+
}
|
|
141
|
+
static and(...conditions) {
|
|
142
|
+
return conditions.map(_Expr.create).reduce((condition, expr) => condition.and(expr), _Expr.value(true));
|
|
143
|
+
}
|
|
144
|
+
static or(...conditions) {
|
|
145
|
+
return conditions.map(_Expr.create).reduce((condition, expr) => condition.or(expr), _Expr.value(false));
|
|
146
|
+
}
|
|
147
|
+
asc() {
|
|
148
|
+
return { expr: this.expr, order: OrderDirection.Asc };
|
|
149
|
+
}
|
|
150
|
+
desc() {
|
|
151
|
+
return { expr: this.expr, order: OrderDirection.Desc };
|
|
152
|
+
}
|
|
153
|
+
not() {
|
|
154
|
+
return unop(this, "Not" /* Not */);
|
|
155
|
+
}
|
|
156
|
+
or(that) {
|
|
157
|
+
const a = this.expr;
|
|
158
|
+
const b = toExpr(that);
|
|
159
|
+
if (isConstant(b, true) || isConstant(a, false))
|
|
160
|
+
return new _Expr(b);
|
|
161
|
+
if (isConstant(a, true) || isConstant(b, false))
|
|
162
|
+
return this;
|
|
163
|
+
return new _Expr(ExprData.BinOp("Or" /* Or */, a, b));
|
|
164
|
+
}
|
|
165
|
+
and(that) {
|
|
166
|
+
const a = this.expr;
|
|
167
|
+
const b = toExpr(that);
|
|
168
|
+
if (isConstant(b, true) || isConstant(a, false))
|
|
169
|
+
return this;
|
|
170
|
+
if (isConstant(a, true) || isConstant(b, false))
|
|
171
|
+
return new _Expr(b);
|
|
172
|
+
return new _Expr(ExprData.BinOp("And" /* And */, a, b));
|
|
173
|
+
}
|
|
174
|
+
isNull() {
|
|
175
|
+
return unop(this, "IsNull" /* IsNull */);
|
|
176
|
+
}
|
|
177
|
+
isNotNull() {
|
|
178
|
+
return this.isNull().not();
|
|
179
|
+
}
|
|
180
|
+
isNot(that) {
|
|
181
|
+
if (that == null || that instanceof _Expr && isConstant(that.expr, null))
|
|
182
|
+
return this.isNotNull();
|
|
183
|
+
return binop(this, "NotEquals" /* NotEquals */, that);
|
|
184
|
+
}
|
|
185
|
+
is(that) {
|
|
186
|
+
if (that === null || that instanceof _Expr && isConstant(that.expr, null))
|
|
187
|
+
return this.isNull();
|
|
188
|
+
return binop(this, "Equals" /* Equals */, that);
|
|
189
|
+
}
|
|
190
|
+
in(that) {
|
|
191
|
+
return binop(this, "In" /* In */, that);
|
|
192
|
+
}
|
|
193
|
+
notIn(that) {
|
|
194
|
+
return binop(this, "NotIn" /* NotIn */, that);
|
|
195
|
+
}
|
|
196
|
+
add(that) {
|
|
197
|
+
return binop(this, "Add" /* Add */, that);
|
|
198
|
+
}
|
|
199
|
+
substract(that) {
|
|
200
|
+
return binop(this, "Subt" /* Subt */, that);
|
|
201
|
+
}
|
|
202
|
+
multiply(that) {
|
|
203
|
+
return binop(this, "Mult" /* Mult */, that);
|
|
204
|
+
}
|
|
205
|
+
remainder(that) {
|
|
206
|
+
return binop(this, "Mod" /* Mod */, that);
|
|
207
|
+
}
|
|
208
|
+
divide(that) {
|
|
209
|
+
return binop(this, "Div" /* Div */, that);
|
|
210
|
+
}
|
|
211
|
+
greater(that) {
|
|
212
|
+
return binop(this, "Greater" /* Greater */, that);
|
|
213
|
+
}
|
|
214
|
+
greaterOrEqual(that) {
|
|
215
|
+
return binop(this, "GreaterOrEqual" /* GreaterOrEqual */, that);
|
|
216
|
+
}
|
|
217
|
+
less(that) {
|
|
218
|
+
return binop(this, "Less" /* Less */, that);
|
|
219
|
+
}
|
|
220
|
+
lessOrEqual(that) {
|
|
221
|
+
return binop(this, "LessOrEqual" /* LessOrEqual */, that);
|
|
222
|
+
}
|
|
223
|
+
concat(that) {
|
|
224
|
+
return binop(this, "Concat" /* Concat */, that);
|
|
225
|
+
}
|
|
226
|
+
like(that) {
|
|
227
|
+
return binop(this, "Like" /* Like */, that);
|
|
228
|
+
}
|
|
229
|
+
glob(that) {
|
|
230
|
+
return binop(this, "Glob" /* Glob */, that);
|
|
231
|
+
}
|
|
232
|
+
match(that) {
|
|
233
|
+
return binop(this, "Match" /* Match */, that);
|
|
234
|
+
}
|
|
235
|
+
with(that) {
|
|
236
|
+
return new _Expr(
|
|
237
|
+
ExprData.Merge(this.expr, ExprData.create(that))
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
at(index) {
|
|
241
|
+
return this.get(`[${Number(index)}]`);
|
|
242
|
+
}
|
|
243
|
+
filter(fn) {
|
|
244
|
+
const alias = `__${Math.random().toString(36).slice(2, 9)}`;
|
|
245
|
+
const target = Target.Expr(this.expr, alias);
|
|
246
|
+
return new _Expr(
|
|
247
|
+
ExprData.Filter(
|
|
248
|
+
target,
|
|
249
|
+
ExprData.create(fn(new _Expr(ExprData.Row(target))))
|
|
250
|
+
)
|
|
251
|
+
);
|
|
252
|
+
}
|
|
253
|
+
map(fn) {
|
|
254
|
+
const alias = `__${Math.random().toString(36).slice(2, 9)}`;
|
|
255
|
+
const target = Target.Expr(this.expr, alias);
|
|
256
|
+
return new _Expr(
|
|
257
|
+
ExprData.Map(
|
|
258
|
+
target,
|
|
259
|
+
ExprData.create(fn(new _Expr(ExprData.Row(target))))
|
|
260
|
+
)
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
sure() {
|
|
264
|
+
return this;
|
|
265
|
+
}
|
|
266
|
+
get(name) {
|
|
267
|
+
return new _Expr(ExprData.Field(this.expr, name));
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
var Expr = _Expr;
|
|
271
|
+
__publicField(Expr, "NULL", toExpr(null));
|
|
272
|
+
function unop(self, type) {
|
|
273
|
+
return new Expr(ExprData.UnOp(type, self.expr));
|
|
274
|
+
}
|
|
275
|
+
function binop(self, type, that) {
|
|
276
|
+
return new Expr(ExprData.BinOp(type, self.expr, toExpr(that)));
|
|
277
|
+
}
|
|
278
|
+
export {
|
|
279
|
+
BinOp,
|
|
280
|
+
Expr,
|
|
281
|
+
ExprData,
|
|
282
|
+
ExprType,
|
|
283
|
+
UnOp
|
|
284
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { Column, PrimaryKey } from './Column';
|
|
2
|
+
import { Expr } from './Expr';
|
|
3
|
+
type ObjectUnion<T> = {
|
|
4
|
+
[K in T extends infer P ? keyof P : never]: T extends infer P ? K extends keyof P ? P[K] : never : never;
|
|
5
|
+
};
|
|
6
|
+
type RecordField<T> = Expr<T> & FieldsOf<ObjectUnion<T>>;
|
|
7
|
+
type Field<T> = [T] extends [Array<any>] ? Expr<T> : [T] extends [Column.IsPrimary<infer V, infer K>] ? Expr<PrimaryKey<V, K>> : [T] extends [Column.IsOptional<infer V>] ? Field<V> : [T] extends [number | string | boolean] ? Expr<T> : [T] extends [Record<string, any> | null] ? RecordField<T> : Expr<T>;
|
|
8
|
+
type FieldsOf<Row> = Row extends Record<string, any> ? {
|
|
9
|
+
[K in keyof Row]-?: Field<Row[K]>;
|
|
10
|
+
} : never;
|
|
11
|
+
type IsStrictlyAny<T> = (T extends never ? true : false) extends false ? false : true;
|
|
12
|
+
export type Fields<T> = IsStrictlyAny<T> extends true ? any : T extends object ? FieldsOf<T> : Field<T>;
|
|
13
|
+
export {};
|
|
File without changes
|