rado 0.2.14 → 0.2.16

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.
@@ -1,109 +1,109 @@
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
+
1
8
  // src/define/Cursor.ts
2
9
  import { Expr, ExprData } from "./Expr.js";
3
10
  import { Functions } from "./Functions.js";
4
11
  import { Query } from "./Query.js";
5
12
  import { Schema } from "./Schema.js";
6
13
  import { Selection } from "./Selection.js";
7
- import { createTable, table } from "./Table.js";
14
+ import { Table, createTable } from "./Table.js";
8
15
  import { Target } from "./Target.js";
9
- var Cursor = class {
10
- [Selection.__cursorType]() {
11
- throw "assert";
12
- }
16
+ var QUERY = Symbol("Cursor.Query");
17
+ var _Cursor = class {
18
+ [(Selection.CursorType, QUERY)];
13
19
  constructor(query) {
14
- Object.defineProperty(this, "query", {
15
- enumerable: false,
16
- value: () => query
17
- });
20
+ this[QUERY] = query;
18
21
  }
19
22
  static all(strings, ...params) {
20
- return new Cursor(Query.Raw({ expectedReturn: "rows", strings, params }));
21
- }
22
- query() {
23
- throw new Error("Not implemented");
23
+ return new _Cursor(Query.Raw({ expectedReturn: "rows", strings, params }));
24
24
  }
25
25
  next(cursor) {
26
- return new Cursor(
26
+ return new _Cursor(
27
27
  Query.Batch({
28
- queries: [this.query(), cursor.query()]
28
+ queries: [this[QUERY], cursor[QUERY]]
29
29
  })
30
30
  );
31
31
  }
32
32
  on(driver) {
33
- return driver.executeQuery(this.query());
33
+ return driver.executeQuery(this[QUERY]);
34
34
  }
35
35
  toSql(driver, options = { forceInline: true }) {
36
- return driver.formatter.compile(this.query(), options).sql;
36
+ return driver.formatter.compile(this[QUERY], options).sql;
37
37
  }
38
38
  toJSON() {
39
- return this.query();
39
+ return this[QUERY];
40
40
  }
41
41
  };
42
+ var Cursor = _Cursor;
43
+ __publicField(Cursor, "Query", QUERY);
42
44
  function addWhere(query, where) {
43
45
  const conditions = where.slice();
44
46
  if (query.where)
45
47
  conditions.push(new Expr(query.where));
46
48
  return {
47
49
  ...query,
48
- where: Expr.and(...conditions).expr
50
+ where: Expr.and(...conditions)[Expr.Data]
49
51
  };
50
52
  }
51
53
  ((Cursor2) => {
52
54
  class Delete extends Cursor2 {
53
55
  query() {
54
- return super.query();
56
+ return super[QUERY];
55
57
  }
56
58
  where(...where) {
57
- return new Delete(addWhere(this.query(), where));
59
+ return new Delete(addWhere(this[QUERY], where));
58
60
  }
59
61
  take(limit) {
60
- return new Delete({ ...this.query(), limit });
62
+ return new Delete({ ...this[QUERY], limit });
61
63
  }
62
64
  skip(offset) {
63
- return new Delete({ ...this.query(), offset });
65
+ return new Delete({ ...this[QUERY], offset });
64
66
  }
65
67
  }
66
68
  Cursor2.Delete = Delete;
67
- class Update extends Cursor2 {
68
- query() {
69
- return super.query();
70
- }
69
+ const _Update = class extends Cursor2 {
71
70
  set(set) {
72
- return new Update({ ...this.query(), set });
71
+ return new _Update({ ...this[QUERY], set });
73
72
  }
74
73
  where(...where) {
75
- return new Update(addWhere(this.query(), where));
74
+ return new _Update(addWhere(this[QUERY], where));
76
75
  }
77
76
  take(limit) {
78
- return new Update({ ...this.query(), limit });
77
+ return new _Update({ ...this[QUERY], limit });
79
78
  }
80
79
  skip(offset) {
81
- return new Update({ ...this.query(), offset });
80
+ return new _Update({ ...this[QUERY], offset });
82
81
  }
83
- }
82
+ };
83
+ let Update = _Update;
84
+ QUERY;
84
85
  Cursor2.Update = Update;
85
86
  class InsertValuesReturning extends Cursor2 {
86
87
  }
87
88
  Cursor2.InsertValuesReturning = InsertValuesReturning;
88
89
  class Inserted extends Cursor2 {
89
- query() {
90
- return super.query();
90
+ constructor(query) {
91
+ super(query);
91
92
  }
92
93
  returning(selection) {
93
94
  return new InsertValuesReturning(
94
- Query.Insert({ ...this.query(), selection: ExprData.create(selection) })
95
+ Query.Insert({ ...this[QUERY], selection: ExprData.create(selection) })
95
96
  );
96
97
  }
97
98
  }
99
+ QUERY;
98
100
  Cursor2.Inserted = Inserted;
99
101
  class Insert {
100
102
  constructor(into) {
101
103
  this.into = into;
102
104
  }
103
105
  selection(query) {
104
- return new Inserted(
105
- Query.Insert({ into: this.into, select: query.query() })
106
- );
106
+ return new Inserted(Query.Insert({ into: this.into, select: query[QUERY] }));
107
107
  }
108
108
  values(...data) {
109
109
  return new Inserted(Query.Insert({ into: this.into, data }));
@@ -111,9 +111,9 @@ function addWhere(query, where) {
111
111
  }
112
112
  Cursor2.Insert = Insert;
113
113
  class CreateTable extends Cursor2 {
114
- constructor(table2) {
115
- super(Schema.create(table2));
116
- this.table = table2;
114
+ constructor(table) {
115
+ super(Schema.create(table));
116
+ this.table = table;
117
117
  }
118
118
  }
119
119
  Cursor2.CreateTable = CreateTable;
@@ -122,21 +122,19 @@ function addWhere(query, where) {
122
122
  super(Query.Batch({ queries }));
123
123
  this.queries = queries;
124
124
  }
125
- query() {
126
- return super.query();
127
- }
128
125
  next(cursor) {
129
126
  return new Cursor2(
130
127
  Query.Batch({
131
- queries: [...this.query().queries, cursor.query()]
128
+ queries: [...this[QUERY].queries, cursor[QUERY]]
132
129
  })
133
130
  );
134
131
  }
135
132
  }
133
+ QUERY;
136
134
  Cursor2.Batch = Batch;
137
135
  function joinTarget(joinType, query, to, on) {
138
- const toQuery = to instanceof Cursor2 ? to.query() : void 0;
139
- const target = toQuery ? toQuery.from : Target.Table(to[table.data]);
136
+ const toQuery = to instanceof Cursor2 ? to[QUERY] : void 0;
137
+ const target = toQuery ? toQuery.from : Target.Table(to[Table.Data]);
140
138
  if (!query.from || !target)
141
139
  throw new Error("No from clause");
142
140
  const conditions = [...on];
@@ -149,22 +147,22 @@ function addWhere(query, where) {
149
147
  query.from,
150
148
  target,
151
149
  joinType,
152
- Expr.and(...conditions).expr
150
+ Expr.and(...conditions)[Expr.Data]
153
151
  );
154
152
  }
155
153
  class SelectMultiple extends Cursor2 {
156
- query() {
157
- return super.query();
154
+ constructor(query) {
155
+ super(query);
158
156
  }
159
157
  leftJoin(that, ...on) {
160
- const query = this.query();
158
+ const query = this[QUERY];
161
159
  return new SelectMultiple({
162
160
  ...query,
163
161
  from: joinTarget("left", query, that, on)
164
162
  });
165
163
  }
166
164
  innerJoin(that, ...on) {
167
- const query = this.query();
165
+ const query = this[QUERY];
168
166
  return new SelectMultiple({
169
167
  ...query,
170
168
  from: joinTarget("inner", query, that, on)
@@ -172,20 +170,20 @@ function addWhere(query, where) {
172
170
  }
173
171
  select(selection) {
174
172
  return new SelectMultiple({
175
- ...this.query(),
173
+ ...this[QUERY],
176
174
  selection: ExprData.create(selection)
177
175
  });
178
176
  }
179
177
  count() {
180
178
  return new SelectSingle({
181
- ...this.query(),
182
- selection: Functions.count().expr,
179
+ ...this[QUERY],
180
+ selection: Functions.count()[Expr.Data],
183
181
  singleResult: true
184
182
  });
185
183
  }
186
184
  orderBy(...orderBy) {
187
185
  return new SelectMultiple({
188
- ...this.query(),
186
+ ...this[QUERY],
189
187
  orderBy: orderBy.map((e) => {
190
188
  return Expr.isExpr(e) ? e.asc() : e;
191
189
  })
@@ -193,45 +191,45 @@ function addWhere(query, where) {
193
191
  }
194
192
  groupBy(...groupBy) {
195
193
  return new SelectMultiple({
196
- ...this.query(),
194
+ ...this[QUERY],
197
195
  groupBy: groupBy.map(ExprData.create)
198
196
  });
199
197
  }
200
198
  first() {
201
- return new SelectSingle({ ...this.query(), singleResult: true });
199
+ return new SelectSingle({ ...this[QUERY], singleResult: true });
202
200
  }
203
201
  sure() {
204
202
  return new SelectSingle({
205
- ...this.query(),
203
+ ...this[QUERY],
206
204
  singleResult: true,
207
205
  validate: true
208
206
  });
209
207
  }
210
208
  where(...where) {
211
- return new SelectMultiple(addWhere(this.query(), where));
209
+ return new SelectMultiple(addWhere(this[QUERY], where));
212
210
  }
213
211
  take(limit) {
214
- return new SelectMultiple({ ...this.query(), limit });
212
+ return new SelectMultiple({ ...this[QUERY], limit });
215
213
  }
216
214
  skip(offset) {
217
- return new SelectMultiple({ ...this.query(), offset });
215
+ return new SelectMultiple({ ...this[QUERY], offset });
218
216
  }
219
- [Expr.toExpr]() {
220
- return new Expr(ExprData.Query(this.query()));
217
+ [(QUERY, Expr.ToExpr)]() {
218
+ return new Expr(ExprData.Query(this[QUERY]));
221
219
  }
222
220
  }
223
221
  Cursor2.SelectMultiple = SelectMultiple;
224
222
  class TableSelect extends SelectMultiple {
225
- constructor(table2, conditions = []) {
226
- const target = Target.Table(table2);
223
+ constructor(table, conditions = []) {
224
+ const target = Target.Table(table);
227
225
  super(
228
226
  Query.Select({
229
227
  from: target,
230
228
  selection: ExprData.Row(target),
231
- where: Expr.and(...conditions).expr
229
+ where: Expr.and(...conditions)[Expr.Data]
232
230
  })
233
231
  );
234
- this.table = table2;
232
+ this.table = table;
235
233
  }
236
234
  as(alias) {
237
235
  return createTable({ ...this.table, alias });
@@ -241,7 +239,7 @@ function addWhere(query, where) {
241
239
  }
242
240
  insertSelect(query) {
243
241
  return new Cursor2.Inserted(
244
- Query.Insert({ into: this.table, select: query.query() })
242
+ Query.Insert({ into: this.table, select: query[QUERY] })
245
243
  );
246
244
  }
247
245
  insertOne(record) {
@@ -261,7 +259,7 @@ function addWhere(query, where) {
261
259
  return new Cursor2.Update(
262
260
  Query.Update({
263
261
  table: this.table,
264
- where: this.query().where
262
+ where: this[QUERY].where
265
263
  })
266
264
  ).set(data);
267
265
  }
@@ -269,7 +267,7 @@ function addWhere(query, where) {
269
267
  return new Cursor2.Delete(
270
268
  Query.Delete({
271
269
  table: this.table,
272
- where: this.query().where
270
+ where: this[QUERY].where
273
271
  })
274
272
  );
275
273
  }
@@ -279,20 +277,21 @@ function addWhere(query, where) {
279
277
  );
280
278
  }
281
279
  }
280
+ QUERY;
282
281
  Cursor2.TableSelect = TableSelect;
283
282
  class SelectSingle extends Cursor2 {
284
- query() {
285
- return super.query();
283
+ constructor(query) {
284
+ super(query);
286
285
  }
287
286
  leftJoin(that, ...on) {
288
- const query = this.query();
287
+ const query = this[QUERY];
289
288
  return new SelectSingle({
290
289
  ...query,
291
290
  from: joinTarget("left", query, that, on)
292
291
  });
293
292
  }
294
293
  innerJoin(that, ...on) {
295
- const query = this.query();
294
+ const query = this[QUERY];
296
295
  return new SelectSingle({
297
296
  ...query,
298
297
  from: joinTarget("inner", query, that, on)
@@ -300,36 +299,36 @@ function addWhere(query, where) {
300
299
  }
301
300
  select(selection) {
302
301
  return new SelectSingle({
303
- ...this.query(),
302
+ ...this[QUERY],
304
303
  selection: ExprData.create(selection)
305
304
  });
306
305
  }
307
306
  orderBy(...orderBy) {
308
307
  return new SelectSingle({
309
- ...this.query(),
308
+ ...this[QUERY],
310
309
  orderBy
311
310
  });
312
311
  }
313
312
  groupBy(...groupBy) {
314
313
  return new SelectSingle({
315
- ...this.query(),
314
+ ...this[QUERY],
316
315
  groupBy: groupBy.map(ExprData.create)
317
316
  });
318
317
  }
319
318
  where(...where) {
320
- return new SelectSingle(addWhere(this.query(), where));
319
+ return new SelectSingle(addWhere(this[QUERY], where));
321
320
  }
322
321
  take(limit) {
323
- return new SelectSingle({ ...this.query(), limit });
322
+ return new SelectSingle({ ...this[QUERY], limit });
324
323
  }
325
324
  skip(offset) {
326
- return new SelectSingle({ ...this.query(), offset });
325
+ return new SelectSingle({ ...this[QUERY], offset });
327
326
  }
328
327
  all() {
329
- return new SelectMultiple({ ...this.query(), singleResult: false });
328
+ return new SelectMultiple({ ...this[QUERY], singleResult: false });
330
329
  }
331
- [Expr.toExpr]() {
332
- return new Expr(ExprData.Query(this.query()));
330
+ [(QUERY, Expr.ToExpr)]() {
331
+ return new Expr(ExprData.Query(this[QUERY]));
333
332
  }
334
333
  }
335
334
  Cursor2.SelectSingle = SelectSingle;
@@ -5,6 +5,9 @@ import { ParamData } from './Param';
5
5
  import { Query } from './Query';
6
6
  import { Selection } from './Selection';
7
7
  import { Target } from './Target';
8
+ declare const TO_EXPR: unique symbol;
9
+ declare const IS_EXPR: unique symbol;
10
+ declare const DATA: unique symbol;
8
11
  export declare enum UnOpType {
9
12
  Not = "Not",
10
13
  IsNull = "IsNull"
@@ -109,41 +112,38 @@ export declare namespace ExprData {
109
112
  defaultCase?: ExprData;
110
113
  };
111
114
  }
112
- export declare const ExprData: {
113
- UnOp(op: UnOpType, expr: ExprData): ExprData;
114
- BinOp(op: BinOpType, a: ExprData, b: ExprData): ExprData;
115
- Field(expr: ExprData, field: string): ExprData;
116
- Param(param: ParamData): ExprData;
117
- Call(method: string, params: Array<ExprData>): ExprData;
118
- Query(query: Query.Select): ExprData;
119
- Record(fields: Record<string, ExprData>): ExprData;
120
- Merge(a: ExprData, b: ExprData): ExprData;
121
- Row(target: Target): ExprData;
122
- Map(target: Target, result: ExprData): ExprData;
123
- Filter(target: Target, condition: ExprData): ExprData;
124
- Case(expr: ExprData, cases: Record<string, ExprData>, defaultCase?: ExprData): ExprData;
125
- create(input: any): ExprData;
126
- };
115
+ export declare namespace ExprData {
116
+ function UnOp(op: UnOpType, expr: ExprData): ExprData;
117
+ function BinOp(op: BinOpType, a: ExprData, b: ExprData): ExprData;
118
+ function Field(expr: ExprData, field: string): ExprData;
119
+ function Param(param: ParamData): ExprData;
120
+ function Call(method: string, params: Array<ExprData>): ExprData;
121
+ function Query(query: Query.Select): ExprData;
122
+ function Record(fields: {
123
+ [key: string]: ExprData;
124
+ }): ExprData;
125
+ function Merge(a: ExprData, b: ExprData): ExprData;
126
+ function Row(target: Target): ExprData;
127
+ function Map(target: Target, result: ExprData): ExprData;
128
+ function Filter(target: Target, condition: ExprData): ExprData;
129
+ function Case(expr: ExprData, cases: {
130
+ [key: string]: ExprData;
131
+ }, defaultCase?: ExprData): ExprData;
132
+ function create(input: any): ExprData;
133
+ }
127
134
  /** Expression or value of type T */
128
135
  export type EV<T> = Expr<T> | T;
129
- declare const isExpr: unique symbol;
130
136
  export declare class Expr<T> {
131
- expr: ExprData;
132
- static NULL: Expr<null>;
133
- static toExpr: symbol;
134
- static value<T>(value: T): Expr<T>;
135
- static create<T>(input: EV<T>): Expr<T>;
136
- static and(...conditions: Array<EV<boolean>>): Expr<boolean>;
137
- static or(...conditions: Array<EV<boolean>>): Expr<boolean>;
138
- static isExpr<T>(input: any): input is Expr<T>;
139
137
  constructor(expr: ExprData);
140
- [isExpr]: boolean;
138
+ [DATA]: ExprData;
139
+ [IS_EXPR]: boolean;
141
140
  asc(): OrderBy;
142
141
  desc(): OrderBy;
143
142
  not(): Expr<boolean>;
144
143
  or(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
145
144
  and(this: Expr<boolean>, that: EV<boolean>): Expr<boolean>;
146
145
  is(that: EV<T> | Cursor.SelectSingle<T>): Expr<boolean>;
146
+ isConstant(value: T): boolean;
147
147
  isNot(that: EV<T>): Expr<boolean>;
148
148
  isNull(): Expr<boolean>;
149
149
  isNotNull(): Expr<boolean>;
@@ -163,6 +163,13 @@ export declare class Expr<T> {
163
163
  glob(this: Expr<string>, that: EV<string>): Expr<boolean>;
164
164
  match(this: Expr<string>, that: EV<string>): Expr<boolean>;
165
165
  with<X extends Selection>(that: X): Selection.With<T, X>;
166
+ /**
167
+ * Dynamic expressions allow accessing runtime fields of JSON properties
168
+ * through a Proxy.
169
+ *
170
+ * Expr.value({a: {b: 1}}).dynamic().a.b.is(1) // true
171
+ **/
172
+ dynamic<X = T>(...path: Array<string>): Fields<X>;
166
173
  at<T>(this: Expr<Array<T>>, index: number): Expr<T | null>;
167
174
  includes<T>(this: Expr<Array<T>>, value: EV<T>): Expr<boolean>;
168
175
  filter<T>(this: Expr<Array<T>>, fn: (cursor: Fields<T>) => Expr<boolean>): Expr<Array<T>>;
@@ -171,8 +178,14 @@ export declare class Expr<T> {
171
178
  get<T>(name: string): Expr<T>;
172
179
  }
173
180
  export declare namespace Expr {
174
- type Record<T> = Expr<T> & {
175
- [K in keyof T]: Expr<T[K]>;
176
- };
181
+ const NULL: Expr<null>;
182
+ const Data: typeof DATA;
183
+ const IsExpr: typeof IS_EXPR;
184
+ const ToExpr: typeof TO_EXPR;
185
+ function value<T>(value: T): Expr<T>;
186
+ function create<T>(input: EV<T>): Expr<T>;
187
+ function and(...conditions: Array<EV<boolean>>): Expr<boolean>;
188
+ function or(...conditions: Array<EV<boolean>>): Expr<boolean>;
189
+ function isExpr<T>(input: any): input is Expr<T>;
177
190
  }
178
191
  export {};