指示应使用TypeORM的实体模式文件对列进行索引。

huangapple go评论55阅读模式
英文:

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'],
    },
  ],
});

huangapple
  • 本文由 发表于 2023年5月18日 07:07:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76276737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定