pqb 0.56.5 → 0.56.7

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.mjs CHANGED
@@ -167,9 +167,7 @@ class ColumnType extends ColumnTypeBase {
167
167
  * // options are described below:
168
168
  * name: t.text().index({ ...options }),
169
169
  * // with a database-level name:
170
- * name: t.text().index('custom_index_name'),
171
- * // with name and options:
172
- * name: t.text().index('custom_index_name', { ...options }),
170
+ * name: t.text().index({ name: 'custom_index_name', ...indexOptions }),
173
171
  * }));
174
172
  * });
175
173
  * ```
@@ -178,6 +176,7 @@ class ColumnType extends ColumnTypeBase {
178
176
  *
179
177
  * ```ts
180
178
  * type IndexOptions = {
179
+ * name?: string,
181
180
  * // NULLS NOT DISTINCT: availabe in Postgres 15+, makes sense only for unique index
182
181
  * nullsNotDistinct?: true;
183
182
  * // index algorithm to use such as GIST, GIN
@@ -204,9 +203,9 @@ class ColumnType extends ColumnTypeBase {
204
203
  * @param args
205
204
  */
206
205
  index(...args) {
206
+ const a = args;
207
207
  return pushColumnData(this, "indexes", {
208
- options: (typeof args[0] === "string" ? args[1] : args[0]) ?? emptyObject,
209
- name: typeof args[0] === "string" ? args[0] : void 0
208
+ options: (typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0]) ?? emptyObject
210
209
  });
211
210
  }
212
211
  /**
@@ -312,21 +311,21 @@ class ColumnType extends ColumnTypeBase {
312
311
  * @param options - index options
313
312
  */
314
313
  searchIndex(...args) {
314
+ const a = args;
315
315
  return pushColumnData(this, "indexes", {
316
316
  options: {
317
- ...typeof args[0] === "string" ? args[1] : args[0],
317
+ ...typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0],
318
318
  ...this.dataType === "tsvector" ? { using: "GIN" } : { tsVector: true }
319
- },
320
- name: typeof args[0] === "string" ? args[0] : void 0
319
+ }
321
320
  });
322
321
  }
323
322
  unique(...args) {
323
+ const a = args;
324
324
  return pushColumnData(this, "indexes", {
325
325
  options: {
326
- ...typeof args[0] === "string" ? args[1] : args[0],
326
+ ...typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0],
327
327
  unique: true
328
- },
329
- name: typeof args[0] === "string" ? args[0] : void 0
328
+ }
330
329
  });
331
330
  }
332
331
  /**
@@ -374,11 +373,11 @@ class ColumnType extends ColumnTypeBase {
374
373
  * }
375
374
  * ```
376
375
  */
377
- exclude(...args) {
376
+ exclude(op, ...args) {
377
+ const a = args;
378
378
  return pushColumnData(this, "excludes", {
379
- with: args[0],
380
- options: (typeof args[1] === "string" ? args[2] : args[1]) ?? emptyObject,
381
- name: typeof args[1] === "string" ? args[1] : void 0
379
+ with: op,
380
+ options: (typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0]) ?? emptyObject
382
381
  });
383
382
  }
384
383
  comment(comment) {
@@ -539,6 +538,7 @@ const indexInnerToCode = (index, t) => {
539
538
  const columnOptions = ["collate", "opclass", "order", "weight"];
540
539
  const indexOptionsKeys = [
541
540
  index.options.tsVector ? "unique" : void 0,
541
+ "name",
542
542
  "using",
543
543
  "nullsNotDistinct",
544
544
  "include",
@@ -584,18 +584,12 @@ const indexInnerToCode = (index, t) => {
584
584
  objects.push("{", props, "},");
585
585
  }
586
586
  }
587
- code.push(["[", objects, hasOptions || index.name ? "]," : "]"]);
588
- if (index.name) {
589
- addCode(code, ` ${singleQuote(index.name)},`);
590
- }
587
+ code.push(["[", objects, hasOptions ? "]," : "]"]);
591
588
  } else {
592
589
  addCode(
593
590
  code,
594
591
  `[${index.columns.map((it) => singleQuote(it.column)).join(", ")}]`
595
592
  );
596
- if (index.name) {
597
- addCode(code, `, ${singleQuote(index.name)}`);
598
- }
599
593
  }
600
594
  if (hasOptions) {
601
595
  if (columnsMultiline) {
@@ -631,6 +625,7 @@ const excludeInnerToCode = (item, t) => {
631
625
  const code = [`${t}.exclude(`];
632
626
  const columnOptions = ["collate", "opclass", "order", "with"];
633
627
  const optionsKeys = [
628
+ "name",
634
629
  "using",
635
630
  "include",
636
631
  "with",
@@ -655,10 +650,7 @@ const excludeInnerToCode = (item, t) => {
655
650
  }
656
651
  objects.push("{", props, "},");
657
652
  }
658
- code.push(["[", objects, hasOptions || item.name ? "]," : "]"]);
659
- if (item.name) {
660
- addCode(code, ` ${singleQuote(item.name)},`);
661
- }
653
+ code.push(["[", objects, hasOptions ? "]," : "]"]);
662
654
  if (hasOptions) {
663
655
  code.push(["{"]);
664
656
  const options = [];
@@ -766,12 +758,10 @@ const foreignKeyArgumentToCode = ({
766
758
  };
767
759
  const columnIndexesToCode = (items) => {
768
760
  const code = [];
769
- for (const { options, name } of items) {
770
- addCode(
771
- code,
772
- `.${options.unique ? "unique" : "index"}(${name ? `${singleQuote(name)}` : ""}`
773
- );
761
+ for (const { options } of items) {
762
+ addCode(code, `.${options.unique ? "unique" : "index"}(`);
774
763
  const arr = [
764
+ options.name && `name: ${singleQuote(options.name)},`,
775
765
  options.collate && `collate: ${singleQuote(options.collate)},`,
776
766
  options.opclass && `opclass: ${singleQuote(options.opclass)},`,
777
767
  options.order && `order: ${singleQuote(options.order)},`,
@@ -783,7 +773,7 @@ const columnIndexesToCode = (items) => {
783
773
  options.where && `where: ${singleQuote(options.where)},`
784
774
  ].filter((x) => !!x);
785
775
  if (arr.length) {
786
- addCode(code, name ? `, {` : "{");
776
+ addCode(code, "{");
787
777
  addCode(code, arr);
788
778
  addCode(code, "}");
789
779
  }
@@ -793,13 +783,13 @@ const columnIndexesToCode = (items) => {
793
783
  };
794
784
  const columnExcludesToCode = (items) => {
795
785
  const code = [];
796
- for (const { options, name, with: w } of items) {
786
+ for (const { options, with: w } of items) {
797
787
  addCode(code, `.exclude('${w}'`);
798
788
  const arr = [
789
+ options.name && `name: ${singleQuote(options.name)},`,
799
790
  options.collate && `collate: ${singleQuote(options.collate)},`,
800
791
  options.opclass && `opclass: ${singleQuote(options.opclass)},`,
801
792
  options.order && `order: ${singleQuote(options.order)},`,
802
- name && `name: ${singleQuote(name)},`,
803
793
  options.using && `using: ${singleQuote(options.using)},`,
804
794
  options.include && `include: ${typeof options.include === "string" ? singleQuote(options.include) : `[${options.include.map(singleQuote).join(", ")}]`},`,
805
795
  options.with && `with: ${singleQuote(options.with)},`,
@@ -878,7 +868,9 @@ const columnCode = (type, ctx, key, code) => {
878
868
  }
879
869
  if (data.explicitSelect) addCode(code, ".select(false)");
880
870
  if (data.isNullable) addCode(code, ".nullable()");
881
- if (data.as) addCode(code, `.as(${data.as.toCode(ctx, key)})`);
871
+ if (data.as && !ctx.migration) {
872
+ addCode(code, `.as(${data.as.toCode(ctx, key)})`);
873
+ }
882
874
  if (data.default !== void 0 && data.default !== data.defaultDefault && (!ctx.migration || typeof data.default !== "function")) {
883
875
  addCode(
884
876
  code,
@@ -1187,6 +1179,17 @@ const quoteJsonValue = (arg, ctx, quotedAs, IN) => {
1187
1179
  }
1188
1180
  return addValue(ctx.values, JSON.stringify(arg)) + "::jsonb";
1189
1181
  };
1182
+ const serializeJsonValue = (arg, ctx, quotedAs) => {
1183
+ if (arg && typeof arg === "object") {
1184
+ if (isExpression(arg)) {
1185
+ return "to_jsonb(" + arg.toSQL(ctx, quotedAs) + ")";
1186
+ }
1187
+ if ("toSQL" in arg) {
1188
+ return `to_jsonb((${getSqlText(arg.toSQL(ctx))}))`;
1189
+ }
1190
+ }
1191
+ return addValue(ctx.values, JSON.stringify(arg));
1192
+ };
1190
1193
  const json = {
1191
1194
  equals: make(
1192
1195
  (key, value, ctx, quotedAs) => value === null ? `nullif(${key}, 'null'::jsonb) IS NULL` : `${key} = ${quoteJsonValue(value, ctx, quotedAs)}`
@@ -1229,21 +1232,24 @@ const json = {
1229
1232
  (key, value, ctx, quotedAs) => `${key} <@ ${quoteValue(value, ctx, quotedAs)}`
1230
1233
  ),
1231
1234
  jsonSet: makeVarArg(
1232
- (key, [path, value], ctx) => `jsonb_set(${key}, ${encodeJsonPath(ctx, path)}, ${addValue(
1233
- ctx.values,
1234
- JSON.stringify(value)
1235
+ (key, [path, value], ctx, quotedAs) => `jsonb_set(${key}, ${encodeJsonPath(ctx, path)}, ${serializeJsonValue(
1236
+ value,
1237
+ ctx,
1238
+ quotedAs
1235
1239
  )})`
1236
1240
  ),
1237
1241
  jsonReplace: makeVarArg(
1238
- (key, [path, value], ctx) => `jsonb_set(${key}, ${encodeJsonPath(ctx, path)}, ${addValue(
1239
- ctx.values,
1240
- JSON.stringify(value)
1242
+ (key, [path, value], ctx, quotedAs) => `jsonb_set(${key}, ${encodeJsonPath(ctx, path)}, ${serializeJsonValue(
1243
+ value,
1244
+ ctx,
1245
+ quotedAs
1241
1246
  )}, false)`
1242
1247
  ),
1243
1248
  jsonInsert: makeVarArg(
1244
- (key, [path, value, options], ctx) => `jsonb_insert(${key}, ${encodeJsonPath(ctx, path)}, ${addValue(
1245
- ctx.values,
1246
- JSON.stringify(value)
1249
+ (key, [path, value, options], ctx, quotedAs) => `jsonb_insert(${key}, ${encodeJsonPath(ctx, path)}, ${serializeJsonValue(
1250
+ value,
1251
+ ctx,
1252
+ quotedAs
1247
1253
  )}${options?.after ? ", true" : ""})`
1248
1254
  ),
1249
1255
  jsonRemove: makeVarArg(
@@ -12817,9 +12823,9 @@ applyMixins(QueryMethods, [
12817
12823
 
12818
12824
  const makeIndex = (columns, first, second) => {
12819
12825
  if (typeof first === "string") {
12820
- const options = second ?? {};
12826
+ const options = { ...second, name: first };
12821
12827
  return {
12822
- index: { columns, options, name: first }
12828
+ index: { columns, options }
12823
12829
  };
12824
12830
  } else {
12825
12831
  const options = first ?? {};
@@ -12832,24 +12838,27 @@ const tableDataMethods = {
12832
12838
  primaryKey(columns, name) {
12833
12839
  return { primaryKey: { columns, name } };
12834
12840
  },
12835
- unique(columns, ...[first, second]) {
12841
+ unique(columns, ...args) {
12842
+ const [first, second] = args;
12836
12843
  const input = makeIndex(columns, first, second);
12837
12844
  input.index.options.unique = true;
12838
12845
  return input;
12839
12846
  },
12840
12847
  index: makeIndex,
12841
- searchIndex(columns, ...[first, second]) {
12848
+ searchIndex(columns, ...args) {
12842
12849
  var _a;
12850
+ const [first, second] = args;
12843
12851
  const input = makeIndex(columns, first, second);
12844
12852
  (_a = input.index.options).using ?? (_a.using = "gin");
12845
12853
  input.index.options.tsVector = true;
12846
12854
  return input;
12847
12855
  },
12848
- exclude(columns, ...[first, second]) {
12856
+ exclude(columns, ...args) {
12857
+ const [first, second] = args;
12849
12858
  if (typeof first === "string") {
12850
12859
  const options = second ?? {};
12851
12860
  return {
12852
- exclude: { columns, options, name: first }
12861
+ exclude: { columns, options: { ...options, name: first } }
12853
12862
  };
12854
12863
  } else {
12855
12864
  const options = first ?? {};