Skip to content

Commit 7dc5c90

Browse files
committed
🚧 wip
1 parent 1b3b79e commit 7dc5c90

File tree

4 files changed

+65
-21
lines changed

4 files changed

+65
-21
lines changed

‎lib/context.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import type {
22
AbilityBuilderType,
33
createAbilityBuilder,
44
} from "./abilityBuilder";
5-
import type { InternalDrizzleInstance } from "./types/drizzleInstanceType";
5+
import { lazy } from "./helpers/lazy";
6+
import type { DrizzleInstance } from "./types/drizzleInstanceType";
67
import type {
78
CustomRumblePothosConfig,
89
RumbleInput,
910
} from "./types/rumbleInput";
1011

1112
export type ContextFunctionType<
1213
UserContext extends Record<string, any>,
13-
DB extends InternalDrizzleInstance,
14+
DB extends DrizzleInstance,
1415
RequestEvent extends Record<string, any>,
1516
Action extends string,
1617
PothosConfig extends CustomRumblePothosConfig,
@@ -27,7 +28,7 @@ export type ContextFunctionType<
2728

2829
export type ContextType<
2930
UserContext extends Record<string, any>,
30-
DB extends InternalDrizzleInstance,
31+
DB extends DrizzleInstance,
3132
RequestEvent extends Record<string, any>,
3233
Action extends string,
3334
PothosConfig extends CustomRumblePothosConfig,
@@ -39,7 +40,7 @@ export type ContextType<
3940

4041
export const createContextFunction = <
4142
UserContext extends Record<string, any>,
42-
DB extends InternalDrizzleInstance,
43+
DB extends DrizzleInstance,
4344
RequestEvent extends Record<string, any>,
4445
Action extends string,
4546
PothosConfig extends CustomRumblePothosConfig,
@@ -58,13 +59,16 @@ export const createContextFunction = <
5859
}: RumbleInput<UserContext, DB, RequestEvent, Action, PothosConfig> & {
5960
abilityBuilder: AbilityBuilder;
6061
}) => {
62+
const builtAbilityBuilder = lazy(() => abilityBuilder._.build());
63+
6164
return async (req: RequestEvent) => {
6265
const userContext = makeUserContext
6366
? await makeUserContext(req)
6467
: ({} as UserContext);
68+
6569
return {
6670
...userContext,
67-
abilities: abilityBuilder._.buildWithUserContext(userContext),
71+
abilities: builtAbilityBuilder(),
6872
};
6973
};
7074
};

‎lib/enum.ts‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { toCamelCase } from "drizzle-orm/casing";
22
import { type PgEnum, PgEnumColumn } from "drizzle-orm/pg-core";
33
import { capitalize } from "es-toolkit";
44
import type { SchemaBuilderType } from "./schemaBuilder";
5-
import type { InternalDrizzleInstance } from "./types/drizzleInstanceType";
5+
import type { DrizzleInstance } from "./types/drizzleInstanceType";
66
import { RumbleError } from "./types/rumbleError";
77
import type {
88
CustomRumblePothosConfig,
@@ -30,7 +30,7 @@ export type NonEnumFields<T> = {
3030

3131
export type EnumImplementerType<
3232
UserContext extends Record<string, any>,
33-
DB extends InternalDrizzleInstance,
33+
DB extends DrizzleInstance,
3434
RequestEvent extends Record<string, any>,
3535
Action extends string,
3636
PothosConfig extends CustomRumblePothosConfig,
@@ -47,7 +47,7 @@ export type EnumImplementerType<
4747

4848
export const createEnumImplementer = <
4949
UserContext extends Record<string, any>,
50-
DB extends InternalDrizzleInstance,
50+
DB extends DrizzleInstance,
5151
RequestEvent extends Record<string, any>,
5252
Action extends string,
5353
PothosConfig extends CustomRumblePothosConfig,
@@ -95,13 +95,13 @@ export const createEnumImplementer = <
9595
let enumValues: any[] | undefined;
9696

9797
if (tsName) {
98-
const schemaEnum = db._.schema[tsName as string];
98+
const schemaEnum = db._.schema![tsName as string];
9999

100100
enumSchemaName = tsName.toString();
101101

102-
const enumCol = Object.values(db._.schema)
102+
const enumCol = Object.values(db._.schema!)
103103
.filter((s) => typeof s === "object")
104-
.map((s) => Object.values(s[Symbol.for("drizzle:Columns")]))
104+
.map((s) => Object.values(s.columns))
105105
.flat(2)
106106
.find((s: any) => s.config?.enum === schemaEnum);
107107

@@ -112,7 +112,7 @@ Please ensure that you use the enum at least once as a column of a table!`);
112112

113113
enumValues = (enumCol as any).enumValues;
114114
} else if (enumColumn) {
115-
const schemaEnumEntry = Object.entries(db._.schema).find(
115+
const schemaEnumEntry = Object.entries(db._.schema!).find(
116116
([, value]) => value === (enumColumn as any).config.enum,
117117
);
118118

‎lib/object.ts‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import type { AbilityBuilderType } from "./abilityBuilder";
77
import { type EnumImplementerType, isEnumSchema } from "./enum";
88
import { mapSQLTypeToGraphQLType } from "./helpers/sqlTypes/mapSQLTypeToTSType";
99
import type { PossibleSQLType } from "./helpers/sqlTypes/types";
10-
import {
11-
type TableIdentifierTSName,
12-
tableHelper,
13-
} from "./helpers/tableHelpers";
10+
import { tableHelper } from "./helpers/tableHelpers";
1411
import type { OrderArgImplementerType } from "./orderArg";
1512
import type { MakePubSubInstanceType } from "./pubsub";
1613
import type { SchemaBuilderType } from "./schemaBuilder";
1714
import { adjustQueryForSearch } from "./search";
18-
import type { InternalDrizzleInstance } from "./types/drizzleInstanceType";
15+
import type {
16+
DrizzleInstance,
17+
DrizzleQueryFunction,
18+
} from "./types/drizzleInstanceType";
1919
import { RumbleError } from "./types/rumbleError";
2020
import type {
2121
CustomRumblePothosConfig,
@@ -49,7 +49,7 @@ const isProbablyAConfigObject = (t: any) => {
4949

5050
export const createObjectImplementer = <
5151
UserContext extends Record<string, any>,
52-
DB extends InternalDrizzleInstance,
52+
DB extends DrizzleInstance,
5353
RequestEvent extends Record<string, any>,
5454
Action extends string,
5555
PothosConfig extends CustomRumblePothosConfig,
@@ -113,7 +113,7 @@ export const createObjectImplementer = <
113113
abilityBuilder: AbilityBuilderInstance;
114114
}) => {
115115
return <
116-
ExplicitTableName extends TableIdentifierTSName<DB>,
116+
ExplicitTableName extends keyof DrizzleQueryFunction<DB>,
117117
RefName extends string,
118118
>({
119119
table,
@@ -187,8 +187,10 @@ export const createObjectImplementer = <
187187
primaryKeyValue: primaryKeyValue,
188188
});
189189
},
190-
applyFilters:
191-
abilityBuilder?._.registeredFilters?.[table as any]?.[readAction],
190+
applyFilters: abilityBuilder?._.registeredFilters({
191+
table,
192+
action: readAction,
193+
}),
192194
fields: (t) => {
193195
const columns = tableSchema.columns;
194196
const mapSQLTypeStringToExposedPothosType = <

‎lib/rumble.ts‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
type YogaServerOptions,
77
} from "graphql-yoga";
88
import { useSofa } from "sofa-api";
9+
import { RumbleError } from "../out";
910
import { createAbilityBuilder } from "./abilityBuilder";
1011
import { clientCreatorImplementer } from "./client/client";
1112
import { createContextFunction } from "./context";
@@ -34,6 +35,38 @@ export const rumble = <
3435
>(
3536
rumbleInput: RumbleInput<UserContext, DB, RequestEvent, Action, PothosConfig>,
3637
) => {
38+
if (!rumbleInput.db._.schema) {
39+
throw new RumbleError(`
40+
rumble could not find any schema in the provided drizzle instance.
41+
Make sure you import the schema and pass it to the drizzle instance:
42+
43+
export const db = drizzle(
44+
"postgres://postgres:postgres@localhost:5432/postgres",
45+
{
46+
relations,
47+
schema, // <--- add this line
48+
},
49+
);
50+
51+
`);
52+
}
53+
54+
if (!rumbleInput.db._.relations) {
55+
throw new RumbleError(`
56+
rumble could not find any relations in the provided drizzle instance.
57+
Make sure you import the relations and pass them to the drizzle instance:
58+
59+
export const db = drizzle(
60+
"postgres://postgres:postgres@localhost:5432/postgres",
61+
{
62+
relations, // <--- add this line
63+
schema,
64+
},
65+
);
66+
67+
`);
68+
}
69+
3770
// to be able to iterate over the actions, we populate the actions array in case the user does not
3871
if (!rumbleInput.actions) {
3972
rumbleInput.actions = ["read", "update", "delete"] as Action[];
@@ -84,6 +117,7 @@ export const rumble = <
84117
Action,
85118
PothosConfig
86119
>({ ...rumbleInput, pubsub });
120+
87121
const enum_ = createEnumImplementer<
88122
UserContext,
89123
DB,
@@ -95,6 +129,7 @@ export const rumble = <
95129
...rumbleInput,
96130
schemaBuilder,
97131
});
132+
98133
const whereArg = createWhereArgImplementer<
99134
UserContext,
100135
DB,
@@ -108,6 +143,7 @@ export const rumble = <
108143
schemaBuilder,
109144
enumImplementer: enum_,
110145
});
146+
111147
const orderArg = createOrderArgImplementer<
112148
UserContext,
113149
DB,
@@ -119,6 +155,7 @@ export const rumble = <
119155
...rumbleInput,
120156
schemaBuilder,
121157
});
158+
122159
const object = createObjectImplementer<
123160
UserContext,
124161
DB,
@@ -140,6 +177,7 @@ export const rumble = <
140177
enumImplementer: enum_,
141178
abilityBuilder,
142179
});
180+
143181
const query = createQueryImplementer<
144182
UserContext,
145183
DB,

0 commit comments

Comments
 (0)