|
| 1 | +import { Inject, Injectable } from '@nestjs/common'; |
| 2 | +import { EnhancedResource, ID, isIdLike, PublicOf } from '~/common'; |
| 3 | +import type { CommonRepository as Neo4jCommonRepository } from '~/core/database'; |
| 4 | +import { ResourceLike, ResourcesHost } from '~/core/resources/resources.host'; |
| 5 | +import type { BaseNode } from '../database/results'; |
| 6 | +import { EdgeDB } from './edgedb.service'; |
| 7 | +import { e } from './reexports'; |
| 8 | + |
| 9 | +/** |
| 10 | + * This provides a few methods out of the box. |
| 11 | + */ |
| 12 | +@Injectable() |
| 13 | +export class CommonRepository implements PublicOf<Neo4jCommonRepository> { |
| 14 | + @Inject(EdgeDB) |
| 15 | + protected readonly db: EdgeDB; |
| 16 | + @Inject(ResourcesHost) |
| 17 | + protected readonly resources: ResourcesHost; |
| 18 | + |
| 19 | + /** |
| 20 | + * Here for compatibility with the Neo4j version. |
| 21 | + * @deprecated this should be replaced with a different output shape, |
| 22 | + * after we finish migration. |
| 23 | + */ |
| 24 | + async getBaseNode(id: ID, fqn?: ResourceLike): Promise<BaseNode | undefined> { |
| 25 | + const res = await this.getBaseNodes([id], fqn); |
| 26 | + return res[0]; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Here for compatibility with the Neo4j version. |
| 31 | + * @deprecated this should be replaced with a different output shape, |
| 32 | + * after we finish migration. |
| 33 | + */ |
| 34 | + async getBaseNodes( |
| 35 | + ids: readonly ID[], |
| 36 | + fqn?: ResourceLike, |
| 37 | + ): Promise<readonly BaseNode[]> { |
| 38 | + const res = fqn |
| 39 | + ? typeof fqn === 'string' |
| 40 | + ? await this.resources.getByEdgeDB(fqn) |
| 41 | + : EnhancedResource.of(fqn) |
| 42 | + : undefined; |
| 43 | + const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) => |
| 44 | + e.select((res?.db ?? e.Object) as typeof e.Object, (obj) => ({ |
| 45 | + id: true, |
| 46 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 47 | + _typeFQN_: obj.__type__.name, |
| 48 | + createdAt: obj.is(e.Mixin.Timestamped).createdAt, |
| 49 | + filter: e.op(obj.id, 'in', e.array_unpack(ids)), |
| 50 | + })), |
| 51 | + ); |
| 52 | + const nodes = await this.db.run(query, { ids }); |
| 53 | + |
| 54 | + return await Promise.all( |
| 55 | + nodes.map(async (node): Promise<BaseNode> => { |
| 56 | + const res = await this.resources.getByEdgeDB(node._typeFQN_); |
| 57 | + return { |
| 58 | + identity: node.id, |
| 59 | + labels: [res.dbLabel], |
| 60 | + properties: { |
| 61 | + id: node.id, |
| 62 | + createdAt: node.createdAt, |
| 63 | + }, |
| 64 | + }; |
| 65 | + }), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + async deleteNode(objectOrId: { id: ID } | ID, _changeset?: ID) { |
| 70 | + const id = isIdLike(objectOrId) ? objectOrId : objectOrId.id; |
| 71 | + const query = e.delete(e.Object, () => ({ |
| 72 | + filter_single: { id }, |
| 73 | + })); |
| 74 | + await this.db.run(query); |
| 75 | + } |
| 76 | +} |
0 commit comments