Skip to content

Commit 19d047f

Browse files
committed
New decoupled way to associated EdgeDB types to our Resources
This was causing problems with TS. TS would follow/compute these DB types for inheritance and our Relations object, which was extra work that we didn't need. It also caused problems with inheritance, which we tried to solve with `abstractType()` This new way allows us to map from Resources to EdgeDB types, without TS needing to consider those DB types when evaluating our Resource types.
1 parent 0fbb531 commit 19d047f

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

src/common/resource.dto.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +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 type { ResourceMap } from '~/core';
7+
import type { ResourceDBMap, ResourceMap } from '~/core';
88
import { $, abstractType, e } from '~/core/edgedb/reexports';
99
import { ScopedRole } from '../components/authorization';
1010
import { CalculatedSymbol } from './calculated.decorator';
@@ -85,6 +85,9 @@ export type ResourceRelationsShape = ResourceShape<any>['Relations'];
8585
* A helper class to query the static info of a resource in a typed way.
8686
*/
8787
export class EnhancedResource<T extends ResourceShape<any>> {
88+
/** @internal */
89+
static readonly dbTypes = new WeakMap<ResourceShape<any>, $.$expr_PathNode>();
90+
8891
private constructor(readonly type: T) {}
8992
private static readonly refs = new WeakMap<
9093
ResourceShape<any>,
@@ -240,16 +243,16 @@ export class EnhancedResource<T extends ResourceShape<any>> {
240243
return new Set(props);
241244
}
242245

243-
get db(): T['DB'] & {} {
244-
const type = this.type.DB;
246+
get db(): DBType<T> {
247+
const type = EnhancedResource.dbTypes.get(this.type);
245248
if (!type) {
246249
throw new ServerException(`No DB type defined for ${this.name}`);
247250
}
248-
return type;
251+
return type as any;
249252
}
250253

251-
get dbFQN(): (T['DB'] & {})['__element__']['__name__'] {
252-
return this.db.__element__.__name__;
254+
get dbFQN(): DBType<T>['__element__']['__name__'] {
255+
return this.db.__element__.__name__ as any;
253256
}
254257

255258
@Once()
@@ -295,6 +298,13 @@ export type ResourceName<TResourceStatic extends ResourceShape<any>> =
295298
}[keyof ResourceMap] &
296299
string;
297300

301+
export type DBType<TResourceStatic extends ResourceShape<any>> =
302+
ResourceName<TResourceStatic> extends keyof ResourceDBMap
303+
? ResourceDBMap[ResourceName<TResourceStatic>] extends infer T extends $.$expr_PathNode
304+
? T
305+
: never
306+
: never;
307+
298308
export type MaybeUnsecuredInstance<TResourceStatic extends ResourceShape<any>> =
299309
MaybeSecured<InstanceType<TResourceStatic>>;
300310

src/core/resources/map.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,6 @@
1414
export interface ResourceMap {
1515
// Use interface merging to add to this interface in the owning module.
1616
}
17+
18+
// eslint-disable-next-line @typescript-eslint/no-empty-interface
19+
export interface ResourceDBMap {}
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
import { ResourceShape } from '~/common';
1+
import { EnhancedResource, ResourceShape } from '~/common';
2+
import type { $ } from '../edgedb';
23
import { __privateDontUseThis } from './resource-map-holder';
34

45
/**
56
* Register a resource for dynamic usage across the codebase.
67
* Be sure to add the type to the type map as well.
78
* See {@link import('./map').ResourceMap} for details on that.
89
*/
9-
export const RegisterResource = () => {
10+
export const RegisterResource = ({ db }: { db?: $.$expr_PathNode } = {}) => {
1011
return <T extends ResourceShape<any>>(target: T) => {
1112
__privateDontUseThis[target.name] = target;
13+
db && EnhancedResource.dbTypes.set(target, db);
1214
return target;
1315
};
1416
};

0 commit comments

Comments
 (0)