@petradb/knex 1.2.0 → 1.2.2

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/README.md CHANGED
@@ -5,7 +5,7 @@ Knex.js dialect for [PetraDB](https://petradb.dev) — use Knex's query builder,
5
5
  ## Install
6
6
 
7
7
  ```bash
8
- npm install @petradb/knex @petradb/engine knex
8
+ npm install @petradb/knex knex
9
9
  ```
10
10
 
11
11
  ## Usage
package/dist/index.d.ts CHANGED
@@ -57,6 +57,10 @@ declare class QueryCompiler_PetraDB extends QueryCompilerBase {
57
57
  sql: string;
58
58
  returning: any;
59
59
  };
60
+ columnInfo(): {
61
+ sql: string;
62
+ output(resp: any): any;
63
+ };
60
64
  _returning(value: any): string;
61
65
  }
62
66
  declare class PetraDBClient extends ClientBase {
@@ -71,6 +75,7 @@ declare class PetraDBClient extends ClientBase {
71
75
  destroyRawConnection(connection: any): Promise<void>;
72
76
  validateConnection(connection: any): boolean;
73
77
  positionBindings(sql: string): string;
78
+ _sanitizeBindings(bindings: any[]): any[];
74
79
  _query(connection: any, obj: any): Promise<any>;
75
80
  processResponse(obj: any, runner: any): any;
76
81
  poolDefaults(): any;
package/dist/index.js CHANGED
@@ -263,6 +263,31 @@ class QueryCompiler_PetraDB extends QueryCompilerBase {
263
263
  returning,
264
264
  };
265
265
  }
266
+ columnInfo() {
267
+ const column = this.single.columnInfo;
268
+ const table = this.client.customWrapIdentifier(this.single.table, (v) => v);
269
+ return {
270
+ sql: `SHOW COLUMNS FROM "${table}"`,
271
+ output(resp) {
272
+ const rows = resp.rows || resp;
273
+ const out = rows.reduce((columns, val) => {
274
+ let type = val.type || "";
275
+ const maxLengthMatch = type.match(/.*\((\d+)\)/);
276
+ const maxLength = maxLengthMatch ? maxLengthMatch[1] : null;
277
+ if (maxLength)
278
+ type = type.split("(")[0];
279
+ columns[val.name] = {
280
+ type: type.toLowerCase(),
281
+ maxLength,
282
+ nullable: !val.required,
283
+ defaultValue: val.default_value || null,
284
+ };
285
+ return columns;
286
+ }, {});
287
+ return (column && out[column]) || out;
288
+ },
289
+ };
290
+ }
266
291
  _returning(value) {
267
292
  return value ? ` returning ${this.formatter.columnize(value)}` : "";
268
293
  }
@@ -322,11 +347,18 @@ class PetraDBClient extends ClientBase {
322
347
  }
323
348
  });
324
349
  }
350
+ _sanitizeBindings(bindings) {
351
+ return bindings.map((val) => {
352
+ if (val instanceof Date)
353
+ return val.toISOString();
354
+ return val;
355
+ });
356
+ }
325
357
  async _query(connection, obj) {
326
358
  if (!obj.sql)
327
359
  throw new Error("The query is empty");
328
360
  const sql = obj.sql;
329
- const bindings = obj.bindings || [];
361
+ const bindings = this._sanitizeBindings(obj.bindings || []);
330
362
  let results;
331
363
  if (bindings.length > 0) {
332
364
  const stmt = connection.prepare(sql);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@petradb/knex",
3
- "version": "1.2.0",
3
+ "version": "1.2.2",
4
4
  "description": "Knex.js dialect for PetraDB",
5
5
  "type": "module",
6
6
  "exports": {