@cadenza.io/service 2.16.0 → 2.17.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.
package/dist/index.js CHANGED
@@ -5728,6 +5728,67 @@ function validateIntentName(intentName) {
5728
5728
  function defaultOperationIntentDescription(operation, tableName) {
5729
5729
  return `Perform a ${operation} operation on the ${tableName} table`;
5730
5730
  }
5731
+ function isExplicitSqlLiteral(value) {
5732
+ return /^'.*'::[a-z_][a-z0-9_]*(\[\])?$/i.test(value.trim());
5733
+ }
5734
+ function isExplicitSqlExpression(value) {
5735
+ const trimmed = value.trim();
5736
+ if (!trimmed) {
5737
+ return false;
5738
+ }
5739
+ if (isExplicitSqlLiteral(trimmed)) {
5740
+ return true;
5741
+ }
5742
+ if (/^'.*'$/.test(trimmed)) {
5743
+ return true;
5744
+ }
5745
+ if (/^[a-z_][a-z0-9_]*\([^)]*\)$/i.test(trimmed)) {
5746
+ return true;
5747
+ }
5748
+ return /^(current_timestamp|current_date|current_time)$/i.test(trimmed);
5749
+ }
5750
+ function serializeFieldDefaultForSql(value, field) {
5751
+ if (value === void 0 || value === null) {
5752
+ return "NULL";
5753
+ }
5754
+ if (typeof value === "number") {
5755
+ return String(value);
5756
+ }
5757
+ if (typeof value === "boolean") {
5758
+ return value ? "TRUE" : "FALSE";
5759
+ }
5760
+ const stringValue = String(value);
5761
+ if (isExplicitSqlExpression(stringValue)) {
5762
+ return stringValue;
5763
+ }
5764
+ if (field?.type === "jsonb") {
5765
+ return `'${JSON.stringify(value).replace(/'/g, "''")}'::jsonb`;
5766
+ }
5767
+ return `'${stringValue.replace(/'/g, "''")}'`;
5768
+ }
5769
+ function serializeInitialDataValueForSql(value, field) {
5770
+ if (value === void 0 || value === null) {
5771
+ return "NULL";
5772
+ }
5773
+ if (typeof value === "number") {
5774
+ return String(value);
5775
+ }
5776
+ if (typeof value === "boolean") {
5777
+ return value ? "TRUE" : "FALSE";
5778
+ }
5779
+ if (field?.type === "jsonb") {
5780
+ if (typeof value === "string" && isExplicitSqlLiteral(value)) {
5781
+ return value;
5782
+ }
5783
+ const jsonString = JSON.stringify(value);
5784
+ return `'${jsonString.replace(/'/g, "''")}'::jsonb`;
5785
+ }
5786
+ const stringValue = String(value);
5787
+ if (isExplicitSqlExpression(stringValue)) {
5788
+ return stringValue;
5789
+ }
5790
+ return `'${stringValue.replace(/'/g, "''")}'`;
5791
+ }
5731
5792
  function readCustomIntentConfig(customIntent) {
5732
5793
  if (typeof customIntent === "string") {
5733
5794
  return {
@@ -6741,14 +6802,12 @@ var DatabaseController = class _DatabaseController {
6741
6802
  if (table.initialData) {
6742
6803
  ddl.push(
6743
6804
  `INSERT INTO ${tableName} (${table.initialData.fields.map(import_lodash_es.snakeCase).join(", ")}) VALUES ${table.initialData.data.map(
6744
- (row) => `(${row.map((value) => {
6745
- if (value === void 0) return "NULL";
6746
- if (value === null) return "NULL";
6747
- if (typeof value === "number") return String(value);
6748
- if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
6749
- const stringValue = String(value);
6750
- return `'${stringValue.replace(/'/g, "''")}'`;
6751
- }).join(", ")})`
6805
+ (row) => `(${row.map(
6806
+ (value, index) => serializeInitialDataValueForSql(
6807
+ value,
6808
+ table.fields[table.initialData.fields[index]]
6809
+ )
6810
+ ).join(", ")})`
6752
6811
  ).join(", ")} ON CONFLICT DO NOTHING;`
6753
6812
  );
6754
6813
  }
@@ -6766,7 +6825,7 @@ var DatabaseController = class _DatabaseController {
6766
6825
  if (field.primary) definition += " PRIMARY KEY";
6767
6826
  if (field.unique) definition += " UNIQUE";
6768
6827
  if (field.default !== void 0) {
6769
- definition += ` DEFAULT ${field.default === "" ? "''" : String(field.default)}`;
6828
+ definition += ` DEFAULT ${serializeFieldDefaultForSql(field.default, field)}`;
6770
6829
  }
6771
6830
  if (field.required && !field.nullable) definition += " NOT NULL";
6772
6831
  if (field.nullable) definition += " NULL";