Skip to content

Commit

Permalink
use typeorm-store in codegen
Browse files Browse the repository at this point in the history
  • Loading branch information
belopash committed Mar 28, 2024
1 parent 0d0c0a7 commit 8269dcb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
2 changes: 1 addition & 1 deletion test/erc20-transfers/src/model/generated/transfer.model.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_} from "typeorm"
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_} from "@subsquid/typeorm-store"
import * as marshal from "./marshal"

@Entity_()
Expand Down
16 changes: 8 additions & 8 deletions typeorm/typeorm-codegen/src/codegen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ export function generateOrmModels(model: Model, dir: OutDir): void {
index.line(`export * from "./${toCamelCase(name)}.model"`)
const out = dir.file(`${toCamelCase(name)}.model.ts`)
const imports = new ImportRegistry(name)
imports.useTypeorm('Entity', 'Column', 'PrimaryColumn')
imports.useTypeormStore('Entity', 'Column', 'PrimaryColumn')
out.lazy(() => imports.render(model, out))
out.line()
printComment(entity, out)
entity.indexes?.forEach(index => {
if (index.fields.length < 2) return
imports.useTypeorm('Index')
imports.useTypeormStore('Index')
out.line(`@Index_([${index.fields.map(f => `"${f.name}"`).join(', ')}], {unique: ${!!index.unique}})`)
})
out.line('@Entity_()')
Expand Down Expand Up @@ -100,14 +100,14 @@ export function generateOrmModels(model: Model, dir: OutDir): void {
break
case 'fk':
if (getFieldIndex(entity, key)?.unique) {
imports.useTypeorm('OneToOne', 'Index', 'JoinColumn')
imports.useTypeormStore('OneToOne', 'Index', 'JoinColumn')
out.line(`@Index_({unique: true})`)
out.line(
`@OneToOne_(() => ${prop.type.entity}, {nullable: true})`
)
out.line(`@JoinColumn_()`)
} else {
imports.useTypeorm('ManyToOne', 'Index')
imports.useTypeormStore('ManyToOne', 'Index')
if (!entity.indexes?.some(index => index.fields[0]?.name == key && index.fields.length > 1)) {
out.line(`@Index_()`)
}
Expand All @@ -120,7 +120,7 @@ export function generateOrmModels(model: Model, dir: OutDir): void {
case 'lookup':
break
case 'list-lookup':
imports.useTypeorm('OneToMany')
imports.useTypeormStore('OneToMany')
out.line(
`@OneToMany_(() => ${prop.type.entity}, e => e.${prop.type.field})`
)
Expand Down Expand Up @@ -495,7 +495,7 @@ function collectVariants(model: Model): Set<string> {
function addIndexAnnotation(entity: Entity, field: string, imports: ImportRegistry, out: Output): void {
let index = getFieldIndex(entity, field)
if (index == null) return
imports.useTypeorm('Index')
imports.useTypeormStore('Index')
if (index.unique) {
out.line(`@Index_({unique: true})`)
} else {
Expand Down Expand Up @@ -531,7 +531,7 @@ class ImportRegistry {

constructor(private owner: string) {}

useTypeorm(...names: string[]): void {
useTypeormStore(...names: string[]): void {
names.forEach((name) => this.typeorm.add(name))
}

Expand Down Expand Up @@ -565,7 +565,7 @@ class ImportRegistry {
const importList = Array.from(this.typeorm).map(
(name) => name + ' as ' + name + '_'
)
out.line(`import {${importList.join(', ')}} from "typeorm"`)
out.line(`import {${importList.join(', ')}} from "@subsquid/typeorm-store"`)
}
if (this.marshal) {
out.line(`import * as marshal from "./marshal"`)
Expand Down

0 comments on commit 8269dcb

Please sign in to comment.