rado 0.2.24 → 0.2.26

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.
Files changed (44) hide show
  1. package/dist/define/Expr.d.ts +5 -4
  2. package/dist/define/Expr.js +3 -2
  3. package/dist/define/Fields.d.ts +2 -2
  4. package/dist/define/Ops.d.ts +13 -7
  5. package/dist/define/Ops.js +17 -7
  6. package/dist/define/Query.d.ts +4 -104
  7. package/dist/define/Query.js +8 -388
  8. package/dist/define/Selection.d.ts +2 -2
  9. package/dist/define/Table.d.ts +10 -16
  10. package/dist/define/Table.js +3 -41
  11. package/dist/define/Target.d.ts +6 -6
  12. package/dist/define/Target.js +8 -8
  13. package/dist/define/VirtualTable.d.ts +33 -0
  14. package/dist/define/VirtualTable.js +41 -0
  15. package/dist/define/query/Batch.d.ts +7 -0
  16. package/dist/define/query/Batch.js +18 -0
  17. package/dist/define/query/CreateTable.d.ts +6 -0
  18. package/dist/define/query/CreateTable.js +12 -0
  19. package/dist/define/query/Delete.d.ts +11 -0
  20. package/dist/define/query/Delete.js +19 -0
  21. package/dist/define/query/Insert.d.ts +19 -0
  22. package/dist/define/query/Insert.js +36 -0
  23. package/dist/define/query/Select.d.ts +40 -0
  24. package/dist/define/query/Select.js +173 -0
  25. package/dist/define/query/TableSelect.d.ts +21 -0
  26. package/dist/define/query/TableSelect.js +71 -0
  27. package/dist/define/query/Union.d.ts +17 -0
  28. package/dist/define/query/Union.js +117 -0
  29. package/dist/define/query/Update.d.ts +12 -0
  30. package/dist/define/query/Update.js +19 -0
  31. package/dist/driver/bun-sqlite.js +3 -2
  32. package/dist/driver/sql.js.d.ts +2 -2
  33. package/dist/driver/sql.js.js +2 -2
  34. package/dist/lib/Driver.d.ts +3 -16
  35. package/dist/lib/Driver.js +0 -28
  36. package/dist/lib/Formatter.d.ts +3 -1
  37. package/dist/lib/Formatter.js +28 -9
  38. package/dist/sqlite/SqliteSchema.js +3 -3
  39. package/dist/util/Alias.d.ts +1 -0
  40. package/dist/util/Alias.js +7 -0
  41. package/dist/util/Callable.d.ts +8 -0
  42. package/package.json +1 -1
  43. package/dist/define/CTE.d.ts +0 -2
  44. package/dist/define/CTE.js +0 -51
@@ -2,6 +2,7 @@ import { Expr } from '../define/Expr';
2
2
  import { Query, QueryData } from '../define/Query';
3
3
  import { SchemaInstructions } from '../define/Schema';
4
4
  import { Table } from '../define/Table';
5
+ import { SelectMultiple } from '../define/query/Select';
5
6
  import { Callable } from '../util/Callable';
6
7
  import { Formatter } from './Formatter';
7
8
  import { Statement } from './Statement';
@@ -9,8 +10,6 @@ declare abstract class DriverBase extends Callable {
9
10
  formatter: Formatter;
10
11
  constructor(formatter: Formatter);
11
12
  compile<T extends Array<Expr<any>>, R>(create: (...params: T) => Query<R>): [QueryData, Statement];
12
- all(...args: Array<any>): any;
13
- get(...args: Array<any>): any;
14
13
  executeTemplate(expectedReturn: QueryData.RawReturn, strings: TemplateStringsArray, ...params: Array<any>): any;
15
14
  abstract executeQuery(query: QueryData): any;
16
15
  }
@@ -32,13 +31,7 @@ declare abstract class SyncDriver extends DriverBase {
32
31
  prepare<T extends Array<Expr<any>>, R>(create: (...params: T) => Query<R>): (...params: ParamTypes<T>) => R;
33
32
  migrateSchema(...tables: Array<Table<any>>): unknown;
34
33
  executeQuery(query: QueryData, stmt?: SyncPreparedStatement, params?: Array<any>): unknown;
35
- all<T>(cursor: Query.SelectSingle<T>): Array<T>;
36
- all<T>(cursor: Query<T>): T;
37
- all<T>(strings: TemplateStringsArray, ...params: Array<any>): Array<T>;
38
- get<T>(cursor: Query.SelectMultiple<T>): T | null;
39
- get<T>(cursor: Query<T>): T;
40
- get<T>(strings: TemplateStringsArray, ...params: Array<any>): T;
41
- iterate<T>(cursor: Query.SelectMultiple<T>): Iterable<T>;
34
+ iterate<T>(cursor: SelectMultiple<T>): Iterable<T>;
42
35
  transaction<T>(run: (query: SyncDriver) => T): T;
43
36
  toAsync(): SyncWrapper;
44
37
  }
@@ -65,13 +58,7 @@ declare abstract class AsyncDriver extends DriverBase {
65
58
  prepare<T extends Array<Expr<any>>, R>(create: (...params: T) => Query<R>): (...params: ParamTypes<T>) => Promise<R>;
66
59
  migrateSchema(...tables: Array<Table<any>>): Promise<unknown>;
67
60
  executeQuery(query: QueryData, stmt?: AsyncPreparedStatement, params?: Array<any>): Promise<unknown>;
68
- all<T>(cursor: Query.SelectSingle<T>): Promise<Array<T>>;
69
- all<T>(cursor: Query<T>): Promise<T>;
70
- all<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<Array<T>>;
71
- get<T>(cursor: Query.SelectMultiple<T>): Promise<T | null>;
72
- get<T>(cursor: Query<T>): Promise<T>;
73
- get<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<T>;
74
- iterate<T>(cursor: Query.SelectMultiple<T>): AsyncIterable<T>;
61
+ iterate<T>(cursor: SelectMultiple<T>): AsyncIterable<T>;
75
62
  transaction<T>(run: (query: AsyncDriver) => T): Promise<T>;
76
63
  }
77
64
  declare class SyncWrapper extends AsyncDriver {
@@ -34,22 +34,6 @@ var DriverBase = class extends Callable {
34
34
  const query = cursor[Query.Data];
35
35
  return [query, this.formatter.compile(query)];
36
36
  }
37
- all(...args) {
38
- const [input, ...rest] = args;
39
- if (Query.isSingle(input))
40
- return this.executeQuery(input.all()[Query.Data]);
41
- if (Query.isQuery(input))
42
- return this.executeQuery(input[Query.Data]);
43
- return this.executeTemplate("rows", input, ...rest);
44
- }
45
- get(...args) {
46
- const [input, ...rest] = args;
47
- if (Query.isMultiple(input))
48
- return this.executeQuery(input.maybeFirst()[Query.Data]);
49
- if (Query.isQuery(input))
50
- return this.executeQuery(input[Query.Data]);
51
- return this.executeTemplate("row", input, ...rest);
52
- }
53
37
  executeTemplate(expectedReturn, strings, ...params) {
54
38
  return this.executeQuery(
55
39
  new QueryData.Raw({ strings, params, expectedReturn })
@@ -133,12 +117,6 @@ var SyncDriver = class extends DriverBase {
133
117
  }
134
118
  }
135
119
  }
136
- all(...args) {
137
- return super.all(...args);
138
- }
139
- get(...args) {
140
- return super.get(...args);
141
- }
142
120
  *iterate(cursor) {
143
121
  const stmt = this.prepareStatement(
144
122
  this.formatter.compile(cursor[Query.Data]),
@@ -255,12 +233,6 @@ var AsyncDriver = class extends DriverBase {
255
233
  }
256
234
  }
257
235
  }
258
- all(...args) {
259
- return super.all(...args);
260
- }
261
- get(...args) {
262
- return super.get(...args);
263
- }
264
236
  async *iterate(cursor) {
265
237
  const stmt = this.prepareStatement(
266
238
  this.formatter.compile(cursor[Query.Data]),
@@ -23,6 +23,7 @@ export interface FormatContext {
23
23
  formatAsIn?: boolean;
24
24
  topLevel?: boolean;
25
25
  selectAsColumns?: boolean;
26
+ formattingCte?: string;
26
27
  }
27
28
  type FormatSubject = (stmt: Statement, mkSubject: () => void) => void;
28
29
  export interface CompileOptions extends Partial<FormatContext>, StatementOptions {
@@ -45,6 +46,7 @@ export declare abstract class Formatter implements Sanitizer {
45
46
  formatAsIn?: boolean | undefined;
46
47
  topLevel?: boolean | undefined;
47
48
  selectAsColumns?: boolean | undefined;
49
+ formattingCte?: string | undefined;
48
50
  skipNewlines?: boolean | undefined;
49
51
  };
50
52
  format(ctx: FormatContext, query: QueryData): Statement;
@@ -62,7 +64,7 @@ export declare abstract class Formatter implements Sanitizer {
62
64
  formatBatch(ctx: FormatContext, { queries }: QueryData.Batch): Statement;
63
65
  formatRaw(ctx: FormatContext, { strings, params }: QueryData.Raw): Statement;
64
66
  formatColumn(ctx: FormatContext, column: ColumnData): Statement;
65
- formatContraintReference(ctx: FormatContext, reference: ExprData): Statement;
67
+ formatConstraintReference(ctx: FormatContext, reference: ExprData): Statement;
66
68
  formatType(ctx: FormatContext, type: ColumnType): Statement;
67
69
  formatInsertRow(ctx: FormatContext, columns: Record<string, ColumnData>, row: Record<string, any>): Statement;
68
70
  formatColumnValue(ctx: FormatContext, column: ColumnData, columnValue: any): Statement;
@@ -89,12 +89,19 @@ var Formatter = class {
89
89
  const { stmt } = ctx;
90
90
  switch (query.from?.type) {
91
91
  case TargetType.CTE:
92
- stmt.add("WITH RECURSIVE").addIdentifier(query.from.name).add("AS").space().openParenthesis();
93
- this.formatUnion(
94
- { ...ctx, topLevel: false, selectAsColumns: true },
95
- query.from.union
96
- );
97
- stmt.closeParenthesis().space();
92
+ if (ctx.formattingCte !== query.from.name) {
93
+ stmt.add("WITH RECURSIVE").addIdentifier(query.from.name).add("AS").space().openParenthesis();
94
+ this.formatUnion(
95
+ {
96
+ ...ctx,
97
+ topLevel: false,
98
+ selectAsColumns: true,
99
+ formattingCte: query.from.name
100
+ },
101
+ query.from.union
102
+ );
103
+ stmt.closeParenthesis().space();
104
+ }
98
105
  default:
99
106
  stmt.raw("SELECT").space();
100
107
  this.formatSelection(
@@ -118,7 +125,7 @@ var Formatter = class {
118
125
  const { stmt } = ctx;
119
126
  this.format(ctx, a);
120
127
  stmt.add(unions[operator]).space();
121
- this.format(ctx, b);
128
+ this.format(ctx, typeof b === "function" ? b() : b);
122
129
  return stmt;
123
130
  }
124
131
  formatInsert(ctx, query) {
@@ -306,10 +313,10 @@ var Formatter = class {
306
313
  }
307
314
  }
308
315
  if (column.references)
309
- this.formatContraintReference(ctx, column.references());
316
+ this.formatConstraintReference(ctx, column.references());
310
317
  return stmt;
311
318
  }
312
- formatContraintReference(ctx, reference) {
319
+ formatConstraintReference(ctx, reference) {
313
320
  const { stmt } = ctx;
314
321
  if (reference.type !== ExprType.Field || reference.expr.type !== ExprType.Row)
315
322
  throw new Error("not supported");
@@ -602,6 +609,8 @@ var Formatter = class {
602
609
  switch (from.type) {
603
610
  case ExprType.Row:
604
611
  switch (from.target.type) {
612
+ case TargetType.CTE:
613
+ return stmt.identifier(from.target.name).raw(".").identifier(field);
605
614
  case TargetType.Table:
606
615
  const column = from.target.table.columns[field];
607
616
  const asBoolean = column?.type === ColumnType.Boolean && ctx.formatAsJson;
@@ -685,6 +694,16 @@ var Formatter = class {
685
694
  throw new Error(`Cannot select empty target`);
686
695
  if (ctx.tableAsExpr)
687
696
  return stmt.identifier(table.alias || table.name);
697
+ if (ctx.selectAsColumns) {
698
+ const inner = { ...ctx, selectAsColumns: false };
699
+ for (const [key, column] of stmt.separate(
700
+ Object.entries(table.columns)
701
+ )) {
702
+ this.formatFieldOf(inner, expr, column.name);
703
+ stmt.add("AS").addIdentifier(key);
704
+ }
705
+ return stmt;
706
+ }
688
707
  stmt.identifier("json_object");
689
708
  for (const [key, column] of stmt.call(Object.entries(table.columns))) {
690
709
  this.formatString(ctx, key).raw(", ");
@@ -1,16 +1,16 @@
1
1
  // src/sqlite/SqliteSchema.ts
2
- import { Query } from "../define/Query.js";
2
+ import { sql } from "../define/Sql.js";
3
3
  import { Statement } from "../lib/Statement.js";
4
4
  import { SqliteFormatter } from "./SqliteFormatter.js";
5
5
  var SqliteSchema;
6
6
  ((SqliteSchema2) => {
7
7
  const formatter = new SqliteFormatter();
8
8
  function tableData(tableName) {
9
- return Query.all`select * from pragma_table_info(${tableName}) order by cid`;
9
+ return sql.all`select * from pragma_table_info(${tableName}) order by cid`;
10
10
  }
11
11
  SqliteSchema2.tableData = tableData;
12
12
  function indexData(tableName) {
13
- return Query.all`select * from sqlite_master where type='index' and tbl_name=${tableName}`;
13
+ return sql.all`select * from sqlite_master where type='index' and tbl_name=${tableName}`;
14
14
  }
15
15
  SqliteSchema2.indexData = indexData;
16
16
  function createInstructions(columnData, indexData2) {
@@ -0,0 +1 @@
1
+ export declare function randomAlias(): string;
@@ -0,0 +1,7 @@
1
+ // src/util/Alias.ts
2
+ function randomAlias() {
3
+ return `__${Math.random().toString(36).slice(2, 9)}`;
4
+ }
5
+ export {
6
+ randomAlias
7
+ };
@@ -1,3 +1,11 @@
1
1
  export declare class Callable {
2
2
  constructor(fn: Function);
3
3
  }
4
+ export declare class ClearFunctionProto {
5
+ get name(): unknown;
6
+ get length(): unknown;
7
+ get call(): unknown;
8
+ get apply(): unknown;
9
+ get bind(): unknown;
10
+ get prototype(): unknown;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.2.24",
3
+ "version": "0.2.26",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -1,2 +0,0 @@
1
- import { Query, QueryData } from './Query';
2
- export declare function makeRecursiveUnion<T extends Query<any>>(initial: QueryData.Select, createUnion: (target: any) => T, operator: QueryData.UnionOperation): QueryData.Select;
@@ -1,51 +0,0 @@
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
- };