orchid-orm 1.4.21 → 1.5.0

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
@@ -1754,7 +1754,11 @@ const createTable = async (_a) => {
1754
1754
  ]);
1755
1755
  const tablePath = params.tablePath(ast.name);
1756
1756
  const baseTablePath = getImportPath(tablePath, params.baseTablePath);
1757
- const props = [`table = ${pqb.singleQuote(ast.name)};`];
1757
+ const props = [];
1758
+ if (ast.schema) {
1759
+ props.push(`schema = ${pqb.singleQuote(ast.schema)};`);
1760
+ }
1761
+ props.push(`table = ${pqb.singleQuote(ast.name)};`);
1758
1762
  if (ast.noPrimaryKey === "ignore") {
1759
1763
  props.push("noPrimaryKey = true;");
1760
1764
  }
@@ -1967,20 +1971,32 @@ const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
1967
1971
  }
1968
1972
  }
1969
1973
  const changedProps = {};
1974
+ const replaced = {};
1970
1975
  for (const item of items.reverse()) {
1971
1976
  if (!ts.is.propertyAccess(item.expression))
1972
1977
  continue;
1973
1978
  const { name } = item.expression;
1974
- const key = name.escapedText.toString();
1979
+ let key = name.escapedText.toString();
1980
+ if (key === "index")
1981
+ key = "indexes";
1982
+ else if (key === "foreignKey")
1983
+ key = "foreignKeys";
1975
1984
  if (!propsToChange[key])
1976
1985
  continue;
1977
- const value = getColumnMethodArgs(to, key);
1978
- if (value) {
1979
- const code = [`${key}(`];
1980
- pqb.addCode(code, value);
1981
- pqb.addCode(code, ")");
1982
- changes.replace(name.pos, item.end, pqb.codeToString(code, "", " ").trim());
1983
- } else {
1986
+ let remove = true;
1987
+ if (!replaced[key]) {
1988
+ const code = getColumnMethodArgs(to, key);
1989
+ if (code) {
1990
+ changes.replace(
1991
+ item.expression.expression.end,
1992
+ item.end,
1993
+ pqb.codeToString(code, spaces + " ", " ").trim()
1994
+ );
1995
+ replaced[key] = true;
1996
+ remove = false;
1997
+ }
1998
+ }
1999
+ if (remove) {
1984
2000
  changes.remove(item.expression.expression.end, item.end);
1985
2001
  }
1986
2002
  changedProps[key] = true;
@@ -1989,12 +2005,9 @@ const changeColumn = ({ changes, t, spaces }, changeItem, prop) => {
1989
2005
  for (const key in propsToChange) {
1990
2006
  if (changedProps[key])
1991
2007
  continue;
1992
- const value = getColumnMethodArgs(to, key);
1993
- if (value !== void 0) {
1994
- const code = [`.${key}(`];
1995
- pqb.addCode(code, value);
1996
- pqb.addCode(code, ")");
1997
- append += pqb.codeToString(code, "", " ").trim();
2008
+ const code = getColumnMethodArgs(to, key);
2009
+ if (code) {
2010
+ append += pqb.codeToString(code, spaces + " ", " ").trim();
1998
2011
  }
1999
2012
  }
2000
2013
  if (append) {
@@ -2049,15 +2062,22 @@ const getColumnMethodArgs = (to, key) => {
2049
2062
  const value = to[key];
2050
2063
  if (!value)
2051
2064
  return;
2065
+ if (key === "indexes") {
2066
+ return pqb.columnIndexesToCode(value);
2067
+ }
2068
+ if (key === "foreignKeys") {
2069
+ return pqb.columnForeignKeysToCode(value);
2070
+ }
2071
+ const code = [`.${key}(`];
2052
2072
  if (key === "collate" || key === "compression") {
2053
- return pqb.singleQuote(value);
2073
+ pqb.addCode(code, pqb.singleQuote(value));
2054
2074
  } else if (key === "default") {
2055
- return pqb.columnDefaultArgumentToCode(value);
2056
- } else if (key === "nullable" || key === "primaryKey") {
2057
- return "";
2058
- } else {
2075
+ pqb.addCode(code, pqb.columnDefaultArgumentToCode(value));
2076
+ } else if (key !== "nullable" && key !== "primaryKey") {
2059
2077
  return;
2060
2078
  }
2079
+ pqb.addCode(code, ")");
2080
+ return code;
2061
2081
  };
2062
2082
  const dropMatchingIndexes = (context, prop, i, call, items) => {
2063
2083
  if (!items.length)
@@ -2197,17 +2217,38 @@ const renameTable = async (_a) => {
2197
2217
  const changes = new FileChanges(content);
2198
2218
  const statements = ts.getStatements(content);
2199
2219
  const className = toPascalCase(ast.from);
2220
+ const changeSchema = ast.fromSchema !== ast.toSchema;
2200
2221
  for (const node of ts.class.iterate(statements)) {
2201
2222
  if (((_a2 = node.name) == null ? void 0 : _a2.escapedText) !== className)
2202
2223
  continue;
2224
+ const addSchema = changeSchema && ast.toSchema && !node.members.some((member) => ts.prop.getName(member) === "schema");
2225
+ if (addSchema && ast.toSchema) {
2226
+ changes.add(
2227
+ node.members.pos,
2228
+ `
2229
+ schema = ${pqb.singleQuote(ast.toSchema)};`
2230
+ );
2231
+ }
2203
2232
  for (const member of node.members) {
2204
2233
  const name = ts.prop.getName(member);
2205
- if (name !== "table")
2234
+ if (name !== "table" && !(changeSchema && name === "schema"))
2206
2235
  continue;
2207
2236
  const { initializer: value } = member;
2208
2237
  if (!value)
2209
2238
  continue;
2210
- changes.replace(value.pos, value.end, ` ${pqb.singleQuote(ast.to)}`);
2239
+ if (name === "schema") {
2240
+ if (ast.toSchema) {
2241
+ changes.replace(
2242
+ value.pos,
2243
+ value.end,
2244
+ ` ${pqb.singleQuote(ast.toSchema)}`
2245
+ );
2246
+ } else {
2247
+ changes.remove(member.pos, member.end);
2248
+ }
2249
+ } else {
2250
+ changes.replace(value.pos, value.end, ` ${pqb.singleQuote(ast.to)}`);
2251
+ }
2211
2252
  }
2212
2253
  }
2213
2254
  await fs__default["default"].writeFile(params.tablePath(ast.to), changes.apply());