rake-db 2.10.1 → 2.10.3

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.d.ts CHANGED
@@ -41,6 +41,7 @@ type TableOptions = {
41
41
  comment?: string;
42
42
  noPrimaryKey?: boolean;
43
43
  snakeCase?: boolean;
44
+ language?: string;
44
45
  };
45
46
  type TextColumnCreator = () => TextColumn;
46
47
  type MigrationColumnTypes<CT extends ColumnTypesBase> = Omit<CT, 'text' | 'string' | 'enum'> & {
@@ -54,6 +55,7 @@ type ColumnsShapeCallback<CT extends ColumnTypesBase, Shape extends ColumnsShape
54
55
  }) => Shape;
55
56
  type ChangeTableOptions = {
56
57
  snakeCase?: boolean;
58
+ language?: string;
57
59
  comment?: string | [string, string] | null;
58
60
  };
59
61
  type ChangeTableCallback<CT extends ColumnTypesBase> = (t: TableChanger<CT>) => TableChangeData;
@@ -941,6 +943,7 @@ type BaseTable<CT extends ColumnTypesBase> = {
941
943
  new (): {
942
944
  columnTypes: CT;
943
945
  snakeCase?: boolean;
946
+ language?: string;
944
947
  };
945
948
  };
946
949
  type InputRakeDbConfig<CT extends ColumnTypesBase> = Partial<Omit<RakeDbConfig<CT>, 'columnTypes'>> & ({
@@ -956,6 +959,7 @@ type RakeDbConfig<CT extends ColumnTypesBase = DefaultColumnTypes> = {
956
959
  recurrentPath: string;
957
960
  migrationsTable: string;
958
961
  snakeCase: boolean;
962
+ language?: string;
959
963
  commands: Record<string, (options: AdapterOptions[], config: RakeDbConfig<CT>, args: string[]) => Promise<void>>;
960
964
  import(path: string): Promise<unknown>;
961
965
  noPrimaryKey?: NoPrimaryKeyOption;
@@ -1095,39 +1099,24 @@ declare const rakeDb: <CT extends Record<string, orchid_core.AnyColumnTypeCreato
1095
1099
  tsquery(): pqb.TsQueryColumn;
1096
1100
  uuid(): pqb.UUIDColumn;
1097
1101
  xml(): pqb.XMLColumn;
1098
- json<Type extends orchid_core.JSONTypeAny>(schemaOrFn: Type | ((j: {
1099
- set: <Value extends orchid_core.JSONTypeAny>(valueType: Value) => orchid_core.JSONSet<Value>;
1100
- tuple: <T_5 extends [] | orchid_core.JSONTupleItems, Rest extends orchid_core.JSONTypeAny | null = null>(items: T_5, rest?: Rest) => orchid_core.JSONTuple<T_5, Rest>;
1101
- union: <T_6 extends [orchid_core.JSONTypeAny, orchid_core.JSONTypeAny, ...orchid_core.JSONTypeAny[]]>(types: T_6) => orchid_core.JSONUnion<T_6>;
1102
- any: () => orchid_core.JSONAny;
1103
- bigint: () => orchid_core.JSONBigInt;
1102
+ json<Type extends orchid_core.JSONType<unknown, {}> = orchid_core.JSONUnknown>(schemaOrFn?: Type | ((j: {
1103
+ unknown: () => orchid_core.JSONUnknown;
1104
1104
  boolean: () => orchid_core.JSONBoolean;
1105
- date: () => orchid_core.JSONDate;
1106
- nan: () => orchid_core.JSONNaN;
1107
- never: () => orchid_core.JSONNever;
1108
1105
  null: () => orchid_core.JSONNull;
1109
1106
  number: () => orchid_core.JSONNumber;
1110
1107
  string: () => orchid_core.JSONString;
1111
- unknown: () => orchid_core.JSONUnknown;
1112
- array: <Type_1 extends orchid_core.JSONTypeAny>(element: Type_1) => orchid_core.JSONArray<Type_1, "many">;
1113
- discriminatedUnion: <Discriminator extends string, DiscriminatorValue extends orchid_core.Primitive, Types extends [orchid_core.JSONDiscriminatedObject<Discriminator, DiscriminatorValue>, orchid_core.JSONDiscriminatedObject<Discriminator, DiscriminatorValue>, ...orchid_core.JSONDiscriminatedObject<Discriminator, DiscriminatorValue>[]]>(discriminator: Discriminator, options: Types) => orchid_core.JSONDiscriminatedUnion<Discriminator, DiscriminatorValue, Types>;
1114
- enum: <U_1 extends string, T_7 extends [U_1, ...U_1[]]>(options: T_7) => orchid_core.JSONEnum<U_1, T_7>;
1115
- instanceOf: <T_8 extends new (...args: any[]) => any>(cls: T_8) => orchid_core.JSONInstanceOf<T_8>;
1116
- intersection: <Left extends orchid_core.JSONTypeAny, Right extends orchid_core.JSONTypeAny>(left: Left, right: Right) => orchid_core.JSONIntersection<Left, Right>;
1117
- lazy: <T_9 extends orchid_core.JSONTypeAny>(fn: () => T_9) => orchid_core.JSONLazy<T_9>;
1118
- literal: <T_10 extends orchid_core.Primitive>(value: T_10) => orchid_core.JSONLiteral<T_10>;
1119
- map: <Key extends orchid_core.JSONTypeAny, Value_1 extends orchid_core.JSONTypeAny>(keyType: Key, valueType: Value_1) => orchid_core.JSONMap<Key, Value_1>;
1120
- nativeEnum: <T_11 extends orchid_core.EnumLike>(givenEnum: T_11) => orchid_core.JSONNativeEnum<T_11>;
1121
- nullable: <T_12 extends orchid_core.JSONTypeAny>(type: T_12) => orchid_core.JSONNullable<T_12>;
1122
- nullish: <T_13 extends orchid_core.JSONTypeAny>(type: T_13) => orchid_core.JSONNullish<T_13>;
1123
- object: <T_14 extends orchid_core.JSONObjectShape, UnknownKeys extends orchid_core.UnknownKeysParam = "strip", Catchall extends orchid_core.JSONTypeAny = orchid_core.JSONTypeAny>(shape: T_14) => orchid_core.JSONObject<T_14, UnknownKeys, Catchall, orchid_core.JSONTypeAny extends Catchall ? orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer T_15 extends object ? { [k in keyof T_15]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }>[k]; } : never : (orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer T_18 extends object ? { [k in keyof T_18]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }>[k]; } : never) & {
1124
- [k: string]: Catchall["type"];
1125
- } extends infer T_16 extends object ? { [k_2 in keyof T_16]: ((orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }> extends infer T_17 extends object ? { [k in keyof T_17]: orchid_core.addQuestionMarks<{ [k_1 in keyof T_14]: T_14[k_1]["type"]; }>[k]; } : never) & {
1126
- [k: string]: Catchall["type"];
1127
- })[k_2]; } : never>;
1128
- optional: <T_19 extends orchid_core.JSONTypeAny>(type: T_19) => orchid_core.JSONOptional<T_19>;
1129
- record: typeof orchid_core.record;
1130
- }) => Type)): pqb.JSONColumn<Type>;
1108
+ array: <T_5 extends orchid_core.JSONType<unknown, {}>>(item: T_5) => orchid_core.JSONArray<T_5, "many">;
1109
+ object: <Shape extends orchid_core.JSONObjectShape>(shape: Shape) => orchid_core.JSONObject<Shape, "strip", orchid_core.JSONType<unknown, {}>>;
1110
+ literal: <T_6 extends orchid_core.JSONPrimitive>(value: T_6) => orchid_core.JSONLiteral<T_6>;
1111
+ discriminatedUnion: <Discriminator extends string, Types extends orchid_core.JSONDiscriminatedUnionArg<Discriminator>>(discriminator: Discriminator, types: Types) => orchid_core.JSONDiscriminatedUnion<Discriminator, Types>;
1112
+ enum: <U_1 extends string, T_7 extends [U_1, ...U_1[]]>(options: T_7) => orchid_core.JSONEnum<string, T_7>;
1113
+ intersection: <Left extends orchid_core.JSONType<unknown, {}>, Right extends orchid_core.JSONType<unknown, {}>>(left: Left, right: Right) => orchid_core.JSONIntersection<Left, Right>;
1114
+ lazy: <T_8 extends orchid_core.JSONType<unknown, {}>>(fn: () => T_8) => orchid_core.JSONLazy<T_8>;
1115
+ nativeEnum: <T_9 extends orchid_core.EnumLike>(type: T_9) => orchid_core.JSONNativeEnum<T_9>;
1116
+ record: <Key extends orchid_core.JSONNumber | orchid_core.JSONString, Value extends orchid_core.JSONType<unknown, {}>>(...args: [value: Value] | [key: Key, value: Value]) => orchid_core.JSONRecord<Key, Value>;
1117
+ tuple: <T_10 extends orchid_core.JSONTupleItems, Rest extends orchid_core.JSONType<unknown, {}> | undefined = undefined>(items: T_10, rest?: Rest | undefined) => orchid_core.JSONTuple<T_10, Rest>;
1118
+ union: <T_11 extends orchid_core.JSONUnionArgs>(...types: T_11) => orchid_core.JSONUnion<T_11>;
1119
+ }) => Type) | undefined): pqb.JSONColumn<Type>;
1131
1120
  jsonText(): pqb.JSONTextColumn;
1132
1121
  array<Item extends pqb.ColumnType<unknown, pqb.BaseOperators, unknown>>(item: Item): pqb.ArrayColumn<Item>;
1133
1122
  type(dataType: string): pqb.CustomTypeColumn;
@@ -1136,7 +1125,8 @@ declare const rakeDb: <CT extends Record<string, orchid_core.AnyColumnTypeCreato
1136
1125
  name?: string | undefined;
1137
1126
  } | undefined): {};
1138
1127
  index(columns: MaybeArray<string | pqb.IndexColumnOptions>, options?: pqb.IndexOptions): {};
1139
- unique(columns: MaybeArray<string | pqb.IndexColumnOptions>, options?: pqb.IndexOptions): {};
1128
+ unique(columns: MaybeArray<string | pqb.IndexColumnOptions>, options?: pqb.IndexOptions | undefined): {};
1129
+ searchIndex(columns: MaybeArray<string | pqb.IndexColumnOptions>, options?: pqb.IndexOptions | undefined): {};
1140
1130
  constraint<Table extends string | (() => orchid_core.ForeignKeyTable), Columns extends Table extends () => orchid_core.ForeignKeyTable ? [orchid_core.ColumnNameOfTable<ReturnType<Table>>, ...orchid_core.ColumnNameOfTable<ReturnType<Table>>[]] : [string, ...string[]]>({ name, references, check, dropMode, }: {
1141
1131
  name?: string | undefined;
1142
1132
  references?: [columns: string[], fnOrTable: Table, foreignColumns: Columns, options?: pqb.ForeignKeyOptions | undefined] | undefined;
package/dist/index.js CHANGED
@@ -100,6 +100,8 @@ const processRakeDbConfig = (config) => {
100
100
  result.columnTypes = proto.columnTypes || pqb.columnTypes;
101
101
  if (proto.snakeCase)
102
102
  result.snakeCase = true;
103
+ if (proto.language)
104
+ result.language = proto.language;
103
105
  } else {
104
106
  result.columnTypes = "columnTypes" in config && (typeof config.columnTypes === "function" ? config.columnTypes(pqb.columnTypes) : config.columnTypes) || pqb.columnTypes;
105
107
  }
@@ -319,6 +321,10 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
319
321
  }
320
322
  if (item.data.identity) {
321
323
  line.push(identityToSql(item.data.identity));
324
+ } else if (item.data.generated) {
325
+ line.push(
326
+ `GENERATED ALWAYS AS (${item.data.generated.toSQL({ values })}) STORED`
327
+ );
322
328
  }
323
329
  if (item.data.isPrimaryKey && !hasMultiplePrimaryKeys) {
324
330
  line.push("PRIMARY KEY");
@@ -462,7 +468,7 @@ const getIndexName = (table, columns) => {
462
468
  }
463
469
  ).join("_")}_idx`;
464
470
  };
465
- const indexesToQuery = (up, { schema, name }, indexes) => {
471
+ const indexesToQuery = (up, { schema, name }, indexes, language) => {
466
472
  return indexes.map(({ columns, options }) => {
467
473
  const indexName = options.name || getIndexName(name, columns);
468
474
  if (!up) {
@@ -477,11 +483,14 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
477
483
  sql.push("UNIQUE");
478
484
  }
479
485
  sql.push(`INDEX "${indexName}" ON ${quoteWithSchema({ schema, name })}`);
480
- if (options.using) {
481
- sql.push(`USING ${options.using}`);
486
+ const using = options.using || options.tsVector && "GIN";
487
+ if (using) {
488
+ sql.push(`USING ${using}`);
482
489
  }
483
490
  const columnsSql = [];
484
- columns.forEach((column) => {
491
+ const lang = options.tsVector && options.languageColumn ? `"${options.languageColumn}"` : options.language ? typeof options.language === "string" ? `'${options.language}'` : options.language.toSQL({ values }) : `'${language || "english"}'`;
492
+ let hasWeight = options.tsVector && columns.some((column) => !!column.weight);
493
+ for (const column of columns) {
485
494
  const columnSql = [
486
495
  "column" in column ? `"${column.column}"` : `(${column.expression})`
487
496
  ];
@@ -494,9 +503,23 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
494
503
  if (column.order) {
495
504
  columnSql.push(column.order);
496
505
  }
497
- columnsSql.push(columnSql.join(" "));
498
- });
499
- sql.push(`(${columnsSql.join(", ")})`);
506
+ let sql2 = columnSql.join(" ");
507
+ if (hasWeight) {
508
+ sql2 = `to_tsvector(${lang}, coalesce(${sql2}, ''))`;
509
+ if (column.weight) {
510
+ hasWeight = true;
511
+ sql2 = `setweight(${sql2}, '${column.weight}')`;
512
+ }
513
+ }
514
+ columnsSql.push(sql2);
515
+ }
516
+ let columnList = columnsSql.join(hasWeight ? " || " : ", ");
517
+ if (!hasWeight && options.tsVector) {
518
+ if (columnsSql.length > 1)
519
+ columnList = `concat_ws(' ', ${columnList})`;
520
+ columnList = `to_tsvector(${lang}, ${columnList})`;
521
+ }
522
+ sql.push(`(${columnList})`);
500
523
  if (options.include) {
501
524
  sql.push(
502
525
  `INCLUDE (${orchidCore.toArray(options.include).map((column) => `"${column}"`).join(", ")})`
@@ -580,12 +603,18 @@ var __objRest$1 = (source, exclude) => {
580
603
  const createTable$1 = async (migration, up, tableName, options, fn) => {
581
604
  var _a;
582
605
  const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
606
+ const language = "language" in options ? options.language : migration.options.language;
583
607
  const types = Object.assign(
584
608
  Object.create(migration.columnTypes),
585
609
  tableMethods
586
610
  );
587
611
  types[orchidCore.snakeCaseKey] = snakeCase;
588
- const shape = pqb.getColumnTypes(types, fn, (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL);
612
+ const shape = pqb.getColumnTypes(
613
+ types,
614
+ fn,
615
+ (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL,
616
+ language
617
+ );
589
618
  const tableData = pqb.getTableData();
590
619
  const ast = makeAst$2(
591
620
  up,
@@ -596,7 +625,7 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
596
625
  migration.options.noPrimaryKey
597
626
  );
598
627
  validatePrimaryKey(ast);
599
- const queries = astToQueries$1(ast, snakeCase);
628
+ const queries = astToQueries$1(ast, snakeCase, language);
600
629
  for (const _b of queries) {
601
630
  const _c = _b, { then } = _c, query = __objRest$1(_c, ["then"]);
602
631
  const result = await migration.adapter.arrays(query);
@@ -664,7 +693,7 @@ You can suppress this error by setting { noPrimaryKey: true } after a table name
664
693
  }
665
694
  }
666
695
  };
667
- const astToQueries$1 = (ast, snakeCase) => {
696
+ const astToQueries$1 = (ast, snakeCase, language) => {
668
697
  var _a, _b;
669
698
  const queries = [];
670
699
  const { shape } = ast;
@@ -736,7 +765,7 @@ const astToQueries$1 = (ast, snakeCase) => {
736
765
  )`,
737
766
  values
738
767
  },
739
- ...indexesToQuery(true, ast, indexes),
768
+ ...indexesToQuery(true, ast, indexes, language),
740
769
  ...commentsToQuery(ast, comments)
741
770
  );
742
771
  if (ast.comment) {
@@ -916,15 +945,17 @@ const tableChangeMethods = __spreadProps$2(__spreadValues$3({}, tableMethods), {
916
945
  });
917
946
  const changeTable = async (migration, up, tableName, options, fn) => {
918
947
  var _a;
948
+ const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
949
+ const language = "language" in options ? options.language : migration.options.language;
950
+ orchidCore.setDefaultLanguage(language);
919
951
  pqb.resetTableData();
920
952
  resetChangeTableData();
921
953
  const tableChanger = Object.create(migration.columnTypes);
922
954
  Object.assign(tableChanger, tableChangeMethods);
923
- const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
924
955
  tableChanger[orchidCore.snakeCaseKey] = snakeCase;
925
956
  const changeData = (fn == null ? void 0 : fn(tableChanger)) || {};
926
957
  const ast = makeAst$1(up, tableName, changeData, changeTableData, options);
927
- const queries = astToQueries(ast, snakeCase);
958
+ const queries = astToQueries(ast, snakeCase, language);
928
959
  for (const query of queries) {
929
960
  const result = await migration.adapter.arrays(query);
930
961
  (_a = query.then) == null ? void 0 : _a.call(query, result);
@@ -957,7 +988,7 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
957
988
  shape
958
989
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
959
990
  };
960
- const astToQueries = (ast, snakeCase) => {
991
+ const astToQueries = (ast, snakeCase, language) => {
961
992
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
962
993
  const queries = [];
963
994
  if (ast.comment !== void 0) {
@@ -1202,8 +1233,8 @@ const astToQueries = (ast, snakeCase) => {
1202
1233
  values
1203
1234
  });
1204
1235
  }
1205
- queries.push(...indexesToQuery(false, ast, dropIndexes));
1206
- queries.push(...indexesToQuery(true, ast, addIndexes));
1236
+ queries.push(...indexesToQuery(false, ast, dropIndexes, language));
1237
+ queries.push(...indexesToQuery(true, ast, addIndexes, language));
1207
1238
  queries.push(...commentsToQuery(ast, comments));
1208
1239
  return queries;
1209
1240
  };