mirror of
https://github.com/nocobase/nocobase
synced 2024-11-15 08:26:21 +00:00
chore: disabled underscored in view collection. (#1633)
* chore: disabled underscored in view collection * fix: test
This commit is contained in:
parent
ecdd46bf83
commit
3f81d7cd7d
@ -149,7 +149,7 @@ export class Collection<
|
||||
tableName() {
|
||||
const { name, tableName } = this.options;
|
||||
const tName = tableName || name;
|
||||
return this.db.options.underscored ? snakeCase(tName) : tName;
|
||||
return this.options.underscored ? snakeCase(tName) : tName;
|
||||
}
|
||||
|
||||
protected sequelizeModelOptions() {
|
||||
@ -246,10 +246,12 @@ export class Collection<
|
||||
}
|
||||
|
||||
checkFieldType(name: string, options: FieldOptions) {
|
||||
if (!this.db.options.underscored) {
|
||||
if (!this.options.underscored) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fieldName = options.field || snakeCase(name);
|
||||
|
||||
const field = this.findField((f) => {
|
||||
if (f.name === name) {
|
||||
return false;
|
||||
@ -259,9 +261,11 @@ export class Collection<
|
||||
}
|
||||
return snakeCase(f.name) === fieldName;
|
||||
});
|
||||
|
||||
if (!field) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.type !== field.type) {
|
||||
throw new Error(`fields with same column must be of the same type ${JSON.stringify(options)}`);
|
||||
}
|
||||
|
@ -311,7 +311,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
||||
|
||||
initListener() {
|
||||
this.on('beforeDefine', (model, options) => {
|
||||
if (this.options.underscored) {
|
||||
if (this.options.underscored && options.underscored === undefined) {
|
||||
options.underscored = true;
|
||||
}
|
||||
});
|
||||
@ -353,6 +353,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
||||
});
|
||||
|
||||
this.on('beforeDefineCollection', (options) => {
|
||||
if (this.options.underscored && options.underscored === undefined) {
|
||||
options.underscored = true;
|
||||
}
|
||||
|
||||
if (options.underscored) {
|
||||
if (lodash.get(options, 'sortable.scopeKey')) {
|
||||
options.sortable.scopeKey = snakeCase(options.sortable.scopeKey);
|
||||
@ -617,7 +621,7 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
||||
throw Error(`unsupported field type ${type}`);
|
||||
}
|
||||
|
||||
if (options.field && this.options.underscored) {
|
||||
if (options.field && context.collection.options.underscored) {
|
||||
options.field = snakeCase(options.field);
|
||||
}
|
||||
|
||||
|
@ -4,6 +4,7 @@ export class ViewCollection extends Collection {
|
||||
constructor(options: CollectionOptions, context: CollectionContext) {
|
||||
options.autoGenId = false;
|
||||
options.timestamps = false;
|
||||
options.underscored = false;
|
||||
|
||||
super(options, context);
|
||||
}
|
||||
|
@ -49,6 +49,34 @@ describe('view collection', function () {
|
||||
expect(results.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should support view with uppercase field in underscored env', async () => {
|
||||
if (!db.options.underscored) {
|
||||
return;
|
||||
}
|
||||
|
||||
const dropViewSQL = `DROP VIEW IF EXISTS test_view`;
|
||||
await db.sequelize.query(dropViewSQL);
|
||||
const viewSQL = `CREATE VIEW test_view AS select 1+1 as "Uppercase"`;
|
||||
await db.sequelize.query(viewSQL);
|
||||
|
||||
await collectionRepository.create({
|
||||
values: {
|
||||
name: 'view_collection',
|
||||
viewName: 'test_view',
|
||||
isView: true,
|
||||
fields: [{ type: 'string', name: 'Uppercase', field: 'Uppercase' }],
|
||||
schema: db.inDialect('postgres') ? 'public' : undefined,
|
||||
},
|
||||
context: {},
|
||||
});
|
||||
|
||||
const viewCollection = db.getCollection('view_collection');
|
||||
|
||||
expect(viewCollection.model.rawAttributes['Uppercase'].field).toEqual('Uppercase');
|
||||
const results = await viewCollection.repository.find();
|
||||
expect(results.length).toBe(1);
|
||||
});
|
||||
|
||||
it('should create view collection by view name', async () => {
|
||||
const dropViewSQL = `DROP VIEW IF EXISTS test_view`;
|
||||
await db.sequelize.query(dropViewSQL);
|
||||
|
Loading…
Reference in New Issue
Block a user