fix: allow custom sort value
This commit is contained in:
parent
1006b1a9a5
commit
aef6b85736
@ -17,7 +17,7 @@ import { getDataTypeKey } from '.';
|
||||
import Table from '../table';
|
||||
import Database from '../database';
|
||||
import Model, { ModelCtor } from '../model';
|
||||
import { whereCompare } from '../utils';
|
||||
import { whereCompare, isNumber } from '../utils';
|
||||
|
||||
export interface IField {
|
||||
|
||||
@ -714,6 +714,10 @@ export class SORT extends NUMBER {
|
||||
|
||||
static async beforeCreateHook(this: SORT, model, options) {
|
||||
const { name, scope = [] } = this.options;
|
||||
// 如果有值,跳过
|
||||
if (isNumber(model.get(name))) {
|
||||
return;
|
||||
}
|
||||
const extremum: number = await this.getNextValue({
|
||||
...options,
|
||||
where: model.getValuesByFieldNames(scope)
|
||||
|
@ -303,3 +303,13 @@ export function requireModule(module: any) {
|
||||
}
|
||||
return module.__esModule ? module.default : module;
|
||||
}
|
||||
|
||||
export function isNumber(num) {
|
||||
if (typeof num === 'number') {
|
||||
return num - num === 0;
|
||||
}
|
||||
if (typeof num === 'string' && num.trim() !== '') {
|
||||
return Number.isFinite ? Number.isFinite(+num) : isFinite(+num);
|
||||
}
|
||||
return false;
|
||||
};
|
@ -159,8 +159,9 @@ export class CollectionModel extends BaseModel {
|
||||
continue;
|
||||
}
|
||||
const Model = this.database.getModel(key);
|
||||
for (const item of data[key]) {
|
||||
for (const index in data[key]) {
|
||||
let model;
|
||||
const item = data[key][index];
|
||||
if (item.name) {
|
||||
model = await Model.findOne({
|
||||
...options,
|
||||
@ -179,11 +180,12 @@ export class CollectionModel extends BaseModel {
|
||||
});
|
||||
}
|
||||
if (model && update) {
|
||||
await model.update(item, options);
|
||||
await model.update({...item, sort: index+1}, options);
|
||||
}
|
||||
if (!model) {
|
||||
model = await Model.create({
|
||||
...item,
|
||||
sort: index+1,
|
||||
collection_name: collection.name,
|
||||
}, options);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user