rado 0.1.52 → 0.1.53

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 (149) hide show
  1. package/dist/CPU.20230126.092143.12244.0.001.cpuprofile +1 -0
  2. package/dist/Column.d.ts +54 -0
  3. package/dist/Column.js +63 -0
  4. package/dist/Cursor.d.ts +77 -0
  5. package/dist/Cursor.js +221 -0
  6. package/dist/Driver.d.ts +89 -0
  7. package/dist/Driver.js +262 -0
  8. package/dist/Expr.d.ts +149 -0
  9. package/dist/Expr.js +284 -0
  10. package/dist/Fields.d.ts +11 -0
  11. package/dist/Fields.js +0 -0
  12. package/dist/Formatter.d.ts +55 -0
  13. package/dist/Formatter.js +496 -0
  14. package/dist/Functions.d.ts +7 -0
  15. package/dist/Functions.js +11 -0
  16. package/dist/Id.d.ts +3 -0
  17. package/dist/Id.js +0 -0
  18. package/dist/Ops.d.ts +8 -0
  19. package/dist/Ops.js +45 -0
  20. package/dist/OrderBy.d.ts +9 -0
  21. package/dist/OrderBy.js +9 -0
  22. package/dist/Param.d.ts +15 -0
  23. package/dist/Param.js +18 -0
  24. package/dist/Query.d.ts +107 -0
  25. package/dist/Query.js +74 -0
  26. package/dist/Sanitizer.d.ts +4 -0
  27. package/dist/Sanitizer.js +0 -0
  28. package/dist/Schema.d.ts +23 -0
  29. package/dist/Schema.js +60 -0
  30. package/dist/Selection.d.ts +20 -0
  31. package/dist/Selection.js +7 -0
  32. package/dist/Statement.d.ts +57 -0
  33. package/dist/Statement.js +184 -0
  34. package/dist/Table.d.ts +56 -0
  35. package/dist/Table.js +104 -0
  36. package/dist/Target.d.ts +30 -0
  37. package/dist/Target.js +37 -0
  38. package/dist/Update.d.ts +4 -0
  39. package/dist/Update.js +0 -0
  40. package/dist/define/Column.d.ts +58 -58
  41. package/dist/define/Cursor.d.ts +85 -85
  42. package/dist/define/Expr.d.ts +154 -154
  43. package/dist/define/Fields.d.ts +13 -13
  44. package/dist/define/Functions.d.ts +5 -5
  45. package/dist/define/Id.d.ts +3 -3
  46. package/dist/define/Index.d.ts +17 -17
  47. package/dist/define/Ops.d.ts +7 -7
  48. package/dist/define/OrderBy.d.ts +9 -9
  49. package/dist/define/Param.d.ts +15 -15
  50. package/dist/define/Query.d.ts +109 -109
  51. package/dist/define/Schema.d.ts +18 -18
  52. package/dist/define/Selection.d.ts +20 -20
  53. package/dist/define/Table.d.ts +62 -62
  54. package/dist/define/Target.d.ts +38 -38
  55. package/dist/define/Update.d.ts +4 -4
  56. package/dist/driver/better-sqlite3.d.ts +15 -15
  57. package/dist/driver/bun-sqlite.d.ts +18 -18
  58. package/dist/driver/sql.js.d.ts +15 -15
  59. package/dist/driver/sqlite3.d.ts +18 -18
  60. package/dist/index.d.ts +21 -21
  61. package/dist/lib/Column.d.ts +57 -0
  62. package/dist/lib/Column.js +63 -0
  63. package/dist/lib/Cursor.d.ts +79 -0
  64. package/dist/lib/Cursor.js +231 -0
  65. package/dist/lib/Driver.d.ts +110 -110
  66. package/dist/lib/Expr.d.ts +154 -0
  67. package/dist/lib/Expr.js +284 -0
  68. package/dist/lib/Fields.d.ts +13 -0
  69. package/dist/lib/Fields.js +0 -0
  70. package/dist/lib/Formatter.d.ts +78 -67
  71. package/dist/lib/Formatter.js +13 -9
  72. package/dist/lib/Functions.d.ts +5 -0
  73. package/dist/lib/Functions.js +11 -0
  74. package/dist/lib/Id.d.ts +3 -0
  75. package/dist/lib/Id.js +0 -0
  76. package/dist/lib/Index.d.ts +17 -0
  77. package/dist/lib/Index.js +20 -0
  78. package/dist/lib/Ops.d.ts +8 -0
  79. package/dist/lib/Ops.js +45 -0
  80. package/dist/lib/OrderBy.d.ts +9 -0
  81. package/dist/lib/OrderBy.js +9 -0
  82. package/dist/lib/Param.d.ts +20 -0
  83. package/dist/lib/Param.js +27 -0
  84. package/dist/lib/Query.d.ts +109 -0
  85. package/dist/lib/Query.js +74 -0
  86. package/dist/lib/Sanitizer.d.ts +5 -5
  87. package/dist/lib/Schema.d.ts +18 -0
  88. package/dist/lib/Schema.js +67 -0
  89. package/dist/lib/Selection.d.ts +20 -0
  90. package/dist/lib/Selection.js +7 -0
  91. package/dist/lib/SqlError.d.ts +3 -3
  92. package/dist/lib/Statement.d.ts +33 -29
  93. package/dist/lib/Statement.js +10 -7
  94. package/dist/lib/Table.d.ts +62 -0
  95. package/dist/lib/Table.js +130 -0
  96. package/dist/lib/Target.d.ts +30 -0
  97. package/dist/lib/Target.js +37 -0
  98. package/dist/lib/Update.d.ts +4 -0
  99. package/dist/lib/Update.js +0 -0
  100. package/dist/sqlite/SqliteFormatter.d.ts +11 -11
  101. package/dist/sqlite/SqliteFunctions.d.ts +165 -165
  102. package/dist/sqlite/SqliteSchema.d.ts +25 -25
  103. package/dist/sqlite/index.d.ts +2 -0
  104. package/dist/sqlite/index.js +3 -0
  105. package/dist/sqlite.d.ts +2 -2
  106. package/dist/src/driver/better-sqlite3.js +49 -0
  107. package/dist/src/driver/sql.js.js +66 -0
  108. package/dist/src/driver/sqlite3.js +87 -0
  109. package/dist/src/index.js +24 -0
  110. package/dist/src/lib/Column.js +63 -0
  111. package/dist/src/lib/Cursor.js +231 -0
  112. package/dist/src/lib/Driver.js +316 -0
  113. package/dist/src/lib/Expr.js +284 -0
  114. package/dist/src/lib/Fields.js +0 -0
  115. package/dist/src/lib/Formatter.js +625 -0
  116. package/dist/src/lib/Functions.js +11 -0
  117. package/dist/src/lib/Id.js +0 -0
  118. package/dist/src/lib/Index.js +20 -0
  119. package/dist/src/lib/Ops.js +45 -0
  120. package/dist/src/lib/OrderBy.js +9 -0
  121. package/dist/src/lib/Param.js +27 -0
  122. package/dist/src/lib/Query.js +74 -0
  123. package/dist/src/lib/Sanitizer.js +0 -0
  124. package/dist/src/lib/Schema.js +67 -0
  125. package/dist/src/lib/Selection.js +7 -0
  126. package/dist/src/lib/Statement.js +103 -0
  127. package/dist/src/lib/Table.js +130 -0
  128. package/dist/src/lib/Target.js +37 -0
  129. package/dist/src/lib/Update.js +0 -0
  130. package/dist/src/sqlite/SqliteFormatter.js +67 -0
  131. package/dist/src/sqlite/SqliteFunctions.js +146 -0
  132. package/dist/src/sqlite/SqliteSchema.js +47 -0
  133. package/dist/src/sqlite.js +3 -0
  134. package/dist/test/DbSuite.js +28 -0
  135. package/dist/test/TSTestTypes.js +6 -0
  136. package/dist/test/TestBasic.js +172 -0
  137. package/dist/test/TestExtend.js +68 -0
  138. package/dist/test/TestFts5.js +34 -0
  139. package/dist/test/TestFunctions.js +28 -0
  140. package/dist/test/TestJoins.js +34 -0
  141. package/dist/test/TestJson.js +33 -0
  142. package/dist/test/TestPrepare.js +27 -0
  143. package/dist/test/TestRawSql.js +31 -0
  144. package/dist/test/TestSchema.js +69 -0
  145. package/dist/test/TestSubQueries.js +79 -0
  146. package/dist/test/TestTransaction.js +54 -0
  147. package/dist/test/TestUpdate.js +95 -0
  148. package/dist/test/index.js +14 -0
  149. package/package.json +60 -60
@@ -1,67 +1,78 @@
1
- import { ColumnData, ColumnType } from '../define/Column';
2
- import { ExprData } from '../define/Expr';
3
- import { OrderBy } from '../define/OrderBy';
4
- import { Query } from '../define/Query';
5
- import { Target } from '../define/Target';
6
- import { Sanitizer } from './Sanitizer';
7
- import { Statement } from './Statement';
8
- export interface FormatContext {
9
- stmt: Statement;
10
- nameResult?: string;
11
- /** Skip prefixing fields with their table name */
12
- skipTableName?: boolean;
13
- /**
14
- * In SQLite table names are used as expressions in the FTS5 plugin.
15
- * To distinguish between formatting a row of the table and just the table name
16
- * this flag can be used.
17
- **/
18
- tableAsExpr?: boolean;
19
- /** Inline all parameters */
20
- forceInline?: boolean;
21
- formatAsJson?: boolean;
22
- formatAsDefault?: boolean;
23
- topLevel?: boolean;
24
- }
25
- type FormatSubject = (stmt: Statement, mkSubject: () => void) => void;
26
- export declare abstract class Formatter implements Sanitizer {
27
- constructor();
28
- abstract escapeValue(value: any): string;
29
- abstract escapeIdentifier(ident: string): string;
30
- abstract formatParamValue(paramValue: any): any;
31
- abstract formatAccess(ctx: FormatContext, mkSubject: () => void, field: string): Statement;
32
- compile<T>(query: Query<T>, options?: Partial<FormatContext>): Statement;
33
- format<T>(ctx: FormatContext, query: Query<T>): Statement;
34
- formatSelect(ctx: FormatContext, query: Query.Select): Statement;
35
- formatInsert(ctx: FormatContext, query: Query.Insert): Statement;
36
- formatUpdate(ctx: FormatContext, query: Query.Update): Statement;
37
- formatDelete(ctx: FormatContext, query: Query.Delete): Statement;
38
- formatCreateTable(ctx: FormatContext, query: Query.CreateTable): Statement;
39
- formatCreateIndex(ctx: FormatContext, query: Query.CreateIndex): Statement;
40
- formatDropIndex(ctx: FormatContext, query: Query.DropIndex): Statement;
41
- formatAlterTable(ctx: FormatContext, query: Query.AlterTable): Statement;
42
- formatTransaction(ctx: FormatContext, { op, id }: Query.Transaction): Statement;
43
- formatBatch(ctx: FormatContext, { queries }: Query.Batch): Statement;
44
- formatRaw(ctx: FormatContext, { strings, params }: Query.Raw): Statement;
45
- formatColumn(ctx: FormatContext, column: ColumnData): Statement;
46
- formatContraintReference(ctx: FormatContext, reference: ExprData): Statement;
47
- formatType(ctx: FormatContext, type: ColumnType): Statement;
48
- formatInsertRow(ctx: FormatContext, columns: Record<string, ColumnData>, row: Record<string, any>): Statement;
49
- formatColumnValue(ctx: FormatContext, column: ColumnData, columnValue: any): Statement;
50
- formatTarget(ctx: FormatContext, target: Target): Statement;
51
- formatWhere(ctx: FormatContext, expr: ExprData | undefined): Statement;
52
- formatHaving(ctx: FormatContext, expr: ExprData | undefined): Statement;
53
- formatGroupBy(ctx: FormatContext, groupBy: Array<ExprData> | undefined): Statement;
54
- formatOrderBy(ctx: FormatContext, orderBy: Array<OrderBy> | undefined): Statement;
55
- formatLimit(ctx: FormatContext, { limit, offset, singleResult }: Query.QueryBase): Statement;
56
- formatSelection(ctx: FormatContext, selection: ExprData, formatSubject?: FormatSubject): Statement;
57
- formatExprJson(ctx: FormatContext, expr: ExprData): Statement;
58
- formatExprValue(ctx: FormatContext, expr: ExprData): Statement;
59
- formatIn(ctx: FormatContext, expr: ExprData): Statement;
60
- retrieveField(expr: ExprData, field: string): ExprData | undefined;
61
- formatField(ctx: FormatContext, expr: ExprData, field: string): Statement;
62
- formatString(ctx: FormatContext, input: string): Statement;
63
- formatInlineValue(ctx: FormatContext, rawValue: any): Statement;
64
- formatValue(ctx: FormatContext, rawValue: any): Statement;
65
- formatExpr(ctx: FormatContext, expr: ExprData): Statement;
66
- }
67
- export {};
1
+ import { ColumnData, ColumnType } from '../define/Column';
2
+ import { ExprData } from '../define/Expr';
3
+ import { OrderBy } from '../define/OrderBy';
4
+ import { Query } from '../define/Query';
5
+ import { Target } from '../define/Target';
6
+ import { Sanitizer } from './Sanitizer';
7
+ import { Statement, StatementOptions } from './Statement';
8
+ export interface FormatContext {
9
+ stmt: Statement;
10
+ nameResult?: string;
11
+ /** Skip prefixing fields with their table name */
12
+ skipTableName?: boolean;
13
+ /**
14
+ * In SQLite table names are used as expressions in the FTS5 plugin.
15
+ * To distinguish between formatting a row of the table and just the table name
16
+ * this flag can be used.
17
+ **/
18
+ tableAsExpr?: boolean;
19
+ /** Inline all parameters */
20
+ forceInline?: boolean;
21
+ formatAsJson?: boolean;
22
+ formatAsDefault?: boolean;
23
+ topLevel?: boolean;
24
+ }
25
+ type FormatSubject = (stmt: Statement, mkSubject: () => void) => void;
26
+ export declare abstract class Formatter implements Sanitizer {
27
+ constructor();
28
+ abstract escapeValue(value: any): string;
29
+ abstract escapeIdentifier(ident: string): string;
30
+ abstract formatParamValue(paramValue: any): any;
31
+ abstract formatAccess(ctx: FormatContext, mkSubject: () => void, field: string): Statement;
32
+ compile<T>(query: Query<T>, options?: Partial<FormatContext>): Statement;
33
+ createContext(options?: Partial<FormatContext> & StatementOptions): {
34
+ stmt: Statement;
35
+ nameResult?: string | undefined;
36
+ skipTableName?: boolean | undefined;
37
+ tableAsExpr?: boolean | undefined;
38
+ forceInline?: boolean | undefined;
39
+ formatAsJson?: boolean | undefined;
40
+ formatAsDefault?: boolean | undefined;
41
+ topLevel?: boolean | undefined;
42
+ skipNewlines?: boolean | undefined;
43
+ };
44
+ format<T>(ctx: FormatContext, query: Query<T>): Statement;
45
+ formatSelect(ctx: FormatContext, query: Query.Select): Statement;
46
+ formatInsert(ctx: FormatContext, query: Query.Insert): Statement;
47
+ formatUpdate(ctx: FormatContext, query: Query.Update): Statement;
48
+ formatDelete(ctx: FormatContext, query: Query.Delete): Statement;
49
+ formatCreateTable(ctx: FormatContext, query: Query.CreateTable): Statement;
50
+ formatCreateIndex(ctx: FormatContext, query: Query.CreateIndex): Statement;
51
+ formatDropIndex(ctx: FormatContext, query: Query.DropIndex): Statement;
52
+ formatAlterTable(ctx: FormatContext, query: Query.AlterTable): Statement;
53
+ formatTransaction(ctx: FormatContext, { op, id }: Query.Transaction): Statement;
54
+ formatBatch(ctx: FormatContext, { queries }: Query.Batch): Statement;
55
+ formatRaw(ctx: FormatContext, { strings, params }: Query.Raw): Statement;
56
+ formatColumn(ctx: FormatContext, column: ColumnData): Statement;
57
+ formatContraintReference(ctx: FormatContext, reference: ExprData): Statement;
58
+ formatType(ctx: FormatContext, type: ColumnType): Statement;
59
+ formatInsertRow(ctx: FormatContext, columns: Record<string, ColumnData>, row: Record<string, any>): Statement;
60
+ formatColumnValue(ctx: FormatContext, column: ColumnData, columnValue: any): Statement;
61
+ formatTarget(ctx: FormatContext, target: Target): Statement;
62
+ formatWhere(ctx: FormatContext, expr: ExprData | undefined): Statement;
63
+ formatHaving(ctx: FormatContext, expr: ExprData | undefined): Statement;
64
+ formatGroupBy(ctx: FormatContext, groupBy: Array<ExprData> | undefined): Statement;
65
+ formatOrderBy(ctx: FormatContext, orderBy: Array<OrderBy> | undefined): Statement;
66
+ formatLimit(ctx: FormatContext, { limit, offset, singleResult }: Query.QueryBase): Statement;
67
+ formatSelection(ctx: FormatContext, selection: ExprData, formatSubject?: FormatSubject): Statement;
68
+ formatExprJson(ctx: FormatContext, expr: ExprData): Statement;
69
+ formatExprValue(ctx: FormatContext, expr: ExprData): Statement;
70
+ formatIn(ctx: FormatContext, expr: ExprData): Statement;
71
+ retrieveField(expr: ExprData, field: string): ExprData | undefined;
72
+ formatField(ctx: FormatContext, expr: ExprData, field: string): Statement;
73
+ formatString(ctx: FormatContext, input: string): Statement;
74
+ formatInlineValue(ctx: FormatContext, rawValue: any): Statement;
75
+ formatValue(ctx: FormatContext, rawValue: any): Statement;
76
+ formatExpr(ctx: FormatContext, expr: ExprData): Statement;
77
+ }
78
+ export {};
@@ -40,10 +40,15 @@ var Formatter = class {
40
40
  constructor() {
41
41
  }
42
42
  compile(query, options) {
43
- const stmt = new Statement(this);
44
- const result = this.format({ stmt, topLevel: true, ...options }, query);
43
+ const result = this.format(
44
+ this.createContext({ topLevel: true, ...options }),
45
+ query
46
+ );
45
47
  return result;
46
48
  }
49
+ createContext(options) {
50
+ return { stmt: new Statement(this, options), ...options };
51
+ }
47
52
  format(ctx, query) {
48
53
  switch (query.type) {
49
54
  case QueryType.Select:
@@ -419,9 +424,7 @@ var Formatter = class {
419
424
  formatIn(ctx, expr) {
420
425
  const { stmt } = ctx;
421
426
  switch (expr.type) {
422
- case ExprType.Query:
423
- return this.formatExprValue(ctx, expr);
424
- default:
427
+ case ExprType.Field:
425
428
  stmt.openParenthesis();
426
429
  stmt.raw("SELECT value FROM json_each");
427
430
  stmt.openParenthesis();
@@ -429,6 +432,8 @@ var Formatter = class {
429
432
  stmt.closeParenthesis();
430
433
  stmt.closeParenthesis();
431
434
  return stmt;
435
+ default:
436
+ return this.formatExprValue(ctx, expr);
432
437
  }
433
438
  }
434
439
  retrieveField(expr, field) {
@@ -495,12 +500,11 @@ var Formatter = class {
495
500
  case Array.isArray(rawValue):
496
501
  if (formatAsDefault || formatAsJson) {
497
502
  stmt.raw("json_array");
498
- stmt.openParenthesis();
499
503
  }
504
+ stmt.openParenthesis();
500
505
  for (const v of stmt.separate(rawValue))
501
506
  this.formatValue({ ...ctx, formatAsJson: false }, v);
502
- if (formatAsDefault || formatAsJson)
503
- stmt.closeParenthesis();
507
+ stmt.closeParenthesis();
504
508
  return stmt;
505
509
  case (typeof rawValue === "string" || typeof rawValue === "number"):
506
510
  if (forceInline)
@@ -542,7 +546,7 @@ var Formatter = class {
542
546
  case ParamType.Value:
543
547
  return this.formatValue(ctx, expr.param.value);
544
548
  case ParamType.Named:
545
- return stmt.param(expr.param.name);
549
+ return stmt.param(expr.param);
546
550
  }
547
551
  case ExprType.Field:
548
552
  return this.formatField(ctx, expr.expr, expr.field);
@@ -0,0 +1,5 @@
1
+ import { Expr } from './Expr';
2
+ export declare const Functions: Functions;
3
+ export type Functions = {
4
+ [key: string]: (...args: Array<any>) => Expr<any>;
5
+ };
@@ -0,0 +1,11 @@
1
+ // src/lib/Functions.ts
2
+ import { Expr, ExprData } from "./Expr.js";
3
+ function get(_, method) {
4
+ return (...args) => {
5
+ return new Expr(ExprData.Call(method, args.map(ExprData.create)));
6
+ };
7
+ }
8
+ var Functions = new Proxy({}, { get });
9
+ export {
10
+ Functions
11
+ };
@@ -0,0 +1,3 @@
1
+ export type Id<T extends {
2
+ id: any;
3
+ }> = T['id'];
package/dist/lib/Id.js ADDED
File without changes
@@ -0,0 +1,17 @@
1
+ import { Expr, ExprData } from './Expr';
2
+ interface PartialIndexData {
3
+ on: Array<ExprData>;
4
+ unique?: boolean;
5
+ where?: ExprData;
6
+ }
7
+ export interface IndexData extends PartialIndexData {
8
+ name: string;
9
+ }
10
+ export declare class Index {
11
+ data: PartialIndexData;
12
+ constructor(data: PartialIndexData);
13
+ unique(): Index;
14
+ where(where: Expr<boolean>): Index;
15
+ }
16
+ export declare function index(...on: Array<Expr<any>>): Index;
17
+ export {};
@@ -0,0 +1,20 @@
1
+ // src/lib/Index.ts
2
+ import { ExprData } from "./Expr.js";
3
+ var Index = class {
4
+ constructor(data) {
5
+ this.data = data;
6
+ }
7
+ unique() {
8
+ return new Index({ ...this.data, unique: true });
9
+ }
10
+ where(where) {
11
+ return new Index({ ...this.data, where: ExprData.create(where) });
12
+ }
13
+ };
14
+ function index(...on) {
15
+ return new Index({ on: on.map(ExprData.create) });
16
+ }
17
+ export {
18
+ Index,
19
+ index
20
+ };
@@ -0,0 +1,8 @@
1
+ import { Cursor } from './Cursor';
2
+ import { Table } from './Table';
3
+ export declare function selectAll<Row>(table: Table<Row>): Cursor.SelectMultiple<Row>;
4
+ export declare function selectFirst<Row>(table: Table<Row>): Cursor.SelectSingle<Row>;
5
+ export declare function update<Row>(table: Table<Row>): Cursor.Update<Row>;
6
+ export declare function insertInto<Row>(table: Table<Row>): Cursor.Insert<Row>;
7
+ export declare function deleteFrom<Row>(table: Table<Row>): Cursor.Delete;
8
+ export declare function create(...tables: Array<Table<any>>): Cursor.Batch;
@@ -0,0 +1,45 @@
1
+ // src/lib/Ops.ts
2
+ import { Cursor } from "./Cursor.js";
3
+ import { ExprData } from "./Expr.js";
4
+ import { Query } from "./Query.js";
5
+ import { Schema } from "./Schema.js";
6
+ import { Target } from "./Target.js";
7
+ function selectAll(table) {
8
+ return new Cursor.SelectMultiple(
9
+ Query.Select({
10
+ from: Target.Table(table.schema()),
11
+ selection: ExprData.Row(Target.Table(table.schema()))
12
+ })
13
+ );
14
+ }
15
+ function selectFirst(table) {
16
+ return new Cursor.SelectSingle(
17
+ Query.Select({
18
+ from: Target.Table(table.schema()),
19
+ selection: ExprData.Row(Target.Table(table.schema())),
20
+ singleResult: true
21
+ })
22
+ );
23
+ }
24
+ function update(table) {
25
+ return new Cursor.Update(Query.Update({ table: table.schema() }));
26
+ }
27
+ function insertInto(table) {
28
+ return new Cursor.Insert(table.schema());
29
+ }
30
+ function deleteFrom(table) {
31
+ return new Cursor.Delete(Query.Delete({ table: table.schema() }));
32
+ }
33
+ function create(...tables) {
34
+ return new Cursor.Batch(
35
+ tables.flatMap((table) => Schema.create(table.schema()).queries)
36
+ );
37
+ }
38
+ export {
39
+ create,
40
+ deleteFrom,
41
+ insertInto,
42
+ selectAll,
43
+ selectFirst,
44
+ update
45
+ };
@@ -0,0 +1,9 @@
1
+ import { ExprData } from './Expr';
2
+ export declare enum OrderDirection {
3
+ Asc = "Asc",
4
+ Desc = "Desc"
5
+ }
6
+ export type OrderBy = {
7
+ expr: ExprData;
8
+ order: OrderDirection;
9
+ };
@@ -0,0 +1,9 @@
1
+ // src/lib/OrderBy.ts
2
+ var OrderDirection = /* @__PURE__ */ ((OrderDirection2) => {
3
+ OrderDirection2["Asc"] = "Asc";
4
+ OrderDirection2["Desc"] = "Desc";
5
+ return OrderDirection2;
6
+ })(OrderDirection || {});
7
+ export {
8
+ OrderDirection
9
+ };
@@ -0,0 +1,20 @@
1
+ import { Expr } from './Expr';
2
+ export declare enum ParamType {
3
+ Value = "Value",
4
+ Named = "Named"
5
+ }
6
+ export type ParamData = {
7
+ type: ParamType.Value;
8
+ value: any;
9
+ } | {
10
+ type: ParamType.Named;
11
+ name: string;
12
+ };
13
+ export declare const ParamData: {
14
+ Value(value: any): ParamData;
15
+ Named(name: string): ParamData;
16
+ };
17
+ export type Params<T> = {
18
+ [K in keyof T]: Expr<T[K]>;
19
+ };
20
+ export declare function createParams<T>(): Params<T>;
@@ -0,0 +1,27 @@
1
+ // src/lib/Param.ts
2
+ import { Expr, ExprData } from "./Expr.js";
3
+ var ParamType = /* @__PURE__ */ ((ParamType2) => {
4
+ ParamType2["Value"] = "Value";
5
+ ParamType2["Named"] = "Named";
6
+ return ParamType2;
7
+ })(ParamType || {});
8
+ var ParamData = {
9
+ Value(value) {
10
+ return { type: "Value" /* Value */, value };
11
+ },
12
+ Named(name) {
13
+ return { type: "Named" /* Named */, name };
14
+ }
15
+ };
16
+ function createParams() {
17
+ return new Proxy({}, {
18
+ get(target, prop) {
19
+ return new Expr(ExprData.Param(ParamData.Named(prop)));
20
+ }
21
+ });
22
+ }
23
+ export {
24
+ ParamData,
25
+ ParamType,
26
+ createParams
27
+ };
@@ -0,0 +1,109 @@
1
+ import { ColumnData } from './Column';
2
+ import { ExprData } from './Expr';
3
+ import { IndexData } from './Index';
4
+ import { OrderBy } from './OrderBy';
5
+ import { Schema } from './Schema';
6
+ import { Target } from './Target';
7
+ export declare enum QueryType {
8
+ Insert = "Insert",
9
+ Select = "Select",
10
+ Update = "Update",
11
+ Delete = "Delete",
12
+ CreateTable = "CreateTable",
13
+ CreateIndex = "CreateIndex",
14
+ DropIndex = "DropIndex",
15
+ AlterTable = "AlterTable",
16
+ Batch = "Batch",
17
+ Transaction = "Transaction",
18
+ Raw = "Raw"
19
+ }
20
+ 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;
21
+ export declare namespace Query {
22
+ interface QueryBase {
23
+ limit?: number;
24
+ offset?: number;
25
+ type: QueryType;
26
+ where?: ExprData;
27
+ orderBy?: Array<OrderBy>;
28
+ groupBy?: Array<ExprData>;
29
+ having?: ExprData;
30
+ selection?: ExprData;
31
+ singleResult?: boolean;
32
+ validate?: boolean;
33
+ }
34
+ interface Insert extends QueryBase {
35
+ type: QueryType.Insert;
36
+ into: Schema;
37
+ data: Array<any>;
38
+ }
39
+ function Insert(insert: Omit<Insert, 'type'>): Query.Insert;
40
+ interface Select extends QueryBase {
41
+ type: QueryType.Select;
42
+ selection: ExprData;
43
+ from: Target;
44
+ }
45
+ function Select(select: Omit<Select, 'type'>): Query.Select;
46
+ interface Update extends QueryBase {
47
+ type: QueryType.Update;
48
+ table: Schema;
49
+ set?: Record<string, any>;
50
+ }
51
+ function Update(update: Omit<Update, 'type'>): Query.Update;
52
+ interface Delete extends QueryBase {
53
+ type: QueryType.Delete;
54
+ table: Schema;
55
+ }
56
+ function Delete(del: Omit<Delete, 'type'>): Query.Delete;
57
+ interface CreateTable extends QueryBase {
58
+ type: QueryType.CreateTable;
59
+ table: Schema;
60
+ ifNotExists?: boolean;
61
+ }
62
+ function CreateTable(create: Omit<CreateTable, 'type'>): Query.CreateTable;
63
+ interface CreateIndex extends QueryBase {
64
+ type: QueryType.CreateIndex;
65
+ table: Schema;
66
+ index: IndexData;
67
+ ifNotExists?: boolean;
68
+ }
69
+ function CreateIndex(create: Omit<CreateIndex, 'type'>): Query.CreateIndex;
70
+ interface DropIndex extends QueryBase {
71
+ type: QueryType.DropIndex;
72
+ table: Schema;
73
+ name: string;
74
+ ifExists?: boolean;
75
+ }
76
+ function DropIndex(drop: Omit<DropIndex, 'type'>): Query.DropIndex;
77
+ interface Batch extends QueryBase {
78
+ type: QueryType.Batch;
79
+ queries: Array<Query<any>>;
80
+ }
81
+ function Batch(batch: Omit<Batch, 'type'>): Query.Batch;
82
+ enum TransactionOperation {
83
+ Begin = "Begin",
84
+ Commit = "Commit",
85
+ Rollback = "Rollback"
86
+ }
87
+ interface Transaction extends QueryBase {
88
+ type: QueryType.Transaction;
89
+ id: string;
90
+ op: TransactionOperation;
91
+ }
92
+ function Transaction(transaction: Omit<Transaction, 'type'>): Query.Transaction;
93
+ type RawReturn = 'row' | 'rows' | undefined;
94
+ interface Raw extends QueryBase {
95
+ type: QueryType.Raw;
96
+ expectedReturn?: 'row' | 'rows';
97
+ strings: ReadonlyArray<string>;
98
+ params: Array<any>;
99
+ }
100
+ function Raw(raw: Omit<Raw, 'type'>): Query.Raw;
101
+ interface AlterTable extends QueryBase {
102
+ type: QueryType.AlterTable;
103
+ table: Schema;
104
+ alterColumn?: ColumnData;
105
+ addColumn?: ColumnData;
106
+ dropColumn?: string;
107
+ }
108
+ function AlterTable(alter: Omit<AlterTable, 'type'>): Query.AlterTable;
109
+ }
@@ -0,0 +1,74 @@
1
+ // src/lib/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
+ };
@@ -1,5 +1,5 @@
1
- export interface Sanitizer {
2
- escapeValue(value: any): string;
3
- escapeIdentifier(ident: string): string;
4
- formatParamValue(paramValue: any): any;
5
- }
1
+ export interface Sanitizer {
2
+ escapeValue(value: any): string;
3
+ escapeIdentifier(ident: string): string;
4
+ formatParamValue(paramValue: any): any;
5
+ }
@@ -0,0 +1,18 @@
1
+ import { ColumnData } from './Column';
2
+ import { Formatter } from './Formatter';
3
+ import { IndexData } from './Index';
4
+ import { Query } from './Query';
5
+ export interface Schema {
6
+ name: string;
7
+ alias?: string;
8
+ columns: Record<string, ColumnData>;
9
+ indexes: Record<string, IndexData>;
10
+ }
11
+ export interface SchemaInstructions {
12
+ columns: Record<string, string>;
13
+ indexes: Record<string, string>;
14
+ }
15
+ export declare namespace Schema {
16
+ function create(schema: Schema): Query.Batch;
17
+ function upgrade(formatter: Formatter, local: SchemaInstructions, schema: Schema): Array<Query>;
18
+ }