@nocobase/database 0.21.0-alpha.8 → 0.21.0-alpha.9

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.
@@ -36,6 +36,7 @@ const _SqlCollection = class _SqlCollection extends import_collection.Collection
36
36
  collectionSchema() {
37
37
  return void 0;
38
38
  }
39
+ /* istanbul ignore next -- @preserve */
39
40
  get filterTargetKey() {
40
41
  var _a;
41
42
  const targetKey = ((_a = this.options) == null ? void 0 : _a.filterTargetKey) || "id";
@@ -67,45 +67,55 @@ const _SQLModel = class _SQLModel extends import_model.Model {
67
67
  if (Array.isArray(ast)) {
68
68
  ast = ast[0];
69
69
  }
70
+ ast.from = ast.from || [];
71
+ ast.columns = ast.columns || [];
70
72
  if (ast.with) {
71
- const tables = /* @__PURE__ */ new Set();
72
73
  ast.with.forEach((withItem) => {
73
- const tableList = withItem.tableList;
74
- tableList.forEach((table) => {
75
- const name = table.split("::")[2];
76
- tables.add(name);
77
- });
74
+ const as = withItem.name;
75
+ const withAst = withItem.stmt.ast;
76
+ ast.from.push(...withAst.from.map((f) => ({ ...f, as })));
77
+ ast.columns.push(
78
+ ...withAst.columns.map((c) => ({
79
+ ...c,
80
+ expr: {
81
+ ...c.expr,
82
+ table: as
83
+ }
84
+ }))
85
+ );
78
86
  });
79
- return Array.from(tables).map((table) => ({ table, columns: "*" }));
80
87
  }
81
88
  if (ast.columns === "*") {
82
- return ast.from.map((fromItem) => ({
83
- table: fromItem.table,
84
- columns: "*"
85
- }));
89
+ const tables = /* @__PURE__ */ new Set();
90
+ ast.from.forEach((fromItem) => {
91
+ tables.add(fromItem.table);
92
+ });
93
+ return Array.from(tables).map((table) => ({ table, columns: "*" }));
86
94
  }
95
+ const tableAliases = {};
96
+ ast.from.forEach((fromItem) => {
97
+ if (!fromItem.as) {
98
+ return;
99
+ }
100
+ tableAliases[fromItem.as] = fromItem.table;
101
+ });
87
102
  const columns = ast.columns.reduce(
88
103
  (tableMp, column) => {
89
104
  if (column.expr.type !== "column_ref") {
90
105
  return tableMp;
91
106
  }
92
107
  const table = column.expr.table;
108
+ const name = tableAliases[table] || table;
93
109
  const columnAttr = { name: column.expr.column, as: column.as };
94
- if (!tableMp[table]) {
95
- tableMp[table] = [columnAttr];
110
+ if (!tableMp[name]) {
111
+ tableMp[name] = [columnAttr];
96
112
  } else {
97
- tableMp[table].push(columnAttr);
113
+ tableMp[name].push(columnAttr);
98
114
  }
99
115
  return tableMp;
100
116
  },
101
117
  {}
102
118
  );
103
- ast.from.forEach((fromItem) => {
104
- if (columns[fromItem.as]) {
105
- columns[fromItem.table] = columns[fromItem.as];
106
- columns[fromItem.as] = void 0;
107
- }
108
- });
109
119
  return Object.entries(columns).filter(([_, columns2]) => columns2).map(([table, columns2]) => ({ table, columns: columns2 }));
110
120
  }
111
121
  static getTableNameWithSchema(table) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "0.21.0-alpha.8",
3
+ "version": "0.21.0-alpha.9",
4
4
  "description": "",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./lib/index.d.ts",
7
7
  "license": "Apache-2.0",
8
8
  "dependencies": {
9
- "@nocobase/logger": "0.21.0-alpha.8",
10
- "@nocobase/utils": "0.21.0-alpha.8",
9
+ "@nocobase/logger": "0.21.0-alpha.9",
10
+ "@nocobase/utils": "0.21.0-alpha.9",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -36,5 +36,5 @@
36
36
  "url": "git+https://github.com/nocobase/nocobase.git",
37
37
  "directory": "packages/database"
38
38
  },
39
- "gitHead": "bed69485709bc8e268285b50debfc1ebfd7a30c7"
39
+ "gitHead": "a6fe6c4ee532c04e5a50ec777bf76436ca624cbd"
40
40
  }