rake-db 2.8.9 → 2.8.10

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,4 +1,4 @@
1
- import { columnTypes, quote, getRaw, EnumColumn, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, TimestampTzColumn, referencesArgsToCode, constraintPropsToCode } from 'pqb';
1
+ import { columnTypes, quote, getRaw, EnumColumn, getColumnTypes, getTableData, ColumnType, resetTableData, UnknownColumn, TransactionAdapter, logParamToLogObject, createDb as createDb$1, Adapter, simplifyColumnDefault, columnsByType, instantiateColumn, DomainColumn, CustomTypeColumn, ArrayColumn, getConstraintKind, primaryKeyToCode, indexToCode, constraintToCode, referencesArgsToCode, constraintPropsToCode, TimestampTZColumn } from 'pqb';
2
2
  import { getCallerFilePath, singleQuote, toSnakeCase, isRaw, toArray, snakeCaseKey, emptyObject, consumeColumnName, deepCompare, raw, pathToLog, emptyArray, getImportPath, toCamelCase, codeToString, addCode, rawToCode, quoteObjectKey, backtickQuote } from 'orchid-core';
3
3
  import path from 'path';
4
4
  import { readdir, mkdir, writeFile } from 'fs/promises';
@@ -2871,17 +2871,14 @@ const createDomain = (ast) => {
2871
2871
  return code;
2872
2872
  };
2873
2873
  const createTable = (config, ast) => {
2874
- var _a, _b, _c, _d, _e, _f;
2875
2874
  const code = [];
2876
2875
  addCode(code, `await db.createTable(${quoteSchemaTable(ast)}, (t) => ({`);
2877
- let hasTimestamps = isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
2878
- const camelCaseTimestamps = !config.snakeCase && hasTimestamps && !((_a = ast.shape.createdAt) == null ? void 0 : _a.data.name) && !((_b = ast.shape.updatedAt) == null ? void 0 : _b.data.name);
2879
- const snakeCaseTimestamps = hasTimestamps && !camelCaseTimestamps && (!config.snakeCase && ((_c = ast.shape.createdAt) == null ? void 0 : _c.data.name) === "created_at" && ((_d = ast.shape.updatedAt) == null ? void 0 : _d.data.name) === "updated_at" || config.snakeCase && !((_e = ast.shape.createdAt) == null ? void 0 : _e.data.name) && !((_f = ast.shape.updatedAt) == null ? void 0 : _f.data.name));
2880
- if (!camelCaseTimestamps && !snakeCaseTimestamps) {
2881
- hasTimestamps = false;
2882
- }
2876
+ const timestamps = getTimestampsInfo(config, ast, TimestampTZColumn);
2877
+ const timestampsNoTZ = getTimestampsInfo(config, ast, TimestampTZColumn);
2878
+ const hasAnyTimestamps = timestamps.hasTimestamps || timestampsNoTZ.hasTimestamps;
2879
+ const hasAnyCamelCaseTimestamps = timestamps.camelCaseTimestamps || timestampsNoTZ.camelCaseTimestamps;
2883
2880
  for (const key in ast.shape) {
2884
- if (hasTimestamps && (key === "createdAt" || key === "updatedAt"))
2881
+ if (hasAnyTimestamps && (key === "createdAt" || key === "updatedAt"))
2885
2882
  continue;
2886
2883
  const line = [`${quoteObjectKey(key)}: `];
2887
2884
  for (const part of ast.shape[key].toCode("t", true)) {
@@ -2890,9 +2887,10 @@ const createTable = (config, ast) => {
2890
2887
  addCode(line, ",");
2891
2888
  code.push(line);
2892
2889
  }
2893
- if (hasTimestamps) {
2890
+ if (hasAnyTimestamps) {
2891
+ const key = timestamps.hasTimestamps ? "timestamps" : "timestampsNoTZ";
2894
2892
  code.push([
2895
- `...t.${camelCaseTimestamps || config.snakeCase ? "timestamps" : "timestampsSnakeCase"}(),`
2893
+ `...t.${hasAnyCamelCaseTimestamps || config.snakeCase ? key : `${key}SnakeCase`}(),`
2896
2894
  ]);
2897
2895
  }
2898
2896
  if (ast.primaryKey) {
@@ -2911,11 +2909,25 @@ const createTable = (config, ast) => {
2911
2909
  addCode(code, "}));");
2912
2910
  return code;
2913
2911
  };
2914
- const isTimestamp = (column) => {
2912
+ const isTimestamp = (column, type) => {
2915
2913
  if (!column)
2916
2914
  return false;
2917
2915
  const { default: def } = column.data;
2918
- return column instanceof TimestampTzColumn && !column.data.isNullable && def && typeof def === "object" && isRaw(def) && def.__raw === "now()";
2916
+ return column instanceof type && !column.data.isNullable && def && typeof def === "object" && isRaw(def) && def.__raw === "now()";
2917
+ };
2918
+ const getTimestampsInfo = (config, ast, type) => {
2919
+ var _a, _b, _c, _d, _e, _f;
2920
+ let hasTimestamps = isTimestamp(ast.shape.createdAt, type) && isTimestamp(ast.shape.updatedAt, type);
2921
+ const camelCaseTimestamps = !config.snakeCase && hasTimestamps && !((_a = ast.shape.createdAt) == null ? void 0 : _a.data.name) && !((_b = ast.shape.updatedAt) == null ? void 0 : _b.data.name);
2922
+ const snakeCaseTimestamps = hasTimestamps && !camelCaseTimestamps && (!config.snakeCase && ((_c = ast.shape.createdAt) == null ? void 0 : _c.data.name) === "created_at" && ((_d = ast.shape.updatedAt) == null ? void 0 : _d.data.name) === "updated_at" || config.snakeCase && !((_e = ast.shape.createdAt) == null ? void 0 : _e.data.name) && !((_f = ast.shape.updatedAt) == null ? void 0 : _f.data.name));
2923
+ if (!camelCaseTimestamps && !snakeCaseTimestamps) {
2924
+ hasTimestamps = false;
2925
+ }
2926
+ return {
2927
+ hasTimestamps,
2928
+ camelCaseTimestamps,
2929
+ snakeCaseTimestamps
2930
+ };
2919
2931
  };
2920
2932
  const createConstraint = (item) => {
2921
2933
  const kind = getConstraintKind(item);