@nocobase/database 1.8.0-beta.1 → 1.8.0-beta.12

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.
@@ -22,6 +22,7 @@ export declare class BelongsToArrayAssociation {
22
22
  targetKey: string;
23
23
  identifierField: string;
24
24
  as: string;
25
+ options: any;
25
26
  constructor(options: {
26
27
  db: Database;
27
28
  source: Model;
@@ -64,8 +64,10 @@ const _BelongsToArrayAssociation = class _BelongsToArrayAssociation {
64
64
  targetKey;
65
65
  identifierField;
66
66
  as;
67
+ options;
67
68
  constructor(options) {
68
69
  const { db, source, as, foreignKey, target, targetKey } = options;
70
+ this.options = options;
69
71
  this.associationType = "BelongsToArray";
70
72
  this.db = db;
71
73
  this.source = source;
@@ -10,7 +10,7 @@ import { Model } from '../model';
10
10
  import { BaseColumnFieldOptions, Field } from './field';
11
11
  export declare class ContextField extends Field {
12
12
  get dataType(): any;
13
- listener: (model: Model, options: any) => Promise<void>;
13
+ listener: (instances: Model[], options: any) => Promise<void>;
14
14
  bind(): void;
15
15
  unbind(): void;
16
16
  }
@@ -48,16 +48,20 @@ const _ContextField = class _ContextField extends import_field.Field {
48
48
  const type = this.options.dataType || "string";
49
49
  return import_sequelize.DataTypes[type.toUpperCase()] || import_sequelize.DataTypes.STRING;
50
50
  }
51
- listener = /* @__PURE__ */ __name(async (model, options) => {
51
+ listener = /* @__PURE__ */ __name(async (instances, options) => {
52
+ instances = Array.isArray(instances) ? instances : [instances];
52
53
  const { name, dataIndex } = this.options;
53
54
  const { context } = options;
54
- model.set(name, import_lodash.default.get(context, dataIndex));
55
- model.changed(name, true);
55
+ for (const instance of instances) {
56
+ instance.set(name, import_lodash.default.get(context, dataIndex));
57
+ instance.changed(name, true);
58
+ }
56
59
  }, "listener");
57
60
  bind() {
58
61
  super.bind();
59
62
  const { createOnly } = this.options;
60
63
  this.on("beforeCreate", this.listener);
64
+ this.on("beforeBulkCreate", this.listener);
61
65
  if (!createOnly) {
62
66
  this.on("beforeUpdate", this.listener);
63
67
  }
@@ -66,6 +70,7 @@ const _ContextField = class _ContextField extends import_field.Field {
66
70
  super.unbind();
67
71
  const { createOnly } = this.options;
68
72
  this.off("beforeCreate", this.listener);
73
+ this.off("beforeBulkCreate", this.listener);
69
74
  if (!createOnly) {
70
75
  this.off("beforeUpdate", this.listener);
71
76
  }
@@ -72,27 +72,32 @@ const _PasswordField = class _PasswordField extends import_field.Field {
72
72
  }
73
73
  init() {
74
74
  const { name } = this.options;
75
- this.listener = async (model) => {
76
- if (!model.changed(name)) {
77
- return;
78
- }
79
- const value = model.get(name);
80
- if (value) {
81
- const hash = await this.hash(value);
82
- model.set(name, hash);
83
- } else {
84
- model.set(name, model.previous(name));
75
+ this.listener = async (instances) => {
76
+ instances = Array.isArray(instances) ? instances : [instances];
77
+ for (const instance of instances) {
78
+ if (!instance.changed(name)) {
79
+ continue;
80
+ }
81
+ const value = instance.get(name);
82
+ if (value) {
83
+ const hash = await this.hash(value);
84
+ instance.set(name, hash);
85
+ } else {
86
+ instance.set(name, instance.previous(name));
87
+ }
85
88
  }
86
89
  };
87
90
  }
88
91
  bind() {
89
92
  super.bind();
90
93
  this.on("beforeCreate", this.listener);
94
+ this.on("beforeBulkCreate", this.listener);
91
95
  this.on("beforeUpdate", this.listener);
92
96
  }
93
97
  unbind() {
94
98
  super.unbind();
95
99
  this.off("beforeCreate", this.listener);
100
+ this.off("beforeBulkCreate", this.listener);
96
101
  this.off("beforeUpdate", this.listener);
97
102
  }
98
103
  };
package/lib/helpers.js CHANGED
@@ -96,6 +96,29 @@ function extractSSLOptionsFromEnv() {
96
96
  });
97
97
  }
98
98
  __name(extractSSLOptionsFromEnv, "extractSSLOptionsFromEnv");
99
+ function getPoolOptions() {
100
+ const options = {};
101
+ if (process.env.DB_POOL_MAX) {
102
+ options.max = Number.parseInt(process.env.DB_POOL_MAX, 10);
103
+ }
104
+ if (process.env.DB_POOL_MIN) {
105
+ options.min = Number.parseInt(process.env.DB_POOL_MIN, 10);
106
+ }
107
+ if (process.env.DB_POOL_IDLE) {
108
+ options.idle = Number.parseInt(process.env.DB_POOL_IDLE, 10);
109
+ }
110
+ if (process.env.DB_POOL_ACQUIRE) {
111
+ options.acquire = Number.parseInt(process.env.DB_POOL_ACQUIRE, 10);
112
+ }
113
+ if (process.env.DB_POOL_EVICT) {
114
+ options.evict = Number.parseInt(process.env.DB_POOL_EVICT, 10);
115
+ }
116
+ if (process.env.DB_POOL_MAX_USES) {
117
+ options.maxUses = Number.parseInt(process.env.DB_POOL_MAX_USES, 10) || Number.POSITIVE_INFINITY;
118
+ }
119
+ return options;
120
+ }
121
+ __name(getPoolOptions, "getPoolOptions");
99
122
  async function parseDatabaseOptionsFromEnv() {
100
123
  const databaseOptions = {
101
124
  logging: process.env.DB_LOGGING == "on" ? customLogger : false,
@@ -109,7 +132,8 @@ async function parseDatabaseOptionsFromEnv() {
109
132
  timezone: process.env.DB_TIMEZONE,
110
133
  tablePrefix: process.env.DB_TABLE_PREFIX,
111
134
  schema: process.env.DB_SCHEMA,
112
- underscored: process.env.DB_UNDERSCORED === "true"
135
+ underscored: process.env.DB_UNDERSCORED === "true",
136
+ pool: getPoolOptions()
113
137
  };
114
138
  const sslOptions = await extractSSLOptionsFromEnv();
115
139
  if (Object.keys(sslOptions).length) {
package/lib/index.d.ts CHANGED
@@ -32,6 +32,7 @@ export * from './relation-repository/hasone-repository';
32
32
  export * from './relation-repository/multiple-relation-repository';
33
33
  export * from './relation-repository/single-relation-repository';
34
34
  export * from './repository';
35
+ export * from './relation-repository/relation-repository';
35
36
  export { default as sqlParser, SQLParserTypes } from './sql-parser';
36
37
  export * from './update-associations';
37
38
  export { snakeCase } from './utils';
package/lib/index.js CHANGED
@@ -87,6 +87,7 @@ __reExport(src_exports, require("./relation-repository/hasone-repository"), modu
87
87
  __reExport(src_exports, require("./relation-repository/multiple-relation-repository"), module.exports);
88
88
  __reExport(src_exports, require("./relation-repository/single-relation-repository"), module.exports);
89
89
  __reExport(src_exports, require("./repository"), module.exports);
90
+ __reExport(src_exports, require("./relation-repository/relation-repository"), module.exports);
90
91
  var import_sql_parser = __toESM(require("./sql-parser"));
91
92
  __reExport(src_exports, require("./update-associations"), module.exports);
92
93
  var import_utils = require("./utils");
@@ -141,6 +142,7 @@ __reExport(src_exports, require("./update-guard"), module.exports);
141
142
  ...require("./relation-repository/multiple-relation-repository"),
142
143
  ...require("./relation-repository/single-relation-repository"),
143
144
  ...require("./repository"),
145
+ ...require("./relation-repository/relation-repository"),
144
146
  ...require("./update-associations"),
145
147
  ...require("./value-parsers"),
146
148
  ...require("./view-collection"),
@@ -15,3 +15,4 @@ export * from './datetime-no-tz-interface';
15
15
  export * from './boolean-interface';
16
16
  export * from './date-interface';
17
17
  export * from './time-interface';
18
+ export * from './textarea-interface';
@@ -32,6 +32,7 @@ __reExport(interfaces_exports, require("./datetime-no-tz-interface"), module.exp
32
32
  __reExport(interfaces_exports, require("./boolean-interface"), module.exports);
33
33
  __reExport(interfaces_exports, require("./date-interface"), module.exports);
34
34
  __reExport(interfaces_exports, require("./time-interface"), module.exports);
35
+ __reExport(interfaces_exports, require("./textarea-interface"), module.exports);
35
36
  // Annotate the CommonJS export names for ESM import in node:
36
37
  0 && (module.exports = {
37
38
  ...require("./base-interface"),
@@ -42,5 +43,6 @@ __reExport(interfaces_exports, require("./time-interface"), module.exports);
42
43
  ...require("./datetime-no-tz-interface"),
43
44
  ...require("./boolean-interface"),
44
45
  ...require("./date-interface"),
45
- ...require("./time-interface")
46
+ ...require("./time-interface"),
47
+ ...require("./textarea-interface")
46
48
  });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+ import { BaseInterface } from './base-interface';
10
+ export declare class TextareaInterface extends BaseInterface {
11
+ toValue(value: any): any;
12
+ validate(value: any): boolean;
13
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * This file is part of the NocoBase (R) project.
3
+ * Copyright (c) 2020-2024 NocoBase Co., Ltd.
4
+ * Authors: NocoBase Team.
5
+ *
6
+ * This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
7
+ * For more information, please refer to: https://www.nocobase.com/agreement.
8
+ */
9
+
10
+ var __create = Object.create;
11
+ var __defProp = Object.defineProperty;
12
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
13
+ var __getOwnPropNames = Object.getOwnPropertyNames;
14
+ var __getProtoOf = Object.getPrototypeOf;
15
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
16
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
17
+ var __export = (target, all) => {
18
+ for (var name in all)
19
+ __defProp(target, name, { get: all[name], enumerable: true });
20
+ };
21
+ var __copyProps = (to, from, except, desc) => {
22
+ if (from && typeof from === "object" || typeof from === "function") {
23
+ for (let key of __getOwnPropNames(from))
24
+ if (!__hasOwnProp.call(to, key) && key !== except)
25
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
26
+ }
27
+ return to;
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
+ ));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+ var textarea_interface_exports = {};
39
+ __export(textarea_interface_exports, {
40
+ TextareaInterface: () => TextareaInterface
41
+ });
42
+ module.exports = __toCommonJS(textarea_interface_exports);
43
+ var import_lodash = __toESM(require("lodash"));
44
+ var import_base_interface = require("./base-interface");
45
+ const _TextareaInterface = class _TextareaInterface extends import_base_interface.BaseInterface {
46
+ toValue(value) {
47
+ if (value === null || value === void 0 || typeof value === "string") {
48
+ return value;
49
+ }
50
+ if (this.validate(value)) {
51
+ return value.toString();
52
+ }
53
+ throw new Error(`Invalid value ${value}, expected textarea value`);
54
+ }
55
+ validate(value) {
56
+ return import_lodash.default.isString(value) || import_lodash.default.isNumber(value);
57
+ }
58
+ };
59
+ __name(_TextareaInterface, "TextareaInterface");
60
+ let TextareaInterface = _TextareaInterface;
61
+ // Annotate the CommonJS export names for ESM import in node:
62
+ 0 && (module.exports = {
63
+ TextareaInterface
64
+ });
@@ -65,7 +65,8 @@ const interfaces = {
65
65
  m2o: import_many_to_one_interface.ManyToOneInterface,
66
66
  m2m: import_many_to_many_interface.ManyToManyInterface,
67
67
  time: import_index.TimeInterface,
68
- input: import_input_interface.InputInterface
68
+ input: import_input_interface.InputInterface,
69
+ textarea: import_index.TextareaInterface
69
70
  };
70
71
  function registerInterfaces(db) {
71
72
  for (const [interfaceName, type] of Object.entries(interfaces)) {
@@ -61,7 +61,7 @@ const toDate = /* @__PURE__ */ __name((date, options = {}) => {
61
61
  val = (0, import_moment.default)(val).utcOffset("+00:00").format("YYYY-MM-DD HH:mm:ss");
62
62
  }
63
63
  if (field.constructor.name === "DateOnlyField") {
64
- val = (0, import_moment.default)(val).format("YYYY-MM-DD HH:mm:ss");
64
+ val = import_moment.default.utc(val).format("YYYY-MM-DD HH:mm:ss");
65
65
  }
66
66
  const eventObj = {
67
67
  val,
@@ -95,6 +95,9 @@ var date_default = {
95
95
  };
96
96
  }
97
97
  if (Array.isArray(r)) {
98
+ console.log(11111111, {
99
+ [import_sequelize.Op.and]: [{ [import_sequelize.Op.gte]: toDate(r[0], { ctx }) }, { [import_sequelize.Op.lt]: toDate(r[1], { ctx }) }]
100
+ });
98
101
  return {
99
102
  [import_sequelize.Op.and]: [{ [import_sequelize.Op.gte]: toDate(r[0], { ctx }) }, { [import_sequelize.Op.lt]: toDate(r[1], { ctx }) }]
100
103
  };
@@ -23,6 +23,6 @@ export declare abstract class MultipleRelationRepository extends RelationReposit
23
23
  filterByTk?: TargetKey | TargetKey[];
24
24
  }, transaction?: Transaction): Promise<boolean>;
25
25
  protected filterHasInclude(filter: Filter, options?: any): boolean;
26
- protected accessors(): MultiAssociationAccessors;
26
+ accessors(): MultiAssociationAccessors;
27
27
  updateOrCreate(options: FirstOrCreateOptions): Promise<any>;
28
28
  }
@@ -43,7 +43,7 @@ export declare abstract class RelationRepository {
43
43
  updateOrCreate(options: FirstOrCreateOptions): Promise<any>;
44
44
  create(options?: CreateOptions): Promise<any>;
45
45
  getSourceModel(transaction?: Transaction): Promise<Model<any, any>>;
46
- protected accessors(): import("sequelize").SingleAssociationAccessors | import("sequelize").MultiAssociationAccessors;
46
+ accessors(): import("sequelize").SingleAssociationAccessors | import("sequelize").MultiAssociationAccessors;
47
47
  protected buildQueryOptions(options: FindOptions): any;
48
48
  protected parseFilter(filter: Filter, options?: any): any;
49
49
  protected getTransaction(options: any, autoGen?: boolean): Promise<Transaction | null>;
@@ -8,19 +8,19 @@
8
8
  */
9
9
  /// <reference types="node" />
10
10
  import { Association, BulkCreateOptions, ModelStatic, FindAndCountOptions as SequelizeAndCountOptions, CountOptions as SequelizeCountOptions, CreateOptions as SequelizeCreateOptions, DestroyOptions as SequelizeDestroyOptions, FindOptions as SequelizeFindOptions, UpdateOptions as SequelizeUpdateOptions, Transactionable, WhereOperators } from 'sequelize';
11
+ import { BelongsToArrayRepository } from './belongs-to-array/belongs-to-array-repository';
11
12
  import { Collection } from './collection';
13
+ import { SmartCursorBuilder } from './cursor-builder';
12
14
  import { Database } from './database';
13
15
  import { ArrayFieldRepository } from './field-repository/array-field-repository';
14
16
  import { Model } from './model';
15
17
  import operators from './operators';
16
- import { BelongsToArrayRepository } from './belongs-to-array/belongs-to-array-repository';
17
18
  import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
18
19
  import { BelongsToRepository } from './relation-repository/belongs-to-repository';
19
20
  import { HasManyRepository } from './relation-repository/hasmany-repository';
20
21
  import { HasOneRepository } from './relation-repository/hasone-repository';
21
22
  import { RelationRepository } from './relation-repository/relation-repository';
22
23
  import { valuesToFilter } from './utils/filter-utils';
23
- import { SmartCursorBuilder } from './cursor-builder';
24
24
  interface CreateManyOptions extends BulkCreateOptions {
25
25
  records: Values[];
26
26
  }
@@ -141,6 +141,7 @@ export interface FirstOrCreateOptions extends Transactionable {
141
141
  values?: Values;
142
142
  hooks?: boolean;
143
143
  context?: any;
144
+ updateAssociationValues?: AssociationKeysToBeUpdate;
144
145
  }
145
146
  export declare class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> {
146
147
  database: Database;
package/lib/repository.js CHANGED
@@ -54,6 +54,9 @@ module.exports = __toCommonJS(repository_exports);
54
54
  var import_utils = require("@nocobase/utils");
55
55
  var import_lodash = __toESM(require("lodash"));
56
56
  var import_sequelize = require("sequelize");
57
+ var import_lodash2 = __toESM(require("lodash"));
58
+ var import_belongs_to_array_repository = require("./belongs-to-array/belongs-to-array-repository");
59
+ var import_cursor_builder = require("./cursor-builder");
57
60
  var import_must_have_filter_decorator = __toESM(require("./decorators/must-have-filter-decorator"));
58
61
  var import_target_collection_decorator = __toESM(require("./decorators/target-collection-decorator"));
59
62
  var import_transaction_decorator = require("./decorators/transaction-decorator");
@@ -62,7 +65,6 @@ var import_array_field_repository = require("./field-repository/array-field-repo
62
65
  var import_fields = require("./fields");
63
66
  var import_filter_parser = __toESM(require("./filter-parser"));
64
67
  var import_options_parser = require("./options-parser");
65
- var import_belongs_to_array_repository = require("./belongs-to-array/belongs-to-array-repository");
66
68
  var import_belongs_to_many_repository = require("./relation-repository/belongs-to-many-repository");
67
69
  var import_belongs_to_repository = require("./relation-repository/belongs-to-repository");
68
70
  var import_hasmany_repository = require("./relation-repository/hasmany-repository");
@@ -70,8 +72,6 @@ var import_hasone_repository = require("./relation-repository/hasone-repository"
70
72
  var import_update_associations = require("./update-associations");
71
73
  var import_update_guard = require("./update-guard");
72
74
  var import_filter_utils = require("./utils/filter-utils");
73
- var import_lodash2 = __toESM(require("lodash"));
74
- var import_cursor_builder = require("./cursor-builder");
75
75
  var import_sequelize2 = require("sequelize");
76
76
  const debug = require("debug")("noco-database");
77
77
  const transaction = (0, import_transaction_decorator.transactionWrapperBuilder)(function() {
@@ -333,16 +333,16 @@ const _Repository = class _Repository {
333
333
  * Get the first record matching the attributes or create it.
334
334
  */
335
335
  async firstOrCreate(options) {
336
- const { filterKeys, values, transaction: transaction2, hooks, context } = options;
336
+ const { filterKeys, values, transaction: transaction2, context, ...rest } = options;
337
337
  const filter = _Repository.valuesToFilter(values, filterKeys);
338
338
  const instance = await this.findOne({ filter, transaction: transaction2, context });
339
339
  if (instance) {
340
340
  return instance;
341
341
  }
342
- return this.create({ values, transaction: transaction2, hooks, context });
342
+ return this.create({ values, transaction: transaction2, context, ...rest });
343
343
  }
344
344
  async updateOrCreate(options) {
345
- const { filterKeys, values, transaction: transaction2, hooks, context } = options;
345
+ const { filterKeys, values, transaction: transaction2, context, ...rest } = options;
346
346
  const filter = _Repository.valuesToFilter(values, filterKeys);
347
347
  const instance = await this.findOne({ filter, transaction: transaction2, context });
348
348
  if (instance) {
@@ -350,11 +350,11 @@ const _Repository = class _Repository {
350
350
  filterByTk: instance.get(this.collection.filterTargetKey || this.collection.model.primaryKeyAttribute),
351
351
  values,
352
352
  transaction: transaction2,
353
- hooks,
354
- context
353
+ context,
354
+ ...rest
355
355
  });
356
356
  }
357
- return this.create({ values, transaction: transaction2, hooks, context });
357
+ return this.create({ values, transaction: transaction2, context, ...rest });
358
358
  }
359
359
  async create(options) {
360
360
  if (Array.isArray(options.values)) {
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@nocobase/database",
3
- "version": "1.8.0-beta.1",
3
+ "version": "1.8.0-beta.12",
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.8.0-beta.1",
10
- "@nocobase/utils": "1.8.0-beta.1",
9
+ "@nocobase/logger": "1.8.0-beta.12",
10
+ "@nocobase/utils": "1.8.0-beta.12",
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": "103935669123174f2942247202e3d9ff15f0d4ed"
41
+ "gitHead": "b244f9b23ce35bd75baf7b357c6d53d0fbddf3be"
42
42
  }