|
1 | 1 | import { Injectable } from '@nestjs/common';
|
2 |
| -import { ID, NotFoundException, Session } from '~/common'; |
3 |
| -import { e, EdgeDB } from '~/core/edgedb'; |
4 |
| -import { CreatePerson, User, UserListInput } from './dto'; |
5 |
| -import { UserRepository } from './user.repository'; |
6 |
| - |
7 |
| -const hydrate = e.shape(e.User, (user) => ({ |
8 |
| - ...user['*'], |
9 |
| - // Other links if needed |
10 |
| -})); |
| 2 | +import { NotImplementedException, PublicOf } from '~/common'; |
| 3 | +import { e, RepoFor, ScopeOf } from '~/core/edgedb'; |
| 4 | +import { |
| 5 | + AssignOrganizationToUser, |
| 6 | + RemoveOrganizationFromUser, |
| 7 | + User, |
| 8 | + UserListInput, |
| 9 | +} from './dto'; |
| 10 | +import type { UserRepository } from './user.repository'; |
11 | 11 |
|
12 | 12 | @Injectable()
|
13 |
| -export class UserEdgedbRepository extends UserRepository { |
14 |
| - constructor(private readonly edgedb: EdgeDB) { |
15 |
| - super(); |
16 |
| - } |
17 |
| - |
18 |
| - async readOne(id: ID, _session: Session | ID) { |
19 |
| - const query = e.select(e.User, (user) => ({ |
20 |
| - ...hydrate(user), |
21 |
| - filter_single: { id }, |
22 |
| - })); |
23 |
| - const user = await this.edgedb.run(query); |
24 |
| - if (!user) { |
25 |
| - throw new NotFoundException('Could not find user'); |
26 |
| - } |
27 |
| - return user; |
28 |
| - } |
29 |
| - |
30 |
| - async readMany(ids: readonly ID[], _session: Session | ID) { |
31 |
| - const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) => |
32 |
| - e.select(e.User, (user) => ({ |
33 |
| - ...hydrate(user), |
34 |
| - filter: e.op(user.id, 'in', e.array_unpack(ids)), |
35 |
| - })), |
36 |
| - ); |
37 |
| - const users = await this.edgedb.run(query, { ids }); |
38 |
| - return users; |
39 |
| - } |
40 |
| - |
| 13 | +export class UserEdgeDBRepository |
| 14 | + extends RepoFor(User).withDefaults() |
| 15 | + implements |
| 16 | + Omit< |
| 17 | + PublicOf<UserRepository>, |
| 18 | + // hydrate is public (not default) and specific to Neo4j, |
| 19 | + // but it will only be called by other neo4j repositories, |
| 20 | + // so it doesn't have to match here |
| 21 | + 'hydrate' |
| 22 | + > |
| 23 | +{ |
41 | 24 | async doesEmailAddressExist(email: string) {
|
42 | 25 | const query = e.select(e.User, () => ({
|
43 | 26 | filter_single: { email },
|
44 | 27 | }));
|
45 |
| - const result = await this.edgedb.run(query); |
| 28 | + const result = await this.db.run(query); |
46 | 29 | return !!result;
|
47 | 30 | }
|
48 | 31 |
|
49 |
| - async list(input: UserListInput, _session: Session) { |
50 |
| - const sortKey = input.sort as keyof (typeof e.User)['*']; |
51 |
| - const all = e.select(e.User, (user) => ({ |
52 |
| - filter: e.all( |
53 |
| - input.filter.pinned != null |
54 |
| - ? e.op(user.pinned, '=', input.filter.pinned) |
55 |
| - : true, |
56 |
| - // More filters here when needed... |
57 |
| - ), |
58 |
| - order_by: { |
59 |
| - expression: user[sortKey], |
60 |
| - direction: input.order, |
61 |
| - }, |
62 |
| - })); |
63 |
| - const thisPage = e.select(all, () => ({ |
64 |
| - offset: (input.page - 1) * input.count, |
65 |
| - limit: input.count + 1, |
66 |
| - })); |
67 |
| - const query = e.select({ |
68 |
| - items: e.select(thisPage, (user) => ({ |
69 |
| - ...hydrate(user), |
70 |
| - limit: input.count, |
71 |
| - })), |
72 |
| - total: e.count(all), |
73 |
| - hasMore: e.op(e.count(thisPage), '>', input.count), |
74 |
| - }); |
75 |
| - return await this.edgedb.run(query); |
| 32 | + protected listFilters(user: ScopeOf<typeof e.User>, input: UserListInput) { |
| 33 | + return [ |
| 34 | + input.filter.pinned != null && |
| 35 | + e.op(user.pinned, '=', input.filter.pinned), |
| 36 | + // More filters here when needed... |
| 37 | + ]; |
76 | 38 | }
|
77 | 39 |
|
78 |
| - async create(input: CreatePerson) { |
79 |
| - const query = e.insert(e.User, { ...input }); |
80 |
| - const result = await this.edgedb.run(query); |
81 |
| - return result.id; |
| 40 | + assignOrganizationToUser(args: AssignOrganizationToUser): Promise<void> { |
| 41 | + throw new NotImplementedException().with(args); |
82 | 42 | }
|
83 |
| - |
84 |
| - async delete(id: ID, _session: Session, _object: User): Promise<void> { |
85 |
| - const query = e.delete(e.User, () => ({ |
86 |
| - filter_single: { id }, |
87 |
| - })); |
88 |
| - await this.edgedb.run(query); |
| 43 | + removeOrganizationFromUser(args: RemoveOrganizationFromUser): Promise<void> { |
| 44 | + throw new NotImplementedException().with(args); |
89 | 45 | }
|
90 | 46 | }
|
0 commit comments