Skip to content

Commit 7a376aa

Browse files
committed
Update repositories for new DB type mapping and their side effects
The Biggest one being, I couldn't figure out a way to default the hydrate method to the splat type. I think it will be rare that we don't need to customize this anyway.
1 parent a4836da commit 7a376aa

File tree

5 files changed

+24
-20
lines changed

5 files changed

+24
-20
lines changed

src/components/funding-account/funding-account.edgedb.repository.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,7 @@ import { FundingAccountRepository } from './funding-account.repository';
66

77
@Injectable()
88
export class FundingAccountEdgeDBRepository
9-
extends RepoFor(FundingAccount).withDefaults()
9+
extends RepoFor(FundingAccount, {
10+
hydrate: (fa) => fa['*'],
11+
}).withDefaults()
1012
implements PublicOf<FundingAccountRepository> {}

src/components/user/user.edgedb.repository.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import type { UserRepository } from './user.repository';
1111

1212
@Injectable()
1313
export class UserEdgeDBRepository
14-
extends RepoFor(User).withDefaults()
14+
extends RepoFor(User, {
15+
hydrate: (user) => user['*'],
16+
}).withDefaults()
1517
implements PublicOf<UserRepository>
1618
{
1719
async doesEmailAddressExist(email: string) {

src/core/edgedb/common.repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class CommonRepository implements PublicOf<Neo4jCommonRepository> {
4141
: EnhancedResource.of(fqn)
4242
: undefined;
4343
const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) =>
44-
e.select((res?.db ?? e.Object) as typeof e.Object, (obj) => ({
44+
e.select(res?.db ?? e.Object, (obj) => ({
4545
id: true,
4646
// eslint-disable-next-line @typescript-eslint/naming-convention
4747
_typeFQN_: obj.__type__.name,

src/core/edgedb/dto.repository.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { lowerCase } from 'lodash';
1212
import { AbstractClass } from 'type-fest';
1313
import {
1414
EnhancedResource,
15+
DBType as GetDBType,
1516
ID,
1617
isSortablePaginationInput,
1718
NotFoundException,
@@ -41,22 +42,21 @@ import { $, e } from './reexports';
4142
*/
4243
export const RepoFor = <
4344
TResourceStatic extends ResourceShape<any>,
44-
DBType extends (TResourceStatic['DB'] & {})['__element__'],
45-
HydratedShape extends objectTypeToSelectShape<DBType> = (TResourceStatic['DB'] & {})['*'],
45+
DBPathNode extends GetDBType<TResourceStatic>,
46+
DBType extends DBPathNode['__element__'],
47+
HydratedShape extends objectTypeToSelectShape<DBType>,
4648
>(
4749
resourceIn: TResourceStatic,
48-
options: {
49-
hydrate?: ShapeFn<$.TypeSet<DBType>, HydratedShape>;
50-
} = {},
50+
{
51+
hydrate,
52+
}: {
53+
hydrate: ShapeFn<$.TypeSet<DBType>, HydratedShape>;
54+
},
5155
) => {
5256
type Dto = $.computeObjectShape<DBType['__pointers__'], HydratedShape>;
5357

5458
const resource = EnhancedResource.of(resourceIn);
55-
56-
const hydrate = e.shape(
57-
resource.db,
58-
(options.hydrate ?? ((obj: any) => obj['*'])) as any,
59-
) as (scope: unknown) => HydratedShape;
59+
const dbType = resource.db as any as $.$expr_PathNode;
6060

6161
@Injectable()
6262
abstract class Repository extends CommonRepository {
@@ -125,7 +125,7 @@ export const RepoFor = <
125125
offset: (input.page - 1) * input.count,
126126
limit: input.count + 1,
127127
}));
128-
const items = e.select(thisPage, (obj) => ({
128+
const items = e.select(thisPage, (obj: any) => ({
129129
...this.hydrate(obj),
130130
limit: input.count,
131131
}));
@@ -179,8 +179,8 @@ export const RepoFor = <
179179

180180
async readMany(ids: readonly ID[]): Promise<readonly Dto[]> {
181181
const query = e.params({ ids: e.array(e.uuid) }, ({ ids }) =>
182-
e.select(this.resource.db, (obj: any) => ({
183-
...(this.hydrate(obj) as any),
182+
e.select(dbType, (obj: any) => ({
183+
...this.hydrate(obj),
184184
filter: e.op(obj.id, 'in', e.array_unpack(ids)),
185185
})),
186186
);
@@ -189,7 +189,7 @@ export const RepoFor = <
189189
}
190190

191191
async list(input: PaginationInput) {
192-
const all = e.select(this.resource.db, (obj: any) => {
192+
const all = e.select(dbType, (obj: any) => {
193193
const filters = many(this.listFilters(obj, input)).filter(isNotFalsy);
194194
const filter =
195195
filters.length === 0
@@ -209,15 +209,15 @@ export const RepoFor = <
209209

210210
async create(input: Omit<InsertShape<DBType>, `@${string}`>): Promise<Dto> {
211211
const query = e.select(
212-
e.insert(this.resource.db, input as any),
212+
(e.insert as any)(dbType, input),
213213
this.hydrate as any,
214214
);
215215
return (await this.db.run(query)) as Dto;
216216
}
217217

218218
async update(
219219
existing: Pick<Dto, 'id'>,
220-
input: UpdateShape<TResourceStatic['DB'] & {}>,
220+
input: UpdateShape<DBPathNode>,
221221
): Promise<Dto> {
222222
const object = e.cast(
223223
this.resource.db,

src/core/resources/resources.host.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class ResourcesHost {
9292
map as Record<string, EnhancedResource<any>>,
9393
(_, r, { SKIP }) => {
9494
try {
95-
return r.dbFQN;
95+
return r.dbFQN as string;
9696
} catch (e) {
9797
return SKIP;
9898
}

0 commit comments

Comments
 (0)