@nocobase/database 2.0.0-beta.1 → 2.0.0-beta.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.
@@ -45,6 +45,7 @@ export declare class OptionsParser {
45
45
  options?: object;
46
46
  raw?: string;
47
47
  };
48
+ protected normalizeAppends(appends: any): string[];
48
49
  protected parseAppends(appends: Appends, filterParams: any): any;
49
50
  }
50
51
  export {};
@@ -41,9 +41,9 @@ __export(options_parser_exports, {
41
41
  });
42
42
  module.exports = __toCommonJS(options_parser_exports);
43
43
  var import_lodash = __toESM(require("lodash"));
44
- var import_qs = __toESM(require("qs"));
45
44
  var import_sequelize = require("sequelize");
46
45
  var import_filter_parser = __toESM(require("./filter-parser"));
46
+ var import_qs = __toESM(require("qs"));
47
47
  const debug = require("debug")("noco-database");
48
48
  const _OptionsParser = class _OptionsParser {
49
49
  options;
@@ -281,10 +281,26 @@ const _OptionsParser = class _OptionsParser {
281
281
  }
282
282
  return obj;
283
283
  }
284
+ normalizeAppends(appends) {
285
+ if (Array.isArray(appends)) {
286
+ return appends.filter((item) => typeof item === "string" && item.length > 0);
287
+ }
288
+ if (import_lodash.default.isPlainObject(appends)) {
289
+ return Object.values(appends).filter((item) => typeof item === "string" && item.length > 0);
290
+ }
291
+ if (typeof appends === "string" && appends.length > 0) {
292
+ return [appends];
293
+ }
294
+ return [];
295
+ }
284
296
  parseAppends(appends, filterParams) {
285
297
  if (!appends) return filterParams;
286
- appends = import_lodash.default.sortBy(appends, (append) => append.split(".").length);
287
- const setInclude = /* @__PURE__ */ __name((model, queryParams, append) => {
298
+ const appendList = this.normalizeAppends(appends);
299
+ if (!appendList.length) {
300
+ return filterParams;
301
+ }
302
+ const sortedAppends = import_lodash.default.sortBy(appendList, (append) => append.split(".").length);
303
+ const setInclude = /* @__PURE__ */ __name((model, queryParams, append, parentAs) => {
288
304
  var _a;
289
305
  const appendWithOptions = this.parseAppendWithOptions(append);
290
306
  append = appendWithOptions.name;
@@ -323,10 +339,21 @@ const _OptionsParser = class _OptionsParser {
323
339
  return;
324
340
  }
325
341
  if (existIncludeIndex == -1) {
326
- queryParams["include"].push({
342
+ const association = associations[appendAssociation];
343
+ if (!association) {
344
+ throw new Error(`association ${appendAssociation} in ${model.name} not found`);
345
+ }
346
+ let includeOptions = {
327
347
  association: appendAssociation,
328
348
  options: appendWithOptions.options || {}
329
- });
349
+ };
350
+ if (association.associationType === "BelongsToArray") {
351
+ includeOptions = {
352
+ ...includeOptions,
353
+ ...association.generateInclude(parentAs)
354
+ };
355
+ }
356
+ queryParams["include"].push(includeOptions);
330
357
  existIncludeIndex = queryParams["include"].length - 1;
331
358
  }
332
359
  if (lastLevel) {
@@ -362,14 +389,11 @@ const _OptionsParser = class _OptionsParser {
362
389
  if (appendWithOptions.raw) {
363
390
  nextAppend += appendWithOptions.raw;
364
391
  }
365
- setInclude(
366
- model.associations[queryParams["include"][existIncludeIndex].association].target,
367
- queryParams["include"][existIncludeIndex],
368
- nextAppend
369
- );
392
+ const association = model.associations[queryParams["include"][existIncludeIndex].association];
393
+ setInclude(association.target, queryParams["include"][existIncludeIndex], nextAppend, association.as);
370
394
  }
371
395
  }, "setInclude");
372
- for (const append of appends) {
396
+ for (const append of sortedAppends) {
373
397
  setInclude(this.model, filterParams, append);
374
398
  }
375
399
  debug("filter params: %o", filterParams);
@@ -219,16 +219,6 @@ async function updateSingleAssociation(model, key, value, options = {}) {
219
219
  );
220
220
  }
221
221
  }, "checkBelongsToForeignKeyValue");
222
- if ((0, import_utils.isStringOrNumber)(value)) {
223
- await model[setAccessor](value, { context, transaction });
224
- return true;
225
- }
226
- if (value instanceof import_model.Model) {
227
- await model[setAccessor](value, { context, transaction });
228
- model.setDataValue(key, value);
229
- return true;
230
- }
231
- const createAccessor = association.accessors.create;
232
222
  let dataKey;
233
223
  let M;
234
224
  if (association.associationType === "BelongsTo") {
@@ -238,6 +228,24 @@ async function updateSingleAssociation(model, key, value, options = {}) {
238
228
  M = association.target;
239
229
  dataKey = M.primaryKeyAttribute;
240
230
  }
231
+ if ((0, import_utils.isStringOrNumber)(value)) {
232
+ const instance2 = await M.findOne({
233
+ where: {
234
+ [dataKey]: value
235
+ },
236
+ transaction
237
+ });
238
+ if (instance2) {
239
+ await model[setAccessor](value, { context, transaction });
240
+ }
241
+ return true;
242
+ }
243
+ if (value instanceof import_model.Model) {
244
+ await model[setAccessor](value, { context, transaction });
245
+ model.setDataValue(key, value);
246
+ return true;
247
+ }
248
+ const createAccessor = association.accessors.create;
241
249
  if ((0, import_utils.isStringOrNumber)(value[dataKey])) {
242
250
  const instance2 = await M.findOne({
243
251
  where: {
@@ -311,7 +319,7 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
311
319
  return;
312
320
  }
313
321
  value = import_lodash.default.castArray(value);
314
- const setItems = [];
322
+ let setItems = [];
315
323
  const objectItems = [];
316
324
  for (const item of value) {
317
325
  if ((0, import_utils.isUndefinedOrNull)(item)) {
@@ -335,6 +343,25 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
335
343
  objectItems.push(item);
336
344
  }
337
345
  }
346
+ if (setItems.length > 0) {
347
+ const TargetModel = association.target;
348
+ const pk2 = TargetModel.primaryKeyAttribute;
349
+ const targetKey2 = association.targetKey || association.options.targetKey || pk2;
350
+ const rawKeys = [];
351
+ const instanceItems = [];
352
+ for (const item of setItems) {
353
+ if (item instanceof import_model.Model) {
354
+ instanceItems.push(item);
355
+ } else if ((0, import_utils.isStringOrNumber)(item)) {
356
+ rawKeys.push(item);
357
+ }
358
+ }
359
+ const validInstances = rawKeys.length ? await TargetModel.findAll({
360
+ where: { [targetKey2]: rawKeys },
361
+ transaction
362
+ }) : [];
363
+ setItems = [...instanceItems, ...validInstances];
364
+ }
338
365
  await model[setAccessor](setItems, { transaction, context, individualHooks: true, validate: false });
339
366
  const newItems = [];
340
367
  const pk = association.target.primaryKeyAttribute;
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "2.0.0-beta.1",
3
+ "version": "2.0.0-beta.10",
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": "2.0.0-beta.1",
10
- "@nocobase/utils": "2.0.0-beta.1",
9
+ "@nocobase/logger": "2.0.0-beta.10",
10
+ "@nocobase/utils": "2.0.0-beta.10",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -39,5 +39,5 @@
39
39
  "url": "git+https://github.com/nocobase/nocobase.git",
40
40
  "directory": "packages/database"
41
41
  },
42
- "gitHead": "b3d1f65848fc91e673372ee734dafe6b1cf80586"
42
+ "gitHead": "9943dc4b0fdedcac3f304714b58635ae441e2560"
43
43
  }