Skip to content

Commit 1d3185b

Browse files
committed
Associate EdgeDB types with our TS resources
1 parent ee8c63c commit 1d3185b

File tree

24 files changed

+87
-1
lines changed

24 files changed

+87
-1
lines changed

src/common/resource.dto.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { LazyGetter as Once } from 'lazy-get-decorator';
44
import { DateTime } from 'luxon';
55
import { keys as keysOf } from 'ts-transformer-keys';
66
import { inspect } from 'util';
7+
import { $, abstractType, e } from '~/core/edgedb/reexports';
78
import { ScopedRole } from '../components/authorization';
89
import { CalculatedSymbol } from './calculated.decorator';
910
import { DataObject } from './data-object';
@@ -35,6 +36,7 @@ export const resolveByTypename =
3536
})
3637
@DbLabel('BaseNode')
3738
export abstract class Resource extends DataObject {
39+
static readonly DB = abstractType(e.Resource);
3840
static readonly Props: string[] = keysOf<Resource>();
3941
static readonly SecuredProps: string[] = [];
4042

@@ -59,6 +61,7 @@ export abstract class Resource extends DataObject {
5961
type Thunk<T> = T | (() => T);
6062

6163
export type ResourceShape<T> = AbstractClassType<T> & {
64+
DB?: $.$expr_PathNode;
6265
Props: string[];
6366
SecuredProps: string[];
6467
// An optional list of props that exist on the BaseNode in the DB.
@@ -236,6 +239,14 @@ export class EnhancedResource<T extends ResourceShape<any>> {
236239
return new Set(props);
237240
}
238241

242+
get db(): T['DB'] & {} {
243+
const type = this.type.DB;
244+
if (!type) {
245+
throw new ServerException(`No DB type defined for ${this.name}`);
246+
}
247+
return type;
248+
}
249+
239250
@Once()
240251
get dbLabels() {
241252
return getDbClassLabels(this.type);

src/components/budget/dto/budget-record.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Field, ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
33
import { BaseNode } from '~/core/database/results';
4+
import { e } from '~/core/edgedb';
45
import { RegisterResource } from '~/core/resources';
56
import {
67
ID,
@@ -22,6 +23,7 @@ import { Budget } from './budget.dto';
2223
implements: [Resource, ChangesetAware],
2324
})
2425
export class BudgetRecord extends IntersectionType(ChangesetAware, Resource) {
26+
static readonly DB = e.Budget.Record;
2527
static readonly Props = keysOf<BudgetRecord>();
2628
static readonly SecuredProps = keysOf<SecuredProps<BudgetRecord>>();
2729
static readonly Parent = import('./budget.dto').then((m) => m.Budget);

src/components/budget/dto/budget.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Field, ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
33
import { BaseNode } from '~/core/database/results';
4+
import { e } from '~/core/edgedb';
45
import { RegisterResource } from '~/core/resources';
56
import {
67
DbLabel,
@@ -24,6 +25,7 @@ import { BudgetStatus } from './budget-status.enum';
2425
implements: [Resource, ChangesetAware],
2526
})
2627
export class Budget extends IntersectionType(ChangesetAware, Resource) {
28+
static readonly DB = e.Budget;
2729
static readonly Props = keysOf<Budget>();
2830
static readonly SecuredProps = keysOf<SecuredProps<Budget>>();
2931
static readonly Relations = {

src/components/ceremony/dto/ceremony.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Field, ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
3+
import { e } from '~/core/edgedb';
34
import { RegisterResource } from '~/core/resources';
45
import {
56
Calculated,
@@ -19,6 +20,7 @@ import { CeremonyType } from './ceremony-type.enum';
1920
implements: [Resource],
2021
})
2122
export class Ceremony extends Resource {
23+
static readonly DB = e.Engagement.Ceremony;
2224
static readonly Props = keysOf<Ceremony>();
2325
static readonly SecuredProps = keysOf<SecuredProps<Ceremony>>();
2426
static readonly Parent = import('../../engagement/dto').then(

src/components/engagement/dto/engagement.dto.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { DateTime } from 'luxon';
44
import { keys as keysOf } from 'ts-transformer-keys';
55
import { MergeExclusive } from 'type-fest';
66
import { BaseNode } from '~/core/database/results';
7+
import { abstractType, e } from '~/core/edgedb';
78
import { RegisterResource } from '~/core/resources';
89
import {
910
Calculated,
@@ -64,6 +65,7 @@ export const resolveEngagementType = (val: Pick<AnyEngagement, '__typename'>) =>
6465
* This should be used for GraphQL but never for TypeScript types.
6566
*/
6667
class Engagement extends ChangesetAwareResource {
68+
static readonly DB = abstractType(e.Engagement);
6769
static readonly Props: string[] = keysOf<Engagement>();
6870
static readonly SecuredProps: string[] = keysOf<SecuredProps<Engagement>>();
6971
static readonly Parent = import('../../project/dto').then((m) => m.IProject);
@@ -152,6 +154,7 @@ export { Engagement as IEngagement, AnyEngagement as Engagement };
152154
implements: [Engagement],
153155
})
154156
export class LanguageEngagement extends Engagement {
157+
static readonly DB = e.LanguageEngagement;
155158
static readonly Props = keysOf<LanguageEngagement>();
156159
static readonly SecuredProps = keysOf<SecuredProps<LanguageEngagement>>();
157160
static readonly Relations = {
@@ -197,6 +200,7 @@ export class LanguageEngagement extends Engagement {
197200
implements: [Engagement],
198201
})
199202
export class InternshipEngagement extends Engagement {
203+
static readonly DB = e.InternshipEngagement;
200204
static readonly Props = keysOf<InternshipEngagement>();
201205
static readonly SecuredProps = keysOf<SecuredProps<InternshipEngagement>>();
202206
static readonly Parent = import('../../project/dto').then(

src/components/ethno-art/dto/ethno-art.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
3+
import { e } from '~/core/edgedb';
34
import { RegisterResource } from '~/core/resources';
45
import {
56
DbUnique,
@@ -21,6 +22,7 @@ declare module '../../product/dto/producible.dto' {
2122
implements: [Producible, Resource],
2223
})
2324
export class EthnoArt extends Producible {
25+
static readonly DB = e.EthnoArt;
2426
static readonly Props = keysOf<EthnoArt>();
2527
static readonly SecuredProps = keysOf<SecuredProps<EthnoArt>>();
2628

src/components/field-region/dto/field-region.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
3+
import { e } from '~/core/edgedb';
34
import { RegisterResource } from '~/core/resources';
45
import {
56
DbUnique,
@@ -18,6 +19,7 @@ import {
1819
implements: [Resource],
1920
})
2021
export class FieldRegion extends Resource {
22+
static readonly DB = e.FieldRegion;
2123
static readonly Props = keysOf<FieldRegion>();
2224
static readonly SecuredProps = keysOf<SecuredProps<FieldRegion>>();
2325

src/components/field-zone/dto/field-zone.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
3+
import { e } from '~/core/edgedb';
34
import { RegisterResource } from '~/core/resources';
45
import {
56
DbUnique,
@@ -17,6 +18,7 @@ import {
1718
implements: [Resource],
1819
})
1920
export class FieldZone extends Resource {
21+
static readonly DB = e.FieldZone;
2022
static readonly Props = keysOf<FieldZone>();
2123
static readonly SecuredProps = keysOf<SecuredProps<FieldZone>>();
2224

src/components/file/dto/node.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { Readable } from 'stream';
66
import { keys as keysOf } from 'ts-transformer-keys';
77
import { MergeExclusive, Opaque } from 'type-fest';
88
import { BaseNode } from '~/core/database/results';
9+
import { abstractType, e } from '~/core/edgedb';
910
import { RegisterResource } from '~/core/resources';
1011
import {
1112
DateTimeField,
@@ -50,6 +51,7 @@ export const resolveFileNode = (val: AnyFileNode) => {
5051
* This should be used for GraphQL but never for TypeScript types.
5152
*/
5253
abstract class FileNode extends Resource {
54+
static readonly DB = abstractType(e.File.Node);
5355
static readonly Props: string[] = keysOf<FileNode>();
5456
static readonly SecuredProps: string[] = keysOf<SecuredProps<FileNode>>();
5557

@@ -104,6 +106,7 @@ abstract class BaseFile extends FileNode {
104106
implements: [FileNode, Resource],
105107
})
106108
export class FileVersion extends BaseFile {
109+
static readonly DB = e.File.Version;
107110
static readonly Props = keysOf<FileVersion>();
108111
static readonly SecuredProps = keysOf<SecuredProps<FileVersion>>();
109112

@@ -115,6 +118,7 @@ export class FileVersion extends BaseFile {
115118
implements: [FileNode, Resource],
116119
})
117120
export class File extends BaseFile {
121+
static readonly DB = e.File;
118122
static readonly Props = keysOf<File>();
119123
static readonly SecuredProps = keysOf<SecuredProps<File>>();
120124

@@ -133,6 +137,7 @@ export class File extends BaseFile {
133137
implements: [FileNode, Resource],
134138
})
135139
export class Directory extends FileNode {
140+
static readonly DB = e.Directory;
136141
static readonly Props = keysOf<Directory>();
137142
static readonly SecuredProps = keysOf<SecuredProps<Directory>>();
138143

src/components/film/dto/film.dto.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ObjectType } from '@nestjs/graphql';
22
import { keys as keysOf } from 'ts-transformer-keys';
3+
import { e } from '~/core/edgedb';
34
import { RegisterResource } from '~/core/resources';
45
import {
56
DbUnique,
@@ -21,6 +22,7 @@ declare module '../../product/dto/producible.dto' {
2122
implements: [Producible, Resource],
2223
})
2324
export class Film extends Producible {
25+
static readonly DB = e.Film;
2426
static readonly Props = keysOf<Film>();
2527
static readonly SecuredProps = keysOf<SecuredProps<Film>>();
2628

0 commit comments

Comments
 (0)