I think there should be a decorator for creating indexes. For instance, I would like to create a unique index on two fields.
would it be better to add index via migration?
@DerekTBrown You mean like so:
@Column({
unique: 'test'
})
field1: string;
@Column({
unique: 'test'
})
field2: string;
@RobinBuschmann that would require mixing interfaces, which I don't think is a good move. Maybe just a @Index decorator that lets you pass in a single index option as passed into the define operation.
Any news or progress on this? I was looking on how to create either FULLTEXT indexes or UNIQUE indexes on certain fields on different models.
@DerekTBrown Sorry, completely overseen your answer. You mean instead of defining them like @Table({indexes: [{index: 'FULLTEXT', fields: ['field1'], ...}]}), you would like to see a decorator like this?
@Index('FULLTEXT') // or @Index({index: 'FULLTEXT', unique: true})
@Column
field1: string;
And in order to use them across multiple columns something like:
const MyIndex = createIndexDecorator({index: 'FULLTEXT'});
@Table
class SomeModel extends Model<SomeModel> {
@MyIndex
field1: string;
@MyIndex
field2: string;
}
@RobinBuschmann I think the latter works perfectly! It's just a lot cleaner and straightforward to be honest.
@DerekTBrown Sorry, completely overseen your answer. You mean instead of defining them like
@Table({indexes: [{index: 'FULLTEXT', fields: ['field1'], ...}]}), you would like to see a decorator like this?@Index('FULLTEXT') // or @Index({index: 'FULLTEXT', unique: true}) @Column field1: string;And in order to use them across multiple columns something like:
const MyIndex = createIndexDecorator({index: 'FULLTEXT'}); @Table class SomeModel extends Model<SomeModel> { @MyIndex field1: string; @MyIndex field2: string; }
Are you plan to do this?
@s97712 It is not that important for now. Currently 1.0.0 and #529 have a higher priority. Keeping 1.0.0 and >=0.6.8 in sync is currently a big issue. So that it's more likely that this feature would only come with the next major version. Unless you one is willing to implement it for both branches master and 1.0.0
Implemented with #707
Is this dependent on utilizing sequelize.sync() verses an actual migration script?
Can not seem to get this decorator to work testing all variations
@Index('gatewayName')
@Column
name: string
Sample Entity:
@Table({
tableName: 'gateways',
timestamps: true,
})
export class Gateway extends Model<Gateway> {
@IsUUID(4)
@PrimaryKey
@Column
id: string
@Default(false)
@Column
isTestData: boolean
@Default('active')
@Column
status: string
@Index('gatewayName')
@Column
name: string
}
Sample Migration Script
import { Gateway } from '../gateways/gateway.entity'
const tableName = 'gateways'
module.exports = {
up: async (queryInterface, schema) =>
await queryInterface.createTable(
{ schema, tableName },
Gateway.rawAttributes,
),
down: async (queryInterface, schema) =>
await queryInterface.dropTable(tableName, schema),
}
Most helpful comment
@s97712 It is not that important for now. Currently 1.0.0 and #529 have a higher priority. Keeping 1.0.0 and >=0.6.8 in sync is currently a big issue. So that it's more likely that this feature would only come with the next major version. Unless you one is willing to implement it for both branches
masterand1.0.0