@graffy/pg 0.15.24 → 0.15.25-alpha.1

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 (3) hide show
  1. package/index.cjs +34 -30
  2. package/index.mjs +43 -39
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -188,22 +188,22 @@ const aggSql = {
188
188
  };
189
189
  const getSelectCols = (table, projection = null) => {
190
190
  if (!projection)
191
- return sql__default["default"]`to_jsonb("${sql.raw(table)}")`;
191
+ return sql__default["default"]`*`;
192
192
  const sqls = [];
193
193
  for (const key in projection) {
194
194
  if (key === "$count") {
195
- sqls.push(sql__default["default"]`'$count', count(*)`);
195
+ sqls.push(sql__default["default"]`count(*) AS "$count"`);
196
196
  } else if (aggSql[key]) {
197
197
  const subSqls = [];
198
198
  for (const prop in projection[key]) {
199
199
  subSqls.push(sql__default["default"]`${prop}::text, ${aggSql[key](prop)}`);
200
200
  }
201
- sqls.push(sql__default["default"]`${key}::text, jsonb_build_object(${sql.join(subSqls, ", ")})`);
201
+ sqls.push(sql__default["default"]`jsonb_build_object(${sql.join(subSqls, ", ")}) AS "${sql.raw(key)}"`);
202
202
  } else {
203
- sqls.push(sql__default["default"]`${key}::text, "${sql.raw(key)}"`);
203
+ sqls.push(sql__default["default"]`"${sql.raw(key)}"`);
204
204
  }
205
205
  }
206
- return sql__default["default"]`jsonb_build_object(${sql.join(sqls, ", ")})`;
206
+ return sql.join(sqls, ", ");
207
207
  };
208
208
  function vertexSql(array, nullValue) {
209
209
  return sql__default["default"]`array[${sql.join(array.map((num) => num === null ? nullValue : num))}]::float8[]`;
@@ -341,19 +341,16 @@ function getSql(filter, options) {
341
341
  }
342
342
  return getNodeSql(getAst(filter));
343
343
  }
344
- const getIdMeta = ({ idCol, verDefault }) => getJsonBuildTrusted({
345
- $key: sql__default["default"]`"${sql.raw(idCol)}"`,
346
- $ver: sql.raw(verDefault)
347
- });
348
- const getArgMeta = (key, { prefix, idCol, verDefault }) => getJsonBuildTrusted({
349
- $key: key,
350
- $ref: sql__default["default"]`jsonb_build_array(${sql.join(prefix.map((k) => sql__default["default"]`${k}::text`))}, "${sql.raw(idCol)}")`,
351
- $ver: sql.raw(verDefault)
352
- });
353
- const getAggMeta = (key, { verDefault }) => getJsonBuildTrusted({
354
- $key: key,
355
- $ver: sql.raw(verDefault)
356
- });
344
+ const getIdMeta = ({ idCol, verDefault }) => sql__default["default"]`"${sql.raw(idCol)}" AS "$key", ${sql.raw(verDefault)} AS "$ver"`;
345
+ const getArgMeta = (key, { prefix, idCol, verDefault }) => sql__default["default"]`
346
+ ${key} AS "$key",
347
+ ${sql.raw(verDefault)} AS "$ver",
348
+ array[
349
+ ${sql.join(prefix.map((k) => sql__default["default"]`${k}::text`))},
350
+ "${sql.raw(idCol)}"
351
+ ]::text[] AS "$ref"
352
+ `;
353
+ const getAggMeta = (key, { verDefault }) => sql__default["default"]`${key} AS "$key", ${sql.raw(verDefault)} AS "$ver"`;
357
354
  function getArgSql(_a, options) {
358
355
  var _b = _a, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _b, rest = __objRest(_b, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
359
356
  const _a2 = rest, { $order, $group } = _a2, filter = __objRest(_a2, ["$order", "$group"]);
@@ -428,7 +425,7 @@ function selectByArgs(args, projection, options) {
428
425
  const clampedLimit = Math.min(MAX_LIMIT, limit || MAX_LIMIT);
429
426
  return sql__default["default"]`
430
427
  SELECT
431
- ${getSelectCols(table, projection)} || ${meta}
428
+ ${getSelectCols(table, projection)}, ${meta}
432
429
  FROM "${sql.raw(table)}"
433
430
  ${where.length ? sql__default["default"]`WHERE ${sql.join(where, ` AND `)}` : sql.empty}
434
431
  ${group ? sql__default["default"]`GROUP BY ${group}` : sql.empty}
@@ -440,7 +437,7 @@ function selectByIds(ids, projection, options) {
440
437
  const { table, idCol } = options;
441
438
  return sql__default["default"]`
442
439
  SELECT
443
- ${getSelectCols(table, projection)} || ${getIdMeta(options)}
440
+ ${getSelectCols(table, projection)}, ${getIdMeta(options)}
444
441
  FROM "${sql.raw(table)}"
445
442
  WHERE "${sql.raw(idCol)}" IN (${sql.join(ids)})
446
443
  `;
@@ -473,7 +470,7 @@ function patch(object, arg, options) {
473
470
  return sql__default["default"]`
474
471
  UPDATE "${sql.raw(table)}" SET ${getUpdates(row, options)}
475
472
  WHERE ${where}
476
- RETURNING (${getSelectCols(table)} || ${meta})`;
473
+ RETURNING ${getSelectCols(table)}, ${meta}`;
477
474
  }
478
475
  function put(object, arg, options) {
479
476
  const { idCol, table } = options;
@@ -490,7 +487,7 @@ function put(object, arg, options) {
490
487
  return sql__default["default"]`
491
488
  INSERT INTO "${sql.raw(table)}" (${cols}) VALUES (${vals})
492
489
  ON CONFLICT (${conflictTarget}) DO UPDATE SET (${cols}) = (${vals})
493
- RETURNING (${getSelectCols(table)} || ${meta})`;
490
+ RETURNING ${getSelectCols(table)}, ${meta}`;
494
491
  }
495
492
  function del(arg, options) {
496
493
  const { table } = options;
@@ -498,7 +495,7 @@ function del(arg, options) {
498
495
  return sql__default["default"]`
499
496
  DELETE FROM "${sql.raw(table)}"
500
497
  WHERE ${where}
501
- RETURNING (${getJsonBuildTrusted({ $key: arg })})`;
498
+ RETURNING ${arg} "$key"`;
502
499
  }
503
500
  const log = debug__default["default"]("graffy:pg:db");
504
501
  class Db {
@@ -510,9 +507,16 @@ class Db {
510
507
  }
511
508
  }
512
509
  async query(sql2) {
513
- sql2.rowMode = "array";
514
510
  log("Making SQL query: " + sql2.text, sql2.values);
515
511
  try {
512
+ sql2.types = {
513
+ getTypeParser: (oid, format) => {
514
+ if (oid === pg$1.types.builtins.INT8) {
515
+ return (value) => parseInt(value, 10);
516
+ }
517
+ return pg$1.types.getTypeParser(oid, format);
518
+ }
519
+ };
516
520
  return await this.client.query(sql2);
517
521
  } catch (e) {
518
522
  const message = [
@@ -527,7 +531,7 @@ class Db {
527
531
  }
528
532
  }
529
533
  async readSql(sql2) {
530
- const result = (await this.query(sql2)).rows.flat();
534
+ const result = (await this.query(sql2)).rows;
531
535
  log("Read result", result);
532
536
  return result;
533
537
  }
@@ -537,7 +541,7 @@ class Db {
537
541
  if (!res.rowCount) {
538
542
  throw Error("pg.nothing_written " + sql2.text + " with " + sql2.values);
539
543
  }
540
- return res.rows[0][0];
544
+ return res.rows[0];
541
545
  }
542
546
  async ensureSchema(tableOptions) {
543
547
  if (tableOptions.schema)
@@ -548,13 +552,13 @@ class Db {
548
552
  FROM information_schema.tables
549
553
  WHERE table_name = ${table}
550
554
  ORDER BY array_position(current_schemas(false)::text[], table_schema::text) ASC
551
- LIMIT 1`)).rows[0][0];
555
+ LIMIT 1`)).rows[0].table_schema;
552
556
  const types = (await this.query(sql__default["default"]`
553
- SELECT jsonb_object_agg(column_name, udt_name)
557
+ SELECT jsonb_object_agg(column_name, udt_name) AS column_types
554
558
  FROM information_schema.columns
555
559
  WHERE
556
560
  table_name = ${table} AND
557
- table_schema = ${tableSchema}`)).rows[0][0];
561
+ table_schema = ${tableSchema}`)).rows[0].column_types;
558
562
  if (!types)
559
563
  throw Error(`pg.missing_table ${table}`);
560
564
  const verDefault = (await this.query(sql__default["default"]`
@@ -563,7 +567,7 @@ class Db {
563
567
  WHERE
564
568
  table_name = ${table} AND
565
569
  table_schema = ${tableSchema} AND
566
- column_name = ${verCol}`)).rows[0][0];
570
+ column_name = ${verCol}`)).rows[0].column_default;
567
571
  if (!verDefault) {
568
572
  throw Error(`pg.verCol_without_default ${verCol}`);
569
573
  }
package/index.mjs CHANGED
@@ -27,7 +27,7 @@ var __objRest = (source, exclude) => {
27
27
  return target;
28
28
  };
29
29
  import { isEmpty, encodePath, isPlainObject, unwrap, decodeArgs, decodeQuery, finalize, wrap, isRange, decodeGraph, mergeObject, merge, encodeGraph, wrapObject, remove } from "@graffy/common";
30
- import { Pool, Client } from "pg";
30
+ import { Pool, Client, types } from "pg";
31
31
  import sql, { join, raw, Sql, empty } from "sql-template-tag";
32
32
  import debug from "debug";
33
33
  const valid = {
@@ -180,22 +180,22 @@ const aggSql = {
180
180
  };
181
181
  const getSelectCols = (table, projection = null) => {
182
182
  if (!projection)
183
- return sql`to_jsonb("${raw(table)}")`;
183
+ return sql`*`;
184
184
  const sqls = [];
185
185
  for (const key in projection) {
186
186
  if (key === "$count") {
187
- sqls.push(sql`'$count', count(*)`);
187
+ sqls.push(sql`count(*) AS "$count"`);
188
188
  } else if (aggSql[key]) {
189
189
  const subSqls = [];
190
190
  for (const prop in projection[key]) {
191
191
  subSqls.push(sql`${prop}::text, ${aggSql[key](prop)}`);
192
192
  }
193
- sqls.push(sql`${key}::text, jsonb_build_object(${join(subSqls, ", ")})`);
193
+ sqls.push(sql`jsonb_build_object(${join(subSqls, ", ")}) AS "${raw(key)}"`);
194
194
  } else {
195
- sqls.push(sql`${key}::text, "${raw(key)}"`);
195
+ sqls.push(sql`"${raw(key)}"`);
196
196
  }
197
197
  }
198
- return sql`jsonb_build_object(${join(sqls, ", ")})`;
198
+ return join(sqls, ", ");
199
199
  };
200
200
  function vertexSql(array, nullValue) {
201
201
  return sql`array[${join(array.map((num) => num === null ? nullValue : num))}]::float8[]`;
@@ -316,10 +316,10 @@ function getSql(filter, options) {
316
316
  return sql`${opSql[op]} (${getNodeSql(ast[1])})`;
317
317
  }
318
318
  const [prefix, ...suffix] = encodePath(ast[1]);
319
- const { types } = options.schema;
320
- if (!types[prefix])
319
+ const { types: types2 } = options.schema;
320
+ if (!types2[prefix])
321
321
  throw Error("pg.no_column " + prefix);
322
- if (types[prefix] === "jsonb") {
322
+ if (types2[prefix] === "jsonb") {
323
323
  const [lhs, textLhs] = suffix.length ? [
324
324
  sql`"${raw(prefix)}" #> ${suffix}`,
325
325
  sql`"${raw(prefix)}" #>> ${suffix}`
@@ -328,24 +328,21 @@ function getSql(filter, options) {
328
328
  } else {
329
329
  if (suffix.length)
330
330
  throw Error("pg.lookup_not_jsonb " + prefix);
331
- return getBinarySql(sql`"${raw(prefix)}"`, types[prefix], op, ast[2]);
331
+ return getBinarySql(sql`"${raw(prefix)}"`, types2[prefix], op, ast[2]);
332
332
  }
333
333
  }
334
334
  return getNodeSql(getAst(filter));
335
335
  }
336
- const getIdMeta = ({ idCol, verDefault }) => getJsonBuildTrusted({
337
- $key: sql`"${raw(idCol)}"`,
338
- $ver: raw(verDefault)
339
- });
340
- const getArgMeta = (key, { prefix, idCol, verDefault }) => getJsonBuildTrusted({
341
- $key: key,
342
- $ref: sql`jsonb_build_array(${join(prefix.map((k) => sql`${k}::text`))}, "${raw(idCol)}")`,
343
- $ver: raw(verDefault)
344
- });
345
- const getAggMeta = (key, { verDefault }) => getJsonBuildTrusted({
346
- $key: key,
347
- $ver: raw(verDefault)
348
- });
336
+ const getIdMeta = ({ idCol, verDefault }) => sql`"${raw(idCol)}" AS "$key", ${raw(verDefault)} AS "$ver"`;
337
+ const getArgMeta = (key, { prefix, idCol, verDefault }) => sql`
338
+ ${key} AS "$key",
339
+ ${raw(verDefault)} AS "$ver",
340
+ array[
341
+ ${join(prefix.map((k) => sql`${k}::text`))},
342
+ "${raw(idCol)}"
343
+ ]::text[] AS "$ref"
344
+ `;
345
+ const getAggMeta = (key, { verDefault }) => sql`${key} AS "$key", ${raw(verDefault)} AS "$ver"`;
349
346
  function getArgSql(_a, options) {
350
347
  var _b = _a, { $first, $last, $after, $before, $since, $until, $all, $cursor: _ } = _b, rest = __objRest(_b, ["$first", "$last", "$after", "$before", "$since", "$until", "$all", "$cursor"]);
351
348
  const _a2 = rest, { $order, $group } = _a2, filter = __objRest(_a2, ["$order", "$group"]);
@@ -420,7 +417,7 @@ function selectByArgs(args, projection, options) {
420
417
  const clampedLimit = Math.min(MAX_LIMIT, limit || MAX_LIMIT);
421
418
  return sql`
422
419
  SELECT
423
- ${getSelectCols(table, projection)} || ${meta}
420
+ ${getSelectCols(table, projection)}, ${meta}
424
421
  FROM "${raw(table)}"
425
422
  ${where.length ? sql`WHERE ${join(where, ` AND `)}` : empty}
426
423
  ${group ? sql`GROUP BY ${group}` : empty}
@@ -432,7 +429,7 @@ function selectByIds(ids, projection, options) {
432
429
  const { table, idCol } = options;
433
430
  return sql`
434
431
  SELECT
435
- ${getSelectCols(table, projection)} || ${getIdMeta(options)}
432
+ ${getSelectCols(table, projection)}, ${getIdMeta(options)}
436
433
  FROM "${raw(table)}"
437
434
  WHERE "${raw(idCol)}" IN (${join(ids)})
438
435
  `;
@@ -465,7 +462,7 @@ function patch(object, arg, options) {
465
462
  return sql`
466
463
  UPDATE "${raw(table)}" SET ${getUpdates(row, options)}
467
464
  WHERE ${where}
468
- RETURNING (${getSelectCols(table)} || ${meta})`;
465
+ RETURNING ${getSelectCols(table)}, ${meta}`;
469
466
  }
470
467
  function put(object, arg, options) {
471
468
  const { idCol, table } = options;
@@ -482,7 +479,7 @@ function put(object, arg, options) {
482
479
  return sql`
483
480
  INSERT INTO "${raw(table)}" (${cols}) VALUES (${vals})
484
481
  ON CONFLICT (${conflictTarget}) DO UPDATE SET (${cols}) = (${vals})
485
- RETURNING (${getSelectCols(table)} || ${meta})`;
482
+ RETURNING ${getSelectCols(table)}, ${meta}`;
486
483
  }
487
484
  function del(arg, options) {
488
485
  const { table } = options;
@@ -490,7 +487,7 @@ function del(arg, options) {
490
487
  return sql`
491
488
  DELETE FROM "${raw(table)}"
492
489
  WHERE ${where}
493
- RETURNING (${getJsonBuildTrusted({ $key: arg })})`;
490
+ RETURNING ${arg} "$key"`;
494
491
  }
495
492
  const log = debug("graffy:pg:db");
496
493
  class Db {
@@ -502,9 +499,16 @@ class Db {
502
499
  }
503
500
  }
504
501
  async query(sql2) {
505
- sql2.rowMode = "array";
506
502
  log("Making SQL query: " + sql2.text, sql2.values);
507
503
  try {
504
+ sql2.types = {
505
+ getTypeParser: (oid, format) => {
506
+ if (oid === types.builtins.INT8) {
507
+ return (value) => parseInt(value, 10);
508
+ }
509
+ return types.getTypeParser(oid, format);
510
+ }
511
+ };
508
512
  return await this.client.query(sql2);
509
513
  } catch (e) {
510
514
  const message = [
@@ -519,7 +523,7 @@ class Db {
519
523
  }
520
524
  }
521
525
  async readSql(sql2) {
522
- const result = (await this.query(sql2)).rows.flat();
526
+ const result = (await this.query(sql2)).rows;
523
527
  log("Read result", result);
524
528
  return result;
525
529
  }
@@ -529,7 +533,7 @@ class Db {
529
533
  if (!res.rowCount) {
530
534
  throw Error("pg.nothing_written " + sql2.text + " with " + sql2.values);
531
535
  }
532
- return res.rows[0][0];
536
+ return res.rows[0];
533
537
  }
534
538
  async ensureSchema(tableOptions) {
535
539
  if (tableOptions.schema)
@@ -540,14 +544,14 @@ class Db {
540
544
  FROM information_schema.tables
541
545
  WHERE table_name = ${table}
542
546
  ORDER BY array_position(current_schemas(false)::text[], table_schema::text) ASC
543
- LIMIT 1`)).rows[0][0];
544
- const types = (await this.query(sql`
545
- SELECT jsonb_object_agg(column_name, udt_name)
547
+ LIMIT 1`)).rows[0].table_schema;
548
+ const types2 = (await this.query(sql`
549
+ SELECT jsonb_object_agg(column_name, udt_name) AS column_types
546
550
  FROM information_schema.columns
547
551
  WHERE
548
552
  table_name = ${table} AND
549
- table_schema = ${tableSchema}`)).rows[0][0];
550
- if (!types)
553
+ table_schema = ${tableSchema}`)).rows[0].column_types;
554
+ if (!types2)
551
555
  throw Error(`pg.missing_table ${table}`);
552
556
  const verDefault = (await this.query(sql`
553
557
  SELECT column_default
@@ -555,12 +559,12 @@ class Db {
555
559
  WHERE
556
560
  table_name = ${table} AND
557
561
  table_schema = ${tableSchema} AND
558
- column_name = ${verCol}`)).rows[0][0];
562
+ column_name = ${verCol}`)).rows[0].column_default;
559
563
  if (!verDefault) {
560
564
  throw Error(`pg.verCol_without_default ${verCol}`);
561
565
  }
562
- log("ensureSchema", types);
563
- tableOptions.schema = { types };
566
+ log("ensureSchema", types2);
567
+ tableOptions.schema = { types: types2 };
564
568
  tableOptions.verDefault = verDefault;
565
569
  }
566
570
  async read(rootQuery, tableOptions) {
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@graffy/pg",
3
3
  "description": "The standard Postgres module for Graffy. Each instance this module mounts a Postgres table as a Graffy subtree.",
4
4
  "author": "aravind (https://github.com/aravindet)",
5
- "version": "0.15.24",
5
+ "version": "0.15.25-alpha.1",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,7 +16,7 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.15.24",
19
+ "@graffy/common": "0.15.25-alpha.1",
20
20
  "sql-template-tag": "^4.1.0",
21
21
  "debug": "^4.3.3"
22
22
  },