@nocobase/database 2.0.0-beta.2 → 2.0.0-beta.20

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.
@@ -19,4 +19,10 @@ export interface ContextFieldOptions extends BaseColumnFieldOptions {
19
19
  dataIndex: string;
20
20
  dataType?: string;
21
21
  createOnly?: boolean;
22
+ /**
23
+ * applyIfUndefined:
24
+ * When true, the context value will be applied
25
+ * only if the field value is `undefined`.
26
+ */
27
+ applyIfUndefined?: boolean;
22
28
  }
@@ -50,9 +50,12 @@ const _ContextField = class _ContextField extends import_field.Field {
50
50
  }
51
51
  listener = /* @__PURE__ */ __name(async (instances, options) => {
52
52
  instances = Array.isArray(instances) ? instances : [instances];
53
- const { name, dataIndex } = this.options;
53
+ const { name, dataIndex, applyIfUndefined } = this.options;
54
54
  const { context } = options;
55
55
  for (const instance of instances) {
56
+ if (applyIfUndefined && instance.get(name) !== void 0) {
57
+ continue;
58
+ }
56
59
  instance.set(name, import_lodash.default.get(context, dataIndex));
57
60
  instance.changed(name, true);
58
61
  }
@@ -61,7 +61,7 @@ const findFilterFieldType = /* @__PURE__ */ __name((ctx) => {
61
61
  }
62
62
  model = modelAssociation.target;
63
63
  }
64
- const collection = db.modelCollection.get(model);
64
+ const collection = db.getCollection(model.name);
65
65
  return collection.getField(fieldName);
66
66
  }, "findFilterFieldType");
67
67
  var empty_default = {
@@ -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;
@@ -300,7 +300,7 @@ const _OptionsParser = class _OptionsParser {
300
300
  return filterParams;
301
301
  }
302
302
  const sortedAppends = import_lodash.default.sortBy(appendList, (append) => append.split(".").length);
303
- const setInclude = /* @__PURE__ */ __name((model, queryParams, append) => {
303
+ const setInclude = /* @__PURE__ */ __name((model, queryParams, append, parentAs) => {
304
304
  var _a;
305
305
  const appendWithOptions = this.parseAppendWithOptions(append);
306
306
  append = appendWithOptions.name;
@@ -339,10 +339,21 @@ const _OptionsParser = class _OptionsParser {
339
339
  return;
340
340
  }
341
341
  if (existIncludeIndex == -1) {
342
- 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 = {
343
347
  association: appendAssociation,
344
348
  options: appendWithOptions.options || {}
345
- });
349
+ };
350
+ if (association.associationType === "BelongsToArray") {
351
+ includeOptions = {
352
+ ...includeOptions,
353
+ ...association.generateInclude(parentAs)
354
+ };
355
+ }
356
+ queryParams["include"].push(includeOptions);
346
357
  existIncludeIndex = queryParams["include"].length - 1;
347
358
  }
348
359
  if (lastLevel) {
@@ -378,11 +389,8 @@ const _OptionsParser = class _OptionsParser {
378
389
  if (appendWithOptions.raw) {
379
390
  nextAppend += appendWithOptions.raw;
380
391
  }
381
- setInclude(
382
- model.associations[queryParams["include"][existIncludeIndex].association].target,
383
- queryParams["include"][existIncludeIndex],
384
- nextAppend
385
- );
392
+ const association = model.associations[queryParams["include"][existIncludeIndex].association];
393
+ setInclude(association.target, queryParams["include"][existIncludeIndex], nextAppend, association.as);
386
394
  }
387
395
  }, "setInclude");
388
396
  for (const append of sortedAppends) {
@@ -140,6 +140,7 @@ const _BelongsToManyRepository = class _BelongsToManyRepository extends import_m
140
140
  [import_sequelize.Op.in]: ids
141
141
  }
142
142
  },
143
+ individualHooks: true,
143
144
  transaction: transaction2
144
145
  });
145
146
  return true;
@@ -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;
@@ -69,7 +69,7 @@ const _UpdateGuard = class _UpdateGuard {
69
69
  }
70
70
  setAssociationKeysToBeUpdate(associationKeysToBeUpdate) {
71
71
  if (this.action == "create") {
72
- this.associationKeysToBeUpdate = Object.keys(this.model.associations);
72
+ this.associationKeysToBeUpdate = (associationKeysToBeUpdate == null ? void 0 : associationKeysToBeUpdate.length) ? associationKeysToBeUpdate : Object.keys(this.model.associations);
73
73
  } else {
74
74
  this.associationKeysToBeUpdate = associationKeysToBeUpdate;
75
75
  }
@@ -40,7 +40,6 @@ __export(view_inference_exports, {
40
40
  ViewFieldInference: () => ViewFieldInference
41
41
  });
42
42
  module.exports = __toCommonJS(view_inference_exports);
43
- var import_mathjs = require("mathjs");
44
43
  var import_field_type_map = __toESM(require("./field-type-map"));
45
44
  const _ViewFieldInference = class _ViewFieldInference {
46
45
  static extractTypeFromDefinition(typeDefinition) {
@@ -137,7 +136,7 @@ const _ViewFieldInference = class _ViewFieldInference {
137
136
  }
138
137
  const queryType = this.extractTypeFromDefinition(options.type);
139
138
  const mappedType = fieldTypeMap[queryType];
140
- if ((0, import_mathjs.isArray)(mappedType)) {
139
+ if (Array.isArray(mappedType)) {
141
140
  return {
142
141
  type: mappedType[0],
143
142
  possibleTypes: mappedType
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "2.0.0-beta.2",
3
+ "version": "2.0.0-beta.20",
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.2",
10
- "@nocobase/utils": "2.0.0-beta.2",
9
+ "@nocobase/logger": "2.0.0-beta.20",
10
+ "@nocobase/utils": "2.0.0-beta.20",
11
11
  "async-mutex": "^0.3.2",
12
12
  "chalk": "^4.1.1",
13
13
  "cron-parser": "4.4.0",
@@ -20,7 +20,6 @@
20
20
  "graphlib": "^2.1.8",
21
21
  "joi": "^17.13.3",
22
22
  "lodash": "^4.17.21",
23
- "mathjs": "^10.6.1",
24
23
  "nanoid": "^3.3.11",
25
24
  "node-fetch": "^2.6.7",
26
25
  "node-sql-parser": "^4.18.0",
@@ -39,5 +38,5 @@
39
38
  "url": "git+https://github.com/nocobase/nocobase.git",
40
39
  "directory": "packages/database"
41
40
  },
42
- "gitHead": "b77a33ee933ae6e09d2d5dce017ca15d8552d57b"
41
+ "gitHead": "f4ab788fc6c17915157617026dfbba6f0d78eaac"
43
42
  }