feat: collection add schema table name method

This commit is contained in:
Chareice 2023-02-14 20:04:49 +08:00
parent 5040182713
commit 1589d11e82
3 changed files with 22 additions and 8 deletions

View File

@ -7,7 +7,7 @@ import {
QueryInterfaceDropTableOptions,
SyncOptions,
Transactionable,
Utils
Utils,
} from 'sequelize';
import { Database } from './database';
import { Field, FieldOptions } from './fields';
@ -532,4 +532,14 @@ export class Collection<
public isParent() {
return this.context.database.inheritanceMap.isParentNode(this.name);
}
public addSchemaTableName() {
const tableName = this.model.tableName;
if (this.options.schema) {
return this.db.utils.addSchema(tableName, this.options.schema);
}
return tableName;
}
}

View File

@ -15,7 +15,7 @@ import {
Sequelize,
SyncOptions,
Transactionable,
Utils
Utils,
} from 'sequelize';
import { SequelizeStorage, Umzug } from 'umzug';
import { Collection, CollectionOptions, RepositoryType } from './collection';
@ -58,7 +58,7 @@ import {
SyncListener,
UpdateListener,
UpdateWithAssociationsListener,
ValidateListener
ValidateListener,
} from './types';
import { snakeCase } from './utils';
@ -324,6 +324,10 @@ export class Database extends EventEmitter implements AsyncEmitter {
});
}
}
if (this.options.schema && !options.schema) {
options.schema = this.options.schema;
}
});
}

View File

@ -107,7 +107,7 @@ export class FieldModel extends MagicAttributeModel {
const queryInterface = this.db.sequelize.getQueryInterface() as any;
const existsIndexes = await queryInterface.showIndex(collection.model.tableName, {
const existsIndexes = await queryInterface.showIndex(collection.addSchemaTableName(), {
transaction: options.transaction,
});
@ -121,7 +121,7 @@ export class FieldModel extends MagicAttributeModel {
if (existUniqueIndex) {
const existsUniqueConstraints = await queryInterface.showConstraint(
collection.model.tableName,
collection.addSchemaTableName(),
constraintName,
{},
);
@ -133,7 +133,7 @@ export class FieldModel extends MagicAttributeModel {
// @ts-ignore
await collection.sync({ ...options, force: false, alter: { drop: false } });
await queryInterface.addConstraint(tableName, {
await queryInterface.addConstraint(collection.addSchemaTableName(), {
type: 'unique',
fields: [columnName],
name: constraintName,
@ -142,7 +142,7 @@ export class FieldModel extends MagicAttributeModel {
}
if (!unique && existsUniqueConstraint) {
await queryInterface.removeConstraint(collection.model.tableName, constraintName, {
await queryInterface.removeConstraint(collection.addSchemaTableName(), constraintName, {
transaction: options.transaction,
});
}
@ -164,7 +164,7 @@ export class FieldModel extends MagicAttributeModel {
const queryInterface = collection.db.sequelize.getQueryInterface();
await queryInterface.changeColumn(
collection.model.tableName,
collection.addSchemaTableName(),
collection.model.rawAttributes[this.get('name')].field,
{
type: field.dataType,