@nocobase/database 0.12.0-alpha.4 → 0.13.0-alpha.1

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/database.js CHANGED
@@ -680,6 +680,7 @@ class Database extends _events().EventEmitter {
680
680
  console.log('Connection has been established successfully.');
681
681
  return true;
682
682
  } catch (error) {
683
+ console.log(`Unable to connect to the database: ${error.message}`);
683
684
  if (count >= retry) {
684
685
  throw new Error('Connection failed, please check your database connection credentials and try again.');
685
686
  }
@@ -146,7 +146,7 @@ class EagerLoadingTree {
146
146
  };
147
147
  const loadRecursive = /*#__PURE__*/function () {
148
148
  var _ref = _asyncToGenerator(function* (node, ids) {
149
- const modelPrimaryKey = node.model.primaryKeyAttribute;
149
+ const modelPrimaryKey = node.model.primaryKeyField || node.model.primaryKeyAttribute;
150
150
  let instances = [];
151
151
  // load instances from database
152
152
  if (!node.parent) {
@@ -120,7 +120,15 @@ class SortField extends _field.Field {
120
120
  const doInit = /*#__PURE__*/function () {
121
121
  var _ref6 = _asyncToGenerator(function* (scopeKey = null, scopeValue = null) {
122
122
  const queryInterface = _this.collection.db.sequelize.getQueryInterface();
123
+ if (scopeKey) {
124
+ const scopeField = _this.collection.getField(scopeKey);
125
+ if (!scopeField) {
126
+ throw new Error(`can not find scope field ${scopeKey} for collection ${_this.collection.name}`);
127
+ }
128
+ scopeKey = _this.collection.model.rawAttributes[scopeKey].field;
129
+ }
123
130
  const quotedOrderField = queryInterface.quoteIdentifier(orderField);
131
+ const sortColumnName = _this.collection.model.rawAttributes[_this.name].field;
124
132
  const sql = `
125
133
  WITH ordered_table AS (
126
134
  SELECT *, ROW_NUMBER() OVER (${scopeKey ? `PARTITION BY ${queryInterface.quoteIdentifier(scopeKey)}` : ''} ORDER BY ${quotedOrderField}) AS new_sequence_number
@@ -136,11 +144,11 @@ class SortField extends _field.Field {
136
144
  )
137
145
  ${_this.collection.db.inDialect('mysql') ? `
138
146
  UPDATE ${_this.collection.quotedTableName()}, ordered_table
139
- SET ${_this.collection.quotedTableName()}.${_this.name} = ordered_table.new_sequence_number
147
+ SET ${_this.collection.quotedTableName()}.${sortColumnName} = ordered_table.new_sequence_number
140
148
  WHERE ${_this.collection.quotedTableName()}.${quotedOrderField} = ordered_table.${quotedOrderField}
141
149
  ` : `
142
150
  UPDATE ${_this.collection.quotedTableName()}
143
- SET ${queryInterface.quoteIdentifier(_this.name)} = ordered_table.new_sequence_number
151
+ SET ${queryInterface.quoteIdentifier(sortColumnName)} = ordered_table.new_sequence_number
144
152
  FROM ordered_table
145
153
  WHERE ${_this.collection.quotedTableName()}.${quotedOrderField} = ${queryInterface.quoteIdentifier('ordered_table')}.${quotedOrderField};
146
154
  `}
@@ -139,19 +139,8 @@ class BelongsToManyRepository extends _multipleRelationRepository.MultipleRelati
139
139
  setTargets(call, options) {
140
140
  var _this4 = this;
141
141
  return _asyncToGenerator(function* () {
142
- let handleKeys;
142
+ const handleKeys = _this4.convertTks(options);
143
143
  const transaction = yield _this4.getTransaction(options, false);
144
- if (_lodash().default.isPlainObject(options)) {
145
- options = options.tk || [];
146
- }
147
- if (_lodash().default.isString(options) || _lodash().default.isNumber(options)) {
148
- handleKeys = [options];
149
- } // if it is type primaryKeyWithThroughValues
150
- else if (_lodash().default.isArray(options) && options.length == 2 && _lodash().default.isPlainObject(options[0][1])) {
151
- handleKeys = [options];
152
- } else {
153
- handleKeys = options;
154
- }
155
144
  const sourceModel = yield _this4.getSourceModel(transaction);
156
145
  const setObj = handleKeys.reduce((carry, item) => {
157
146
  if (Array.isArray(item)) {
@@ -1,10 +1,9 @@
1
- import { AggregateOptions, DestroyOptions, FindOptions, TargetKey, TK } from '../repository';
1
+ import { AggregateOptions, DestroyOptions, FindOptions, TK, TargetKey } from '../repository';
2
2
  import { AssociatedOptions, MultipleRelationRepository } from './multiple-relation-repository';
3
3
  export declare class HasManyRepository extends MultipleRelationRepository {
4
4
  find(options?: FindOptions): Promise<any>;
5
5
  aggregate(options: AggregateOptions): Promise<any>;
6
6
  destroy(options?: TK | DestroyOptions): Promise<boolean>;
7
- handleKeyOfAdd(options: any): any;
8
7
  set(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
9
8
  add(options: TargetKey | TargetKey[] | AssociatedOptions): Promise<void>;
10
9
  accessors(): import("sequelize").MultiAssociationAccessors;
@@ -101,21 +101,12 @@ class HasManyRepository extends _multipleRelationRepository.MultipleRelationRepo
101
101
  return true;
102
102
  })();
103
103
  }
104
- handleKeyOfAdd(options) {
105
- let handleKeys;
106
- if (typeof options !== 'object' && !Array.isArray(options)) {
107
- handleKeys = [options];
108
- } else {
109
- handleKeys = options['pk'];
110
- }
111
- return handleKeys;
112
- }
113
104
  set(options) {
114
105
  var _this4 = this;
115
106
  return _asyncToGenerator(function* () {
116
107
  const transaction = yield _this4.getTransaction(options);
117
108
  const sourceModel = yield _this4.getSourceModel(transaction);
118
- yield sourceModel[_this4.accessors().set](_this4.handleKeyOfAdd(options), {
109
+ yield sourceModel[_this4.accessors().set](_this4.convertTks(options), {
119
110
  transaction
120
111
  });
121
112
  })();
@@ -125,7 +116,7 @@ class HasManyRepository extends _multipleRelationRepository.MultipleRelationRepo
125
116
  return _asyncToGenerator(function* () {
126
117
  const transaction = yield _this5.getTransaction(options);
127
118
  const sourceModel = yield _this5.getSourceModel(transaction);
128
- yield sourceModel[_this5.accessors().add](_this5.handleKeyOfAdd(options), {
119
+ yield sourceModel[_this5.accessors().add](_this5.convertTks(options), {
129
120
  transaction
130
121
  });
131
122
  })();
@@ -143,13 +134,13 @@ __decorate([(0, _relationRepository.transaction)((args, transaction) => {
143
134
  })], HasManyRepository.prototype, "destroy", null);
144
135
  __decorate([(0, _relationRepository.transaction)((args, transaction) => {
145
136
  return {
146
- pk: args[0],
137
+ tk: args[0],
147
138
  transaction
148
139
  };
149
140
  })], HasManyRepository.prototype, "set", null);
150
141
  __decorate([(0, _relationRepository.transaction)((args, transaction) => {
151
142
  return {
152
- pk: args[0],
143
+ tk: args[0],
153
144
  transaction
154
145
  };
155
146
  })], HasManyRepository.prototype, "add", null);
@@ -1,5 +1,5 @@
1
1
  import { MultiAssociationAccessors, Transaction, Transactionable } from 'sequelize';
2
- import { CommonFindOptions, CountOptions, DestroyOptions, Filter, FindOneOptions, FindOptions, TargetKey, TK, UpdateOptions } from '../repository';
2
+ import { CommonFindOptions, CountOptions, DestroyOptions, Filter, FindOneOptions, FindOptions, TK, TargetKey, UpdateOptions } from '../repository';
3
3
  import { RelationRepository } from './relation-repository';
4
4
  export type FindAndCountOptions = CommonFindOptions;
5
5
  export interface AssociatedOptions extends Transactionable {
@@ -4,16 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.MultipleRelationRepository = void 0;
7
- function _sequelize() {
8
- const data = require("sequelize");
9
- _sequelize = function _sequelize() {
7
+ function _lodash() {
8
+ const data = _interopRequireDefault(require("lodash"));
9
+ _lodash = function _lodash() {
10
10
  return data;
11
11
  };
12
12
  return data;
13
13
  }
14
- function _lodash() {
15
- const data = _interopRequireDefault(require("lodash"));
16
- _lodash = function _lodash() {
14
+ function _sequelize() {
15
+ const data = require("sequelize");
16
+ _sequelize = function _sequelize() {
17
17
  return data;
18
18
  };
19
19
  return data;
@@ -113,12 +113,8 @@ class MultipleRelationRepository extends _relationRepository.RelationRepository
113
113
  var _this5 = this;
114
114
  return _asyncToGenerator(function* () {
115
115
  const transaction = yield _this5.getTransaction(options);
116
- let handleKeys = options['tk'];
117
- if (!Array.isArray(handleKeys)) {
118
- handleKeys = [handleKeys];
119
- }
120
116
  const sourceModel = yield _this5.getSourceModel(transaction);
121
- yield sourceModel[_this5.accessors().removeMultiple](handleKeys, {
117
+ yield sourceModel[_this5.accessors().removeMultiple](_this5.convertTks(options), {
122
118
  transaction
123
119
  });
124
120
  return;
@@ -17,6 +17,8 @@ export declare abstract class RelationRepository {
17
17
  db: Database;
18
18
  constructor(sourceCollection: Collection, association: string, sourceKeyValue: string | number);
19
19
  get collection(): Collection<any, any>;
20
+ convertTk(options: any): any;
21
+ convertTks(options: any): any[];
20
22
  targetKey(): any;
21
23
  protected accessors(): import("sequelize").SingleAssociationAccessors | import("sequelize").MultiAssociationAccessors;
22
24
  create(options?: CreateOptions): Promise<any>;
@@ -58,6 +58,20 @@ class RelationRepository {
58
58
  get collection() {
59
59
  return this.db.getCollection(this.targetModel.name);
60
60
  }
61
+ convertTk(options) {
62
+ let tk = options;
63
+ if (typeof options === 'object' && options['tk']) {
64
+ tk = options['tk'];
65
+ }
66
+ return tk;
67
+ }
68
+ convertTks(options) {
69
+ let tk = this.convertTk(options);
70
+ if (typeof tk === 'string') {
71
+ tk = tk.split(',');
72
+ }
73
+ return _lodash().default.castArray(tk);
74
+ }
61
75
  targetKey() {
62
76
  return this.associationField.targetKey;
63
77
  }
@@ -44,9 +44,8 @@ class SingleRelationRepository extends _relationRepository.RelationRepository {
44
44
  var _this2 = this;
45
45
  return _asyncToGenerator(function* () {
46
46
  const transaction = yield _this2.getTransaction(options);
47
- const handleKey = _lodash().default.isPlainObject(options) ? options.tk : options;
48
47
  const sourceModel = yield _this2.getSourceModel(transaction);
49
- return yield sourceModel[_this2.accessors().set](handleKey, {
48
+ return yield sourceModel[_this2.accessors().set](_this2.convertTk(options), {
50
49
  transaction
51
50
  });
52
51
  })();
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "0.12.0-alpha.4",
3
+ "version": "0.13.0-alpha.1",
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.12.0-alpha.4",
10
- "@nocobase/utils": "0.12.0-alpha.4",
9
+ "@nocobase/logger": "0.13.0-alpha.1",
10
+ "@nocobase/utils": "0.13.0-alpha.1",
11
11
  "async-mutex": "^0.3.2",
12
12
  "cron-parser": "4.4.0",
13
13
  "dayjs": "^1.11.8",
@@ -29,5 +29,5 @@
29
29
  "url": "git+https://github.com/nocobase/nocobase.git",
30
30
  "directory": "packages/database"
31
31
  },
32
- "gitHead": "215dc3b2437c501ca903b56cc378ab5e81c8a11c"
32
+ "gitHead": "0ebd4e85a1b0b0d0943768ab6cb5c3d824562239"
33
33
  }