英文:
Indicate that a column should be indexed with TypeORM's entity schema files
问题
我正在使用 TypeORM 与实体模式文件,我需要指示在列上创建索引。如果我使用装饰器,我可以简单地这样做:
@Entity()
class Foo {
    @Index()
    @Column({ type: String })
    toBeIndexed: string;
}
然而,似乎没有办法只使用实体模式文件来实现同样的功能。
英文:
I'm using TypeORM with entity schema files and I need to indicate that an index needs to be created on a column. If I were using decorators, I could simply do:
@Entity()
class Foo {
    @Index()
    @Column({ type: String })
    toBeIndexed: string;
}
However there seems to be no way to do the same thing just with entity schema files?
答案1
得分: 1
让我与您分享我的示例。希望这段代码片段能给您一些提示。
import { EntitySchema, Column, Index } from 'typeorm';
export const UserSchema = new EntitySchema({
  name: 'User',
  columns: {
    id: {
      type: Number,
      primary: true,
      generated: true,
    },
    name: {
      type: String,
    },
    age: {
      type: Number,
    },
  },
  indices: [
    {
      name: 'IDX_NAME',
      columns: ['name'],
    },
  ],
});
英文:
let me share my example with you. hope this code snippet will give you a hint.
import { EntitySchema, Column, Index } from 'typeorm';
export const UserSchema = new EntitySchema({
  name: 'User',
  columns: {
    id: {
      type: Number,
      primary: true,
      generated: true,
    },
    name: {
      type: String,
    },
    age: {
      type: Number,
    },
  },
  indices: [
    {
      name: 'IDX_NAME',
      columns: ['name'],
    },
  ],
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论