@nocobase/database 1.6.0-alpha.7 → 1.6.0-alpha.8
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/fields/unix-timestamp-field.js +7 -4
- package/lib/relation-repository/relation-repository.d.ts +1 -1
- package/lib/relation-repository/relation-repository.js +8 -7
- package/lib/relation-repository/types.d.ts +2 -1
- package/lib/repository.d.ts +3 -2
- package/lib/repository.js +11 -10
- package/package.json +4 -4
|
@@ -52,7 +52,7 @@ const _UnixTimestampField = class _UnixTimestampField extends import_date_field.
|
|
|
52
52
|
if (accuracy === "millisecond") {
|
|
53
53
|
rationalNumber = 1;
|
|
54
54
|
}
|
|
55
|
-
return Math.floor(new Date(val).getTime() / rationalNumber);
|
|
55
|
+
return Math.floor(typeof val === "number" ? val : new Date(val).getTime() / rationalNumber);
|
|
56
56
|
}
|
|
57
57
|
additionalSequelizeOptions() {
|
|
58
58
|
var _a, _b, _c, _d, _e;
|
|
@@ -71,16 +71,19 @@ const _UnixTimestampField = class _UnixTimestampField extends import_date_field.
|
|
|
71
71
|
return {
|
|
72
72
|
get() {
|
|
73
73
|
const value = this.getDataValue(name);
|
|
74
|
-
if (value
|
|
74
|
+
if (value == null) {
|
|
75
75
|
return value;
|
|
76
76
|
}
|
|
77
77
|
return new Date(value * rationalNumber);
|
|
78
78
|
},
|
|
79
79
|
set(value) {
|
|
80
|
-
if (value
|
|
80
|
+
if (value == null) {
|
|
81
81
|
this.setDataValue(name, value);
|
|
82
82
|
} else {
|
|
83
|
-
this.setDataValue(
|
|
83
|
+
this.setDataValue(
|
|
84
|
+
name,
|
|
85
|
+
Math.floor(typeof value === "number" ? value : new Date(value).getTime() / rationalNumber)
|
|
86
|
+
);
|
|
84
87
|
}
|
|
85
88
|
}
|
|
86
89
|
};
|
|
@@ -11,7 +11,7 @@ import { Collection } from '../collection';
|
|
|
11
11
|
import Database from '../database';
|
|
12
12
|
import { RelationField } from '../fields/relation-field';
|
|
13
13
|
import { Model } from '../model';
|
|
14
|
-
import { CreateOptions, Filter, FindOptions, TargetKey, UpdateOptions
|
|
14
|
+
import { CreateOptions, Filter, FindOptions, FirstOrCreateOptions, TargetKey, UpdateOptions } from './types';
|
|
15
15
|
export declare const transaction: (transactionInjector?: any) => (target: any, name: any, descriptor: any) => any;
|
|
16
16
|
export declare abstract class RelationRepository {
|
|
17
17
|
sourceCollection: Collection;
|
|
@@ -143,18 +143,18 @@ const _RelationRepository = class _RelationRepository {
|
|
|
143
143
|
return this.associationField.targetKey;
|
|
144
144
|
}
|
|
145
145
|
async firstOrCreate(options) {
|
|
146
|
-
const { filterKeys, values, transaction: transaction2, hooks } = options;
|
|
146
|
+
const { filterKeys, values, transaction: transaction2, hooks, context } = options;
|
|
147
147
|
const filter = (0, import_filter_utils.valuesToFilter)(values, filterKeys);
|
|
148
|
-
const instance = await this.findOne({ filter, transaction: transaction2 });
|
|
148
|
+
const instance = await this.findOne({ filter, transaction: transaction2, context });
|
|
149
149
|
if (instance) {
|
|
150
150
|
return instance;
|
|
151
151
|
}
|
|
152
|
-
return this.create({ values, transaction: transaction2, hooks });
|
|
152
|
+
return this.create({ values, transaction: transaction2, hooks, context });
|
|
153
153
|
}
|
|
154
154
|
async updateOrCreate(options) {
|
|
155
|
-
const { filterKeys, values, transaction: transaction2, hooks } = options;
|
|
155
|
+
const { filterKeys, values, transaction: transaction2, hooks, context } = options;
|
|
156
156
|
const filter = (0, import_filter_utils.valuesToFilter)(values, filterKeys);
|
|
157
|
-
const instance = await this.findOne({ filter, transaction: transaction2 });
|
|
157
|
+
const instance = await this.findOne({ filter, transaction: transaction2, context });
|
|
158
158
|
if (instance) {
|
|
159
159
|
return await this.update({
|
|
160
160
|
filterByTk: instance.get(
|
|
@@ -162,10 +162,11 @@ const _RelationRepository = class _RelationRepository {
|
|
|
162
162
|
),
|
|
163
163
|
values,
|
|
164
164
|
transaction: transaction2,
|
|
165
|
-
hooks
|
|
165
|
+
hooks,
|
|
166
|
+
context
|
|
166
167
|
});
|
|
167
168
|
}
|
|
168
|
-
return this.create({ values, transaction: transaction2, hooks });
|
|
169
|
+
return this.create({ values, transaction: transaction2, hooks, context });
|
|
169
170
|
}
|
|
170
171
|
async create(options) {
|
|
171
172
|
if (Array.isArray(options.values)) {
|
|
@@ -6,9 +6,9 @@
|
|
|
6
6
|
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
|
+
import { AssociationKeysToBeUpdate, BlackList, Values, WhiteList } from '@nocobase/database';
|
|
9
10
|
import { Transaction } from 'sequelize';
|
|
10
11
|
import { CreateOptions as SequelizeCreateOptions, UpdateOptions as SequelizeUpdateOptions } from 'sequelize/types/model';
|
|
11
|
-
import { AssociationKeysToBeUpdate, BlackList, Values, WhiteList } from '@nocobase/database';
|
|
12
12
|
export type TargetKey = string | number | {
|
|
13
13
|
[key: string]: any;
|
|
14
14
|
};
|
|
@@ -67,6 +67,7 @@ export interface FirstOrCreateOptions extends CommonOptions {
|
|
|
67
67
|
filterKeys: string[];
|
|
68
68
|
values: any;
|
|
69
69
|
hooks?: boolean;
|
|
70
|
+
context?: any;
|
|
70
71
|
}
|
|
71
72
|
export interface ThroughValues {
|
|
72
73
|
[key: string]: any;
|
package/lib/repository.d.ts
CHANGED
|
@@ -7,18 +7,18 @@
|
|
|
7
7
|
* For more information, please refer to: https://www.nocobase.com/agreement.
|
|
8
8
|
*/
|
|
9
9
|
/// <reference types="node" />
|
|
10
|
-
import { Association, BulkCreateOptions, CountOptions as SequelizeCountOptions, CreateOptions as SequelizeCreateOptions, DestroyOptions as SequelizeDestroyOptions,
|
|
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
11
|
import { Collection } from './collection';
|
|
12
12
|
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
17
|
import { BelongsToManyRepository } from './relation-repository/belongs-to-many-repository';
|
|
17
18
|
import { BelongsToRepository } from './relation-repository/belongs-to-repository';
|
|
18
19
|
import { HasManyRepository } from './relation-repository/hasmany-repository';
|
|
19
20
|
import { HasOneRepository } from './relation-repository/hasone-repository';
|
|
20
21
|
import { RelationRepository } from './relation-repository/relation-repository';
|
|
21
|
-
import { BelongsToArrayRepository } from './relation-repository/belongs-to-array-repository';
|
|
22
22
|
import { valuesToFilter } from './utils/filter-utils';
|
|
23
23
|
interface CreateManyOptions extends BulkCreateOptions {
|
|
24
24
|
records: Values[];
|
|
@@ -139,6 +139,7 @@ export interface FirstOrCreateOptions extends Transactionable {
|
|
|
139
139
|
filterKeys: string[];
|
|
140
140
|
values?: Values;
|
|
141
141
|
hooks?: boolean;
|
|
142
|
+
context?: any;
|
|
142
143
|
}
|
|
143
144
|
export declare class Repository<TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes> {
|
|
144
145
|
database: Database;
|
package/lib/repository.js
CHANGED
|
@@ -51,9 +51,9 @@ __export(repository_exports, {
|
|
|
51
51
|
Transactionable: () => import_sequelize2.Transactionable
|
|
52
52
|
});
|
|
53
53
|
module.exports = __toCommonJS(repository_exports);
|
|
54
|
+
var import_utils = require("@nocobase/utils");
|
|
54
55
|
var import_lodash = __toESM(require("lodash"));
|
|
55
56
|
var import_sequelize = require("sequelize");
|
|
56
|
-
var import_utils = require("@nocobase/utils");
|
|
57
57
|
var import_must_have_filter_decorator = __toESM(require("./decorators/must-have-filter-decorator"));
|
|
58
58
|
var import_target_collection_decorator = __toESM(require("./decorators/target-collection-decorator"));
|
|
59
59
|
var import_transaction_decorator = require("./decorators/transaction-decorator");
|
|
@@ -62,13 +62,13 @@ 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
66
|
var import_belongs_to_many_repository = require("./relation-repository/belongs-to-many-repository");
|
|
66
67
|
var import_belongs_to_repository = require("./relation-repository/belongs-to-repository");
|
|
67
68
|
var import_hasmany_repository = require("./relation-repository/hasmany-repository");
|
|
68
69
|
var import_hasone_repository = require("./relation-repository/hasone-repository");
|
|
69
70
|
var import_update_associations = require("./update-associations");
|
|
70
71
|
var import_update_guard = require("./update-guard");
|
|
71
|
-
var import_belongs_to_array_repository = require("./relation-repository/belongs-to-array-repository");
|
|
72
72
|
var import_filter_utils = require("./utils/filter-utils");
|
|
73
73
|
var import_sequelize2 = require("sequelize");
|
|
74
74
|
const debug = require("debug")("noco-database");
|
|
@@ -304,27 +304,28 @@ const _Repository = class _Repository {
|
|
|
304
304
|
* Get the first record matching the attributes or create it.
|
|
305
305
|
*/
|
|
306
306
|
async firstOrCreate(options) {
|
|
307
|
-
const { filterKeys, values, transaction: transaction2, hooks } = options;
|
|
307
|
+
const { filterKeys, values, transaction: transaction2, hooks, context } = options;
|
|
308
308
|
const filter = _Repository.valuesToFilter(values, filterKeys);
|
|
309
|
-
const instance = await this.findOne({ filter, transaction: transaction2 });
|
|
309
|
+
const instance = await this.findOne({ filter, transaction: transaction2, context });
|
|
310
310
|
if (instance) {
|
|
311
311
|
return instance;
|
|
312
312
|
}
|
|
313
|
-
return this.create({ values, transaction: transaction2, hooks });
|
|
313
|
+
return this.create({ values, transaction: transaction2, hooks, context });
|
|
314
314
|
}
|
|
315
315
|
async updateOrCreate(options) {
|
|
316
|
-
const { filterKeys, values, transaction: transaction2, hooks } = options;
|
|
316
|
+
const { filterKeys, values, transaction: transaction2, hooks, context } = options;
|
|
317
317
|
const filter = _Repository.valuesToFilter(values, filterKeys);
|
|
318
|
-
const instance = await this.findOne({ filter, transaction: transaction2 });
|
|
318
|
+
const instance = await this.findOne({ filter, transaction: transaction2, context });
|
|
319
319
|
if (instance) {
|
|
320
320
|
return await this.update({
|
|
321
321
|
filterByTk: instance.get(this.collection.filterTargetKey || this.collection.model.primaryKeyAttribute),
|
|
322
322
|
values,
|
|
323
323
|
transaction: transaction2,
|
|
324
|
-
hooks
|
|
324
|
+
hooks,
|
|
325
|
+
context
|
|
325
326
|
});
|
|
326
327
|
}
|
|
327
|
-
return this.create({ values, transaction: transaction2, hooks });
|
|
328
|
+
return this.create({ values, transaction: transaction2, hooks, context });
|
|
328
329
|
}
|
|
329
330
|
async create(options) {
|
|
330
331
|
if (Array.isArray(options.values)) {
|
|
@@ -465,7 +466,7 @@ const _Repository = class _Repository {
|
|
|
465
466
|
throw new Error(`filterByTk is not supported for collection that has no primary key`);
|
|
466
467
|
}
|
|
467
468
|
}
|
|
468
|
-
if (filterByTk && !options.filter) {
|
|
469
|
+
if (filterByTk && !(0, import_utils.isValidFilter)(options.filter)) {
|
|
469
470
|
const where = [];
|
|
470
471
|
for (const tk of filterByTk) {
|
|
471
472
|
const optionParser = new import_options_parser.OptionsParser(
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nocobase/database",
|
|
3
|
-
"version": "1.6.0-alpha.
|
|
3
|
+
"version": "1.6.0-alpha.8",
|
|
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.
|
|
10
|
-
"@nocobase/utils": "1.6.0-alpha.
|
|
9
|
+
"@nocobase/logger": "1.6.0-alpha.8",
|
|
10
|
+
"@nocobase/utils": "1.6.0-alpha.8",
|
|
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": "
|
|
41
|
+
"gitHead": "f7ab757a5d7fb723c96bc9c1340291e4deec03ea"
|
|
42
42
|
}
|