@graffy/pg 0.16.21-alpha.1 → 0.17.0

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/index.cjs CHANGED
@@ -3,6 +3,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
3
  const common = require("@graffy/common");
4
4
  const debug = require("debug");
5
5
  const pg$1 = require("pg");
6
+ const sqlFormatter = require("sql-formatter");
6
7
  class Sql {
7
8
  constructor(rawStrings, rawValues) {
8
9
  if (rawStrings.length - 1 !== rawValues.length) {
@@ -84,14 +85,14 @@ function formatSql(sql2) {
84
85
  const values = sql2.values.slice(0);
85
86
  const output = [];
86
87
  while (strings.length) {
87
- output.push(strings.shift().replace(/\s+/g, " "));
88
+ output.push(strings.shift());
88
89
  if (!values.length) break;
89
90
  const value = values.shift();
90
91
  output.push(
91
92
  typeof value === "number" ? value.toString() : typeof value === "object" ? `'${JSON.stringify(value)}'` : `'${value}'`
92
93
  );
93
94
  }
94
- return output.join("").trim();
95
+ return sqlFormatter.format(output.join(""), { language: "postgresql" });
95
96
  }
96
97
  const getJsonBuildTrusted = (variadic) => {
97
98
  const args = join(
@@ -171,23 +172,23 @@ const getSelectCols = (options, projection = null) => {
171
172
  sqls.push(
172
173
  sql`jsonb_build_object(${join(subSqls, ", ")}) AS "${raw(key)}"`
173
174
  );
174
- }
175
- if (key[0] === "$") continue;
176
- if (typeof projection[key] === "object") {
177
- const optimisedJsonBuild = getOptimisedJsonBuild(
178
- projection[key],
179
- [],
180
- key,
181
- options
182
- );
183
- sqls.push(
184
- sql`jsonb_build_object(${join(optimisedJsonBuild, ", ")}) AS "${raw(key)}"`
185
- );
186
175
  } else {
187
- sqls.push(sql`"${raw(key)}"`);
176
+ if (typeof projection[key] === "object") {
177
+ const optimisedJsonBuild = getOptimisedJsonBuild(
178
+ projection[key],
179
+ [],
180
+ key,
181
+ options
182
+ );
183
+ sqls.push(
184
+ sql`jsonb_build_object(${join(optimisedJsonBuild, ", ")}) AS "${raw(key)}"`
185
+ );
186
+ } else {
187
+ sqls.push(sql`"${raw(key)}"`);
188
+ }
188
189
  }
189
190
  }
190
- return sqls.length ? join(sqls, ", ") : sql`TRUE AS "$"`;
191
+ return join(sqls, ", ");
191
192
  };
192
193
  function vertexSql(array, nullValue) {
193
194
  return sql`array[${join(
@@ -826,9 +827,9 @@ class Db {
826
827
  common.merge(results, wrappedGraph);
827
828
  };
828
829
  const explainArgs = async (args, projection) => {
829
- const { $analyze, ...qArgs } = args.$explain;
830
+ const { analyze, $explain: qArgs } = args;
830
831
  const qSql = selectByArgs(qArgs, null, tableOptions);
831
- const sql$1 = sql`EXPLAIN (${$analyze ? sql`ANALYZE, BUFFERS, TIMING, ` : sql``}COSTS, VERBOSE, FORMAT JSON) ${qSql}`;
832
+ const sql$1 = sql`EXPLAIN (${analyze ? sql`ANALYZE, BUFFERS, TIMING, ` : sql``}COSTS, VERBOSE, FORMAT JSON) ${qSql}`;
832
833
  const result = await this.readSql(sql$1, tableOptions);
833
834
  const wrappedGraph = common.encodeGraph(
834
835
  common.wrapObject(
package/index.mjs CHANGED
@@ -1,6 +1,7 @@
1
1
  import { isEmpty, isPlainObject, encodePath, unwrap, decodeArgs, decodeQuery, finalize, wrap, isRange, cmp, decodeGraph, mergeObject, wrapObject, merge, encodeGraph, remove } from "@graffy/common";
2
2
  import debug from "debug";
3
3
  import pg$1 from "pg";
4
+ import { format } from "sql-formatter";
4
5
  class Sql {
5
6
  constructor(rawStrings, rawValues) {
6
7
  if (rawStrings.length - 1 !== rawValues.length) {
@@ -82,14 +83,14 @@ function formatSql(sql2) {
82
83
  const values = sql2.values.slice(0);
83
84
  const output = [];
84
85
  while (strings.length) {
85
- output.push(strings.shift().replace(/\s+/g, " "));
86
+ output.push(strings.shift());
86
87
  if (!values.length) break;
87
88
  const value = values.shift();
88
89
  output.push(
89
90
  typeof value === "number" ? value.toString() : typeof value === "object" ? `'${JSON.stringify(value)}'` : `'${value}'`
90
91
  );
91
92
  }
92
- return output.join("").trim();
93
+ return format(output.join(""), { language: "postgresql" });
93
94
  }
94
95
  const getJsonBuildTrusted = (variadic) => {
95
96
  const args = join(
@@ -169,23 +170,23 @@ const getSelectCols = (options, projection = null) => {
169
170
  sqls.push(
170
171
  sql`jsonb_build_object(${join(subSqls, ", ")}) AS "${raw(key)}"`
171
172
  );
172
- }
173
- if (key[0] === "$") continue;
174
- if (typeof projection[key] === "object") {
175
- const optimisedJsonBuild = getOptimisedJsonBuild(
176
- projection[key],
177
- [],
178
- key,
179
- options
180
- );
181
- sqls.push(
182
- sql`jsonb_build_object(${join(optimisedJsonBuild, ", ")}) AS "${raw(key)}"`
183
- );
184
173
  } else {
185
- sqls.push(sql`"${raw(key)}"`);
174
+ if (typeof projection[key] === "object") {
175
+ const optimisedJsonBuild = getOptimisedJsonBuild(
176
+ projection[key],
177
+ [],
178
+ key,
179
+ options
180
+ );
181
+ sqls.push(
182
+ sql`jsonb_build_object(${join(optimisedJsonBuild, ", ")}) AS "${raw(key)}"`
183
+ );
184
+ } else {
185
+ sqls.push(sql`"${raw(key)}"`);
186
+ }
186
187
  }
187
188
  }
188
- return sqls.length ? join(sqls, ", ") : sql`TRUE AS "$"`;
189
+ return join(sqls, ", ");
189
190
  };
190
191
  function vertexSql(array, nullValue) {
191
192
  return sql`array[${join(
@@ -722,7 +723,7 @@ class Db {
722
723
  const cubeOid = Number.parseInt(((_b = (_a = tableOptions == null ? void 0 : tableOptions.schema) == null ? void 0 : _a.typeOids) == null ? void 0 : _b.cube) || "0") || null;
723
724
  try {
724
725
  sql2.types = {
725
- getTypeParser: (oid, format) => {
726
+ getTypeParser: (oid, format2) => {
726
727
  if (oid === types.builtins.INT8) {
727
728
  return (value) => Number.parseInt(value, 10);
728
729
  }
@@ -734,7 +735,7 @@ class Db {
734
735
  return array.length > 1 ? array : array[0];
735
736
  };
736
737
  }
737
- return types.getTypeParser(oid, format);
738
+ return types.getTypeParser(oid, format2);
738
739
  }
739
740
  };
740
741
  return await this.client.query(sql2);
@@ -824,9 +825,9 @@ class Db {
824
825
  merge(results, wrappedGraph);
825
826
  };
826
827
  const explainArgs = async (args, projection) => {
827
- const { $analyze, ...qArgs } = args.$explain;
828
+ const { analyze, $explain: qArgs } = args;
828
829
  const qSql = selectByArgs(qArgs, null, tableOptions);
829
- const sql$1 = sql`EXPLAIN (${$analyze ? sql`ANALYZE, BUFFERS, TIMING, ` : sql``}COSTS, VERBOSE, FORMAT JSON) ${qSql}`;
830
+ const sql$1 = sql`EXPLAIN (${analyze ? sql`ANALYZE, BUFFERS, TIMING, ` : sql``}COSTS, VERBOSE, FORMAT JSON) ${qSql}`;
830
831
  const result = await this.readSql(sql$1, tableOptions);
831
832
  const wrappedGraph = encodeGraph(
832
833
  wrapObject(
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.16.21-alpha.1",
5
+ "version": "0.17.0",
6
6
  "main": "./index.cjs",
7
7
  "exports": {
8
8
  "import": "./index.mjs",
@@ -16,8 +16,9 @@
16
16
  },
17
17
  "license": "Apache-2.0",
18
18
  "dependencies": {
19
- "@graffy/common": "0.16.21-alpha.1",
20
- "debug": "^4.3.3"
19
+ "@graffy/common": "0.17.0",
20
+ "debug": "^4.3.3",
21
+ "sql-formatter": "^15.6.2"
21
22
  },
22
23
  "peerDependencies": {
23
24
  "pg": "^8.0.0"
@@ -1 +1,5 @@
1
- export default function formatSql(sql: any): string;
1
+ /**
2
+ * @param {import('sql-template-tag').Sql} sql
3
+ * @returns {string}
4
+ */
5
+ export default function formatSql(sql: import("sql-template-tag").Sql): string;