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:
Junyi 2022-09-11 21:58:49 +08:00 committed by GitHub
parent e18b235777
commit b92f3b3b95
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 83 additions and 56 deletions

View File

@ -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[];

View File

@ -22,6 +22,7 @@ module.exports = (cli) => {
} else {
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}`);
if (index > 0) {
process.argv.splice(index, 1);

View File

@ -268,8 +268,10 @@ describe('database', () => {
});
await Test.sync();
expect(Test.model.prototype).toBeInstanceOf(CustomModel);
const test = await Test.model.create<any>();
expect(test).toBeInstanceOf(CustomModel);
test.customMethod();
expect(test.get('abc')).toBe('abc');
});

View File

@ -30,4 +30,16 @@ describe('ne operator', () => {
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);
});
});

View File

@ -215,7 +215,7 @@ export class Collection<
return this.db.collectionExistsInDb(this.name, options);
}
removeField(name) {
removeField(name: string): void | Field {
if (!this.fields.has(name)) {
return;
}
@ -347,7 +347,7 @@ export class Collection<
async sync(syncOptions?: SyncOptions) {
const modelNames = new Set([this.model.name]);
const associations = this.model.associations;
const { associations } = this.model;
for (const associationKey in associations) {
const association = associations[associationKey];

View File

@ -16,7 +16,7 @@ export class FormulaField extends Field {
return result;
}
async initFieldData({ transaction }) {
initFieldData = async ({ transaction }) => {
const { expression, name } = this.options;
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 scope = instance.toJSON();
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) {
this.options = Object.assign(this.options, instance.options);
const { name, expression } = this.options;
@ -64,7 +64,7 @@ export class FormulaField extends Field {
order: [this.collection.model.primaryKeyAttribute],
transaction,
});
for (const record of records) {
const scope = record.toJSON();
const result = this.caculate(expression, scope);
@ -84,18 +84,18 @@ export class FormulaField extends Field {
bind() {
super.bind();
this.on('afterSync', this.initFieldData.bind(this));
this.database.on('fields.afterUpdate', this.updateFieldData.bind(this));
this.on('beforeCreate', this.caculateField.bind(this));
this.on('beforeUpdate', this.caculateField.bind(this));
this.on('afterSync', this.initFieldData);
// TODO: should not depends on fields table (which is defined by other plugin)
this.database.on('fields.afterUpdate', this.updateFieldData);
this.on('beforeSave', this.caculateField);
}
unbind() {
super.unbind();
this.off('beforeCreate', this.caculateField.bind(this));
this.off('beforeUpdate', this.caculateField.bind(this));
this.database.off('fields.afterUpdate', this.updateFieldData.bind(this));
this.off('afterSync', this.initFieldData.bind(this));
this.off('beforeSave', this.caculateField);
// TODO: should not depends on fields table
this.database.off('fields.afterUpdate', this.updateFieldData);
this.off('afterSync', this.initFieldData);
}
}
@ -103,4 +103,4 @@ export interface FormulaFieldOptions extends BaseColumnFieldOptions {
type: 'formula';
expression: string;
}
}

View File

@ -11,6 +11,16 @@ export interface IntegerFieldOptions extends BaseColumnFieldOptions {
type: 'integer';
}
export class BigIntField extends Field {
get dataType() {
return DataTypes.BIGINT;
}
}
export interface BigIntFieldOptions extends BaseColumnFieldOptions {
type: 'bigInt';
}
export class FloatField extends Field {
get dataType() {
return DataTypes.FLOAT;

View File

@ -13,38 +13,36 @@ export class RadioField extends Field {
return DataTypes.BOOLEAN;
}
init() {
listener = async (model, { transaction }) => {
const { name } = this.options;
this.listener = async (model, { transaction }) => {
if (!model.changed(name as any)) {
return;
}
const value = model.get(name) as boolean;
if (value) {
const M = this.collection.model;
await M.update(
{ [name]: false },
{
where: {
[name]: true,
},
transaction,
hooks: false,
if (!model.changed(name as any)) {
return;
}
const value = model.get(name) as boolean;
if (value) {
const M = this.collection.model;
await M.update(
{ [name]: false },
{
where: {
[name]: true,
},
);
}
};
}
transaction,
hooks: false,
},
);
}
};
bind() {
super.bind();
this.on('beforeCreate', this.listener.bind(this));
this.on('beforeUpdate', this.listener.bind(this));
this.on('beforeCreate', this.listener);
this.on('beforeUpdate', this.listener);
}
unbind() {
super.unbind();
this.off('beforeCreate', this.listener.bind(this));
this.off('beforeUpdate', this.listener.bind(this));
this.off('beforeCreate', this.listener);
this.off('beforeUpdate', this.listener);
}
}

View File

@ -10,7 +10,7 @@ export class SortField extends Field {
return DataTypes.INTEGER;
}
async setSortValue(instance, options) {
setSortValue = async (instance, options) => {
const { name, scopeKey } = this.options;
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;
if (scopeKey && !instance.isNewRecord && instance._previousDataValues[scopeKey] != instance[scopeKey]) {
await this.setSortValue(instance, options);
}
}
async initRecordsSortValue({ transaction }) {
initRecordsSortValue = async ({ transaction }) => {
const totalCount = await this.collection.repository.count({
transaction,
});
@ -77,16 +77,16 @@ export class SortField extends Field {
bind() {
super.bind();
this.on('afterSync', this.initRecordsSortValue.bind(this));
this.on('beforeUpdate', this.onScopeChange.bind(this));
this.on('beforeCreate', this.setSortValue.bind(this));
this.on('afterSync', this.initRecordsSortValue);
this.on('beforeUpdate', this.onScopeChange);
this.on('beforeCreate', this.setSortValue);
}
unbind() {
super.unbind();
this.off('beforeUpdate', this.onScopeChange.bind(this));
this.off('beforeCreate', this.setSortValue.bind(this));
this.off('afterSync', this.initRecordsSortValue.bind(this));
this.off('beforeUpdate', this.onScopeChange);
this.off('beforeCreate', this.setSortValue);
this.off('afterSync', this.initRecordsSortValue);
}
}

View File

@ -2,11 +2,15 @@ import { Op } from 'sequelize';
export default {
$ne(val, ctx) {
return {
[Op.or]: {
[Op.ne]: val,
[Op.is]: null,
},
};
return val === null
? {
[Op.ne]: null,
}
: {
[Op.or]: {
[Op.ne]: val,
[Op.is]: null,
},
};
},
};