Skip to content

Commit

Permalink
💥 entity uses new store
Browse files Browse the repository at this point in the history
  • Loading branch information
vikiival committed Dec 28, 2023
1 parent 604cabf commit 6dd1237
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions metasquid/src/entity.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { FindOneOptions } from '@subsquid/typeorm-store'
import {
FindOneOptions,
FindOptionsRelations,
FindOptionsWhere,
In,
Repository
Repository,
type EntityManager
} from 'typeorm'
import { toEntity, toMap } from './shared'
import { EntityConstructor, Store } from './types'
Expand Down Expand Up @@ -57,7 +58,7 @@ function _get<T extends EntityWithId>(
entityConstructor: EntityConstructor<T>,
id: string,
optional?: boolean
): Promise<T | null> {
): Promise<T | undefined> {
const where: FindOptionsWhere<T> = { id } as FindOptionsWhere<T>
const callback = optional ? store.findOneBy : store.findOneByOrFail
return callback<T>(entityConstructor, where)
Expand All @@ -67,15 +68,15 @@ export function get<T extends EntityWithId>(
store: Store,
entityConstructor: EntityConstructor<T>,
id: string
): Promise<T | null> {
): Promise<T | undefined> {
return getOptional(store, entityConstructor, id)
}

export function getOptional<T extends EntityWithId>(
store: Store,
entityConstructor: EntityConstructor<T>,
id: string
): Promise<T | null> {
): Promise<T | undefined> {
const where: FindOptionsWhere<T> = { id } as FindOptionsWhere<T>
return store.findOneBy<T>(entityConstructor, where)
}
Expand Down Expand Up @@ -126,7 +127,7 @@ function findOne<T extends EntityWithId>(
entityConstructor: EntityConstructor<T>,
id: string,
options?: FindOneOptions<T>
): Promise<T | null> {
): Promise<T | undefined> {
const where: FindOptionsWhere<T> = { id } as FindOptionsWhere<T>
return store.findOne<T>(entityConstructor, { ...options, where })
}
Expand All @@ -136,7 +137,7 @@ export function findOneWithJoin<T extends EntityWithId>(
entityConstructor: EntityConstructor<T>,
id: string,
relations?: FindOptionsRelations<T>
): Promise<T | null> {
): Promise<T | undefined> {
return findOne(store, entityConstructor, id, { relations })
}

Expand Down Expand Up @@ -184,7 +185,7 @@ export function findByRawQuery<T extends EntityWithId>(
query: string,
args?: any[]
): Promise<T[]> {
const repository = store.getRepository(entityConstructor)
const repository = emOf(store).getRepository(entityConstructor)
return genericRepositoryQuery<T, T[]>(repository, query, args)
.then(res => res.map(el => toEntity(entityConstructor, el)))
}
Expand All @@ -195,7 +196,7 @@ export function has<T extends EntityWithId>(
idOrOptions: string | FindOptionsWhere<T>
): Promise<boolean> {
const where: FindOptionsWhere<T> = typeof idOrOptions === 'string' ? { id: idOrOptions } as FindOptionsWhere<T> : idOrOptions
return store.exists(entityConstructor, { where })
return emOf(store).exists(entityConstructor, { where })
}

export function genericRepositoryQuery<T extends EntityWithId, V>(
Expand All @@ -205,3 +206,10 @@ export function genericRepositoryQuery<T extends EntityWithId, V>(
): Promise<V> {
return repository.query(query, args) as Promise<V>
}

/**
* Returns the EntityManager of the provided store
*/
export function emOf(store: Store): EntityManager {
return (store as any).em() as EntityManager
}

0 comments on commit 6dd1237

Please sign in to comment.