@nocobase/database 1.9.0-beta.4 → 1.9.0-beta.5
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/lib/cursor-builder.js +4 -3
- package/lib/index.d.ts +1 -0
- package/lib/index.js +3 -0
- package/lib/magic-attribute-model.js +3 -0
- package/lib/operators/empty.d.ts +1 -4
- package/lib/operators/index.d.ts +1 -2
- package/lib/options-parser.js +1 -1
- package/lib/repository.d.ts +2 -0
- package/lib/repository.js +77 -0
- package/package.json +4 -4
package/lib/cursor-builder.js
CHANGED
|
@@ -87,7 +87,7 @@ const _SmartCursorBuilder = class _SmartCursorBuilder {
|
|
|
87
87
|
AND a.attnum = ANY(ix.indkey)
|
|
88
88
|
AND t.relkind = 'r'
|
|
89
89
|
AND n.nspname = current_schema()
|
|
90
|
-
AND t.relname =
|
|
90
|
+
AND t.relname = ?
|
|
91
91
|
ORDER BY
|
|
92
92
|
i.relname,
|
|
93
93
|
array_position(ix.indkey, a.attnum)
|
|
@@ -145,8 +145,9 @@ const _SmartCursorBuilder = class _SmartCursorBuilder {
|
|
|
145
145
|
isUnique: indexType < 3
|
|
146
146
|
});
|
|
147
147
|
}
|
|
148
|
-
const
|
|
149
|
-
index
|
|
148
|
+
const seqInIndex = dialect === "postgres" ? row.seq_in_index : row.SEQ_IN_INDEX;
|
|
149
|
+
const index = indexes.get(indexName);
|
|
150
|
+
index.columns[seqInIndex - 1] = columnName;
|
|
150
151
|
}
|
|
151
152
|
for (const index of indexes.values()) {
|
|
152
153
|
if (index.isPrimary) {
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -56,6 +56,7 @@ __export(src_exports, {
|
|
|
56
56
|
fieldTypeMap: () => import_field_type_map.default,
|
|
57
57
|
fn: () => import_sequelize.fn,
|
|
58
58
|
literal: () => import_sequelize.literal,
|
|
59
|
+
operators: () => import_operators.default,
|
|
59
60
|
snakeCase: () => import_utils.snakeCase,
|
|
60
61
|
sqlParser: () => import_sql_parser.default,
|
|
61
62
|
where: () => import_sequelize.where
|
|
@@ -96,6 +97,7 @@ __reExport(src_exports, require("./view-collection"), module.exports);
|
|
|
96
97
|
var import_field_type_map = __toESM(require("./view/field-type-map"));
|
|
97
98
|
__reExport(src_exports, require("./view/view-inference"), module.exports);
|
|
98
99
|
__reExport(src_exports, require("./update-guard"), module.exports);
|
|
100
|
+
var import_operators = __toESM(require("./operators"));
|
|
99
101
|
// Annotate the CommonJS export names for ESM import in node:
|
|
100
102
|
0 && (module.exports = {
|
|
101
103
|
BaseError,
|
|
@@ -116,6 +118,7 @@ __reExport(src_exports, require("./update-guard"), module.exports);
|
|
|
116
118
|
fieldTypeMap,
|
|
117
119
|
fn,
|
|
118
120
|
literal,
|
|
121
|
+
operators,
|
|
119
122
|
snakeCase,
|
|
120
123
|
sqlParser,
|
|
121
124
|
where,
|
|
@@ -126,6 +126,9 @@ const _MagicAttributeModel = class _MagicAttributeModel extends import_model.Mod
|
|
|
126
126
|
return this;
|
|
127
127
|
}
|
|
128
128
|
if (!options) options = {};
|
|
129
|
+
if (this.dataValues[this.magicAttribute] === null) {
|
|
130
|
+
this.dataValues[this.magicAttribute] = {};
|
|
131
|
+
}
|
|
129
132
|
if (!options.raw) {
|
|
130
133
|
originalValue = this.dataValues[key];
|
|
131
134
|
}
|
package/lib/operators/empty.d.ts
CHANGED
|
@@ -6,8 +6,5 @@
|
|
|
6
6
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
|
-
declare const _default:
|
|
10
|
-
$empty(_: any, ctx: any): any;
|
|
11
|
-
$notEmpty(_: any, ctx: any): any;
|
|
12
|
-
};
|
|
9
|
+
declare const _default: Record<string, any>;
|
|
13
10
|
export default _default;
|
package/lib/operators/index.d.ts
CHANGED
package/lib/options-parser.js
CHANGED
|
@@ -150,7 +150,7 @@ const _OptionsParser = class _OptionsParser {
|
|
|
150
150
|
if (defaultSortField && !((_b = this.options) == null ? void 0 : _b.group)) {
|
|
151
151
|
defaultSortField = import_lodash.default.castArray(defaultSortField);
|
|
152
152
|
for (const key of defaultSortField) {
|
|
153
|
-
if (!sort.includes(key)) {
|
|
153
|
+
if (!sort.includes(key) && !sort.includes(`-${key}`)) {
|
|
154
154
|
sort.push(key);
|
|
155
155
|
}
|
|
156
156
|
}
|
package/lib/repository.d.ts
CHANGED
|
@@ -154,6 +154,8 @@ export declare class Repository<TModelAttributes extends {} = any, TCreationAttr
|
|
|
154
154
|
* return count by filter
|
|
155
155
|
*/
|
|
156
156
|
count(countOptions?: CountOptions): Promise<number>;
|
|
157
|
+
getEstimatedRowCount(): Promise<number>;
|
|
158
|
+
private getOracleSchema;
|
|
157
159
|
aggregate(options: AggregateOptions & {
|
|
158
160
|
optionsTransformer?: (options: any) => any;
|
|
159
161
|
}): Promise<any>;
|
package/lib/repository.js
CHANGED
|
@@ -163,6 +163,83 @@ const _Repository = class _Repository {
|
|
|
163
163
|
transaction: transaction2
|
|
164
164
|
});
|
|
165
165
|
}
|
|
166
|
+
async getEstimatedRowCount() {
|
|
167
|
+
var _a, _b, _c, _d;
|
|
168
|
+
if (import_lodash2.default.isFunction(this.collection["isSql"]) && this.collection["isSql"]()) {
|
|
169
|
+
return 0;
|
|
170
|
+
}
|
|
171
|
+
if (import_lodash2.default.isFunction(this.collection["isView"]) && this.collection["isView"]()) {
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
const tableName = this.collection.tableName();
|
|
175
|
+
try {
|
|
176
|
+
if (this.database.isMySQLCompatibleDialect()) {
|
|
177
|
+
await this.database.sequelize.query(`ANALYZE TABLE ${this.collection.getTableNameWithSchema()}`);
|
|
178
|
+
const results = await this.database.sequelize.query(
|
|
179
|
+
`
|
|
180
|
+
SELECT table_rows FROM information_schema.tables
|
|
181
|
+
WHERE table_schema = DATABASE()
|
|
182
|
+
AND table_name = ?
|
|
183
|
+
`,
|
|
184
|
+
{ replacements: [tableName], type: import_sequelize.QueryTypes.SELECT }
|
|
185
|
+
);
|
|
186
|
+
return Number(((_a = results == null ? void 0 : results[0]) == null ? void 0 : _a.table_rows) ?? 0);
|
|
187
|
+
}
|
|
188
|
+
if (this.database.isPostgresCompatibleDialect()) {
|
|
189
|
+
await this.database.sequelize.query(`ANALYZE ${this.collection.getTableNameWithSchema()}`);
|
|
190
|
+
const results = await this.database.sequelize.query(
|
|
191
|
+
`
|
|
192
|
+
SELECT reltuples::BIGINT AS estimate
|
|
193
|
+
FROM pg_class c JOIN pg_namespace n ON c.relnamespace = n.oid
|
|
194
|
+
WHERE c.relname = ? AND n.nspname = current_schema();
|
|
195
|
+
`,
|
|
196
|
+
{ replacements: [tableName], type: import_sequelize.QueryTypes.SELECT }
|
|
197
|
+
);
|
|
198
|
+
return Number(((_b = results == null ? void 0 : results[0]) == null ? void 0 : _b.estimate) ?? 0);
|
|
199
|
+
}
|
|
200
|
+
if (this.database.sequelize.getDialect() === "mssql") {
|
|
201
|
+
const results = await this.database.sequelize.query(
|
|
202
|
+
`
|
|
203
|
+
SELECT SUM(row_count) AS estimate
|
|
204
|
+
FROM sys.dm_db_partition_stats
|
|
205
|
+
WHERE object_id = OBJECT_ID(?) AND (index_id = 0 OR index_id = 1)
|
|
206
|
+
`,
|
|
207
|
+
{ replacements: [tableName], type: import_sequelize.QueryTypes.SELECT }
|
|
208
|
+
);
|
|
209
|
+
return Number(((_c = results == null ? void 0 : results[0]) == null ? void 0 : _c.estimate) ?? 0);
|
|
210
|
+
}
|
|
211
|
+
if (this.database.sequelize.getDialect() === "oracle") {
|
|
212
|
+
const tableName2 = this.collection.name.toUpperCase();
|
|
213
|
+
const schemaName = (await this.getOracleSchema()).toUpperCase();
|
|
214
|
+
await this.database.sequelize.query(`BEGIN DBMS_STATS.GATHER_TABLE_STATS(:schema, :table); END;`, {
|
|
215
|
+
replacements: { schema: schemaName, table: tableName2 },
|
|
216
|
+
type: import_sequelize.QueryTypes.RAW
|
|
217
|
+
});
|
|
218
|
+
const results = await this.database.sequelize.query(
|
|
219
|
+
`
|
|
220
|
+
SELECT NUM_ROWS AS "estimate"
|
|
221
|
+
FROM ALL_TABLES
|
|
222
|
+
WHERE TABLE_NAME = :table AND OWNER = :schema
|
|
223
|
+
`,
|
|
224
|
+
{
|
|
225
|
+
replacements: { table: tableName2, schema: schemaName },
|
|
226
|
+
type: import_sequelize.QueryTypes.SELECT
|
|
227
|
+
}
|
|
228
|
+
);
|
|
229
|
+
return Number(((_d = results == null ? void 0 : results[0]) == null ? void 0 : _d.estimate) ?? 0);
|
|
230
|
+
}
|
|
231
|
+
} catch (error) {
|
|
232
|
+
this.database.logger.error(`Failed to get estimated row count for ${this.collection.name}:`, error);
|
|
233
|
+
return 0;
|
|
234
|
+
}
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
async getOracleSchema() {
|
|
238
|
+
const [result] = await this.database.sequelize.query(`SELECT USER FROM DUAL`, {
|
|
239
|
+
type: import_sequelize.QueryTypes.SELECT
|
|
240
|
+
});
|
|
241
|
+
return (result == null ? void 0 : result["USER"]) ?? "";
|
|
242
|
+
}
|
|
166
243
|
async aggregate(options) {
|
|
167
244
|
var _a;
|
|
168
245
|
const { method, field } = options;
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "1.9.0-beta.
|
|
3
|
+
"version": "1.9.0-beta.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"types": "./lib/index.d.ts",
|
|
7
7
|
"license": "AGPL-3.0",
|
|
8
8
|
"dependencies": {
|
|
9
|
-
"@nocobase/logger": "1.9.0-beta.
|
|
10
|
-
"@nocobase/utils": "1.9.0-beta.
|
|
9
|
+
"@nocobase/logger": "1.9.0-beta.5",
|
|
10
|
+
"@nocobase/utils": "1.9.0-beta.5",
|
|
11
11
|
"async-mutex": "^0.3.2",
|
|
12
12
|
"chalk": "^4.1.1",
|
|
13
13
|
"cron-parser": "4.4.0",
|
|
@@ -38,5 +38,5 @@
|
|
|
38
38
|
"url": "git+https://github.com/nocobase/nocobase.git",
|
|
39
39
|
"directory": "packages/database"
|
|
40
40
|
},
|
|
41
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "9a61c60dd3db5af64244e82447309b0cb17aabb6"
|
|
42
42
|
}
|