@nocobase/database 1.6.0-alpha.18 → 1.6.0-alpha.19

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.
@@ -9,9 +9,10 @@
9
9
  import { Transactionable } from 'sequelize/types';
10
10
  import { Collection } from '../collection';
11
11
  import { FindOptions } from '../repository';
12
- import { MultipleRelationRepository } from './multiple-relation-repository';
12
+ import { MultipleRelationRepository } from '../relation-repository/multiple-relation-repository';
13
13
  import Database from '../database';
14
14
  import { Model } from '../model';
15
+ import { UpdateAssociationOptions } from '../update-associations';
15
16
  export declare class BelongsToArrayAssociation {
16
17
  db: Database;
17
18
  associationType: string;
@@ -33,6 +34,7 @@ export declare class BelongsToArrayAssociation {
33
34
  generateInclude(): {
34
35
  on: import("sequelize/types/utils").Literal;
35
36
  };
37
+ update(instance: Model, value: any, options?: UpdateAssociationOptions): Promise<void>;
36
38
  }
37
39
  export declare class BelongsToArrayRepository extends MultipleRelationRepository {
38
40
  private belongsToArrayAssociation;
@@ -7,9 +7,11 @@
7
7
  * For more information, please refer to: https://www.nocobase.com/agreement.
8
8
  */
9
9
 
10
+ var __create = Object.create;
10
11
  var __defProp = Object.defineProperty;
11
12
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
12
13
  var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
13
15
  var __hasOwnProp = Object.prototype.hasOwnProperty;
14
16
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
15
17
  var __export = (target, all) => {
@@ -24,6 +26,14 @@ var __copyProps = (to, from, except, desc) => {
24
26
  }
25
27
  return to;
26
28
  };
29
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
30
+ // If the importer is in node compatibility mode or this is not an ESM
31
+ // file that has been converted to a CommonJS file using a Babel-
32
+ // compatible transform (i.e. "__esModule" has not been set), then set
33
+ // "default" to the CommonJS "module.exports" for node compatibility.
34
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
35
+ mod
36
+ ));
27
37
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
38
  var __decorateClass = (decorators, target, key, kind) => {
29
39
  var result = kind > 1 ? void 0 : kind ? __getOwnPropDesc(target, key) : target;
@@ -39,9 +49,9 @@ __export(belongs_to_array_repository_exports, {
39
49
  BelongsToArrayRepository: () => BelongsToArrayRepository
40
50
  });
41
51
  module.exports = __toCommonJS(belongs_to_array_repository_exports);
42
- var import_lodash = require("lodash");
52
+ var import_lodash = __toESM(require("lodash"));
43
53
  var import_transaction_decorator = require("../decorators/transaction-decorator");
44
- var import_multiple_relation_repository = require("./multiple-relation-repository");
54
+ var import_multiple_relation_repository = require("../relation-repository/multiple-relation-repository");
45
55
  const transaction = (0, import_transaction_decorator.transactionWrapperBuilder)(function() {
46
56
  return this.collection.model.sequelize.transaction();
47
57
  });
@@ -83,6 +93,19 @@ const _BelongsToArrayAssociation = class _BelongsToArrayAssociation {
83
93
  on: this.db.sequelize.literal(`${left}=any(${right})`)
84
94
  };
85
95
  }
96
+ async update(instance, value, options = {}) {
97
+ await instance.update(
98
+ {
99
+ [this.as]: value
100
+ },
101
+ {
102
+ values: {
103
+ [this.as]: value
104
+ },
105
+ transaction: options == null ? void 0 : options.transaction
106
+ }
107
+ );
108
+ }
86
109
  };
87
110
  __name(_BelongsToArrayAssociation, "BelongsToArrayAssociation");
88
111
  let BelongsToArrayAssociation = _BelongsToArrayAssociation;
@@ -109,7 +132,7 @@ const _BelongsToArrayRepository = class _BelongsToArrayRepository extends import
109
132
  addFilter[targetKey] = options.filterByTk;
110
133
  }
111
134
  const findOptions = {
112
- ...(0, import_lodash.omit)(options, ["filterByTk", "where", "values", "attributes"]),
135
+ ...import_lodash.default.omit(options, ["filterByTk", "where", "values", "attributes"]),
113
136
  filter: {
114
137
  $and: [options.filter || {}, addFilter]
115
138
  }
package/lib/database.js CHANGED
@@ -220,7 +220,7 @@ const _Database = class _Database extends import_events.EventEmitter {
220
220
  autoGenId: false,
221
221
  timestamps: false,
222
222
  dumpRules: "required",
223
- migrationRules: ["schema-only", "overwrite", "skip"],
223
+ migrationRules: ["schema-only", "overwrite"],
224
224
  origin: "@nocobase/database",
225
225
  fields: [{ type: "string", name: "name", primaryKey: true }]
226
226
  });
package/lib/index.d.ts CHANGED
@@ -26,7 +26,7 @@ export * from './relation-repository/belongs-to-repository';
26
26
  export * from './relation-repository/hasmany-repository';
27
27
  export * from './relation-repository/multiple-relation-repository';
28
28
  export * from './relation-repository/single-relation-repository';
29
- export * from './relation-repository/belongs-to-array-repository';
29
+ export * from './belongs-to-array/belongs-to-array-repository';
30
30
  export * from './repository';
31
31
  export * from './update-associations';
32
32
  export { snakeCase } from './utils';
package/lib/index.js CHANGED
@@ -81,7 +81,7 @@ __reExport(src_exports, require("./relation-repository/belongs-to-repository"),
81
81
  __reExport(src_exports, require("./relation-repository/hasmany-repository"), module.exports);
82
82
  __reExport(src_exports, require("./relation-repository/multiple-relation-repository"), module.exports);
83
83
  __reExport(src_exports, require("./relation-repository/single-relation-repository"), module.exports);
84
- __reExport(src_exports, require("./relation-repository/belongs-to-array-repository"), module.exports);
84
+ __reExport(src_exports, require("./belongs-to-array/belongs-to-array-repository"), module.exports);
85
85
  __reExport(src_exports, require("./repository"), module.exports);
86
86
  __reExport(src_exports, require("./update-associations"), module.exports);
87
87
  var import_utils = require("./utils");
@@ -133,7 +133,7 @@ __reExport(src_exports, require("./dialects"), module.exports);
133
133
  ...require("./relation-repository/hasmany-repository"),
134
134
  ...require("./relation-repository/multiple-relation-repository"),
135
135
  ...require("./relation-repository/single-relation-repository"),
136
- ...require("./relation-repository/belongs-to-array-repository"),
136
+ ...require("./belongs-to-array/belongs-to-array-repository"),
137
137
  ...require("./repository"),
138
138
  ...require("./update-associations"),
139
139
  ...require("./value-parsers"),
@@ -31,17 +31,30 @@ __export(boolean_exports, {
31
31
  module.exports = __toCommonJS(boolean_exports);
32
32
  var import_sequelize = require("sequelize");
33
33
  var boolean_default = {
34
- $isFalsy() {
34
+ $isFalsy(value) {
35
+ if (value === true || value === "true") {
36
+ return {
37
+ [import_sequelize.Op.or]: {
38
+ [import_sequelize.Op.is]: null,
39
+ [import_sequelize.Op.eq]: false
40
+ }
41
+ };
42
+ }
43
+ return {
44
+ [import_sequelize.Op.eq]: true
45
+ };
46
+ },
47
+ $isTruly(value) {
48
+ if (value === true || value === "true") {
49
+ return {
50
+ [import_sequelize.Op.eq]: true
51
+ };
52
+ }
35
53
  return {
36
54
  [import_sequelize.Op.or]: {
37
55
  [import_sequelize.Op.is]: null,
38
56
  [import_sequelize.Op.eq]: false
39
57
  }
40
58
  };
41
- },
42
- $isTruly() {
43
- return {
44
- [import_sequelize.Op.eq]: true
45
- };
46
59
  }
47
60
  };
@@ -51,6 +51,7 @@ module.exports = __toCommonJS(single_relation_repository_exports);
51
51
  var import_target_collection_decorator = __toESM(require("../decorators/target-collection-decorator"));
52
52
  var import_update_associations = require("../update-associations");
53
53
  var import_relation_repository = require("./relation-repository");
54
+ var import_lodash = __toESM(require("lodash"));
54
55
  const _SingleRelationRepository = class _SingleRelationRepository extends import_relation_repository.RelationRepository {
55
56
  async remove(options) {
56
57
  const transaction2 = await this.getTransaction(options);
@@ -103,7 +104,7 @@ const _SingleRelationRepository = class _SingleRelationRepository extends import
103
104
  throw new Error("The record does not exist");
104
105
  }
105
106
  await (0, import_update_associations.updateModelByValues)(target, options == null ? void 0 : options.values, {
106
- ...options,
107
+ ...import_lodash.default.omit(options, "values"),
107
108
  transaction: transaction2
108
109
  });
109
110
  if (options.hooks !== false) {
@@ -13,7 +13,7 @@ import { Database } from './database';
13
13
  import { ArrayFieldRepository } from './field-repository/array-field-repository';
14
14
  import { Model } from './model';
15
15
  import operators from './operators';
16
- import { BelongsToArrayRepository } from './relation-repository/belongs-to-array-repository';
16
+ import { BelongsToArrayRepository } from './belongs-to-array/belongs-to-array-repository';
17
17
  import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
18
18
  import { BelongsToRepository } from './relation-repository/belongs-to-repository';
19
19
  import { HasManyRepository } from './relation-repository/hasmany-repository';
package/lib/repository.js CHANGED
@@ -62,7 +62,7 @@ var import_array_field_repository = require("./field-repository/array-field-repo
62
62
  var import_fields = require("./fields");
63
63
  var import_filter_parser = __toESM(require("./filter-parser"));
64
64
  var import_options_parser = require("./options-parser");
65
- var import_belongs_to_array_repository = require("./relation-repository/belongs-to-array-repository");
65
+ var import_belongs_to_array_repository = require("./belongs-to-array/belongs-to-array-repository");
66
66
  var import_belongs_to_many_repository = require("./relation-repository/belongs-to-many-repository");
67
67
  var import_belongs_to_repository = require("./relation-repository/belongs-to-repository");
68
68
  var import_hasmany_repository = require("./relation-repository/hasmany-repository");
@@ -13,7 +13,9 @@ export declare function modelAssociations(instance: Model): {
13
13
  [key: string]: Association<import("sequelize").Model<any, any>, import("sequelize").Model<any, any>>;
14
14
  };
15
15
  export declare function belongsToManyAssociations(instance: Model): Array<BelongsToMany>;
16
- export declare function modelAssociationByKey(instance: Model, key: string): Association;
16
+ export declare function modelAssociationByKey(instance: Model, key: string): Association & {
17
+ update?: (instance: Model, value: any, options: UpdateAssociationOptions) => Promise<any>;
18
+ };
17
19
  type UpdateValue = {
18
20
  [key: string]: any;
19
21
  };
@@ -26,7 +28,7 @@ interface UpdateOptions extends Transactionable {
26
28
  sanitized?: boolean;
27
29
  sourceModel?: Model;
28
30
  }
29
- interface UpdateAssociationOptions extends Transactionable, Hookable {
31
+ export interface UpdateAssociationOptions extends Transactionable, Hookable {
30
32
  updateAssociationValues?: string[];
31
33
  sourceModel?: Model;
32
34
  context?: any;
@@ -49,7 +51,7 @@ export declare function updateAssociations(instance: Model, values: any, options
49
51
  * @param value
50
52
  * @param options
51
53
  */
52
- export declare function updateAssociation(instance: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<boolean>;
54
+ export declare function updateAssociation(instance: Model, key: string, value: any, options?: UpdateAssociationOptions): Promise<any>;
53
55
  /**
54
56
  * update belongsTo and HasOne
55
57
  * @param model
@@ -51,18 +51,7 @@ module.exports = __toCommonJS(update_associations_exports);
51
51
  var import_lodash = __toESM(require("lodash"));
52
52
  var import_model = require("./model");
53
53
  var import_update_guard = require("./update-guard");
54
- function isUndefinedOrNull(value) {
55
- return typeof value === "undefined" || value === null;
56
- }
57
- __name(isUndefinedOrNull, "isUndefinedOrNull");
58
- function isStringOrNumber(value) {
59
- return typeof value === "string" || typeof value === "number";
60
- }
61
- __name(isStringOrNumber, "isStringOrNumber");
62
- function getKeysByPrefix(keys, prefix) {
63
- return keys.filter((key) => key.startsWith(`${prefix}.`)).map((key) => key.substring(prefix.length + 1));
64
- }
65
- __name(getKeysByPrefix, "getKeysByPrefix");
54
+ var import_utils = require("./utils");
66
55
  function modelAssociations(instance) {
67
56
  return instance.constructor.associations;
68
57
  }
@@ -183,6 +172,9 @@ async function updateAssociation(instance, key, value, options = {}) {
183
172
  if (options.associationContext && isReverseAssociationPair(association, options.associationContext)) {
184
173
  return false;
185
174
  }
175
+ if (association.update) {
176
+ return association.update(instance, value, options);
177
+ }
186
178
  switch (association.associationType) {
187
179
  case "HasOne":
188
180
  case "BelongsTo":
@@ -205,14 +197,14 @@ async function updateSingleAssociation(model, key, value, options = {}) {
205
197
  throw new Error(`The value of '${key}' cannot be in array format`);
206
198
  }
207
199
  const { recursive, context, updateAssociationValues = [], transaction } = options;
208
- const keys = getKeysByPrefix(updateAssociationValues, key);
200
+ const keys = (0, import_utils.getKeysByPrefix)(updateAssociationValues, key);
209
201
  const setAccessor = association.accessors.set;
210
202
  const removeAssociation = /* @__PURE__ */ __name(async () => {
211
203
  await model[setAccessor](null, { transaction });
212
204
  model.setDataValue(key, null);
213
205
  return true;
214
206
  }, "removeAssociation");
215
- if (isUndefinedOrNull(value)) {
207
+ if ((0, import_utils.isUndefinedOrNull)(value)) {
216
208
  return await removeAssociation();
217
209
  }
218
210
  if (association.associationType === "HasOne" && !model.get(association.sourceKeyAttribute)) {
@@ -226,7 +218,7 @@ async function updateSingleAssociation(model, key, value, options = {}) {
226
218
  );
227
219
  }
228
220
  }, "checkBelongsToForeignKeyValue");
229
- if (isStringOrNumber(value)) {
221
+ if ((0, import_utils.isStringOrNumber)(value)) {
230
222
  await model[setAccessor](value, { context, transaction });
231
223
  return true;
232
224
  }
@@ -245,7 +237,7 @@ async function updateSingleAssociation(model, key, value, options = {}) {
245
237
  M = association.target;
246
238
  dataKey = M.primaryKeyAttribute;
247
239
  }
248
- if (isStringOrNumber(value[dataKey])) {
240
+ if ((0, import_utils.isStringOrNumber)(value[dataKey])) {
249
241
  const instance2 = await M.findOne({
250
242
  where: {
251
243
  [dataKey]: value[dataKey]
@@ -298,10 +290,10 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
298
290
  return false;
299
291
  }
300
292
  const { recursive, context, updateAssociationValues = [], transaction } = options;
301
- const keys = getKeysByPrefix(updateAssociationValues, key);
293
+ const keys = (0, import_utils.getKeysByPrefix)(updateAssociationValues, key);
302
294
  const setAccessor = association.accessors.set;
303
295
  const createAccessor = association.accessors.create;
304
- if (isUndefinedOrNull(value)) {
296
+ if ((0, import_utils.isUndefinedOrNull)(value)) {
305
297
  await model[setAccessor](null, { transaction, context, individualHooks: true, validate: false });
306
298
  model.setDataValue(key, null);
307
299
  return;
@@ -309,7 +301,7 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
309
301
  if (association.associationType === "HasMany" && !model.get(association.sourceKeyAttribute)) {
310
302
  throw new Error(`The source key ${association.sourceKeyAttribute} is not set in ${model.constructor.name}`);
311
303
  }
312
- if (isStringOrNumber(value)) {
304
+ if ((0, import_utils.isStringOrNumber)(value)) {
313
305
  await model[setAccessor](value, { transaction, context, individualHooks: true, validate: false });
314
306
  return;
315
307
  }
@@ -317,10 +309,10 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
317
309
  const setItems = [];
318
310
  const objectItems = [];
319
311
  for (const item of value) {
320
- if (isUndefinedOrNull(item)) {
312
+ if ((0, import_utils.isUndefinedOrNull)(item)) {
321
313
  continue;
322
314
  }
323
- if (isStringOrNumber(item)) {
315
+ if ((0, import_utils.isStringOrNumber)(item)) {
324
316
  setItems.push(item);
325
317
  } else if (item instanceof import_model.Model) {
326
318
  setItems.push(item);
@@ -360,10 +352,10 @@ async function updateMultipleAssociation(model, key, value, options = {}) {
360
352
  if (throughValue) {
361
353
  accessorOptions["through"] = throughValue;
362
354
  }
363
- if (pk !== targetKey && !isUndefinedOrNull(item[pk]) && isUndefinedOrNull(item[targetKey])) {
355
+ if (pk !== targetKey && !(0, import_utils.isUndefinedOrNull)(item[pk]) && (0, import_utils.isUndefinedOrNull)(item[targetKey])) {
364
356
  throw new Error(`${targetKey} field value is empty`);
365
357
  }
366
- if (isUndefinedOrNull(item[targetKey])) {
358
+ if ((0, import_utils.isUndefinedOrNull)(item[targetKey])) {
367
359
  const instance = await model[createAccessor](item, accessorOptions);
368
360
  await updateAssociations(instance, item, {
369
361
  ...options,
package/lib/utils.d.ts CHANGED
@@ -13,3 +13,6 @@ export declare function getTableName(collectionName: string, options: any): any;
13
13
  export declare function snakeCase(name: string): any;
14
14
  export declare function patchSequelizeQueryInterface(db: Database): void;
15
15
  export declare function percent2float(value: string): number;
16
+ export declare function isUndefinedOrNull(value: any): boolean;
17
+ export declare function isStringOrNumber(value: any): boolean;
18
+ export declare function getKeysByPrefix(keys: string[], prefix: string): string[];
package/lib/utils.js CHANGED
@@ -38,7 +38,10 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
38
38
  var utils_exports = {};
39
39
  __export(utils_exports, {
40
40
  checkIdentifier: () => checkIdentifier,
41
+ getKeysByPrefix: () => getKeysByPrefix,
41
42
  getTableName: () => getTableName,
43
+ isStringOrNumber: () => isStringOrNumber,
44
+ isUndefinedOrNull: () => isUndefinedOrNull,
42
45
  md5: () => md5,
43
46
  patchSequelizeQueryInterface: () => patchSequelizeQueryInterface,
44
47
  percent2float: () => percent2float,
@@ -125,10 +128,25 @@ function percent2float(value) {
125
128
  return parseFloat(value) * v / (100 * v);
126
129
  }
127
130
  __name(percent2float, "percent2float");
131
+ function isUndefinedOrNull(value) {
132
+ return typeof value === "undefined" || value === null;
133
+ }
134
+ __name(isUndefinedOrNull, "isUndefinedOrNull");
135
+ function isStringOrNumber(value) {
136
+ return typeof value === "string" || typeof value === "number";
137
+ }
138
+ __name(isStringOrNumber, "isStringOrNumber");
139
+ function getKeysByPrefix(keys, prefix) {
140
+ return keys.filter((key) => key.startsWith(`${prefix}.`)).map((key) => key.substring(prefix.length + 1));
141
+ }
142
+ __name(getKeysByPrefix, "getKeysByPrefix");
128
143
  // Annotate the CommonJS export names for ESM import in node:
129
144
  0 && (module.exports = {
130
145
  checkIdentifier,
146
+ getKeysByPrefix,
131
147
  getTableName,
148
+ isStringOrNumber,
149
+ isUndefinedOrNull,
132
150
  md5,
133
151
  patchSequelizeQueryInterface,
134
152
  percent2float,
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.6.0-alpha.18",
3
+ "version": "1.6.0-alpha.19",
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.6.0-alpha.18",
10
- "@nocobase/utils": "1.6.0-alpha.18",
9
+ "@nocobase/logger": "1.6.0-alpha.19",
10
+ "@nocobase/utils": "1.6.0-alpha.19",
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": "db361bebe9ed4a5ca19fc14183fe4576443568ef"
41
+ "gitHead": "667b67a161c841c01d4ab337afa42cd29d6cf540"
42
42
  }