@graffy/pg 0.17.1-alpha.1 → 0.17.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.
Files changed (3) hide show
  1. package/index.cjs +23 -12
  2. package/index.mjs +24 -13
  3. package/package.json +2 -2
package/index.cjs CHANGED
@@ -80,19 +80,30 @@ const empty = raw("");
80
80
  function sql(strings, ...values) {
81
81
  return new Sql(strings, values);
82
82
  }
83
- function formatSql(sql2) {
84
- const strings = sql2.strings.slice(0);
85
- const values = sql2.values.slice(0);
86
- const output = [];
87
- while (strings.length) {
88
- output.push(strings.shift());
89
- if (!values.length) break;
90
- const value = values.shift();
91
- output.push(
92
- typeof value === "number" ? value.toString() : value === null ? "null" : typeof value === "object" ? `'${JSON.stringify(value)}'` : `'${value}'`
93
- );
83
+ function formatSqlParam(value) {
84
+ if (value === null || value === void 0) return "null";
85
+ if (["number", "boolean"].includes(typeof value)) return value.toString();
86
+ switch (typeof value) {
87
+ case "number":
88
+ case "boolean":
89
+ return value.toString();
90
+ case "string":
91
+ return pg$1.escapeLiteral(value);
92
+ case "object":
93
+ if (Array.isArray(value))
94
+ return `array[${value.map(formatSqlParam).join(", ")}]`;
95
+ return `'${JSON.stringify(value)}'`;
96
+ default:
97
+ return "";
94
98
  }
95
- return sqlFormatter.format(output.join(""), { language: "postgresql" });
99
+ }
100
+ function formatSql(sql2) {
101
+ return sqlFormatter.format(sql2.text, {
102
+ language: "postgresql",
103
+ params: Object.fromEntries(
104
+ sql2.values.map((value, idx) => [idx + 1, formatSqlParam(value)])
105
+ )
106
+ });
96
107
  }
97
108
  const getJsonBuildTrusted = (variadic) => {
98
109
  const args = join(
package/index.mjs CHANGED
@@ -1,6 +1,6 @@
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
- import pg$1 from "pg";
3
+ import pg$1, { escapeLiteral } from "pg";
4
4
  import { format } from "sql-formatter";
5
5
  class Sql {
6
6
  constructor(rawStrings, rawValues) {
@@ -78,19 +78,30 @@ const empty = raw("");
78
78
  function sql(strings, ...values) {
79
79
  return new Sql(strings, values);
80
80
  }
81
- function formatSql(sql2) {
82
- const strings = sql2.strings.slice(0);
83
- const values = sql2.values.slice(0);
84
- const output = [];
85
- while (strings.length) {
86
- output.push(strings.shift());
87
- if (!values.length) break;
88
- const value = values.shift();
89
- output.push(
90
- typeof value === "number" ? value.toString() : value === null ? "null" : typeof value === "object" ? `'${JSON.stringify(value)}'` : `'${value}'`
91
- );
81
+ function formatSqlParam(value) {
82
+ if (value === null || value === void 0) return "null";
83
+ if (["number", "boolean"].includes(typeof value)) return value.toString();
84
+ switch (typeof value) {
85
+ case "number":
86
+ case "boolean":
87
+ return value.toString();
88
+ case "string":
89
+ return escapeLiteral(value);
90
+ case "object":
91
+ if (Array.isArray(value))
92
+ return `array[${value.map(formatSqlParam).join(", ")}]`;
93
+ return `'${JSON.stringify(value)}'`;
94
+ default:
95
+ return "";
92
96
  }
93
- return format(output.join(""), { language: "postgresql" });
97
+ }
98
+ function formatSql(sql2) {
99
+ return format(sql2.text, {
100
+ language: "postgresql",
101
+ params: Object.fromEntries(
102
+ sql2.values.map((value, idx) => [idx + 1, formatSqlParam(value)])
103
+ )
104
+ });
94
105
  }
95
106
  const getJsonBuildTrusted = (variadic) => {
96
107
  const args = join(
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.17.1-alpha.1",
5
+ "version": "0.17.2",
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.17.1-alpha.1",
19
+ "@graffy/common": "0.17.2",
20
20
  "debug": "^4.4.1",
21
21
  "sql-formatter": "^15.6.4"
22
22
  },