rado 0.1.1 → 0.1.3

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/dist/Driver.d.ts CHANGED
@@ -10,13 +10,15 @@ declare class Callable extends Function {
10
10
  declare abstract class DriverBase extends Callable {
11
11
  formatter: Formatter;
12
12
  constructor(formatter: Formatter);
13
- all(strings: TemplateStringsArray, ...params: Array<any>): any;
14
- get(strings: TemplateStringsArray, ...params: Array<any>): any;
13
+ all(...args: Array<any>): any;
14
+ get(...args: Array<any>): any;
15
+ sql(strings: TemplateStringsArray, ...params: Array<any>): Cursor<unknown>;
15
16
  executeTemplate(expectedReturn: Query.RawReturn, strings: TemplateStringsArray, ...params: Array<any>): any;
16
17
  abstract executeQuery(query: Query<any>): any;
17
18
  }
18
19
  interface SyncDriver {
19
20
  <T>(query: Cursor<T>): T;
21
+ <T>(...queries: Array<Cursor<any>>): T;
20
22
  (strings: TemplateStringsArray, ...values: any[]): any;
21
23
  }
22
24
  declare abstract class SyncDriver extends DriverBase {
@@ -30,13 +32,17 @@ declare abstract class SyncDriver extends DriverBase {
30
32
  abstract schemaInstructions(tableName: string): SchemaInstructions | undefined;
31
33
  migrateSchema(...tables: Array<Table<any>>): unknown;
32
34
  executeQuery<T>(query: Query<T>): T;
35
+ all<T>(cursor: Cursor<T>): Array<T>;
33
36
  all<T>(strings: TemplateStringsArray, ...params: Array<any>): Array<T>;
37
+ get<T>(cursor: Cursor.SelectMultiple<T>): T | null;
38
+ get<T>(cursor: Cursor<T>): T;
34
39
  get<T>(strings: TemplateStringsArray, ...params: Array<any>): T;
35
40
  transaction<T>(run: (query: SyncDriver) => T): T;
36
41
  toAsync(): SyncWrapper;
37
42
  }
38
43
  interface AsyncDriver {
39
44
  <T>(query: Cursor<T>): Promise<T>;
45
+ <T>(...queries: Array<Cursor<any>>): T;
40
46
  (strings: TemplateStringsArray, ...values: any[]): Promise<any>;
41
47
  }
42
48
  declare abstract class AsyncDriver extends DriverBase {
@@ -51,7 +57,10 @@ declare abstract class AsyncDriver extends DriverBase {
51
57
  abstract schemaInstructions(tableName: string): Promise<SchemaInstructions | undefined>;
52
58
  migrateSchema(...tables: Array<Table<any>>): Promise<unknown>;
53
59
  executeQuery<T>(query: Query<T>): Promise<T>;
60
+ all<T>(cursor: Cursor<T>): Promise<Array<T>>;
54
61
  all<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<Array<T>>;
62
+ get<T>(cursor: Cursor.SelectMultiple<T>): Promise<T | null>;
63
+ get<T>(cursor: Cursor<T>): Promise<T>;
55
64
  get<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<T>;
56
65
  transaction<T>(run: (query: AsyncDriver) => T): Promise<T>;
57
66
  }
package/dist/Driver.js CHANGED
@@ -12,25 +12,43 @@ var Callable = class extends Function {
12
12
  });
13
13
  }
14
14
  };
15
+ function isTemplateStringsArray(input) {
16
+ return Boolean(Array.isArray(input) && input.raw);
17
+ }
15
18
  var DriverBase = class extends Callable {
16
19
  constructor(formatter) {
17
20
  super((...args) => {
18
21
  const [input, ...rest] = args;
19
- if (input instanceof Cursor)
22
+ if (input instanceof Cursor && rest.length === 0)
20
23
  return this.executeQuery(input.query());
21
- return this.executeTemplate(void 0, input, ...rest);
24
+ if (isTemplateStringsArray(input))
25
+ return this.executeTemplate(void 0, input, ...rest);
26
+ return this.executeQuery(
27
+ Query.Batch({
28
+ queries: args.filter((arg) => arg instanceof Cursor).map((arg) => arg.query())
29
+ })
30
+ );
22
31
  });
23
32
  this.formatter = formatter;
24
33
  }
25
- all(strings, ...params) {
26
- return this.executeTemplate("rows", strings, ...params);
34
+ all(...args) {
35
+ const [input, ...rest] = args;
36
+ if (input instanceof Cursor)
37
+ return this.executeQuery(input.query());
38
+ return this.executeTemplate("rows", input, ...rest);
39
+ }
40
+ get(...args) {
41
+ const [input, ...rest] = args;
42
+ if (input instanceof Cursor.SelectMultiple)
43
+ return this.executeQuery(input.first().query());
44
+ if (input instanceof Cursor)
45
+ return this.executeQuery(input.query());
46
+ return this.executeTemplate("row", input, ...rest);
27
47
  }
28
- get(strings, ...params) {
29
- return this.executeTemplate("row", strings, ...params);
48
+ sql(strings, ...params) {
49
+ return new Cursor(Query.Raw({ strings, params }));
30
50
  }
31
51
  executeTemplate(expectedReturn, strings, ...params) {
32
- if (strings.some((chunk) => chunk.includes("?")))
33
- throw new TypeError("SQL injection hazard");
34
52
  return this.executeQuery(Query.Raw({ strings, params, expectedReturn }));
35
53
  }
36
54
  };
@@ -87,11 +105,11 @@ var SyncDriver = class extends DriverBase {
87
105
  }
88
106
  }
89
107
  }
90
- all(strings, ...params) {
91
- return super.all(strings, ...params);
108
+ all(...args) {
109
+ return super.all(...args);
92
110
  }
93
- get(strings, ...params) {
94
- return super.get(strings, ...params);
111
+ get(...args) {
112
+ return super.get(...args);
95
113
  }
96
114
  transaction(run) {
97
115
  const id = `t${this.transactionId++}`;
@@ -170,11 +188,11 @@ var AsyncDriver = class extends DriverBase {
170
188
  }
171
189
  }
172
190
  }
173
- all(strings, ...params) {
174
- return super.all(strings, ...params);
191
+ all(...args) {
192
+ return super.all(...args);
175
193
  }
176
- get(strings, ...params) {
177
- return super.get(strings, ...params);
194
+ get(...args) {
195
+ return super.get(...args);
178
196
  }
179
197
  async transaction(run) {
180
198
  const id = `t${this.transactionId++}`;
package/dist/Formatter.js CHANGED
@@ -130,7 +130,7 @@ var Formatter = class {
130
130
  ).add(this.formatWhere(query.where, ctx));
131
131
  }
132
132
  formatDropIndex(query, ctx) {
133
- return raw("drop index").addIf(query.ifExists, "if exists").addIdentifier(query.index.name);
133
+ return raw("drop index").addIf(query.ifExists, "if exists").addIdentifier(query.name);
134
134
  }
135
135
  formatAlterTable(query, ctx) {
136
136
  let stmt = raw("alter table").addIdentifier(query.table.name);
package/dist/Query.d.ts CHANGED
@@ -68,7 +68,7 @@ export declare namespace Query {
68
68
  interface DropIndex extends QueryBase {
69
69
  type: QueryType.DropIndex;
70
70
  table: Schema;
71
- index: Index;
71
+ name: string;
72
72
  ifExists?: boolean;
73
73
  }
74
74
  function DropIndex(drop: Omit<DropIndex, 'type'>): Query.DropIndex;
package/dist/Query.js CHANGED
@@ -58,6 +58,8 @@ var Query;
58
58
  }
59
59
  Query2.Transaction = Transaction;
60
60
  function Raw(raw) {
61
+ if (raw.strings.some((chunk) => chunk.includes("?")))
62
+ throw new TypeError("SQL injection hazard");
61
63
  return { type: "Raw" /* Raw */, ...raw };
62
64
  }
63
65
  Query2.Raw = Raw;
package/dist/Schema.js CHANGED
@@ -40,13 +40,13 @@ var Schema;
40
40
  if (!localInstruction) {
41
41
  res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
42
42
  } else if (!schemaIndex) {
43
- res.push(Query.DropIndex({ table: schema, index: schemaIndex }));
43
+ res.push(Query.DropIndex({ table: schema, name: indexName }));
44
44
  } else {
45
45
  const [instruction] = formatter.formatCreateIndex(
46
46
  Query.CreateIndex({ table: schema, index: schemaIndex })
47
47
  ).compile(formatter);
48
48
  if (localInstruction !== instruction) {
49
- res.push(Query.DropIndex({ table: schema, index: schemaIndex }));
49
+ res.push(Query.DropIndex({ table: schema, name: indexName }));
50
50
  res.push(Query.CreateIndex({ table: schema, index: schemaIndex }));
51
51
  }
52
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {