rado 0.1.28 → 0.1.30

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,5 +1,6 @@
1
1
  // src/lib/Schema.ts
2
2
  import { Query } from "./Query.js";
3
+ import { Statement } from "./Statement.js";
3
4
  var Schema;
4
5
  ((Schema2) => {
5
6
  function create(schema) {
@@ -27,7 +28,10 @@ var Schema;
27
28
  } else if (!schemaCol) {
28
29
  res.push(Query.AlterTable({ table: schema, dropColumn: columnName }));
29
30
  } else {
30
- const { sql: instruction } = formatter.formatColumn({ ...schemaCol, references: void 0 }).compile(formatter);
31
+ const { sql: instruction } = formatter.formatColumn(
32
+ { stmt: new Statement() },
33
+ { ...schemaCol, references: void 0 }
34
+ ).compile(formatter);
31
35
  if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
32
36
  res.push(Query.AlterTable({ table: schema, alterColumn: schemaCol }));
33
37
  }
@@ -46,6 +50,7 @@ var Schema;
46
50
  res.unshift(Query.DropIndex({ table: schema, name: indexName }));
47
51
  } else {
48
52
  const { sql: instruction } = formatter.formatCreateIndex(
53
+ { stmt: new Statement() },
49
54
  Query.CreateIndex({ table: schema, index: schemaIndex })
50
55
  ).compile(formatter);
51
56
  if (removeLeadingWhitespace(localInstruction) !== removeLeadingWhitespace(instruction)) {
@@ -25,30 +25,26 @@ export interface CompileOptions {
25
25
  }
26
26
  export declare class Statement {
27
27
  tokens: Array<Token>;
28
- constructor(tokens: Array<Token>);
29
- concat(...tokens: Array<Token | Statement>): Statement;
30
- static create(from: string | Statement): Statement;
28
+ constructor(tokens?: Array<Token>);
29
+ concat(...tokens: Array<Token>): this;
31
30
  static tag(strings: ReadonlyArray<string>, params: Array<Statement>): Statement;
32
- space(): Statement;
33
- call(method: string, ...args: Array<Statement>): Statement;
34
- addCall(method: string, ...args: Array<Statement>): Statement;
35
- add(addition: undefined | string | Statement): Statement;
36
- addLine(addition: undefined | string | Statement): Statement;
37
- addIf(condition: any, addition: string | Statement | (() => string | Statement)): Statement;
38
- indent(): Statement;
39
- dedent(): Statement;
40
- newline(ignore?: boolean): Statement;
41
- identifier(name: string): Statement;
42
- addIdentifier(name: string): Statement;
43
- value(value: any): Statement;
44
- addValue(value: any): Statement;
45
- param(name: string): Statement;
46
- addParam(name: string): Statement;
47
- raw(query: string): Statement;
48
- parenthesis(inner: string | Statement): Statement;
49
- addParenthesis(stmnt: string | Statement): Statement;
50
- separated(input: Array<Statement>, separator?: string): Statement;
51
- addSeparated(input: Array<Statement>, separator?: string): Statement;
31
+ space(): this;
32
+ add(addition: undefined | string): this;
33
+ addLine(addition: undefined | string): this;
34
+ indent(): this;
35
+ dedent(): this;
36
+ newline(ignore?: boolean): this;
37
+ identifier(name: string): this;
38
+ addIdentifier(name: string): this;
39
+ value(value: any): this;
40
+ addValue(value: any): this;
41
+ param(name: string): this;
42
+ addParam(name: string): this;
43
+ raw(query: string): this;
44
+ openParenthesis(): this;
45
+ closeParenthesis(): this;
46
+ call<T>(parts: Array<T>, separator?: string): Generator<T, void, unknown>;
47
+ separate<T>(parts: Array<T>, separator?: string): Generator<T, void, unknown>;
52
48
  isEmpty(): boolean;
53
49
  compile(sanitizer: Sanitizer, formatInline?: boolean): CompiledStatement;
54
50
  }
@@ -60,13 +56,4 @@ export declare class CompiledStatement {
60
56
  params(input?: Record<string, any>): Array<any>;
61
57
  toString(): string;
62
58
  }
63
- export declare function newline(): Statement;
64
- export declare function raw(raw: string): Statement;
65
- export declare function identifier(name: string): Statement;
66
- export declare function value(value: any): Statement;
67
- export declare function param(name: string): Statement;
68
- export declare function empty(): Statement;
69
- export declare function parenthesis(stmnt: Statement): Statement;
70
- export declare function call(method: string, ...args: Array<Statement>): Statement;
71
- export declare function separated(input: Array<Statement>, separator?: string): Statement;
72
59
  export {};
@@ -24,58 +24,39 @@ var Token = class {
24
24
  return new Token("Param" /* Param */, name);
25
25
  }
26
26
  };
27
- var SEPARATE = ",";
27
+ var SEPARATE = ", ";
28
28
  var WHITESPACE = " ";
29
29
  var NEWLINE = "\n";
30
30
  var Statement = class {
31
- constructor(tokens) {
31
+ constructor(tokens = []) {
32
32
  this.tokens = tokens;
33
33
  }
34
34
  concat(...tokens) {
35
- return new Statement(
36
- this.tokens.concat(
37
- ...tokens.flatMap((t) => t instanceof Statement ? t.tokens : [t])
38
- )
39
- );
40
- }
41
- static create(from) {
42
- return typeof from === "string" ? raw(from) : from;
35
+ this.tokens.push(...tokens);
36
+ return this;
43
37
  }
44
38
  static tag(strings, params) {
45
39
  return new Statement(
46
40
  strings.flatMap((s, i) => {
47
- const param2 = params[i];
48
- return [Token.Raw(s)].concat(param2 ? param2.tokens : []);
41
+ const param = params[i];
42
+ return [Token.Raw(s)].concat(param ? param.tokens : []);
49
43
  })
50
44
  );
51
45
  }
52
46
  space() {
53
- return this.concat(Token.Raw(WHITESPACE));
54
- }
55
- call(method, ...args) {
56
- return this.identifier(method).parenthesis(separated(args));
57
- }
58
- addCall(method, ...args) {
59
- return this.space().call(method, ...args);
47
+ if (this.tokens.length === 0)
48
+ return this;
49
+ return this.raw(WHITESPACE);
60
50
  }
61
51
  add(addition) {
62
52
  if (!addition)
63
53
  return this;
64
- if (addition instanceof Statement && addition.isEmpty())
65
- return this;
66
- return this.space().concat(Statement.create(addition));
54
+ return this.space().raw(addition);
67
55
  }
68
56
  addLine(addition) {
69
57
  if (!addition)
70
58
  return this;
71
- if (addition instanceof Statement && addition.isEmpty())
72
- return this;
73
- return this.newline().concat(Statement.create(addition));
74
- }
75
- addIf(condition, addition) {
76
- if (!condition)
77
- return this;
78
- return this.add(typeof addition === "function" ? addition() : addition);
59
+ return this.newline().add(addition);
79
60
  }
80
61
  indent() {
81
62
  return this.concat(Token.Indent());
@@ -94,11 +75,11 @@ var Statement = class {
94
75
  addIdentifier(name) {
95
76
  return this.space().identifier(name);
96
77
  }
97
- value(value2) {
98
- return this.concat(Token.Value(value2));
78
+ value(value) {
79
+ return this.concat(Token.Value(value));
99
80
  }
100
- addValue(value2) {
101
- return this.space().value(value2);
81
+ addValue(value) {
82
+ return this.space().value(value);
102
83
  }
103
84
  param(name) {
104
85
  return this.concat(Token.Param(name));
@@ -111,21 +92,23 @@ var Statement = class {
111
92
  return this;
112
93
  return this.concat(Token.Raw(query));
113
94
  }
114
- parenthesis(inner) {
115
- return this.raw("(").indent().newline().concat(Statement.create(inner)).dedent().newline().raw(")");
95
+ openParenthesis() {
96
+ return this.raw("(").indent().newline();
116
97
  }
117
- addParenthesis(stmnt) {
118
- return this.space().parenthesis(stmnt);
98
+ closeParenthesis() {
99
+ return this.dedent().newline().raw(")");
119
100
  }
120
- separated(input, separator = SEPARATE) {
121
- return this.concat(
122
- ...input.flatMap(
123
- (stmt, i) => i === 0 ? stmt.tokens : [Token.Raw(separator), Token.Raw(NEWLINE), ...stmt.tokens]
124
- )
125
- );
101
+ *call(parts, separator = SEPARATE) {
102
+ this.openParenthesis();
103
+ yield* this.separate(parts, separator);
104
+ this.closeParenthesis();
126
105
  }
127
- addSeparated(input, separator = SEPARATE) {
128
- return this.space().separated(input, separator);
106
+ *separate(parts, separator = SEPARATE) {
107
+ for (let i = 0; i < parts.length; i++) {
108
+ if (i > 0)
109
+ this.raw(separator).newline();
110
+ yield parts[i];
111
+ }
129
112
  }
130
113
  isEmpty() {
131
114
  return this.tokens.length === 0 || this.tokens.length === 1 && this.tokens[0].type === "Raw" /* Raw */ && this.tokens[0].data === "";
@@ -173,56 +156,20 @@ var CompiledStatement = class {
173
156
  this.paramData = paramData;
174
157
  }
175
158
  params(input) {
176
- return this.paramData.map((param2) => {
177
- if (param2.type === ParamType.Named) {
178
- if (input && param2.name in input)
179
- return this.sanitizer.formatParamValue(input[param2.name]);
180
- throw new TypeError(`Missing parameter ${param2.name}`);
159
+ return this.paramData.map((param) => {
160
+ if (param.type === ParamType.Named) {
161
+ if (input && param.name in input)
162
+ return this.sanitizer.formatParamValue(input[param.name]);
163
+ throw new TypeError(`Missing parameter ${param.name}`);
181
164
  }
182
- return this.sanitizer.formatParamValue(param2.value);
165
+ return this.sanitizer.formatParamValue(param.value);
183
166
  });
184
167
  }
185
168
  toString() {
186
169
  return this.sql;
187
170
  }
188
171
  };
189
- function newline() {
190
- return new Statement([Token.Raw(NEWLINE)]);
191
- }
192
- function raw(raw2) {
193
- return new Statement([Token.Raw(raw2)]);
194
- }
195
- function identifier(name) {
196
- return new Statement([Token.Identifier(name)]);
197
- }
198
- function value(value2) {
199
- return new Statement([Token.Value(value2)]);
200
- }
201
- function param(name) {
202
- return new Statement([Token.Param(name)]);
203
- }
204
- function empty() {
205
- return new Statement([]);
206
- }
207
- function parenthesis(stmnt) {
208
- return empty().parenthesis(stmnt);
209
- }
210
- function call(method, ...args) {
211
- return identifier(method).parenthesis(separated(args));
212
- }
213
- function separated(input, separator = SEPARATE) {
214
- return empty().separated(input, separator);
215
- }
216
172
  export {
217
173
  CompiledStatement,
218
- Statement,
219
- call,
220
- empty,
221
- identifier,
222
- newline,
223
- param,
224
- parenthesis,
225
- raw,
226
- separated,
227
- value
174
+ Statement
228
175
  };
@@ -9,7 +9,7 @@ import { Update } from './Update';
9
9
  export declare class Table<T> extends Cursor.SelectMultiple<Table.Normalize<T>> {
10
10
  [Selection.__tableType](): Table.Normalize<T>;
11
11
  constructor(schema: Schema);
12
- insertOne(record: Table.Insert<T>): Cursor.Batch<Table.Normalize<T>>;
12
+ insertOne(record: Table.Insert<T>): Cursor<Table.Normalize<T>>;
13
13
  insertAll(data: Array<Table.Insert<T>>): Cursor.InsertValues;
14
14
  set(data: Update<Table.Normalize<T>>): Cursor.Update<Table.Normalize<T>>;
15
15
  delete(): Cursor.Delete;
package/dist/lib/Table.js CHANGED
@@ -33,14 +33,14 @@ var Table = class extends Cursor.SelectMultiple {
33
33
  });
34
34
  }
35
35
  insertOne(record) {
36
- return new Cursor.Batch([
36
+ return new Cursor(
37
37
  Query.Insert({
38
38
  into: this.schema(),
39
39
  data: [record],
40
40
  selection: ExprData.Row(Target.Table(this.schema())),
41
41
  singleResult: true
42
42
  })
43
- ]);
43
+ );
44
44
  }
45
45
  insertAll(data) {
46
46
  return new Cursor.Insert(this.schema()).values(...data);
@@ -6,7 +6,6 @@ export declare class SqliteFormatter extends Formatter {
6
6
  escapeValue(value: any): string;
7
7
  escapeIdentifier(input: string): string;
8
8
  escapeString(input: string): string;
9
- formatSqlAccess(on: Statement, field: string): Statement;
10
- formatJsonAccess(on: Statement, field: string): Statement;
11
- formatExpr(expr: ExprData, ctx: FormatContext): Statement;
9
+ formatAccess(ctx: FormatContext, mkSubject: () => void, field: string): Statement;
10
+ formatExpr(ctx: FormatContext, expr: ExprData): Statement;
12
11
  }
@@ -1,7 +1,6 @@
1
1
  // src/sqlite/SqliteFormatter.ts
2
2
  import { ExprType } from "../lib/Expr.js";
3
3
  import { Formatter } from "../lib/Formatter.js";
4
- import { identifier } from "../lib/Statement.js";
5
4
  import { TargetType } from "../lib/Target.js";
6
5
  function escapeWithin(input, outer) {
7
6
  let buf = outer;
@@ -43,15 +42,14 @@ var SqliteFormatter = class extends Formatter {
43
42
  escapeString(input) {
44
43
  return escapeWithin(input, "'");
45
44
  }
46
- formatSqlAccess(on, field) {
47
- const target = this.formatString(`$.${field}`);
48
- return on.raw("->>").concat(target);
45
+ formatAccess(ctx, mkSubject, field) {
46
+ const { stmt, formatAsJson } = ctx;
47
+ mkSubject();
48
+ stmt.raw(formatAsJson ? "->" : "->>");
49
+ return this.formatString(ctx, `$.${field}`);
49
50
  }
50
- formatJsonAccess(on, field) {
51
- const target = this.formatString(`$.${field}`);
52
- return on.raw("->").concat(target);
53
- }
54
- formatExpr(expr, ctx) {
51
+ formatExpr(ctx, expr) {
52
+ const { stmt } = ctx;
55
53
  switch (expr.type) {
56
54
  case ExprType.Call:
57
55
  if (expr.method === "match") {
@@ -60,10 +58,12 @@ var SqliteFormatter = class extends Formatter {
60
58
  throw new Error("not implemented");
61
59
  if (from.target.type !== TargetType.Table)
62
60
  throw new Error("not implemented");
63
- return identifier(from.target.table.alias || from.target.table.name).raw(" MATCH ").concat(this.formatExprValue(query, ctx));
61
+ stmt.identifier(from.target.table.alias || from.target.table.name).raw(" MATCH ");
62
+ this.formatExprValue(ctx, query);
63
+ return stmt;
64
64
  }
65
65
  default:
66
- return super.formatExpr(expr, ctx);
66
+ return super.formatExpr(ctx, expr);
67
67
  }
68
68
  }
69
69
  };
@@ -1,6 +1,6 @@
1
1
  // src/sqlite/SqliteSchema.ts
2
2
  import { Cursor } from "../lib/Cursor.js";
3
- import { identifier, raw } from "../lib/Statement.js";
3
+ import { Statement } from "../lib/Statement.js";
4
4
  import { SqliteFormatter } from "./SqliteFormatter.js";
5
5
  var SqliteSchema;
6
6
  ((SqliteSchema2) => {
@@ -25,13 +25,16 @@ var SqliteSchema;
25
25
  }
26
26
  SqliteSchema2.createInstructions = createInstructions;
27
27
  function columnInstruction(column) {
28
- return [
29
- column.name,
30
- identifier(column.name).add(column.type).addIf(column.pk === 1, "PRIMARY KEY").addIf(column.notnull === 1, "NOT NULL").addIf(
31
- column.dflt_value !== null,
32
- raw("DEFAULT").addParenthesis(column.dflt_value)
33
- ).compile(formatter).sql
34
- ];
28
+ const stmt = new Statement();
29
+ stmt.identifier(column.name).add(column.type);
30
+ if (column.pk === 1)
31
+ stmt.add("PRIMARY KEY");
32
+ if (column.notnull === 1)
33
+ stmt.add("NOT NULL");
34
+ if (column.dflt_value !== null) {
35
+ stmt.add("DEFAULT").space().openParenthesis().raw(column.dflt_value).closeParenthesis();
36
+ }
37
+ return [column.name, stmt.compile(formatter).sql];
35
38
  }
36
39
  SqliteSchema2.columnInstruction = columnInstruction;
37
40
  function indexInstruction(index) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.28",
3
+ "version": "0.1.30",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {