@graffy/pg 0.15.25 → 0.16.0-alpha.10

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 +14 -12
  2. package/index.mjs +15 -13
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -210,11 +210,11 @@ const getJsonBuildValue = (value) => {
210
210
  return sql`${JSON.stringify(stripAttributes(value))}::jsonb`;
211
211
  };
212
212
  const lookup = (prop) => {
213
- const [prefix, ...suffix] = common.encodePath(prop);
213
+ const [prefix, ...suffix] = prop.split(".");
214
214
  return suffix.length ? sql`"${raw(prefix)}" #> ${suffix}` : sql`"${raw(prefix)}"`;
215
215
  };
216
216
  const lookupNumeric = (prop) => {
217
- const [prefix, ...suffix] = common.encodePath(prop);
217
+ const [prefix, ...suffix] = prop.split(".");
218
218
  return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb THEN 0 ELSE ("${raw(
219
219
  prefix
220
220
  )}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
@@ -388,7 +388,7 @@ function getSql(filter, options) {
388
388
  } else if (op === "$not") {
389
389
  return sql`${opSql[op]} (${getNodeSql(ast[1])})`;
390
390
  }
391
- const [prefix, ...suffix] = common.encodePath(ast[1]);
391
+ const [prefix, ...suffix] = ast[1].split(".");
392
392
  const { types: types2 } = options.schema;
393
393
  if (!types2[prefix])
394
394
  throw Error("pg.no_column " + prefix);
@@ -653,13 +653,14 @@ class Db {
653
653
  const idQueries = {};
654
654
  const promises = [];
655
655
  const results = [];
656
- const { prefix } = tableOptions;
656
+ const { prefix: rawPrefix } = tableOptions;
657
+ const prefix = common.encodePath(rawPrefix);
657
658
  await this.ensureSchema(tableOptions);
658
659
  const getByArgs = async (args, projection) => {
659
660
  const result = await this.readSql(
660
661
  selectByArgs(args, projection, tableOptions)
661
662
  );
662
- const wrappedGraph = common.encodeGraph(common.wrapObject(result, prefix));
663
+ const wrappedGraph = common.encodeGraph(common.wrapObject(result, rawPrefix));
663
664
  log("getByArgs", wrappedGraph);
664
665
  common.merge(results, wrappedGraph);
665
666
  };
@@ -668,7 +669,7 @@ class Db {
668
669
  selectByIds(Object.keys(idQueries), null, tableOptions)
669
670
  );
670
671
  result.forEach((object) => {
671
- const wrappedGraph = common.encodeGraph(common.wrapObject(object, prefix));
672
+ const wrappedGraph = common.encodeGraph(common.wrapObject(object, rawPrefix));
672
673
  log("getByIds", wrappedGraph);
673
674
  common.merge(results, wrappedGraph);
674
675
  });
@@ -688,7 +689,7 @@ class Db {
688
689
  promises.push(getByArgs(args, projection));
689
690
  }
690
691
  } else {
691
- idQueries[node.key] = node.children;
692
+ idQueries[args] = node.children;
692
693
  }
693
694
  }
694
695
  if (!common.isEmpty(idQueries))
@@ -698,13 +699,14 @@ class Db {
698
699
  return common.finalize(results, common.wrap(query, prefix));
699
700
  }
700
701
  async write(rootChange, tableOptions) {
701
- const { prefix } = tableOptions;
702
+ const { prefix: rawPrefix } = tableOptions;
703
+ const prefix = common.encodePath(rawPrefix);
702
704
  await this.ensureSchema(tableOptions);
703
705
  const change = common.unwrap(rootChange, prefix);
704
706
  const sqls = change.map((node) => {
705
707
  const arg = common.decodeArgs(node);
706
708
  if (common.isRange(node)) {
707
- if (node.key === node.end)
709
+ if (common.cmp(node.key, node.end) === 0)
708
710
  return del(arg, tableOptions);
709
711
  throw Error("pg_write.write_range_unsupported");
710
712
  }
@@ -723,7 +725,7 @@ class Db {
723
725
  await Promise.all(
724
726
  sqls.map(
725
727
  (sql2) => this.writeSql(sql2).then((object) => {
726
- common.merge(result, common.encodeGraph(common.wrapObject(object, prefix)));
728
+ common.merge(result, common.encodeGraph(common.wrapObject(object, rawPrefix)));
727
729
  })
728
730
  )
729
731
  );
@@ -748,7 +750,7 @@ const pg = ({ table, idCol, verCol, connection, schema, verDefault }) => (store)
748
750
  const { pgClient } = options;
749
751
  const db = pgClient ? new Db(pgClient) : defaultDb;
750
752
  const readPromise = db.read(query, tableOpts);
751
- const remainingQuery = common.remove(query, prefix);
753
+ const remainingQuery = common.remove(query, common.encodePath(prefix));
752
754
  const nextPromise = next(remainingQuery);
753
755
  return Promise.all([readPromise, nextPromise]).then(
754
756
  ([readRes, nextRes]) => {
@@ -760,7 +762,7 @@ const pg = ({ table, idCol, verCol, connection, schema, verDefault }) => (store)
760
762
  const { pgClient } = options;
761
763
  const db = pgClient ? new Db(pgClient) : defaultDb;
762
764
  const writePromise = db.write(change, tableOpts);
763
- const remainingChange = common.remove(change, prefix);
765
+ const remainingChange = common.remove(change, common.encodePath(prefix));
764
766
  const nextPromise = next(remainingChange);
765
767
  return Promise.all([writePromise, nextPromise]).then(
766
768
  ([writeRes, nextRes]) => {
package/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- import { encodePath, isEmpty, isPlainObject, unwrap, decodeArgs, decodeQuery, finalize, wrap, isRange, decodeGraph, mergeObject, merge, encodeGraph, wrapObject, remove } from "@graffy/common";
1
+ import { isEmpty, isPlainObject, encodePath, unwrap, decodeArgs, decodeQuery, finalize, wrap, isRange, cmp, decodeGraph, mergeObject, merge, encodeGraph, wrapObject, remove } from "@graffy/common";
2
2
  import pg$1 from "pg";
3
3
  import debug from "debug";
4
4
  class Sql {
@@ -205,11 +205,11 @@ const getJsonBuildValue = (value) => {
205
205
  return sql`${JSON.stringify(stripAttributes(value))}::jsonb`;
206
206
  };
207
207
  const lookup = (prop) => {
208
- const [prefix, ...suffix] = encodePath(prop);
208
+ const [prefix, ...suffix] = prop.split(".");
209
209
  return suffix.length ? sql`"${raw(prefix)}" #> ${suffix}` : sql`"${raw(prefix)}"`;
210
210
  };
211
211
  const lookupNumeric = (prop) => {
212
- const [prefix, ...suffix] = encodePath(prop);
212
+ const [prefix, ...suffix] = prop.split(".");
213
213
  return suffix.length ? sql`CASE WHEN "${raw(prefix)}" #> ${suffix} = 'null'::jsonb THEN 0 ELSE ("${raw(
214
214
  prefix
215
215
  )}" #> ${suffix})::numeric END` : sql`"${raw(prefix)}"`;
@@ -383,7 +383,7 @@ function getSql(filter, options) {
383
383
  } else if (op === "$not") {
384
384
  return sql`${opSql[op]} (${getNodeSql(ast[1])})`;
385
385
  }
386
- const [prefix, ...suffix] = encodePath(ast[1]);
386
+ const [prefix, ...suffix] = ast[1].split(".");
387
387
  const { types: types2 } = options.schema;
388
388
  if (!types2[prefix])
389
389
  throw Error("pg.no_column " + prefix);
@@ -648,13 +648,14 @@ class Db {
648
648
  const idQueries = {};
649
649
  const promises = [];
650
650
  const results = [];
651
- const { prefix } = tableOptions;
651
+ const { prefix: rawPrefix } = tableOptions;
652
+ const prefix = encodePath(rawPrefix);
652
653
  await this.ensureSchema(tableOptions);
653
654
  const getByArgs = async (args, projection) => {
654
655
  const result = await this.readSql(
655
656
  selectByArgs(args, projection, tableOptions)
656
657
  );
657
- const wrappedGraph = encodeGraph(wrapObject(result, prefix));
658
+ const wrappedGraph = encodeGraph(wrapObject(result, rawPrefix));
658
659
  log("getByArgs", wrappedGraph);
659
660
  merge(results, wrappedGraph);
660
661
  };
@@ -663,7 +664,7 @@ class Db {
663
664
  selectByIds(Object.keys(idQueries), null, tableOptions)
664
665
  );
665
666
  result.forEach((object) => {
666
- const wrappedGraph = encodeGraph(wrapObject(object, prefix));
667
+ const wrappedGraph = encodeGraph(wrapObject(object, rawPrefix));
667
668
  log("getByIds", wrappedGraph);
668
669
  merge(results, wrappedGraph);
669
670
  });
@@ -683,7 +684,7 @@ class Db {
683
684
  promises.push(getByArgs(args, projection));
684
685
  }
685
686
  } else {
686
- idQueries[node.key] = node.children;
687
+ idQueries[args] = node.children;
687
688
  }
688
689
  }
689
690
  if (!isEmpty(idQueries))
@@ -693,13 +694,14 @@ class Db {
693
694
  return finalize(results, wrap(query, prefix));
694
695
  }
695
696
  async write(rootChange, tableOptions) {
696
- const { prefix } = tableOptions;
697
+ const { prefix: rawPrefix } = tableOptions;
698
+ const prefix = encodePath(rawPrefix);
697
699
  await this.ensureSchema(tableOptions);
698
700
  const change = unwrap(rootChange, prefix);
699
701
  const sqls = change.map((node) => {
700
702
  const arg = decodeArgs(node);
701
703
  if (isRange(node)) {
702
- if (node.key === node.end)
704
+ if (cmp(node.key, node.end) === 0)
703
705
  return del(arg, tableOptions);
704
706
  throw Error("pg_write.write_range_unsupported");
705
707
  }
@@ -718,7 +720,7 @@ class Db {
718
720
  await Promise.all(
719
721
  sqls.map(
720
722
  (sql2) => this.writeSql(sql2).then((object) => {
721
- merge(result, encodeGraph(wrapObject(object, prefix)));
723
+ merge(result, encodeGraph(wrapObject(object, rawPrefix)));
722
724
  })
723
725
  )
724
726
  );
@@ -743,7 +745,7 @@ const pg = ({ table, idCol, verCol, connection, schema, verDefault }) => (store)
743
745
  const { pgClient } = options;
744
746
  const db = pgClient ? new Db(pgClient) : defaultDb;
745
747
  const readPromise = db.read(query, tableOpts);
746
- const remainingQuery = remove(query, prefix);
748
+ const remainingQuery = remove(query, encodePath(prefix));
747
749
  const nextPromise = next(remainingQuery);
748
750
  return Promise.all([readPromise, nextPromise]).then(
749
751
  ([readRes, nextRes]) => {
@@ -755,7 +757,7 @@ const pg = ({ table, idCol, verCol, connection, schema, verDefault }) => (store)
755
757
  const { pgClient } = options;
756
758
  const db = pgClient ? new Db(pgClient) : defaultDb;
757
759
  const writePromise = db.write(change, tableOpts);
758
- const remainingChange = remove(change, prefix);
760
+ const remainingChange = remove(change, encodePath(prefix));
759
761
  const nextPromise = next(remainingChange);
760
762
  return Promise.all([writePromise, nextPromise]).then(
761
763
  ([writeRes, nextRes]) => {
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.25",
5
+ "version": "0.16.0-alpha.10",
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.25",
19
+ "@graffy/common": "0.16.0-alpha.10",
20
20
  "debug": "^4.3.3"
21
21
  },
22
22
  "peerDependencies": {