rake-db 2.10.1 → 2.10.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.
package/dist/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  import { columnTypes, quote, EnumColumn, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, raw, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, referencesArgsToCode, constraintPropsToCode, TimestampTZColumn } from 'pqb';
2
- import { getCallerFilePath, singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, consumeColumnName, deepCompare, pathToLog, emptyArray, getImportPath, toCamelCase, codeToString, addCode, quoteObjectKey, backtickQuote } from 'orchid-core';
2
+ import { getCallerFilePath, singleQuote, toSnakeCase, isRawSQL, toArray, snakeCaseKey, emptyObject, consumeColumnName, setDefaultLanguage, deepCompare, pathToLog, emptyArray, getImportPath, toCamelCase, codeToString, addCode, quoteObjectKey, backtickQuote } from 'orchid-core';
3
3
  import path, { join } from 'path';
4
4
  import { readdir, mkdir, writeFile, stat, readFile } from 'fs/promises';
5
5
  import prompts from 'prompts';
@@ -79,6 +79,8 @@ const processRakeDbConfig = (config) => {
79
79
  result.columnTypes = proto.columnTypes || columnTypes;
80
80
  if (proto.snakeCase)
81
81
  result.snakeCase = true;
82
+ if (proto.language)
83
+ result.language = proto.language;
82
84
  } else {
83
85
  result.columnTypes = "columnTypes" in config && (typeof config.columnTypes === "function" ? config.columnTypes(columnTypes) : config.columnTypes) || columnTypes;
84
86
  }
@@ -298,6 +300,10 @@ const columnToSql = (name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
298
300
  }
299
301
  if (item.data.identity) {
300
302
  line.push(identityToSql(item.data.identity));
303
+ } else if (item.data.generated) {
304
+ line.push(
305
+ `GENERATED ALWAYS AS (${item.data.generated.toSQL({ values })}) STORED`
306
+ );
301
307
  }
302
308
  if (item.data.isPrimaryKey && !hasMultiplePrimaryKeys) {
303
309
  line.push("PRIMARY KEY");
@@ -441,7 +447,7 @@ const getIndexName = (table, columns) => {
441
447
  }
442
448
  ).join("_")}_idx`;
443
449
  };
444
- const indexesToQuery = (up, { schema, name }, indexes) => {
450
+ const indexesToQuery = (up, { schema, name }, indexes, language) => {
445
451
  return indexes.map(({ columns, options }) => {
446
452
  const indexName = options.name || getIndexName(name, columns);
447
453
  if (!up) {
@@ -456,11 +462,14 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
456
462
  sql.push("UNIQUE");
457
463
  }
458
464
  sql.push(`INDEX "${indexName}" ON ${quoteWithSchema({ schema, name })}`);
459
- if (options.using) {
460
- sql.push(`USING ${options.using}`);
465
+ const using = options.using || options.tsVector && "GIN";
466
+ if (using) {
467
+ sql.push(`USING ${using}`);
461
468
  }
462
469
  const columnsSql = [];
463
- columns.forEach((column) => {
470
+ const lang = options.tsVector && options.languageColumn ? `"${options.languageColumn}"` : options.language ? typeof options.language === "string" ? `'${options.language}'` : options.language.toSQL({ values }) : `'${language || "english"}'`;
471
+ let hasWeight = options.tsVector && columns.some((column) => !!column.weight);
472
+ for (const column of columns) {
464
473
  const columnSql = [
465
474
  "column" in column ? `"${column.column}"` : `(${column.expression})`
466
475
  ];
@@ -473,9 +482,23 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
473
482
  if (column.order) {
474
483
  columnSql.push(column.order);
475
484
  }
476
- columnsSql.push(columnSql.join(" "));
477
- });
478
- sql.push(`(${columnsSql.join(", ")})`);
485
+ let sql2 = columnSql.join(" ");
486
+ if (hasWeight) {
487
+ sql2 = `to_tsvector(${lang}, coalesce(${sql2}, ''))`;
488
+ if (column.weight) {
489
+ hasWeight = true;
490
+ sql2 = `setweight(${sql2}, '${column.weight}')`;
491
+ }
492
+ }
493
+ columnsSql.push(sql2);
494
+ }
495
+ let columnList = columnsSql.join(hasWeight ? " || " : ", ");
496
+ if (!hasWeight && options.tsVector) {
497
+ if (columnsSql.length > 1)
498
+ columnList = `concat_ws(' ', ${columnList})`;
499
+ columnList = `to_tsvector(${lang}, ${columnList})`;
500
+ }
501
+ sql.push(`(${columnList})`);
479
502
  if (options.include) {
480
503
  sql.push(
481
504
  `INCLUDE (${toArray(options.include).map((column) => `"${column}"`).join(", ")})`
@@ -559,12 +582,18 @@ var __objRest$1 = (source, exclude) => {
559
582
  const createTable$1 = async (migration, up, tableName, options, fn) => {
560
583
  var _a;
561
584
  const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
585
+ const language = "language" in options ? options.language : migration.options.language;
562
586
  const types = Object.assign(
563
587
  Object.create(migration.columnTypes),
564
588
  tableMethods
565
589
  );
566
590
  types[snakeCaseKey] = snakeCase;
567
- const shape = getColumnTypes(types, fn, (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL);
591
+ const shape = getColumnTypes(
592
+ types,
593
+ fn,
594
+ (_a = migration.options.baseTable) == null ? void 0 : _a.nowSQL,
595
+ language
596
+ );
568
597
  const tableData = getTableData();
569
598
  const ast = makeAst$2(
570
599
  up,
@@ -575,7 +604,7 @@ const createTable$1 = async (migration, up, tableName, options, fn) => {
575
604
  migration.options.noPrimaryKey
576
605
  );
577
606
  validatePrimaryKey(ast);
578
- const queries = astToQueries$1(ast, snakeCase);
607
+ const queries = astToQueries$1(ast, snakeCase, language);
579
608
  for (const _b of queries) {
580
609
  const _c = _b, { then } = _c, query = __objRest$1(_c, ["then"]);
581
610
  const result = await migration.adapter.arrays(query);
@@ -643,7 +672,7 @@ You can suppress this error by setting { noPrimaryKey: true } after a table name
643
672
  }
644
673
  }
645
674
  };
646
- const astToQueries$1 = (ast, snakeCase) => {
675
+ const astToQueries$1 = (ast, snakeCase, language) => {
647
676
  var _a, _b;
648
677
  const queries = [];
649
678
  const { shape } = ast;
@@ -715,7 +744,7 @@ const astToQueries$1 = (ast, snakeCase) => {
715
744
  )`,
716
745
  values
717
746
  },
718
- ...indexesToQuery(true, ast, indexes),
747
+ ...indexesToQuery(true, ast, indexes, language),
719
748
  ...commentsToQuery(ast, comments)
720
749
  );
721
750
  if (ast.comment) {
@@ -895,15 +924,17 @@ const tableChangeMethods = __spreadProps$2(__spreadValues$3({}, tableMethods), {
895
924
  });
896
925
  const changeTable = async (migration, up, tableName, options, fn) => {
897
926
  var _a;
927
+ const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
928
+ const language = "language" in options ? options.language : migration.options.language;
929
+ setDefaultLanguage(language);
898
930
  resetTableData();
899
931
  resetChangeTableData();
900
932
  const tableChanger = Object.create(migration.columnTypes);
901
933
  Object.assign(tableChanger, tableChangeMethods);
902
- const snakeCase = "snakeCase" in options ? options.snakeCase : migration.options.snakeCase;
903
934
  tableChanger[snakeCaseKey] = snakeCase;
904
935
  const changeData = (fn == null ? void 0 : fn(tableChanger)) || {};
905
936
  const ast = makeAst$1(up, tableName, changeData, changeTableData, options);
906
- const queries = astToQueries(ast, snakeCase);
937
+ const queries = astToQueries(ast, snakeCase, language);
907
938
  for (const query of queries) {
908
939
  const result = await migration.adapter.arrays(query);
909
940
  (_a = query.then) == null ? void 0 : _a.call(query, result);
@@ -936,7 +967,7 @@ const makeAst$1 = (up, name, changeData, changeTableData2, options) => {
936
967
  shape
937
968
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
938
969
  };
939
- const astToQueries = (ast, snakeCase) => {
970
+ const astToQueries = (ast, snakeCase, language) => {
940
971
  var _a, _b, _c, _d, _e, _f, _g, _h, _i;
941
972
  const queries = [];
942
973
  if (ast.comment !== void 0) {
@@ -1181,8 +1212,8 @@ const astToQueries = (ast, snakeCase) => {
1181
1212
  values
1182
1213
  });
1183
1214
  }
1184
- queries.push(...indexesToQuery(false, ast, dropIndexes));
1185
- queries.push(...indexesToQuery(true, ast, addIndexes));
1215
+ queries.push(...indexesToQuery(false, ast, dropIndexes, language));
1216
+ queries.push(...indexesToQuery(true, ast, addIndexes, language));
1186
1217
  queries.push(...commentsToQuery(ast, comments));
1187
1218
  return queries;
1188
1219
  };