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, QueryInterfaceDropTableOptions,
SyncOptions, SyncOptions,
Transactionable, Transactionable,
Utils Utils,
} from 'sequelize'; } from 'sequelize';
import { Database } from './database'; import { Database } from './database';
import { Field, FieldOptions } from './fields'; import { Field, FieldOptions } from './fields';
@ -532,4 +532,14 @@ export class Collection<
public isParent() { public isParent() {
return this.context.database.inheritanceMap.isParentNode(this.name); 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, Sequelize,
SyncOptions, SyncOptions,
Transactionable, Transactionable,
Utils Utils,
} from 'sequelize'; } from 'sequelize';
import { SequelizeStorage, Umzug } from 'umzug'; import { SequelizeStorage, Umzug } from 'umzug';
import { Collection, CollectionOptions, RepositoryType } from './collection'; import { Collection, CollectionOptions, RepositoryType } from './collection';
@ -58,7 +58,7 @@ import {
SyncListener, SyncListener,
UpdateListener, UpdateListener,
UpdateWithAssociationsListener, UpdateWithAssociationsListener,
ValidateListener ValidateListener,
} from './types'; } from './types';
import { snakeCase } from './utils'; 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 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, transaction: options.transaction,
}); });
@ -121,7 +121,7 @@ export class FieldModel extends MagicAttributeModel {
if (existUniqueIndex) { if (existUniqueIndex) {
const existsUniqueConstraints = await queryInterface.showConstraint( const existsUniqueConstraints = await queryInterface.showConstraint(
collection.model.tableName, collection.addSchemaTableName(),
constraintName, constraintName,
{}, {},
); );
@ -133,7 +133,7 @@ export class FieldModel extends MagicAttributeModel {
// @ts-ignore // @ts-ignore
await collection.sync({ ...options, force: false, alter: { drop: false } }); await collection.sync({ ...options, force: false, alter: { drop: false } });
await queryInterface.addConstraint(tableName, { await queryInterface.addConstraint(collection.addSchemaTableName(), {
type: 'unique', type: 'unique',
fields: [columnName], fields: [columnName],
name: constraintName, name: constraintName,
@ -142,7 +142,7 @@ export class FieldModel extends MagicAttributeModel {
} }
if (!unique && existsUniqueConstraint) { if (!unique && existsUniqueConstraint) {
await queryInterface.removeConstraint(collection.model.tableName, constraintName, { await queryInterface.removeConstraint(collection.addSchemaTableName(), constraintName, {
transaction: options.transaction, transaction: options.transaction,
}); });
} }
@ -164,7 +164,7 @@ export class FieldModel extends MagicAttributeModel {
const queryInterface = collection.db.sequelize.getQueryInterface(); const queryInterface = collection.db.sequelize.getQueryInterface();
await queryInterface.changeColumn( await queryInterface.changeColumn(
collection.model.tableName, collection.addSchemaTableName(),
collection.model.rawAttributes[this.get('name')].field, collection.model.rawAttributes[this.get('name')].field,
{ {
type: field.dataType, type: field.dataType,