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() {
|
tableName() {
|
||||||
const { name, tableName } = this.options;
|
const { name, tableName } = this.options;
|
||||||
const tName = tableName || name;
|
const tName = tableName || name;
|
||||||
return this.db.options.underscored ? snakeCase(tName) : tName;
|
return this.options.underscored ? snakeCase(tName) : tName;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected sequelizeModelOptions() {
|
protected sequelizeModelOptions() {
|
||||||
@ -246,10 +246,12 @@ export class Collection<
|
|||||||
}
|
}
|
||||||
|
|
||||||
checkFieldType(name: string, options: FieldOptions) {
|
checkFieldType(name: string, options: FieldOptions) {
|
||||||
if (!this.db.options.underscored) {
|
if (!this.options.underscored) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const fieldName = options.field || snakeCase(name);
|
const fieldName = options.field || snakeCase(name);
|
||||||
|
|
||||||
const field = this.findField((f) => {
|
const field = this.findField((f) => {
|
||||||
if (f.name === name) {
|
if (f.name === name) {
|
||||||
return false;
|
return false;
|
||||||
@ -259,9 +261,11 @@ export class Collection<
|
|||||||
}
|
}
|
||||||
return snakeCase(f.name) === fieldName;
|
return snakeCase(f.name) === fieldName;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!field) {
|
if (!field) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.type !== field.type) {
|
if (options.type !== field.type) {
|
||||||
throw new Error(`fields with same column must be of the same type ${JSON.stringify(options)}`);
|
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() {
|
initListener() {
|
||||||
this.on('beforeDefine', (model, options) => {
|
this.on('beforeDefine', (model, options) => {
|
||||||
if (this.options.underscored) {
|
if (this.options.underscored && options.underscored === undefined) {
|
||||||
options.underscored = true;
|
options.underscored = true;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -353,6 +353,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.on('beforeDefineCollection', (options) => {
|
this.on('beforeDefineCollection', (options) => {
|
||||||
|
if (this.options.underscored && options.underscored === undefined) {
|
||||||
|
options.underscored = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (options.underscored) {
|
if (options.underscored) {
|
||||||
if (lodash.get(options, 'sortable.scopeKey')) {
|
if (lodash.get(options, 'sortable.scopeKey')) {
|
||||||
options.sortable.scopeKey = snakeCase(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}`);
|
throw Error(`unsupported field type ${type}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (options.field && this.options.underscored) {
|
if (options.field && context.collection.options.underscored) {
|
||||||
options.field = snakeCase(options.field);
|
options.field = snakeCase(options.field);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,6 +4,7 @@ export class ViewCollection extends Collection {
|
|||||||
constructor(options: CollectionOptions, context: CollectionContext) {
|
constructor(options: CollectionOptions, context: CollectionContext) {
|
||||||
options.autoGenId = false;
|
options.autoGenId = false;
|
||||||
options.timestamps = false;
|
options.timestamps = false;
|
||||||
|
options.underscored = false;
|
||||||
|
|
||||||
super(options, context);
|
super(options, context);
|
||||||
}
|
}
|
||||||
|
@ -49,6 +49,34 @@ describe('view collection', function () {
|
|||||||
expect(results.length).toBe(1);
|
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 () => {
|
it('should create view collection by view name', async () => {
|
||||||
const dropViewSQL = `DROP VIEW IF EXISTS test_view`;
|
const dropViewSQL = `DROP VIEW IF EXISTS test_view`;
|
||||||
await db.sequelize.query(dropViewSQL);
|
await db.sequelize.query(dropViewSQL);
|
||||||
|
Loading…
Reference in New Issue
Block a user