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 Table from '../table';
|
||||||
import Database from '../database';
|
import Database from '../database';
|
||||||
import Model, { ModelCtor } from '../model';
|
import Model, { ModelCtor } from '../model';
|
||||||
import { whereCompare } from '../utils';
|
import { whereCompare, isNumber } from '../utils';
|
||||||
|
|
||||||
export interface IField {
|
export interface IField {
|
||||||
|
|
||||||
@ -714,6 +714,10 @@ export class SORT extends NUMBER {
|
|||||||
|
|
||||||
static async beforeCreateHook(this: SORT, model, options) {
|
static async beforeCreateHook(this: SORT, model, options) {
|
||||||
const { name, scope = [] } = this.options;
|
const { name, scope = [] } = this.options;
|
||||||
|
// 如果有值,跳过
|
||||||
|
if (isNumber(model.get(name))) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
const extremum: number = await this.getNextValue({
|
const extremum: number = await this.getNextValue({
|
||||||
...options,
|
...options,
|
||||||
where: model.getValuesByFieldNames(scope)
|
where: model.getValuesByFieldNames(scope)
|
||||||
|
@ -303,3 +303,13 @@ export function requireModule(module: any) {
|
|||||||
}
|
}
|
||||||
return module.__esModule ? module.default : module;
|
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;
|
continue;
|
||||||
}
|
}
|
||||||
const Model = this.database.getModel(key);
|
const Model = this.database.getModel(key);
|
||||||
for (const item of data[key]) {
|
for (const index in data[key]) {
|
||||||
let model;
|
let model;
|
||||||
|
const item = data[key][index];
|
||||||
if (item.name) {
|
if (item.name) {
|
||||||
model = await Model.findOne({
|
model = await Model.findOne({
|
||||||
...options,
|
...options,
|
||||||
@ -179,11 +180,12 @@ export class CollectionModel extends BaseModel {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (model && update) {
|
if (model && update) {
|
||||||
await model.update(item, options);
|
await model.update({...item, sort: index+1}, options);
|
||||||
}
|
}
|
||||||
if (!model) {
|
if (!model) {
|
||||||
model = await Model.create({
|
model = await Model.create({
|
||||||
...item,
|
...item,
|
||||||
|
sort: index+1,
|
||||||
collection_name: collection.name,
|
collection_name: collection.name,
|
||||||
}, options);
|
}, options);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user