Skip to content

Commit 65f63ee

Browse files
committed
Apply sane defaults for insert & hydrate for EdgeDB to reduce boilerplate in the concrete strategies
1 parent 064cc0c commit 65f63ee

File tree

4 files changed

+45
-46
lines changed

4 files changed

+45
-46
lines changed

src/components/comments/mention-notification/comment-via-mention-notification.strategy.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { node, Query, relation } from 'cypher-query-builder';
22
import { createRelationships, exp } from '~/core/database/query';
3-
import { e } from '~/core/edgedb';
43
import {
54
INotificationStrategy,
65
InputOf,
@@ -19,12 +18,6 @@ export class CommentViaMentionNotificationStrategy extends INotificationStrategy
1918
);
2019
}
2120

22-
insertForEdgeDB(input: InputOf<Mentioned>) {
23-
return e.insert(e.Notification.CommentMentioned, {
24-
comment: e.cast(e.Comments.Comment, e.uuid(input.comment)),
25-
});
26-
}
27-
2821
hydrateExtraForNeo4j(outVar: string) {
2922
return (query: Query) =>
3023
query
@@ -39,10 +32,4 @@ export class CommentViaMentionNotificationStrategy extends INotificationStrategy
3932
}).as(outVar),
4033
);
4134
}
42-
43-
hydrateExtraForEdgeDB() {
44-
return e.is(e.Notification.CommentMentioned, {
45-
comment: true,
46-
});
47-
}
4835
}

src/components/notification-system/system-notification.strategy.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,4 @@ export class SystemNotificationStrategy extends INotificationStrategy<SystemNoti
1313
recipientsForEdgeDB() {
1414
return e.User; // all users
1515
}
16-
17-
insertForEdgeDB(input: SystemNotification) {
18-
return e.insert(e.Notification.System, {
19-
message: input.message,
20-
});
21-
}
22-
23-
hydrateExtraForEdgeDB() {
24-
return e.is(e.Notification.System, {
25-
message: true,
26-
});
27-
}
2816
}

src/components/notifications/notification.edgedb.repository.ts

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { forwardRef, Inject, Injectable } from '@nestjs/common';
2-
import { Nil } from '@seedcompany/common';
3-
import { ID, PublicOf, ResourceShape } from '~/common';
2+
import { mapValues, Nil, setOf } from '@seedcompany/common';
3+
import { EnhancedResource, ID, PublicOf, ResourceShape } from '~/common';
44
import { e, RepoFor, ScopeOf } from '~/core/edgedb';
5+
import { mapToSetBlock } from '~/core/edgedb/query-util/map-to-set-block';
56
import {
67
MarkNotificationReadArgs,
78
Notification,
@@ -31,17 +32,30 @@ export class NotificationRepository
3132
async onModuleInit() {
3233
await this.service.ready.wait();
3334

34-
(this as any).hydrate = e.shape(e.Notification, (notification) => {
35-
return Object.assign(
36-
{
37-
__typename: notification.__type__.name,
38-
},
39-
notification['*'],
40-
...[...this.service.strategyMap.values()].flatMap(
41-
(strategy) => strategy.hydrateExtraForEdgeDB() ?? [],
42-
),
43-
);
44-
});
35+
const basePointers = setOf(
36+
Object.keys(this.resource.db.__element__.__pointers__),
37+
);
38+
const hydrateConcretes = Object.assign(
39+
{},
40+
...[...this.service.strategyMap].flatMap(([type, strategy]) => {
41+
if (strategy.hydrateExtraForEdgeDB) {
42+
return strategy.hydrateExtraForEdgeDB();
43+
}
44+
const dbType = EnhancedResource.of(type as typeof Notification).db;
45+
const ownPointers = Object.keys(dbType.__element__.__pointers__).filter(
46+
(p) => !p.startsWith('<') && !basePointers.has(p),
47+
);
48+
return e.is(
49+
dbType,
50+
mapValues.fromList(ownPointers, () => true).asRecord,
51+
);
52+
}),
53+
);
54+
(this as any).hydrate = e.shape(e.Notification, (notification) => ({
55+
__typename: notification.__type__.name,
56+
...notification['*'],
57+
...hydrateConcretes,
58+
}));
4559
}
4660

4761
async create(
@@ -51,7 +65,10 @@ export class NotificationRepository
5165
) {
5266
const strategy = this.service.getStrategy(type);
5367

54-
const created = strategy.insertForEdgeDB(input);
68+
const dbType = EnhancedResource.of(type as typeof Notification).db;
69+
const created =
70+
strategy.insertForEdgeDB?.(input) ??
71+
e.insert(dbType, mapToSetBlock(dbType, input, false));
5572

5673
const recipientsQuery = recipients
5774
? e.cast(e.User, e.cast(e.uuid, e.set(...recipients)))

src/components/notifications/notification.strategy.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,19 @@ export abstract class INotificationStrategy<
4343
return (query: Query) => query.setValues({ node: input }, true);
4444
}
4545

46-
abstract insertForEdgeDB(
46+
hydrateExtraForNeo4j(outVar: string): QueryFragment | undefined {
47+
const _used = outVar;
48+
return undefined;
49+
}
50+
}
51+
52+
/* eslint-disable @typescript-eslint/method-signature-style */
53+
// eslint-disable-next-line @typescript-eslint/naming-convention
54+
export interface INotificationStrategy<
55+
TNotification extends Notification,
56+
TInput = InputOf<TNotification>,
57+
> {
58+
insertForEdgeDB?(
4759
input: TInput,
4860
): $.Expression<
4961
$.TypeSet<
@@ -52,10 +64,5 @@ export abstract class INotificationStrategy<
5264
>
5365
>;
5466

55-
hydrateExtraForNeo4j(outVar: string): QueryFragment | undefined {
56-
const _used = outVar;
57-
return undefined;
58-
}
59-
60-
abstract hydrateExtraForEdgeDB(): Record<string, any>;
67+
hydrateExtraForEdgeDB?(): Record<string, any>;
6168
}

0 commit comments

Comments
 (0)