refactor(database): fix some fields and types (#820)
* refactor(database): fix some fields and types * fix(database): fix operator ne to null
This commit is contained in:
parent
e18b235777
commit
b92f3b3b95
@ -1,3 +1,3 @@
|
|||||||
import { PluginsConfigurations } from '@nocobase/server';
|
import { PluginConfiguration } from '@nocobase/server';
|
||||||
|
|
||||||
export default ['@nocobase/preset-nocobase'] as PluginsConfigurations;
|
export default ['@nocobase/preset-nocobase'] as PluginConfiguration[];
|
||||||
|
@ -22,6 +22,7 @@ module.exports = (cli) => {
|
|||||||
} else {
|
} else {
|
||||||
process.env.DUMI_THEME = isAbsolute(docThemePath) ? docThemePath : resolve(process.cwd(), docThemePath);
|
process.env.DUMI_THEME = isAbsolute(docThemePath) ? docThemePath : resolve(process.cwd(), docThemePath);
|
||||||
}
|
}
|
||||||
|
// TODO(bug): space replace = not work
|
||||||
const index = process.argv.indexOf(`--lang=${options.lang}`);
|
const index = process.argv.indexOf(`--lang=${options.lang}`);
|
||||||
if (index > 0) {
|
if (index > 0) {
|
||||||
process.argv.splice(index, 1);
|
process.argv.splice(index, 1);
|
||||||
|
@ -268,8 +268,10 @@ describe('database', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
await Test.sync();
|
await Test.sync();
|
||||||
|
expect(Test.model.prototype).toBeInstanceOf(CustomModel);
|
||||||
|
|
||||||
const test = await Test.model.create<any>();
|
const test = await Test.model.create<any>();
|
||||||
|
expect(test).toBeInstanceOf(CustomModel);
|
||||||
test.customMethod();
|
test.customMethod();
|
||||||
expect(test.get('abc')).toBe('abc');
|
expect(test.get('abc')).toBe('abc');
|
||||||
});
|
});
|
||||||
|
@ -30,4 +30,16 @@ describe('ne operator', () => {
|
|||||||
|
|
||||||
expect(results).toEqual(1);
|
expect(results).toEqual(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('compare with null', async () => {
|
||||||
|
await db.getRepository('tests').create({});
|
||||||
|
|
||||||
|
const results = await db.getRepository('tests').count({
|
||||||
|
filter: {
|
||||||
|
'name.$ne': null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(results).toBe(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
@ -215,7 +215,7 @@ export class Collection<
|
|||||||
return this.db.collectionExistsInDb(this.name, options);
|
return this.db.collectionExistsInDb(this.name, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
removeField(name) {
|
removeField(name: string): void | Field {
|
||||||
if (!this.fields.has(name)) {
|
if (!this.fields.has(name)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -347,7 +347,7 @@ export class Collection<
|
|||||||
async sync(syncOptions?: SyncOptions) {
|
async sync(syncOptions?: SyncOptions) {
|
||||||
const modelNames = new Set([this.model.name]);
|
const modelNames = new Set([this.model.name]);
|
||||||
|
|
||||||
const associations = this.model.associations;
|
const { associations } = this.model;
|
||||||
|
|
||||||
for (const associationKey in associations) {
|
for (const associationKey in associations) {
|
||||||
const association = associations[associationKey];
|
const association = associations[associationKey];
|
||||||
|
@ -16,7 +16,7 @@ export class FormulaField extends Field {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
async initFieldData({ transaction }) {
|
initFieldData = async ({ transaction }) => {
|
||||||
const { expression, name } = this.options;
|
const { expression, name } = this.options;
|
||||||
|
|
||||||
const records = await this.collection.repository.find({
|
const records = await this.collection.repository.find({
|
||||||
@ -42,7 +42,7 @@ export class FormulaField extends Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async caculateField(instance) {
|
caculateField = async (instance) => {
|
||||||
const { expression, name } = this.options;
|
const { expression, name } = this.options;
|
||||||
const scope = instance.toJSON();
|
const scope = instance.toJSON();
|
||||||
let result;
|
let result;
|
||||||
@ -55,7 +55,7 @@ export class FormulaField extends Field {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async updateFieldData(instance, { transaction }) {
|
updateFieldData = async (instance, { transaction }) => {
|
||||||
if (this.collection.name === instance.collectionName && instance.name === this.options.name) {
|
if (this.collection.name === instance.collectionName && instance.name === this.options.name) {
|
||||||
this.options = Object.assign(this.options, instance.options);
|
this.options = Object.assign(this.options, instance.options);
|
||||||
const { name, expression } = this.options;
|
const { name, expression } = this.options;
|
||||||
@ -64,7 +64,7 @@ export class FormulaField extends Field {
|
|||||||
order: [this.collection.model.primaryKeyAttribute],
|
order: [this.collection.model.primaryKeyAttribute],
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
|
|
||||||
for (const record of records) {
|
for (const record of records) {
|
||||||
const scope = record.toJSON();
|
const scope = record.toJSON();
|
||||||
const result = this.caculate(expression, scope);
|
const result = this.caculate(expression, scope);
|
||||||
@ -84,18 +84,18 @@ export class FormulaField extends Field {
|
|||||||
|
|
||||||
bind() {
|
bind() {
|
||||||
super.bind();
|
super.bind();
|
||||||
this.on('afterSync', this.initFieldData.bind(this));
|
this.on('afterSync', this.initFieldData);
|
||||||
this.database.on('fields.afterUpdate', this.updateFieldData.bind(this));
|
// TODO: should not depends on fields table (which is defined by other plugin)
|
||||||
this.on('beforeCreate', this.caculateField.bind(this));
|
this.database.on('fields.afterUpdate', this.updateFieldData);
|
||||||
this.on('beforeUpdate', this.caculateField.bind(this));
|
this.on('beforeSave', this.caculateField);
|
||||||
}
|
}
|
||||||
|
|
||||||
unbind() {
|
unbind() {
|
||||||
super.unbind();
|
super.unbind();
|
||||||
this.off('beforeCreate', this.caculateField.bind(this));
|
this.off('beforeSave', this.caculateField);
|
||||||
this.off('beforeUpdate', this.caculateField.bind(this));
|
// TODO: should not depends on fields table
|
||||||
this.database.off('fields.afterUpdate', this.updateFieldData.bind(this));
|
this.database.off('fields.afterUpdate', this.updateFieldData);
|
||||||
this.off('afterSync', this.initFieldData.bind(this));
|
this.off('afterSync', this.initFieldData);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -103,4 +103,4 @@ export interface FormulaFieldOptions extends BaseColumnFieldOptions {
|
|||||||
type: 'formula';
|
type: 'formula';
|
||||||
|
|
||||||
expression: string;
|
expression: string;
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,16 @@ export interface IntegerFieldOptions extends BaseColumnFieldOptions {
|
|||||||
type: 'integer';
|
type: 'integer';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class BigIntField extends Field {
|
||||||
|
get dataType() {
|
||||||
|
return DataTypes.BIGINT;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface BigIntFieldOptions extends BaseColumnFieldOptions {
|
||||||
|
type: 'bigInt';
|
||||||
|
}
|
||||||
|
|
||||||
export class FloatField extends Field {
|
export class FloatField extends Field {
|
||||||
get dataType() {
|
get dataType() {
|
||||||
return DataTypes.FLOAT;
|
return DataTypes.FLOAT;
|
||||||
|
@ -13,38 +13,36 @@ export class RadioField extends Field {
|
|||||||
return DataTypes.BOOLEAN;
|
return DataTypes.BOOLEAN;
|
||||||
}
|
}
|
||||||
|
|
||||||
init() {
|
listener = async (model, { transaction }) => {
|
||||||
const { name } = this.options;
|
const { name } = this.options;
|
||||||
this.listener = async (model, { transaction }) => {
|
if (!model.changed(name as any)) {
|
||||||
if (!model.changed(name as any)) {
|
return;
|
||||||
return;
|
}
|
||||||
}
|
const value = model.get(name) as boolean;
|
||||||
const value = model.get(name) as boolean;
|
if (value) {
|
||||||
if (value) {
|
const M = this.collection.model;
|
||||||
const M = this.collection.model;
|
await M.update(
|
||||||
await M.update(
|
{ [name]: false },
|
||||||
{ [name]: false },
|
{
|
||||||
{
|
where: {
|
||||||
where: {
|
[name]: true,
|
||||||
[name]: true,
|
|
||||||
},
|
|
||||||
transaction,
|
|
||||||
hooks: false,
|
|
||||||
},
|
},
|
||||||
);
|
transaction,
|
||||||
}
|
hooks: false,
|
||||||
};
|
},
|
||||||
}
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
bind() {
|
bind() {
|
||||||
super.bind();
|
super.bind();
|
||||||
this.on('beforeCreate', this.listener.bind(this));
|
this.on('beforeCreate', this.listener);
|
||||||
this.on('beforeUpdate', this.listener.bind(this));
|
this.on('beforeUpdate', this.listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
unbind() {
|
unbind() {
|
||||||
super.unbind();
|
super.unbind();
|
||||||
this.off('beforeCreate', this.listener.bind(this));
|
this.off('beforeCreate', this.listener);
|
||||||
this.off('beforeUpdate', this.listener.bind(this));
|
this.off('beforeUpdate', this.listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ export class SortField extends Field {
|
|||||||
return DataTypes.INTEGER;
|
return DataTypes.INTEGER;
|
||||||
}
|
}
|
||||||
|
|
||||||
async setSortValue(instance, options) {
|
setSortValue = async (instance, options) => {
|
||||||
const { name, scopeKey } = this.options;
|
const { name, scopeKey } = this.options;
|
||||||
const { model } = this.context.collection;
|
const { model } = this.context.collection;
|
||||||
|
|
||||||
@ -34,14 +34,14 @@ export class SortField extends Field {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async onScopeChange(instance, options) {
|
onScopeChange = async (instance, options) => {
|
||||||
const { scopeKey } = this.options;
|
const { scopeKey } = this.options;
|
||||||
if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey] != instance[scopeKey]) {
|
if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey] != instance[scopeKey]) {
|
||||||
await this.setSortValue(instance, options);
|
await this.setSortValue(instance, options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async initRecordsSortValue({ transaction }) {
|
initRecordsSortValue = async ({ transaction }) => {
|
||||||
const totalCount = await this.collection.repository.count({
|
const totalCount = await this.collection.repository.count({
|
||||||
transaction,
|
transaction,
|
||||||
});
|
});
|
||||||
@ -77,16 +77,16 @@ export class SortField extends Field {
|
|||||||
|
|
||||||
bind() {
|
bind() {
|
||||||
super.bind();
|
super.bind();
|
||||||
this.on('afterSync', this.initRecordsSortValue.bind(this));
|
this.on('afterSync', this.initRecordsSortValue);
|
||||||
this.on('beforeUpdate', this.onScopeChange.bind(this));
|
this.on('beforeUpdate', this.onScopeChange);
|
||||||
this.on('beforeCreate', this.setSortValue.bind(this));
|
this.on('beforeCreate', this.setSortValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
unbind() {
|
unbind() {
|
||||||
super.unbind();
|
super.unbind();
|
||||||
this.off('beforeUpdate', this.onScopeChange.bind(this));
|
this.off('beforeUpdate', this.onScopeChange);
|
||||||
this.off('beforeCreate', this.setSortValue.bind(this));
|
this.off('beforeCreate', this.setSortValue);
|
||||||
this.off('afterSync', this.initRecordsSortValue.bind(this));
|
this.off('afterSync', this.initRecordsSortValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,11 +2,15 @@ import { Op } from 'sequelize';
|
|||||||
|
|
||||||
export default {
|
export default {
|
||||||
$ne(val, ctx) {
|
$ne(val, ctx) {
|
||||||
return {
|
return val === null
|
||||||
[Op.or]: {
|
? {
|
||||||
[Op.ne]: val,
|
[Op.ne]: null,
|
||||||
[Op.is]: null,
|
}
|
||||||
},
|
: {
|
||||||
};
|
[Op.or]: {
|
||||||
|
[Op.ne]: val,
|
||||||
|
[Op.is]: null,
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user