rado 0.1.39 → 0.1.40

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.
@@ -6,6 +6,9 @@ var PreparedStatement = class {
6
6
  constructor(stmt) {
7
7
  this.stmt = stmt;
8
8
  }
9
+ iterate(params = []) {
10
+ return this.stmt.iterate(...params);
11
+ }
9
12
  all(params = []) {
10
13
  return this.stmt.all(...params);
11
14
  }
@@ -7,12 +7,13 @@ var PreparedStatement = class {
7
7
  this.db = db;
8
8
  this.stmt = stmt;
9
9
  }
10
- all(params) {
10
+ *iterate(params) {
11
11
  this.stmt.bind(params);
12
- const res = [];
13
12
  while (this.stmt.step())
14
- res.push(this.stmt.getAsObject());
15
- return res;
13
+ yield this.stmt.getAsObject();
14
+ }
15
+ all(params) {
16
+ return Array.from(this.iterate(params));
16
17
  }
17
18
  run(params) {
18
19
  this.stmt.run(params);
@@ -6,6 +6,37 @@ var PreparedStatement = class {
6
6
  constructor(stmt) {
7
7
  this.stmt = stmt;
8
8
  }
9
+ async *iterate(params) {
10
+ let rows = [], done = false, error;
11
+ let resolve;
12
+ let promise = new Promise((r) => resolve = r);
13
+ this.stmt.each(
14
+ params,
15
+ (err, row) => {
16
+ if (err)
17
+ error = err;
18
+ else
19
+ rows.push(row);
20
+ resolve();
21
+ },
22
+ () => {
23
+ done = true;
24
+ resolve();
25
+ }
26
+ );
27
+ while (true) {
28
+ const mustWait = !(rows.length || error || done);
29
+ if (mustWait)
30
+ await promise;
31
+ promise = new Promise((r) => resolve = r);
32
+ if (error)
33
+ throw error;
34
+ if (rows.length)
35
+ yield rows.shift();
36
+ if (done)
37
+ return;
38
+ }
39
+ }
9
40
  all(params) {
10
41
  return new Promise((resolve, reject) => {
11
42
  this.stmt.all(params, (err, rows) => {
@@ -41,6 +41,7 @@ declare abstract class SyncDriver extends DriverBase {
41
41
  get<T>(cursor: Cursor.SelectMultiple<T>): T | null;
42
42
  get<T>(cursor: Cursor<T>): T;
43
43
  get<T>(strings: TemplateStringsArray, ...params: Array<any>): T;
44
+ iterate<T>(cursor: Cursor.SelectMultiple<T>): Iterable<T>;
44
45
  transaction<T>(run: (query: SyncDriver) => T): T;
45
46
  toAsync(): SyncWrapper;
46
47
  }
@@ -48,6 +49,7 @@ interface SyncPreparedStatement {
48
49
  run(params?: Array<any>): {
49
50
  rowsAffected: number;
50
51
  };
52
+ iterate<T>(params?: Array<any>): Iterable<T>;
51
53
  all<T>(params?: Array<any>): Array<T>;
52
54
  get<T>(params?: Array<any>): T;
53
55
  execute(params?: Array<any>): void;
@@ -71,6 +73,7 @@ declare abstract class AsyncDriver extends DriverBase {
71
73
  get<T>(cursor: Cursor.SelectMultiple<T>): Promise<T | null>;
72
74
  get<T>(cursor: Cursor<T>): Promise<T>;
73
75
  get<T>(strings: TemplateStringsArray, ...params: Array<any>): Promise<T>;
76
+ iterate<T>(cursor: Cursor.SelectMultiple<T>): AsyncIterable<T>;
74
77
  transaction<T>(run: (query: AsyncDriver) => T): Promise<T>;
75
78
  }
76
79
  declare class SyncWrapper extends AsyncDriver {
@@ -86,6 +89,7 @@ interface AsyncPreparedStatement {
86
89
  run(params?: Array<any>): Promise<{
87
90
  rowsAffected: number;
88
91
  }>;
92
+ iterate<T>(params?: Array<any>): AsyncIterable<T>;
89
93
  all<T>(params?: Array<any>): Promise<Array<T>>;
90
94
  get<T>(params?: Array<any>): Promise<T>;
91
95
  execute(params?: Array<any>): Promise<void>;
@@ -141,6 +141,12 @@ var SyncDriver = class extends DriverBase {
141
141
  get(...args) {
142
142
  return super.get(...args);
143
143
  }
144
+ *iterate(cursor) {
145
+ const stmt = this.prepareStatement(this.formatter.compile(cursor.query()));
146
+ for (const row of stmt.iterate()) {
147
+ yield JSON.parse(row.result).result;
148
+ }
149
+ }
144
150
  transaction(run) {
145
151
  const id = `t${this.transactionId++}`;
146
152
  this.executeQuery(
@@ -240,6 +246,12 @@ var AsyncDriver = class extends DriverBase {
240
246
  get(...args) {
241
247
  return super.get(...args);
242
248
  }
249
+ async *iterate(cursor) {
250
+ const stmt = this.prepareStatement(this.formatter.compile(cursor.query()));
251
+ for await (const row of stmt.iterate()) {
252
+ yield JSON.parse(row.result).result;
253
+ }
254
+ }
243
255
  async transaction(run) {
244
256
  const id = `t${this.transactionId++}`;
245
257
  const [connection, release] = this.isolate();
@@ -266,6 +278,10 @@ var SyncPreparedStatementWrapper = class {
266
278
  constructor(stmt) {
267
279
  this.stmt = stmt;
268
280
  }
281
+ async *iterate(params) {
282
+ for (const row of this.stmt.iterate(params))
283
+ yield row;
284
+ }
269
285
  async run(params) {
270
286
  return this.stmt.run(params);
271
287
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rado",
3
- "version": "0.1.39",
3
+ "version": "0.1.40",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "exports": {