@m1212e/rumble 0.0.1 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +176 -7
- package/index.cjs +1 -1
- package/index.cjs.map +1 -1
- package/index.d.cts +53 -11
- package/index.d.ts +53 -11
- package/index.js +1 -1
- package/index.js.map +1 -1
- package/package.json +1 -24
package/README.md
CHANGED
@@ -1,15 +1,184 @@
|
|
1
1
|
# rumble
|
2
|
+
rumble is a combined ability and graphql builder built around [drizzle](https://orm.drizzle.team/docs/overview) and [pothos](https://pothos-graphql.dev/docs/plugins/drizzle), inspired by [CASL](https://casl.js.org/v6/en/). It takes much of the required configuration of your shoulders and makes creating a GraphQL server very easy!
|
2
3
|
|
3
|
-
|
4
|
+
> Please note that drizzle hasn't reached a full stable release yet and, as shown in the warning [here](https://pothos-graphql.dev/docs/plugins/drizzle), this is not stable yet.
|
4
5
|
|
5
|
-
|
6
|
-
|
6
|
+
> Using rumble and reading these docs requires some basic knowledge about the above mentioned tools. If you feel stuck, please make sure to familiarize yourself with those first!
|
7
|
+
|
8
|
+
> This is still in a very early stage and needs more testing. Please feel free to report everything you find/your feedback via the issues/discussion section of this repo!
|
9
|
+
|
10
|
+
## Getting started
|
11
|
+
The following example is an excerpt from the example setup you can find [here](./example).
|
12
|
+
|
13
|
+
First install into your existing TS project:
|
14
|
+
```
|
15
|
+
bun add @m1212e/rumble
|
16
|
+
npm i @m1212e/rumble
|
17
|
+
```
|
18
|
+
then call the rumble creator:
|
19
|
+
```ts
|
20
|
+
import * as schema from "./db/schema";
|
21
|
+
import { rumble } from "@m1212e/rumble";
|
22
|
+
|
23
|
+
export const db = drizzle(
|
24
|
+
"postgres://postgres:postgres@localhost:5432/postgres",
|
25
|
+
{ schema }
|
26
|
+
);
|
27
|
+
|
28
|
+
const { abilityBuilder, schemaBuilder, yoga, implementDefaultObject } = rumble({ db });
|
29
|
+
```
|
30
|
+
> If the creation of a drizzle instance with the schema definition seems unfamiliar to you, please see their excellent [getting started guide](https://orm.drizzle.team/docs/get-started)
|
31
|
+
|
32
|
+
### Defining abilities
|
33
|
+
Now we can start to allow things. We call these allowed things `abilities`. To allow reading posts we can e.g. do this:
|
34
|
+
```ts
|
35
|
+
abilityBuilder.posts.allow("read");
|
7
36
|
```
|
37
|
+
This example assumes that our database has a posts table set up. Now everyone can read posts. Thats ok for simple applications but most of the time we want to allow access based on certain conditions being met. To do this, we also can add a more restrictive ability. E.g. if we only want to allow reading posts we could add a `published` boolean to our database model and then define our ability around that column:
|
38
|
+
```ts
|
39
|
+
abilityBuilder.posts.allow("read").when({
|
40
|
+
where: eq(schema.posts.published, true),
|
41
|
+
});
|
42
|
+
```
|
43
|
+
The `when` call accepts a variety of restrictions which have different effects. E.g. you could also set a limit on how many results can be queried at a time. The accepted type is the same as the drizzle query api (e.g. the `findMany` call) which will be relevant later.
|
8
44
|
|
9
|
-
|
45
|
+
#### Dynamic abilities
|
46
|
+
Most of the time we want to allow things based on who the user is. If they are logged in they should be able to change their username. But only theirs, not the ones of any other users. For this, abilities allow for conditions based on the call context of a request. To use this, we need to create a context callback when initiating rumble first:
|
47
|
+
```ts
|
48
|
+
const { abilityBuilder, schemaBuilder, yoga, implementDefaultObject } =
|
49
|
+
rumble({
|
50
|
+
db,
|
51
|
+
// the type of the request parameter may vary based on the HTTP library you are using.
|
52
|
+
context(request) {
|
53
|
+
// here you could access the cookies of the
|
54
|
+
// request object and extract the user ID or some
|
55
|
+
// other form of permissions
|
56
|
+
return {
|
57
|
+
userId: 2, // we mock this for now
|
58
|
+
};
|
59
|
+
},
|
60
|
+
});
|
61
|
+
```
|
62
|
+
we now can use the data from the request context in our abilities:
|
63
|
+
```ts
|
64
|
+
abilityBuilder.users
|
65
|
+
.allow(["read", "update", "delete"])
|
66
|
+
.when(({ userId }) => ({ where: eq(schema.users.id, userId) }));
|
10
67
|
|
11
|
-
```bash
|
12
|
-
bun run index.ts
|
13
68
|
```
|
14
69
|
|
15
|
-
|
70
|
+
### Defining objects
|
71
|
+
Pothos, the underlying GraphQL schema builder rumble uses needs the schema of the objects which can be returned, defined, before we can use them. Therefore we will define those first:
|
72
|
+
> If this is unfamiliar to you, please read through the pothos docs, especially the page on the [drizzle plugin](https://pothos-graphql.dev/docs/plugins/drizzle)
|
73
|
+
```ts
|
74
|
+
const PostRef = schemaBuilder.drizzleObject("posts", {
|
75
|
+
name: "Post",
|
76
|
+
fields: (t) => ({
|
77
|
+
id: t.exposeInt("id"),
|
78
|
+
content: t.exposeString("content"),
|
79
|
+
author: t.relation("author", {
|
80
|
+
// this is how you can apply the above abilities to the queries
|
81
|
+
query: (_args, ctx) => ctx.abilities.users.filter("read"),
|
82
|
+
}),
|
83
|
+
}),
|
84
|
+
});
|
85
|
+
```
|
86
|
+
In the above object definition we tell pothos to expose the id and content so the fields will be just passed along from out database results and we define a relation to the posts author. We also restrict which author can be read. If the user which sends this request is not the author of a post, they cannot see the author and the request will fail. The `ctx.abilities.users.filter("read")` call simply injects the filter we defined in the abilities earlier and therefore restricts what can be returned.
|
87
|
+
|
88
|
+
#### Automatic object implementation
|
89
|
+
Since this can get a little extensive, especially for large models, rumble offers the `implementDefaultObject` helper. This does all of the above and will simply expose all fields and relations but with the ability restrictions applied.
|
90
|
+
```ts
|
91
|
+
const UserRef = implementDefaultObject({
|
92
|
+
name: "User",
|
93
|
+
tableName: "users",
|
94
|
+
});
|
95
|
+
```
|
96
|
+
|
97
|
+
### Definign queries and mutations
|
98
|
+
Now we can define some things you can do. Again we use pothos for that. So please refer to [the docs](https://pothos-graphql.dev/docs/plugins/drizzle) if something is unclear.
|
99
|
+
```ts
|
100
|
+
schemaBuilder.queryFields((t) => {
|
101
|
+
return {
|
102
|
+
findManyUsers: t.drizzleField({
|
103
|
+
type: [UserRef],
|
104
|
+
resolve: (query, root, args, ctx, info) => {
|
105
|
+
return db.query.users.findMany({
|
106
|
+
...query,
|
107
|
+
// this is how we can apply the abilities to the request
|
108
|
+
...ctx.abilities.users.filter("read"),
|
109
|
+
});
|
110
|
+
},
|
111
|
+
}),
|
112
|
+
};
|
113
|
+
});
|
114
|
+
|
115
|
+
schemaBuilder.queryFields((t) => {
|
116
|
+
return {
|
117
|
+
findManyPosts: t.drizzleField({
|
118
|
+
type: [PostRef],
|
119
|
+
resolve: (query, root, args, ctx, info) => {
|
120
|
+
return db.query.posts.findMany({
|
121
|
+
...query,
|
122
|
+
...ctx.abilities.posts.filter("read"),
|
123
|
+
});
|
124
|
+
},
|
125
|
+
}),
|
126
|
+
};
|
127
|
+
});
|
128
|
+
|
129
|
+
import {
|
130
|
+
assertFindFirstExists,
|
131
|
+
assertFirstEntryExists,
|
132
|
+
} from "@m1212e/rumble";
|
133
|
+
schemaBuilder.queryFields((t) => {
|
134
|
+
return {
|
135
|
+
findFirstUser: t.drizzleField({
|
136
|
+
type: UserRef,
|
137
|
+
resolve: (query, root, args, ctx, info) => {
|
138
|
+
return (
|
139
|
+
db.query.users
|
140
|
+
.findFirst({
|
141
|
+
...query,
|
142
|
+
// again, here we apply the abilities from above
|
143
|
+
where: ctx.abilities.users.filter("read").where,
|
144
|
+
})
|
145
|
+
// note that we need to manually raise an error if the value is not found since there is a type mismatch between GraphQL and the drizzle query result.
|
146
|
+
.then(assertFindFirstExists)
|
147
|
+
);
|
148
|
+
},
|
149
|
+
}),
|
150
|
+
};
|
151
|
+
});
|
152
|
+
```
|
153
|
+
A mutation could look like this:
|
154
|
+
```ts
|
155
|
+
schemaBuilder.mutationFields((t) => {
|
156
|
+
return {
|
157
|
+
updateUsername: t.drizzleField({
|
158
|
+
type: UserRef,
|
159
|
+
args: {
|
160
|
+
userId: t.arg.int({ required: true }),
|
161
|
+
newName: t.arg.string({ required: true }),
|
162
|
+
},
|
163
|
+
resolve: (query, root, args, ctx, info) => {
|
164
|
+
return (
|
165
|
+
db
|
166
|
+
.update(schema.users)
|
167
|
+
.set({
|
168
|
+
name: args.newName,
|
169
|
+
})
|
170
|
+
.where(
|
171
|
+
and(
|
172
|
+
eq(schema.users.id, args.userId),
|
173
|
+
ctx.abilities.users.filter("update").where
|
174
|
+
)
|
175
|
+
)
|
176
|
+
.returning({ id: schema.users.id, name: schema.users.name })
|
177
|
+
// note the different error mapper
|
178
|
+
.then(assertFirstEntryExists)
|
179
|
+
);
|
180
|
+
},
|
181
|
+
}),
|
182
|
+
};
|
183
|
+
});
|
184
|
+
```
|
package/index.cjs
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
'use strict';var P=require('@pothos/core'),O=require('@pothos/plugin-drizzle'),graphqlYoga=require('graphql-yoga'),drizzleOrm=require('drizzle-orm');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var P__default=/*#__PURE__*/_interopDefault(P);var O__default=/*#__PURE__*/_interopDefault(O);function
|
1
|
+
'use strict';var P=require('@pothos/core'),O=require('@pothos/plugin-drizzle'),graphqlYoga=require('graphql-yoga'),drizzleOrm=require('drizzle-orm');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var P__default=/*#__PURE__*/_interopDefault(P);var O__default=/*#__PURE__*/_interopDefault(O);function g(t){return typeof t!="function"}function A(t){return typeof t=="function"&&t.constructor.name!=="AsyncFunction"}var h=({db:t})=>{let s={},l={},b=u=>({allow:a=>{let i=l[u];i||(i={},l[u]=i);let r=Array.isArray(a)?a:[a];for(let p of r){let n=i[p];n||(n=[],i[p]=n);}return {when:p=>{for(let n of r)i[n].push(p);}}}});for(let u of Object.keys(t.query))s[u]=b(u);return {...s,registeredConditions:l,buildWithUserContext:u=>{let a={},i=r=>({filter:p=>{let n=l[r];if(!n)throw "TODO (No allowed entry found for this condition) #1";let c=n[p];if(!c)throw "TODO (No allowed entry found for this condition) #2";let C=c.filter(g),x=c.filter(A).map(e=>e(u)),o=[...C,...x],m;for(let e of o)e.limit&&(m===void 0||e.limit>m)&&(m=e.limit);let f;for(let e of o)e.columns&&(f===void 0?f=e.columns:f={...f,...e.columns});let D=o.filter(e=>e.where).map(e=>e.where);return {where:D.length>0?drizzleOrm.or(...D):void 0,columns:f,limit:m}}});for(let r of Object.keys(t.query))a[r]=i(r);return a}}};var d=class extends Error{constructor(s){super(s),this.name="RumbleError";}};var R=({db:t,nativeServerOptions:s,context:l,onlyQuery:b=!1,actions:u=["create","read","update","delete"]})=>{let a=h({db:t}),i=async n=>{let c=l?await l(n):{};return {...c,abilities:a.buildWithUserContext(c)}},r=new P__default.default({plugins:[O__default.default],drizzle:{client:t},defaultFieldNullability:!1,defaultInputFieldRequiredness:!0});return r.queryType({}),b||r.mutationType({}),{abilityBuilder:a,schemaBuilder:r,yoga:()=>graphqlYoga.createYoga({...s,schema:r.toSchema(),context:i}),implementDefaultObject:({tableName:n,name:c,readAction:C="read"})=>{let x=t._.schema[n];return r.drizzleObject(n,{name:c,fields:o=>{let m=(y,e)=>{switch(y){case"serial":return o.exposeInt(e);case"int":return o.exposeInt(e);case"string":return o.exposeString(e);case"text":return o.exposeString(e);case"boolean":return o.exposeBoolean(e);default:throw new d(`Unknown SQL type: ${y}. Please open an issue so it can be added.`)}},f=Object.entries(x.columns).reduce((y,[e,B])=>(y[e]=m(B.getSQLType(),B.name),y),{}),D=Object.entries(x.relations).reduce((y,[e,B])=>(y[e]=o.relation(e,{query:(U,T)=>T.abilities[n].filter(C)}),y),{});return {...f,...D}}})}}};var w=t=>{if(!t)throw new d("Value not found but required (findFirst)");return t},Q=t=>{let s=t.at(0);if(!s)throw new d("Value not found but required (firstEntry)");return s};exports.RumbleError=d;exports.assertFindFirstExists=w;exports.assertFirstEntryExists=Q;exports.rumble=R;//# sourceMappingURL=index.cjs.map
|
2
2
|
//# sourceMappingURL=index.cjs.map
|
package/index.cjs.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../lib/abilities/builder.ts","../lib/gql/builder.ts","../lib/helpers/rumbleError.ts","../lib/helpers/helper.ts"],"names":["isSimpleCondition","condition","isSyncFunctionCondition","createAbilityBuilder","db","actions","builder","registeredConditions","createEntityObject","entityKey","action","conditionsPerEntity","conditionsPerEntityAndAction","userContext","simpleConditions","syncFunctionConditions","allConditionObjects","highestLimit","conditionObject","combinedAllowedColumns","accumulatedWhereConditions","o","or","rumble","nativeServerOptions","makeUserContext","abilityBuilder","makeContext","req","nativeBuilder","SchemaBuilder","DrizzlePlugin","createYoga","RumbleError","message","assertFindFirstExists","value","assertFirstEntryExists","v"],"mappings":"oTAsBA,SAASA,EACRC,CAC6C,CAAA,CAC7C,OAAO,OAAOA,CAAAA,EAAc,UAC7B,CAEA,SAASC,CAAAA,CACRD,EACgE,CAChE,OACC,OAAOA,CAAc,EAAA,UAAA,EACrBA,EAAU,WAAY,CAAA,IAAA,GAAS,eAEjC,CAWO,IAAME,CAAAA,CAAuB,CAIlC,CACD,EAAA,CAAAC,CACA,CAAA,OAAA,CAAAC,CAAU,CAAA,CAAC,SAAU,MAAQ,CAAA,QAAA,CAAU,QAAQ,CAChD,CAGM,GAAA,CAGL,IAAMC,CAEF,CAAA,GAEEC,CAQF,CAAA,GAEEC,CAAsBC,CAAAA,CAAAA,GAA4B,CACvD,KAAA,CAAQC,CAA8B,EAAA,CAGrC,IAAIC,CAAsBJ,CAAAA,CAAAA,CAAqBE,CAAS,CAAA,CACnDE,CACJA,GAAAA,CAAAA,CAAsB,EACtBJ,CAAAA,CAAAA,CAAqBE,CAAS,CAAA,CAAIE,CAGnC,CAAA,CAAA,IAAMN,EAAU,KAAM,CAAA,OAAA,CAAQK,CAAM,CAAIA,CAAAA,CAAAA,CAAS,CAACA,CAAM,CAAA,CACxD,IAAWA,IAAAA,CAAAA,IAAUL,CAAS,CAAA,CAC7B,IAAIO,CAA+BD,CAAAA,CAAAA,CAAoBD,CAAM,CAAA,CACxDE,CACJA,GAAAA,CAAAA,CAA+B,EAC/BD,CAAAA,CAAAA,CAAoBD,CAAM,CAAA,CAAIE,CAEhC,EAAA,CAEA,OAAO,CACN,IAAA,CAAOX,GAAoD,CAC1D,IAAA,IAAWS,KAAUL,CACiBM,CAAAA,CAAAA,CAAoBD,CAAM,CAAA,CAClC,IAAKT,CAAAA,CAAS,EAE7C,CACD,CACD,C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,sBAAyB,CAAA,CACxB,GAAGR,CAAAA,CACH,OAAQK,CAAc,CAAA,QAAA,EACtB,CAAA,OAAA,CAASF,CACV,CAAC,CACH,CACD,MCrHaM,CAAN,CAAA,cAA0B,KAAM,CACtC,WAAA,CAAYC,CAAiB,CAAA,CAC5B,KAAMA,CAAAA,CAAO,EACb,IAAK,CAAA,IAAA,CAAO,cACb,CACD,EC2BO,IAAMC,EAA4BC,CAA4B,EAAA,CACpE,GAAI,CAACA,CAAO,CAAA,MAAM,IAAIH,CAAY,CAAA,0CAA0C,EAC5E,OAAOG,CACR,EAyCaC,CAA6BD,CAAAA,CAAAA,EAAkB,CAC3D,IAAME,CAAIF,CAAAA,CAAAA,CAAM,GAAG,CAAC,CAAA,CACpB,GAAI,CAACE,CAAG,CAAA,MAAM,IAAIL,CAAY,CAAA,2CAA2C,CACzE,CAAA,OAAOK,CACR","file":"index.cjs","sourcesContent":["import { or } from \"drizzle-orm\";\nimport type {\n\tGenericDrizzleDbTypeConstraints,\n\tQueryConditionObject,\n} from \"../types/genericDrizzleDbType\";\n\nexport type AbilityBuilder = ReturnType<typeof createAbilityBuilder>;\n\ntype Condition<DBParameters, UserContext> =\n\t| SimpleCondition<DBParameters>\n\t| SyncFunctionCondition<DBParameters, UserContext>;\n// | AsyncFunctionCondition<DBParameters, UserContext>;\n\ntype SimpleCondition<DBParameters> = DBParameters;\ntype SyncFunctionCondition<DBParameters, UserContext> = (\n\tcontext: UserContext,\n) => DBParameters;\n// type AsyncFunctionCondition<DBParameters, UserContext> = (\n// \tcontext: UserContext,\n// ) => Promise<DBParameters>;\n\n// type guards for the condition types\nfunction isSimpleCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SimpleCondition<DBParameters> {\n\treturn typeof condition !== \"function\";\n}\n\nfunction isSyncFunctionCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SyncFunctionCondition<DBParameters, UserContext> {\n\treturn (\n\t\ttypeof condition === \"function\" &&\n\t\tcondition.constructor.name !== \"AsyncFunction\"\n\t);\n}\n\n// function isAsyncFunctionCondition<DBParameters, UserContext>(\n// \tcondition: Condition<DBParameters, UserContext>,\n// ): condition is AsyncFunctionCondition<DBParameters, UserContext> {\n// \treturn (\n// \t\ttypeof condition === \"function\" &&\n// \t\tcondition.constructor.name === \"AsyncFunction\"\n// \t);\n// }\n\nexport const createAbilityBuilder = <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tAction extends string = \"create\" | \"read\" | \"update\" | \"delete\",\n>({\n\tdb,\n\tactions = [\"create\", \"read\", \"update\", \"delete\"] as Action[],\n}: {\n\tdb: DB;\n\tactions?: Action[];\n}) => {\n\ttype DBEntityKey = keyof DB[\"query\"];\n\n\tconst builder: {\n\t\t[key in DBEntityKey]: ReturnType<typeof createEntityObject>;\n\t} = {} as any;\n\n\tconst registeredConditions: {\n\t\t[key in DBEntityKey]: {\n\t\t\t[key in Action[number]]: (\n\t\t\t\t| QueryConditionObject\n\t\t\t\t| ((context: UserContext) => QueryConditionObject)\n\t\t\t)[];\n\t\t\t// | ((context: UserContext) => Promise<QueryConditionObject>)\n\t\t};\n\t} = {} as any;\n\n\tconst createEntityObject = (entityKey: DBEntityKey) => ({\n\t\tallow: (action: Action | Action[]) => {\n\t\t\ttype DBParameters = Parameters<DB[\"query\"][DBEntityKey][\"findMany\"]>[0];\n\n\t\t\tlet conditionsPerEntity = registeredConditions[entityKey];\n\t\t\tif (!conditionsPerEntity) {\n\t\t\t\tconditionsPerEntity = {} as any;\n\t\t\t\tregisteredConditions[entityKey] = conditionsPerEntity;\n\t\t\t}\n\n\t\t\tconst actions = Array.isArray(action) ? action : [action];\n\t\t\tfor (const action of actions) {\n\t\t\t\tlet conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\tconditionsPerEntityAndAction = [];\n\t\t\t\t\tconditionsPerEntity[action] = conditionsPerEntityAndAction;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twhen: (condition: Condition<DBParameters, UserContext>) => {\n\t\t\t\t\tfor (const action of actions) {\n\t\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\t\tconditionsPerEntityAndAction.push(condition);\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\n\tfor (const entityKey of Object.keys(db.query) as DBEntityKey[]) {\n\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t}\n\treturn {\n\t\t...builder,\n\t\tregisteredConditions,\n\t\tbuildWithUserContext: (userContext: UserContext) => {\n\t\t\tconst builder: {\n\t\t\t\t[key in DBEntityKey]: ReturnType<typeof createEntityObject>;\n\t\t\t} = {} as any;\n\n\t\t\tconst createEntityObject = (entityKey: DBEntityKey) => ({\n\t\t\t\tfilter: (action: Action) => {\n\t\t\t\t\tconst conditionsPerEntity = registeredConditions[entityKey];\n\t\t\t\t\tif (!conditionsPerEntity) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #1\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #2\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst simpleConditions =\n\t\t\t\t\t\tconditionsPerEntityAndAction.filter(isSimpleCondition);\n\n\t\t\t\t\tconst syncFunctionConditions = conditionsPerEntityAndAction\n\t\t\t\t\t\t.filter(isSyncFunctionCondition)\n\t\t\t\t\t\t.map((condition) => condition(userContext));\n\n\t\t\t\t\t// const asyncFunctionConditions = await Promise.all(\n\t\t\t\t\t// \tconditionsPerEntityAndAction\n\t\t\t\t\t// \t\t.filter(isAsyncFunctionCondition)\n\t\t\t\t\t// \t\t.map((condition) => condition(userContext)),\n\t\t\t\t\t// );\n\n\t\t\t\t\tconst allConditionObjects = [\n\t\t\t\t\t\t...simpleConditions,\n\t\t\t\t\t\t...syncFunctionConditions,\n\t\t\t\t\t\t// ...asyncFunctionConditions,\n\t\t\t\t\t];\n\n\t\t\t\t\tlet highestLimit = undefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.limit) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\thighestLimit === undefined ||\n\t\t\t\t\t\t\t\tconditionObject.limit > highestLimit\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\thighestLimit = conditionObject.limit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet combinedAllowedColumns: Record<string, any> | undefined =\n\t\t\t\t\t\tundefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.columns) {\n\t\t\t\t\t\t\tif (combinedAllowedColumns === undefined) {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = conditionObject.columns;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = {\n\t\t\t\t\t\t\t\t\t...combinedAllowedColumns,\n\t\t\t\t\t\t\t\t\t...conditionObject.columns,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tconst accumulatedWhereConditions = allConditionObjects\n\t\t\t\t\t\t.filter((o) => o.where)\n\t\t\t\t\t\t.map((o) => o.where);\n\n\t\t\t\t\tconst combinedWhere =\n\t\t\t\t\t\taccumulatedWhereConditions.length > 0\n\t\t\t\t\t\t\t? or(...accumulatedWhereConditions)\n\t\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\twhere: combinedWhere,\n\t\t\t\t\t\tcolumns: combinedAllowedColumns,\n\t\t\t\t\t\tlimit: highestLimit,\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tfor (const entityKey of Object.keys(db.query) as DBEntityKey[]) {\n\t\t\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t\t\t}\n\n\t\t\treturn builder;\n\t\t},\n\t};\n};\n","import SchemaBuilder from \"@pothos/core\";\nimport DrizzlePlugin from \"@pothos/plugin-drizzle\";\nimport { type YogaServerOptions, createYoga } from \"graphql-yoga\";\nimport { createAbilityBuilder } from \"../abilities/builder\";\nimport type { GenericDrizzleDbTypeConstraints } from \"../types/genericDrizzleDbType\";\n\nexport const rumble = async <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tRequestEvent extends Record<string, any>,\n>({\n\tdb,\n\tnativeServerOptions,\n\tcontext: makeUserContext,\n}: {\n\t/**\n\t * Your drizzle database instance\n\t */\n\tdb: DB;\n\t/**\n\t * Optional options for the native GraphQL Yoga server\n\t */\n\tnativeServerOptions?:\n\t\t| Omit<YogaServerOptions<RequestEvent, any>, \"schema\" | \"context\">\n\t\t| undefined;\n\t/**\n\t * A function for providing context for each request based on the incoming HTTP Request.\n\t * The type of the parameter equals the HTTPRequest type of your chosen server.\n\t */\n\tcontext?:\n\t\t| ((event: RequestEvent) => Promise<UserContext> | UserContext)\n\t\t| undefined;\n}) => {\n\tconst abilityBuilder = createAbilityBuilder<UserContext, DB>({\n\t\tdb,\n\t});\n\n\tconst makeContext = async (req: RequestEvent) => {\n\t\tconst userContext = makeUserContext\n\t\t\t? await makeUserContext(req)\n\t\t\t: ({} as UserContext);\n\t\treturn {\n\t\t\t...userContext,\n\t\t\tabilities: abilityBuilder.buildWithUserContext(userContext),\n\t\t};\n\t};\n\n\tconst nativeBuilder = new SchemaBuilder<{\n\t\tContext: Awaited<ReturnType<typeof makeContext>>;\n\t\t// Scalars: Scalars<Prisma.Decimal, Prisma.InputJsonValue | null, Prisma.InputJsonValue> & {\n\t\t// \tFile: {\n\t\t// \t\tInput: File;\n\t\t// \t\tOutput: never;\n\t\t// \t};\n\t\t// \tJSONObject: {\n\t\t// \t\tInput: any;\n\t\t// \t\tOutput: any;\n\t\t// \t};\n\t\t// };\n\t\tDrizzleSchema: DB[\"_\"][\"fullSchema\"];\n\t\tDefaultFieldNullability: false;\n\t\tDefaultArgumentNullability: false;\n\t\tDefaultInputFieldRequiredness: true;\n\t}>({\n\t\tplugins: [DrizzlePlugin],\n\t\tdrizzle: {\n\t\t\tclient: db,\n\t\t},\n\t\tdefaultFieldNullability: false,\n\t\tdefaultInputFieldRequiredness: true,\n\t});\n\n\tnativeBuilder.queryType({});\n\tnativeBuilder.mutationType({});\n\n\treturn {\n\t\t/**\n * The ability builder. Use it to declare whats allowed for each entity in your DB.\n * \n * @example\n * \n * ```ts\n * // users can edit themselves\n abilityBuilder.users\n .allow([\"read\", \"update\", \"delete\"])\n .when(({ userId }) => ({ where: eq(schema.users.id, userId) }));\n \n // everyone can read posts\n abilityBuilder.posts.allow(\"read\");\n * \n * ```\n */\n\t\tabilityBuilder,\n\t\t/**\n\t\t * The pothos schema builder. See https://pothos-graphql.dev/docs/plugins/drizzle\n\t\t */\n\t\tschemaBuilder: nativeBuilder,\n\t\t/**\n * The native yoga instance. Can be used to run an actual HTTP server.\n * \n * @example\n * \n * ```ts\n import { createServer } from \"node:http\";\n * const server = createServer(yoga());\n server.listen(3000, () => {\n console.log(\"Visit http://localhost:3000/graphql\");\n });\n * ```\n */\n\t\tyoga: () =>\n\t\t\tcreateYoga<RequestEvent>({\n\t\t\t\t...nativeServerOptions,\n\t\t\t\tschema: nativeBuilder.toSchema(),\n\t\t\t\tcontext: makeContext,\n\t\t\t}),\n\t};\n};\n","export class RumbleError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"RumbleError\";\n\t}\n}\n","import { RumbleError } from \"./rumbleError\";\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n * schemaBuilder.queryFields((t) => {\n return {\n findFirstUser: t.drizzleField({\n type: UserRef,\n resolve: (query, root, args, ctx, info) => {\n return (\n db.query.users\n .findFirst({\n ...query,\n where: ctx.abilities.users.filter(\"read\").where,\n })\n // note that we need to manually raise an error if the value is not found\n .then(assertFindFirstExists)\n );\n },\n }),\n };\n });\n * ```\n */\nexport const assertFindFirstExists = <T>(value: T | undefined): T => {\n\tif (!value) throw new RumbleError(\"Value not found but required (findFirst)\");\n\treturn value;\n};\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n schemaBuilder.mutationFields((t) => {\n return {\n updateUsername: t.drizzleField({\n type: UserRef,\n args: {\n userId: t.arg.int({ required: true }),\n newName: t.arg.string({ required: true }),\n },\n resolve: (query, root, args, ctx, info) => {\n return db\n .update(schema.users)\n .set({\n name: args.newName,\n })\n .where(\n and(\n eq(schema.users.id, args.userId),\n ctx.abilities.users.filter(\"update\").where\n )\n )\n .returning({ id: schema.users.id, name: schema.users.name })\n // note that we need to manually raise an error if the value is not found\n .then(assertFirstEntryExists);\n },\n }),\n };\n });\n * ```\n */\nexport const assertFirstEntryExists = <T>(value: T[]): T => {\n\tconst v = value.at(0);\n\tif (!v) throw new RumbleError(\"Value not found but required (firstEntry)\");\n\treturn v;\n};\n"]}
|
1
|
+
{"version":3,"sources":["../lib/abilities/builder.ts","../lib/helpers/rumbleError.ts","../lib/gql/builder.ts","../lib/helpers/helper.ts"],"names":["isSimpleCondition","condition","isSyncFunctionCondition","createAbilityBuilder","db","builder","registeredConditions","createEntityObject","entityKey","action","conditionsPerEntity","actions","conditionsPerEntityAndAction","userContext","simpleConditions","syncFunctionConditions","allConditionObjects","highestLimit","conditionObject","combinedAllowedColumns","accumulatedWhereConditions","o","or","RumbleError","message","rumble","nativeServerOptions","makeUserContext","onlyQuery","abilityBuilder","makeContext","req","nativeBuilder","SchemaBuilder","DrizzlePlugin","createYoga","tableName","name","readAction","schema","t","mapSQLTypeStringToExposedPothosType","sqlType","columnName","fields","acc","key","value","relations","_args","ctx","assertFindFirstExists","assertFirstEntryExists","v"],"mappings":"oTAwBA,SAASA,CACRC,CAAAA,CAAAA,CAC6C,CAC7C,OAAO,OAAOA,CAAAA,EAAc,UAC7B,CAEA,SAASC,CACRD,CAAAA,CAAAA,CACgE,CAChE,OACC,OAAOA,CAAc,EAAA,UAAA,EACrBA,CAAU,CAAA,WAAA,CAAY,OAAS,eAEjC,CAWO,IAAME,CAAAA,CAAuB,CAIlC,CACD,EAAAC,CAAAA,CACD,IAEM,CAIL,IAAMC,CAEF,CAAA,EAEEC,CAAAA,CAAAA,CAQF,EAAC,CAECC,EAAsBC,CAA2B,GAAA,CACtD,KAAQC,CAAAA,CAAAA,EAA8B,CACrC,IAAIC,CAAsBJ,CAAAA,CAAAA,CAAqBE,CAAS,CACnDE,CAAAA,CAAAA,GACJA,CAAsB,CAAA,GACtBJ,CAAqBE,CAAAA,CAAS,CAAIE,CAAAA,CAAAA,CAAAA,CAGnC,IAAMC,CAAU,CAAA,KAAA,CAAM,OAAQF,CAAAA,CAAM,CAAIA,CAAAA,CAAAA,CAAS,CAACA,CAAM,EACxD,IAAWA,IAAAA,CAAAA,IAAUE,CAAS,CAAA,CAC7B,IAAIC,CAAAA,CAA+BF,CAAoBD,CAAAA,CAAM,EACxDG,CACJA,GAAAA,CAAAA,CAA+B,EAAC,CAChCF,CAAoBD,CAAAA,CAAM,CAAIG,CAAAA,CAAAA,EAEhC,CAEA,OAAO,CACN,IAAOX,CAAAA,CAAAA,EAAoD,CAC1D,IAAWQ,IAAAA,CAAAA,IAAUE,CACiBD,CAAAA,CAAAA,CAAoBD,CAAM,CAClC,CAAA,IAAA,CAAKR,CAAS,EAE7C,CACD,CACD,CACD,CAAA,CAAA,CAEA,QAAWO,CAAa,IAAA,MAAA,CAAO,IAAKJ,CAAAA,CAAAA,CAAG,KAAK,CAAA,CAC3CC,CA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A,MAAA,CAC7C,CAACM,CAAK,CAAA,CAACC,CAAKC,CAAAA,CAAK,CAChBF,IAAAA,CAAAA,CAAIC,CAAG,CAAA,CAAIL,EACVM,CAAM,CAAA,UAAA,EACNA,CAAAA,CAAAA,CAAM,IACP,CAAA,CACOF,CAER,CAAA,CAAA,EAID,CAEMG,CAAAA,CAAAA,CAAY,MAAO,CAAA,OAAA,CAAQT,EAAO,SAAS,CAAA,CAAE,MAClD,CAAA,CAACM,EAAK,CAACC,CAAAA,CAAKC,CAAK,CAAA,IAChBF,CAAIC,CAAAA,CAAG,CAAIN,CAAAA,CAAAA,CAAE,SAASM,CAAK,CAAA,CAC1B,KAAO,CAAA,CAACG,CAAYC,CAAAA,CAAAA,GACnBA,CAAI,CAAA,SAAA,CAAUd,CAAgB,CAAE,CAAA,MAAA,CAAOE,CAAU,CACnD,CAAQ,CAAA,CACDO,CAER,CAAA,CAAA,EAID,CAEA,CAAA,OAAO,CACN,GAAGD,EACH,GAAGI,CACJ,CACD,CACD,CAAC,CACF,CA+CA,CACD,ECxLaG,IAAAA,CAAAA,CAA4BJ,CAA4B,EAAA,CACpE,GAAI,CAACA,CAAAA,CAAO,MAAM,IAAIxB,CAAY,CAAA,0CAA0C,CAC5E,CAAA,OAAOwB,CACR,CAyCaK,CAAAA,CAAAA,CAA6BL,CAAkB,EAAA,CAC3D,IAAMM,CAAAA,CAAIN,CAAM,CAAA,EAAA,CAAG,CAAC,CACpB,CAAA,GAAI,CAACM,CAAAA,CAAG,MAAM,IAAI9B,CAAAA,CAAY,2CAA2C,CAAA,CACzE,OAAO8B,CACR","file":"index.cjs","sourcesContent":["import { SQL, or } from \"drizzle-orm\";\nimport type { ExcludeFromUnionIfNotOnlyType } from \"../helpers/unionTypeExclude\";\nimport type { ExcludeFirst } from \"../helpers/unionTypeExtractor\";\nimport type {\n\tGenericDrizzleDbTypeConstraints,\n\tQueryConditionObject,\n} from \"../types/genericDrizzleDbType\";\n\nexport type AbilityBuilder = ReturnType<typeof createAbilityBuilder>;\n\ntype Condition<DBParameters, UserContext> =\n\t| SimpleCondition<DBParameters>\n\t| SyncFunctionCondition<DBParameters, UserContext>;\n// | AsyncFunctionCondition<DBParameters, UserContext>;\n\ntype SimpleCondition<DBParameters> = DBParameters;\ntype SyncFunctionCondition<DBParameters, UserContext> = (\n\tcontext: UserContext,\n) => DBParameters;\n// type AsyncFunctionCondition<DBParameters, UserContext> = (\n// \tcontext: UserContext,\n// ) => Promise<DBParameters>;\n\n// type guards for the condition types\nfunction isSimpleCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SimpleCondition<DBParameters> {\n\treturn typeof condition !== \"function\";\n}\n\nfunction isSyncFunctionCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SyncFunctionCondition<DBParameters, UserContext> {\n\treturn (\n\t\ttypeof condition === \"function\" &&\n\t\tcondition.constructor.name !== \"AsyncFunction\"\n\t);\n}\n\n// function isAsyncFunctionCondition<DBParameters, UserContext>(\n// \tcondition: Condition<DBParameters, UserContext>,\n// ): condition is AsyncFunctionCondition<DBParameters, UserContext> {\n// \treturn (\n// \t\ttypeof condition === \"function\" &&\n// \t\tcondition.constructor.name === \"AsyncFunction\"\n// \t);\n// }\n\nexport const createAbilityBuilder = <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tAction extends string,\n>({\n\tdb,\n}: {\n\tdb: DB;\n}) => {\n\ttype DBQueryKey = keyof DB[\"query\"];\n\ttype DBParameters = Parameters<DB[\"query\"][DBQueryKey][\"findMany\"]>[0];\n\n\tconst builder: {\n\t\t[key in DBQueryKey]: ReturnType<typeof createEntityObject>;\n\t} = {} as any;\n\n\tconst registeredConditions: {\n\t\t[key in DBQueryKey]: {\n\t\t\t[key in Action[number]]: (\n\t\t\t\t| QueryConditionObject\n\t\t\t\t| ((context: UserContext) => QueryConditionObject)\n\t\t\t)[];\n\t\t\t// | ((context: UserContext) => Promise<QueryConditionObject>)\n\t\t};\n\t} = {} as any;\n\n\tconst createEntityObject = (entityKey: DBQueryKey) => ({\n\t\tallow: (action: Action | Action[]) => {\n\t\t\tlet conditionsPerEntity = registeredConditions[entityKey];\n\t\t\tif (!conditionsPerEntity) {\n\t\t\t\tconditionsPerEntity = {} as any;\n\t\t\t\tregisteredConditions[entityKey] = conditionsPerEntity;\n\t\t\t}\n\n\t\t\tconst actions = Array.isArray(action) ? action : [action];\n\t\t\tfor (const action of actions) {\n\t\t\t\tlet conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\tconditionsPerEntityAndAction = [];\n\t\t\t\t\tconditionsPerEntity[action] = conditionsPerEntityAndAction;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twhen: (condition: Condition<DBParameters, UserContext>) => {\n\t\t\t\t\tfor (const action of actions) {\n\t\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\t\tconditionsPerEntityAndAction.push(condition);\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\n\tfor (const entityKey of Object.keys(db.query) as DBQueryKey[]) {\n\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t}\n\treturn {\n\t\t...builder,\n\t\tregisteredConditions,\n\t\tbuildWithUserContext: (userContext: UserContext) => {\n\t\t\tconst builder: {\n\t\t\t\t[key in DBQueryKey]: ReturnType<typeof createEntityObject>;\n\t\t\t} = {} as any;\n\n\t\t\tconst createEntityObject = (entityKey: DBQueryKey) => ({\n\t\t\t\tfilter: (action: Action) => {\n\t\t\t\t\tconst conditionsPerEntity = registeredConditions[entityKey];\n\t\t\t\t\tif (!conditionsPerEntity) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #1\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #2\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst simpleConditions =\n\t\t\t\t\t\tconditionsPerEntityAndAction.filter(isSimpleCondition);\n\n\t\t\t\t\tconst syncFunctionConditions = conditionsPerEntityAndAction\n\t\t\t\t\t\t.filter(isSyncFunctionCondition)\n\t\t\t\t\t\t.map((condition) => condition(userContext));\n\n\t\t\t\t\t// const asyncFunctionConditions = await Promise.all(\n\t\t\t\t\t// \tconditionsPerEntityAndAction\n\t\t\t\t\t// \t\t.filter(isAsyncFunctionCondition)\n\t\t\t\t\t// \t\t.map((condition) => condition(userContext)),\n\t\t\t\t\t// );\n\n\t\t\t\t\tconst allConditionObjects = [\n\t\t\t\t\t\t...simpleConditions,\n\t\t\t\t\t\t...syncFunctionConditions,\n\t\t\t\t\t\t// ...asyncFunctionConditions,\n\t\t\t\t\t];\n\n\t\t\t\t\tlet highestLimit = undefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.limit) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\thighestLimit === undefined ||\n\t\t\t\t\t\t\t\tconditionObject.limit > highestLimit\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\thighestLimit = conditionObject.limit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet combinedAllowedColumns: Record<string, any> | undefined =\n\t\t\t\t\t\tundefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.columns) {\n\t\t\t\t\t\t\tif (combinedAllowedColumns === undefined) {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = conditionObject.columns;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = {\n\t\t\t\t\t\t\t\t\t...combinedAllowedColumns,\n\t\t\t\t\t\t\t\t\t...conditionObject.columns,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tconst accumulatedWhereConditions = allConditionObjects\n\t\t\t\t\t\t.filter((o) => o.where)\n\t\t\t\t\t\t.map((o) => o.where);\n\n\t\t\t\t\tconst combinedWhere =\n\t\t\t\t\t\taccumulatedWhereConditions.length > 0\n\t\t\t\t\t\t\t? or(...accumulatedWhereConditions)\n\t\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\twhere: combinedWhere,\n\t\t\t\t\t\tcolumns: combinedAllowedColumns,\n\t\t\t\t\t\tlimit: highestLimit,\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tfor (const entityKey of Object.keys(db.query) as DBQueryKey[]) {\n\t\t\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t\t\t}\n\n\t\t\treturn builder;\n\t\t},\n\t};\n};\n","export class RumbleError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"RumbleError\";\n\t}\n}\n","import SchemaBuilder from \"@pothos/core\";\nimport DrizzlePlugin from \"@pothos/plugin-drizzle\";\nimport { type YogaServerOptions, createYoga } from \"graphql-yoga\";\nimport { createAbilityBuilder } from \"../abilities/builder\";\nimport { RumbleError } from \"../helpers/rumbleError\";\nimport type { GenericDrizzleDbTypeConstraints } from \"../types/genericDrizzleDbType\";\n\nexport const rumble = <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tRequestEvent extends Record<string, any>,\n\tAction extends string = \"create\" | \"read\" | \"update\" | \"delete\",\n>({\n\tdb,\n\tnativeServerOptions,\n\tcontext: makeUserContext,\n\tonlyQuery = false,\n\tactions = [\"create\", \"read\", \"update\", \"delete\"] as Action[],\n}: {\n\t/**\n\t * Your drizzle database instance\n\t */\n\tdb: DB;\n\t/**\n\t * Optional options for the native GraphQL Yoga server\n\t */\n\tnativeServerOptions?:\n\t\t| Omit<YogaServerOptions<RequestEvent, any>, \"schema\" | \"context\">\n\t\t| undefined;\n\t/**\n\t * A function for providing context for each request based on the incoming HTTP Request.\n\t * The type of the parameter equals the HTTPRequest type of your chosen server.\n\t */\n\tcontext?:\n\t\t| ((event: RequestEvent) => Promise<UserContext> | UserContext)\n\t\t| undefined;\n\t/**\n\t * If you only want to create queries and do not need mutations, enable this\n\t */\n\tonlyQuery?: boolean;\n\t/**\n\t * The actions that are available\n\t */\n\tactions?: Action[];\n}) => {\n\ttype DBQueryKey = keyof DB[\"query\"];\n\tconst abilityBuilder = createAbilityBuilder<UserContext, DB, Action>({\n\t\tdb,\n\t});\n\n\tconst makeContext = async (req: RequestEvent) => {\n\t\tconst userContext = makeUserContext\n\t\t\t? await makeUserContext(req)\n\t\t\t: ({} as UserContext);\n\t\treturn {\n\t\t\t...userContext,\n\t\t\tabilities: abilityBuilder.buildWithUserContext(userContext),\n\t\t};\n\t};\n\n\tconst nativeBuilder = new SchemaBuilder<{\n\t\tContext: Awaited<ReturnType<typeof makeContext>>;\n\t\t// Scalars: Scalars<Prisma.Decimal, Prisma.InputJsonValue | null, Prisma.InputJsonValue> & {\n\t\t// \tFile: {\n\t\t// \t\tInput: File;\n\t\t// \t\tOutput: never;\n\t\t// \t};\n\t\t// \tJSONObject: {\n\t\t// \t\tInput: any;\n\t\t// \t\tOutput: any;\n\t\t// \t};\n\t\t// };\n\t\tDrizzleSchema: DB[\"_\"][\"fullSchema\"];\n\t\tDefaultFieldNullability: false;\n\t\tDefaultArgumentNullability: false;\n\t\tDefaultInputFieldRequiredness: true;\n\t}>({\n\t\tplugins: [DrizzlePlugin],\n\t\tdrizzle: {\n\t\t\tclient: db,\n\t\t},\n\t\tdefaultFieldNullability: false,\n\t\tdefaultInputFieldRequiredness: true,\n\t});\n\n\tnativeBuilder.queryType({});\n\n\tif (!onlyQuery) {\n\t\tnativeBuilder.mutationType({});\n\t}\n\n\tconst implementDefaultObject = <\n\t\tExplicitTableName extends DBQueryKey,\n\t\tRefName extends string,\n\t>({\n\t\ttableName,\n\t\tname,\n\t\treadAction = \"read\" as Action,\n\t}: {\n\t\ttableName: ExplicitTableName;\n\t\tname: RefName;\n\t\treadAction?: Action;\n\t}) => {\n\t\tconst schema = db._.schema![tableName as any];\n\t\treturn nativeBuilder.drizzleObject(tableName as any, {\n\t\t\tname,\n\t\t\tfields: (t) => {\n\t\t\t\tconst mapSQLTypeStringToExposedPothosType = (\n\t\t\t\t\tsqlType: string,\n\t\t\t\t\tcolumnName: string,\n\t\t\t\t) => {\n\t\t\t\t\tswitch (sqlType) {\n\t\t\t\t\t\tcase \"serial\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeInt(columnName);\n\t\t\t\t\t\tcase \"int\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeInt(columnName);\n\t\t\t\t\t\tcase \"string\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeString(columnName);\n\t\t\t\t\t\tcase \"text\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeString(columnName);\n\t\t\t\t\t\tcase \"boolean\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeBoolean(columnName);\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthrow new RumbleError(\n\t\t\t\t\t\t\t\t`Unknown SQL type: ${sqlType}. Please open an issue so it can be added.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tconst fields = Object.entries(schema.columns).reduce(\n\t\t\t\t\t(acc, [key, value]) => {\n\t\t\t\t\t\tacc[key] = mapSQLTypeStringToExposedPothosType(\n\t\t\t\t\t\t\tvalue.getSQLType(),\n\t\t\t\t\t\t\tvalue.name,\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn acc;\n\t\t\t\t\t},\n\t\t\t\t\t{} as Record<\n\t\t\t\t\t\tstring,\n\t\t\t\t\t\tReturnType<typeof mapSQLTypeStringToExposedPothosType>\n\t\t\t\t\t>,\n\t\t\t\t);\n\n\t\t\t\tconst relations = Object.entries(schema.relations).reduce(\n\t\t\t\t\t(acc, [key, value]) => {\n\t\t\t\t\t\tacc[key] = t.relation(key, {\n\t\t\t\t\t\t\tquery: (_args: any, ctx: any) =>\n\t\t\t\t\t\t\t\tctx.abilities[tableName as any].filter(readAction),\n\t\t\t\t\t\t} as any) as any;\n\t\t\t\t\t\treturn acc;\n\t\t\t\t\t},\n\t\t\t\t\t{} as Record<\n\t\t\t\t\t\tstring,\n\t\t\t\t\t\tReturnType<typeof mapSQLTypeStringToExposedPothosType>\n\t\t\t\t\t>,\n\t\t\t\t);\n\n\t\t\t\treturn {\n\t\t\t\t\t...fields,\n\t\t\t\t\t...relations,\n\t\t\t\t} as any;\n\t\t\t},\n\t\t});\n\t};\n\n\treturn {\n\t\t/**\n * The ability builder. Use it to declare whats allowed for each entity in your DB.\n * \n * @example\n * \n * ```ts\n * // users can edit themselves\n abilityBuilder.users\n .allow([\"read\", \"update\", \"delete\"])\n .when(({ userId }) => ({ where: eq(schema.users.id, userId) }));\n \n // everyone can read posts\n abilityBuilder.posts.allow(\"read\");\n * \n * ```\n */\n\t\tabilityBuilder,\n\t\t/**\n\t\t * The pothos schema builder. See https://pothos-graphql.dev/docs/plugins/drizzle\n\t\t */\n\t\tschemaBuilder: nativeBuilder,\n\t\t/**\n * The native yoga instance. Can be used to run an actual HTTP server.\n * \n * @example\n * \n * ```ts\n import { createServer } from \"node:http\";\n * const server = createServer(yoga());\n server.listen(3000, () => {\n console.log(\"Visit http://localhost:3000/graphql\");\n });\n * ```\n */\n\t\tyoga: () =>\n\t\t\tcreateYoga<RequestEvent>({\n\t\t\t\t...nativeServerOptions,\n\t\t\t\tschema: nativeBuilder.toSchema(),\n\t\t\t\tcontext: makeContext,\n\t\t\t}),\n\t\t/**\n\t\t * A function for creating default objects for your schema\n\t\t */\n\t\timplementDefaultObject,\n\t};\n};\n","import { RumbleError } from \"./rumbleError\";\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n * schemaBuilder.queryFields((t) => {\n return {\n findFirstUser: t.drizzleField({\n type: UserRef,\n resolve: (query, root, args, ctx, info) => {\n return (\n db.query.users\n .findFirst({\n ...query,\n where: ctx.abilities.users.filter(\"read\").where,\n })\n // note that we need to manually raise an error if the value is not found\n .then(assertFindFirstExists)\n );\n },\n }),\n };\n });\n * ```\n */\nexport const assertFindFirstExists = <T>(value: T | undefined): T => {\n\tif (!value) throw new RumbleError(\"Value not found but required (findFirst)\");\n\treturn value;\n};\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n schemaBuilder.mutationFields((t) => {\n return {\n updateUsername: t.drizzleField({\n type: UserRef,\n args: {\n userId: t.arg.int({ required: true }),\n newName: t.arg.string({ required: true }),\n },\n resolve: (query, root, args, ctx, info) => {\n return db\n .update(schema.users)\n .set({\n name: args.newName,\n })\n .where(\n and(\n eq(schema.users.id, args.userId),\n ctx.abilities.users.filter(\"update\").where\n )\n )\n .returning({ id: schema.users.id, name: schema.users.name })\n // note that we need to manually raise an error if the value is not found\n .then(assertFirstEntryExists);\n },\n }),\n };\n });\n * ```\n */\nexport const assertFirstEntryExists = <T>(value: T[]): T => {\n\tconst v = value.at(0);\n\tif (!v) throw new RumbleError(\"Value not found but required (firstEntry)\");\n\treturn v;\n};\n"]}
|
package/index.d.cts
CHANGED
@@ -1,8 +1,22 @@
|
|
1
|
+
import { SchemaTypes, ObjectRef, abstractReturnShapeKey } from '@pothos/core';
|
2
|
+
import { WithBrand } from './types.js';
|
1
3
|
import * as graphql_yoga from 'graphql-yoga';
|
2
4
|
import { YogaServerOptions } from 'graphql-yoga';
|
3
5
|
import * as drizzle_orm from 'drizzle-orm';
|
4
6
|
import { DrizzleClient } from '@pothos/plugin-drizzle';
|
5
7
|
|
8
|
+
declare const drizzleTableKey: unique symbol;
|
9
|
+
declare class DrizzleObjectRef<Types extends SchemaTypes, Table extends keyof Types["DrizzleRelationSchema"] = keyof Types["DrizzleRelationSchema"], T = {}> extends ObjectRef<Types, T> {
|
10
|
+
[drizzleTableKey]: Types["DrizzleRelationSchema"][Table];
|
11
|
+
[abstractReturnShapeKey]: WithBrand<T>;
|
12
|
+
tableName: string;
|
13
|
+
constructor(name: string, tableName: string);
|
14
|
+
addBrand<V extends T | T[]>(value: V): V extends T[] ? {
|
15
|
+
[K in keyof V]: WithBrand<V[K]>;
|
16
|
+
} : WithBrand<V>;
|
17
|
+
hasBrand(value: unknown): boolean;
|
18
|
+
}
|
19
|
+
|
6
20
|
type QueryConditionObject = {
|
7
21
|
where: any;
|
8
22
|
columns: any;
|
@@ -16,7 +30,7 @@ type GenericDrizzleDbTypeConstraints = {
|
|
16
30
|
};
|
17
31
|
} & DrizzleClient;
|
18
32
|
|
19
|
-
declare const rumble: <UserContext extends Record<string, any>, DB extends GenericDrizzleDbTypeConstraints, RequestEvent extends Record<string, any
|
33
|
+
declare const rumble: <UserContext extends Record<string, any>, DB extends GenericDrizzleDbTypeConstraints, RequestEvent extends Record<string, any>, Action extends string = "create" | "read" | "update" | "delete">({ db, nativeServerOptions, context: makeUserContext, onlyQuery, actions, }: {
|
20
34
|
/**
|
21
35
|
* Your drizzle database instance
|
22
36
|
*/
|
@@ -30,7 +44,15 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
30
44
|
* The type of the parameter equals the HTTPRequest type of your chosen server.
|
31
45
|
*/
|
32
46
|
context?: ((event: RequestEvent) => Promise<UserContext> | UserContext) | undefined;
|
33
|
-
|
47
|
+
/**
|
48
|
+
* If you only want to create queries and do not need mutations, enable this
|
49
|
+
*/
|
50
|
+
onlyQuery?: boolean;
|
51
|
+
/**
|
52
|
+
* The actions that are available
|
53
|
+
*/
|
54
|
+
actions?: Action[];
|
55
|
+
}) => {
|
34
56
|
/**
|
35
57
|
* The ability builder. Use it to declare whats allowed for each entity in your DB.
|
36
58
|
*
|
@@ -48,15 +70,13 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
48
70
|
* ```
|
49
71
|
*/
|
50
72
|
abilityBuilder: (keyof DB["query"] extends infer T extends keyof DB["query"] ? { [key in T]: {
|
51
|
-
allow: (action:
|
73
|
+
allow: (action: Action | Action[]) => {
|
52
74
|
when: (condition: Parameters<DB["query"][keyof DB["query"]]["findMany"]>[0] | ((context: UserContext) => Parameters<DB["query"][keyof DB["query"]]["findMany"]>[0])) => void;
|
53
75
|
};
|
54
76
|
}; } : never) & {
|
55
|
-
registeredConditions: keyof DB["query"] extends infer T_1 extends keyof DB["query"] ? { [key_1 in T_1]: {
|
56
|
-
|
57
|
-
|
58
|
-
buildWithUserContext: (userContext: UserContext) => keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_2 in T_2]: {
|
59
|
-
filter: (action: "create" | "read" | "update" | "delete") => {
|
77
|
+
registeredConditions: keyof DB["query"] extends infer T_1 extends keyof DB["query"] ? { [key_1 in T_1]: { [key_2 in Action[number]]: (QueryConditionObject | ((context: UserContext) => QueryConditionObject))[]; }; } : never;
|
78
|
+
buildWithUserContext: (userContext: UserContext) => keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_3 in T_2]: {
|
79
|
+
filter: (action: Action) => {
|
60
80
|
where: drizzle_orm.SQL<unknown> | undefined;
|
61
81
|
columns: Record<string, any> | undefined;
|
62
82
|
limit: any;
|
@@ -68,8 +88,8 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
68
88
|
*/
|
69
89
|
schemaBuilder: PothosSchemaTypes.SchemaBuilder<PothosSchemaTypes.ExtendDefaultTypes<{
|
70
90
|
Context: Awaited<ReturnType<(req: RequestEvent) => Promise<UserContext & {
|
71
|
-
abilities: keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [
|
72
|
-
filter: (action:
|
91
|
+
abilities: keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_3 in T_2]: {
|
92
|
+
filter: (action: Action) => {
|
73
93
|
where: drizzle_orm.SQL<unknown> | undefined;
|
74
94
|
columns: Record<string, any> | undefined;
|
75
95
|
limit: any;
|
@@ -95,7 +115,29 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
95
115
|
* ```
|
96
116
|
*/
|
97
117
|
yoga: () => graphql_yoga.YogaServerInstance<RequestEvent, {}>;
|
98
|
-
|
118
|
+
/**
|
119
|
+
* A function for creating default objects for your schema
|
120
|
+
*/
|
121
|
+
implementDefaultObject: <ExplicitTableName extends keyof DB["query"], RefName extends string>({ tableName, name, readAction, }: {
|
122
|
+
tableName: ExplicitTableName;
|
123
|
+
name: RefName;
|
124
|
+
readAction?: Action;
|
125
|
+
}) => DrizzleObjectRef<PothosSchemaTypes.ExtendDefaultTypes<{
|
126
|
+
Context: Awaited<ReturnType<(req: RequestEvent) => Promise<UserContext & {
|
127
|
+
abilities: keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_3 in T_2]: {
|
128
|
+
filter: (action: Action) => {
|
129
|
+
where: drizzle_orm.SQL<unknown> | undefined;
|
130
|
+
columns: Record<string, any> | undefined;
|
131
|
+
limit: any;
|
132
|
+
};
|
133
|
+
}; } : never;
|
134
|
+
}>>>;
|
135
|
+
DrizzleSchema: DB["_"]["fullSchema"];
|
136
|
+
DefaultFieldNullability: false;
|
137
|
+
DefaultArgumentNullability: false;
|
138
|
+
DefaultInputFieldRequiredness: true;
|
139
|
+
}>, any, ({ [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; } | undefined extends Record<string, unknown> ? { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; } extends infer T_5 ? { [K_2 in keyof T_5]: { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; }[K_2]; } : never : { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; } extends infer T_6 ? { [K_5 in keyof T_6]: { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; }[K_5]; } : never) & {} & ({ [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; } | undefined extends Record<string, unknown> ? drizzle_orm.BuildRelationResult<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, Record<string, unknown> & { [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; }, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]> : {}) extends infer T_2 ? { [K in keyof T_2]: (({ [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; } | undefined extends Record<string, unknown> ? { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; } extends infer T_3 ? { [K_2 in keyof T_3]: { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; }[K_2]; } : never : { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; } extends infer T_4 ? { [K_5 in keyof T_4]: { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; }[K_5]; } : never) & {} & ({ [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; } | undefined extends Record<string, unknown> ? drizzle_orm.BuildRelationResult<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, Record<string, unknown> & { [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; }, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]> : {}))[K]; } : never>;
|
140
|
+
};
|
99
141
|
|
100
142
|
/**
|
101
143
|
*
|
package/index.d.ts
CHANGED
@@ -1,8 +1,22 @@
|
|
1
|
+
import { SchemaTypes, ObjectRef, abstractReturnShapeKey } from '@pothos/core';
|
2
|
+
import { WithBrand } from './types.js';
|
1
3
|
import * as graphql_yoga from 'graphql-yoga';
|
2
4
|
import { YogaServerOptions } from 'graphql-yoga';
|
3
5
|
import * as drizzle_orm from 'drizzle-orm';
|
4
6
|
import { DrizzleClient } from '@pothos/plugin-drizzle';
|
5
7
|
|
8
|
+
declare const drizzleTableKey: unique symbol;
|
9
|
+
declare class DrizzleObjectRef<Types extends SchemaTypes, Table extends keyof Types["DrizzleRelationSchema"] = keyof Types["DrizzleRelationSchema"], T = {}> extends ObjectRef<Types, T> {
|
10
|
+
[drizzleTableKey]: Types["DrizzleRelationSchema"][Table];
|
11
|
+
[abstractReturnShapeKey]: WithBrand<T>;
|
12
|
+
tableName: string;
|
13
|
+
constructor(name: string, tableName: string);
|
14
|
+
addBrand<V extends T | T[]>(value: V): V extends T[] ? {
|
15
|
+
[K in keyof V]: WithBrand<V[K]>;
|
16
|
+
} : WithBrand<V>;
|
17
|
+
hasBrand(value: unknown): boolean;
|
18
|
+
}
|
19
|
+
|
6
20
|
type QueryConditionObject = {
|
7
21
|
where: any;
|
8
22
|
columns: any;
|
@@ -16,7 +30,7 @@ type GenericDrizzleDbTypeConstraints = {
|
|
16
30
|
};
|
17
31
|
} & DrizzleClient;
|
18
32
|
|
19
|
-
declare const rumble: <UserContext extends Record<string, any>, DB extends GenericDrizzleDbTypeConstraints, RequestEvent extends Record<string, any
|
33
|
+
declare const rumble: <UserContext extends Record<string, any>, DB extends GenericDrizzleDbTypeConstraints, RequestEvent extends Record<string, any>, Action extends string = "create" | "read" | "update" | "delete">({ db, nativeServerOptions, context: makeUserContext, onlyQuery, actions, }: {
|
20
34
|
/**
|
21
35
|
* Your drizzle database instance
|
22
36
|
*/
|
@@ -30,7 +44,15 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
30
44
|
* The type of the parameter equals the HTTPRequest type of your chosen server.
|
31
45
|
*/
|
32
46
|
context?: ((event: RequestEvent) => Promise<UserContext> | UserContext) | undefined;
|
33
|
-
|
47
|
+
/**
|
48
|
+
* If you only want to create queries and do not need mutations, enable this
|
49
|
+
*/
|
50
|
+
onlyQuery?: boolean;
|
51
|
+
/**
|
52
|
+
* The actions that are available
|
53
|
+
*/
|
54
|
+
actions?: Action[];
|
55
|
+
}) => {
|
34
56
|
/**
|
35
57
|
* The ability builder. Use it to declare whats allowed for each entity in your DB.
|
36
58
|
*
|
@@ -48,15 +70,13 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
48
70
|
* ```
|
49
71
|
*/
|
50
72
|
abilityBuilder: (keyof DB["query"] extends infer T extends keyof DB["query"] ? { [key in T]: {
|
51
|
-
allow: (action:
|
73
|
+
allow: (action: Action | Action[]) => {
|
52
74
|
when: (condition: Parameters<DB["query"][keyof DB["query"]]["findMany"]>[0] | ((context: UserContext) => Parameters<DB["query"][keyof DB["query"]]["findMany"]>[0])) => void;
|
53
75
|
};
|
54
76
|
}; } : never) & {
|
55
|
-
registeredConditions: keyof DB["query"] extends infer T_1 extends keyof DB["query"] ? { [key_1 in T_1]: {
|
56
|
-
|
57
|
-
|
58
|
-
buildWithUserContext: (userContext: UserContext) => keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_2 in T_2]: {
|
59
|
-
filter: (action: "create" | "read" | "update" | "delete") => {
|
77
|
+
registeredConditions: keyof DB["query"] extends infer T_1 extends keyof DB["query"] ? { [key_1 in T_1]: { [key_2 in Action[number]]: (QueryConditionObject | ((context: UserContext) => QueryConditionObject))[]; }; } : never;
|
78
|
+
buildWithUserContext: (userContext: UserContext) => keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_3 in T_2]: {
|
79
|
+
filter: (action: Action) => {
|
60
80
|
where: drizzle_orm.SQL<unknown> | undefined;
|
61
81
|
columns: Record<string, any> | undefined;
|
62
82
|
limit: any;
|
@@ -68,8 +88,8 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
68
88
|
*/
|
69
89
|
schemaBuilder: PothosSchemaTypes.SchemaBuilder<PothosSchemaTypes.ExtendDefaultTypes<{
|
70
90
|
Context: Awaited<ReturnType<(req: RequestEvent) => Promise<UserContext & {
|
71
|
-
abilities: keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [
|
72
|
-
filter: (action:
|
91
|
+
abilities: keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_3 in T_2]: {
|
92
|
+
filter: (action: Action) => {
|
73
93
|
where: drizzle_orm.SQL<unknown> | undefined;
|
74
94
|
columns: Record<string, any> | undefined;
|
75
95
|
limit: any;
|
@@ -95,7 +115,29 @@ declare const rumble: <UserContext extends Record<string, any>, DB extends Gener
|
|
95
115
|
* ```
|
96
116
|
*/
|
97
117
|
yoga: () => graphql_yoga.YogaServerInstance<RequestEvent, {}>;
|
98
|
-
|
118
|
+
/**
|
119
|
+
* A function for creating default objects for your schema
|
120
|
+
*/
|
121
|
+
implementDefaultObject: <ExplicitTableName extends keyof DB["query"], RefName extends string>({ tableName, name, readAction, }: {
|
122
|
+
tableName: ExplicitTableName;
|
123
|
+
name: RefName;
|
124
|
+
readAction?: Action;
|
125
|
+
}) => DrizzleObjectRef<PothosSchemaTypes.ExtendDefaultTypes<{
|
126
|
+
Context: Awaited<ReturnType<(req: RequestEvent) => Promise<UserContext & {
|
127
|
+
abilities: keyof DB["query"] extends infer T_2 extends keyof DB["query"] ? { [key_3 in T_2]: {
|
128
|
+
filter: (action: Action) => {
|
129
|
+
where: drizzle_orm.SQL<unknown> | undefined;
|
130
|
+
columns: Record<string, any> | undefined;
|
131
|
+
limit: any;
|
132
|
+
};
|
133
|
+
}; } : never;
|
134
|
+
}>>>;
|
135
|
+
DrizzleSchema: DB["_"]["fullSchema"];
|
136
|
+
DefaultFieldNullability: false;
|
137
|
+
DefaultArgumentNullability: false;
|
138
|
+
DefaultInputFieldRequiredness: true;
|
139
|
+
}>, any, ({ [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; } | undefined extends Record<string, unknown> ? { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; } extends infer T_5 ? { [K_2 in keyof T_5]: { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; }[K_2]; } : never : { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; } extends infer T_6 ? { [K_5 in keyof T_6]: { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; }[K_5]; } : never) & {} & ({ [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; } | undefined extends Record<string, unknown> ? drizzle_orm.BuildRelationResult<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, Record<string, unknown> & { [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; }, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]> : {}) extends infer T_2 ? { [K in keyof T_2]: (({ [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; } | undefined extends Record<string, unknown> ? { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; } extends infer T_3 ? { [K_2 in keyof T_3]: { [Key in (drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]) & string as Key]: { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["notNull"] extends true ? { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] : { [K_4 in drizzle_orm.Equal<Exclude<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] | (string & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"])], undefined>, false> extends true ? Exclude<keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"], drizzle_orm.NonUndefinedKeysOnly<Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; }>> : { [K_3 in keyof (Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })]: drizzle_orm.Equal<(Record<string, unknown> & { [K_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]?: boolean | undefined; })[K_3], true> extends true ? K_3 : never; }[string | keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]] & keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"]]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][K_4]; }[Key]["_"]["data"] | null; }[K_2]; } : never : { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; } extends infer T_4 ? { [K_5 in keyof T_4]: { [Key_1 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"] & string as Key_1]: drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["notNull"] extends true ? drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] : drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["columns"][Key_1]["_"]["data"] | null; }[K_5]; } : never) & {} & ({ [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; } | undefined extends Record<string, unknown> ? drizzle_orm.BuildRelationResult<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, Record<string, unknown> & { [K_6 in keyof drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]]?: true | drizzle_orm.DBQueryConfig<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6] extends drizzle_orm.One<string, boolean> ? "one" : "many", false, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.FindTableByDBName<drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"][K_6]["referencedTableName"]>> | undefined; }, drizzle_orm.ExtractTablesWithRelations<DB["_"]["fullSchema"] & {}>[any]["relations"]> : {}))[K]; } : never>;
|
140
|
+
};
|
99
141
|
|
100
142
|
/**
|
101
143
|
*
|
package/index.js
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
import P from'@pothos/core';import O from'@pothos/plugin-drizzle';import {createYoga}from'graphql-yoga';import {or}from'drizzle-orm';function
|
1
|
+
import P from'@pothos/core';import O from'@pothos/plugin-drizzle';import {createYoga}from'graphql-yoga';import {or}from'drizzle-orm';function g(t){return typeof t!="function"}function A(t){return typeof t=="function"&&t.constructor.name!=="AsyncFunction"}var h=({db:t})=>{let s={},l={},b=u=>({allow:a=>{let i=l[u];i||(i={},l[u]=i);let r=Array.isArray(a)?a:[a];for(let p of r){let n=i[p];n||(n=[],i[p]=n);}return {when:p=>{for(let n of r)i[n].push(p);}}}});for(let u of Object.keys(t.query))s[u]=b(u);return {...s,registeredConditions:l,buildWithUserContext:u=>{let a={},i=r=>({filter:p=>{let n=l[r];if(!n)throw "TODO (No allowed entry found for this condition) #1";let c=n[p];if(!c)throw "TODO (No allowed entry found for this condition) #2";let C=c.filter(g),x=c.filter(A).map(e=>e(u)),o=[...C,...x],m;for(let e of o)e.limit&&(m===void 0||e.limit>m)&&(m=e.limit);let f;for(let e of o)e.columns&&(f===void 0?f=e.columns:f={...f,...e.columns});let D=o.filter(e=>e.where).map(e=>e.where);return {where:D.length>0?or(...D):void 0,columns:f,limit:m}}});for(let r of Object.keys(t.query))a[r]=i(r);return a}}};var d=class extends Error{constructor(s){super(s),this.name="RumbleError";}};var R=({db:t,nativeServerOptions:s,context:l,onlyQuery:b=!1,actions:u=["create","read","update","delete"]})=>{let a=h({db:t}),i=async n=>{let c=l?await l(n):{};return {...c,abilities:a.buildWithUserContext(c)}},r=new P({plugins:[O],drizzle:{client:t},defaultFieldNullability:!1,defaultInputFieldRequiredness:!0});return r.queryType({}),b||r.mutationType({}),{abilityBuilder:a,schemaBuilder:r,yoga:()=>createYoga({...s,schema:r.toSchema(),context:i}),implementDefaultObject:({tableName:n,name:c,readAction:C="read"})=>{let x=t._.schema[n];return r.drizzleObject(n,{name:c,fields:o=>{let m=(y,e)=>{switch(y){case"serial":return o.exposeInt(e);case"int":return o.exposeInt(e);case"string":return o.exposeString(e);case"text":return o.exposeString(e);case"boolean":return o.exposeBoolean(e);default:throw new d(`Unknown SQL type: ${y}. Please open an issue so it can be added.`)}},f=Object.entries(x.columns).reduce((y,[e,B])=>(y[e]=m(B.getSQLType(),B.name),y),{}),D=Object.entries(x.relations).reduce((y,[e,B])=>(y[e]=o.relation(e,{query:(U,T)=>T.abilities[n].filter(C)}),y),{});return {...f,...D}}})}}};var w=t=>{if(!t)throw new d("Value not found but required (findFirst)");return t},Q=t=>{let s=t.at(0);if(!s)throw new d("Value not found but required (firstEntry)");return s};export{d as RumbleError,w as assertFindFirstExists,Q as assertFirstEntryExists,R as rumble};//# sourceMappingURL=index.js.map
|
2
2
|
//# sourceMappingURL=index.js.map
|
package/index.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../lib/abilities/builder.ts","../lib/gql/builder.ts","../lib/helpers/rumbleError.ts","../lib/helpers/helper.ts"],"names":["isSimpleCondition","condition","isSyncFunctionCondition","createAbilityBuilder","db","actions","builder","registeredConditions","createEntityObject","entityKey","action","conditionsPerEntity","conditionsPerEntityAndAction","userContext","simpleConditions","syncFunctionConditions","allConditionObjects","highestLimit","conditionObject","combinedAllowedColumns","accumulatedWhereConditions","o","or","rumble","nativeServerOptions","makeUserContext","abilityBuilder","makeContext","req","nativeBuilder","SchemaBuilder","DrizzlePlugin","createYoga","RumbleError","message","assertFindFirstExists","value","assertFirstEntryExists","v"],"mappings":"qIAsBA,SAASA,EACRC,CAC6C,CAAA,CAC7C,OAAO,OAAOA,CAAAA,EAAc,UAC7B,CAEA,SAASC,CAAAA,CACRD,EACgE,CAChE,OACC,OAAOA,CAAc,EAAA,UAAA,EACrBA,EAAU,WAAY,CAAA,IAAA,GAAS,eAEjC,CAWO,IAAME,CAAAA,CAAuB,CAIlC,CACD,EAAA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D,IAAME,CAAIF,CAAAA,CAAAA,CAAM,GAAG,CAAC,CAAA,CACpB,GAAI,CAACE,CAAG,CAAA,MAAM,IAAIL,CAAY,CAAA,2CAA2C,CACzE,CAAA,OAAOK,CACR","file":"index.js","sourcesContent":["import { or } from \"drizzle-orm\";\nimport type {\n\tGenericDrizzleDbTypeConstraints,\n\tQueryConditionObject,\n} from \"../types/genericDrizzleDbType\";\n\nexport type AbilityBuilder = ReturnType<typeof createAbilityBuilder>;\n\ntype Condition<DBParameters, UserContext> =\n\t| SimpleCondition<DBParameters>\n\t| SyncFunctionCondition<DBParameters, UserContext>;\n// | AsyncFunctionCondition<DBParameters, UserContext>;\n\ntype SimpleCondition<DBParameters> = DBParameters;\ntype SyncFunctionCondition<DBParameters, UserContext> = (\n\tcontext: UserContext,\n) => DBParameters;\n// type AsyncFunctionCondition<DBParameters, UserContext> = (\n// \tcontext: UserContext,\n// ) => Promise<DBParameters>;\n\n// type guards for the condition types\nfunction isSimpleCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SimpleCondition<DBParameters> {\n\treturn typeof condition !== \"function\";\n}\n\nfunction isSyncFunctionCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SyncFunctionCondition<DBParameters, UserContext> {\n\treturn (\n\t\ttypeof condition === \"function\" &&\n\t\tcondition.constructor.name !== \"AsyncFunction\"\n\t);\n}\n\n// function isAsyncFunctionCondition<DBParameters, UserContext>(\n// \tcondition: Condition<DBParameters, UserContext>,\n// ): condition is AsyncFunctionCondition<DBParameters, UserContext> {\n// \treturn (\n// \t\ttypeof condition === \"function\" &&\n// \t\tcondition.constructor.name === \"AsyncFunction\"\n// \t);\n// }\n\nexport const createAbilityBuilder = <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tAction extends string = \"create\" | \"read\" | \"update\" | \"delete\",\n>({\n\tdb,\n\tactions = [\"create\", \"read\", \"update\", \"delete\"] as Action[],\n}: {\n\tdb: DB;\n\tactions?: Action[];\n}) => {\n\ttype DBEntityKey = keyof DB[\"query\"];\n\n\tconst builder: {\n\t\t[key in DBEntityKey]: ReturnType<typeof createEntityObject>;\n\t} = {} as any;\n\n\tconst registeredConditions: {\n\t\t[key in DBEntityKey]: {\n\t\t\t[key in Action[number]]: (\n\t\t\t\t| QueryConditionObject\n\t\t\t\t| ((context: UserContext) => QueryConditionObject)\n\t\t\t)[];\n\t\t\t// | ((context: UserContext) => Promise<QueryConditionObject>)\n\t\t};\n\t} = {} as any;\n\n\tconst createEntityObject = (entityKey: DBEntityKey) => ({\n\t\tallow: (action: Action | Action[]) => {\n\t\t\ttype DBParameters = Parameters<DB[\"query\"][DBEntityKey][\"findMany\"]>[0];\n\n\t\t\tlet conditionsPerEntity = registeredConditions[entityKey];\n\t\t\tif (!conditionsPerEntity) {\n\t\t\t\tconditionsPerEntity = {} as any;\n\t\t\t\tregisteredConditions[entityKey] = conditionsPerEntity;\n\t\t\t}\n\n\t\t\tconst actions = Array.isArray(action) ? action : [action];\n\t\t\tfor (const action of actions) {\n\t\t\t\tlet conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\tconditionsPerEntityAndAction = [];\n\t\t\t\t\tconditionsPerEntity[action] = conditionsPerEntityAndAction;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twhen: (condition: Condition<DBParameters, UserContext>) => {\n\t\t\t\t\tfor (const action of actions) {\n\t\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\t\tconditionsPerEntityAndAction.push(condition);\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\n\tfor (const entityKey of Object.keys(db.query) as DBEntityKey[]) {\n\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t}\n\treturn {\n\t\t...builder,\n\t\tregisteredConditions,\n\t\tbuildWithUserContext: (userContext: UserContext) => {\n\t\t\tconst builder: {\n\t\t\t\t[key in DBEntityKey]: ReturnType<typeof createEntityObject>;\n\t\t\t} = {} as any;\n\n\t\t\tconst createEntityObject = (entityKey: DBEntityKey) => ({\n\t\t\t\tfilter: (action: Action) => {\n\t\t\t\t\tconst conditionsPerEntity = registeredConditions[entityKey];\n\t\t\t\t\tif (!conditionsPerEntity) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #1\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #2\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst simpleConditions =\n\t\t\t\t\t\tconditionsPerEntityAndAction.filter(isSimpleCondition);\n\n\t\t\t\t\tconst syncFunctionConditions = conditionsPerEntityAndAction\n\t\t\t\t\t\t.filter(isSyncFunctionCondition)\n\t\t\t\t\t\t.map((condition) => condition(userContext));\n\n\t\t\t\t\t// const asyncFunctionConditions = await Promise.all(\n\t\t\t\t\t// \tconditionsPerEntityAndAction\n\t\t\t\t\t// \t\t.filter(isAsyncFunctionCondition)\n\t\t\t\t\t// \t\t.map((condition) => condition(userContext)),\n\t\t\t\t\t// );\n\n\t\t\t\t\tconst allConditionObjects = [\n\t\t\t\t\t\t...simpleConditions,\n\t\t\t\t\t\t...syncFunctionConditions,\n\t\t\t\t\t\t// ...asyncFunctionConditions,\n\t\t\t\t\t];\n\n\t\t\t\t\tlet highestLimit = undefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.limit) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\thighestLimit === undefined ||\n\t\t\t\t\t\t\t\tconditionObject.limit > highestLimit\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\thighestLimit = conditionObject.limit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet combinedAllowedColumns: Record<string, any> | undefined =\n\t\t\t\t\t\tundefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.columns) {\n\t\t\t\t\t\t\tif (combinedAllowedColumns === undefined) {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = conditionObject.columns;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = {\n\t\t\t\t\t\t\t\t\t...combinedAllowedColumns,\n\t\t\t\t\t\t\t\t\t...conditionObject.columns,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tconst accumulatedWhereConditions = allConditionObjects\n\t\t\t\t\t\t.filter((o) => o.where)\n\t\t\t\t\t\t.map((o) => o.where);\n\n\t\t\t\t\tconst combinedWhere =\n\t\t\t\t\t\taccumulatedWhereConditions.length > 0\n\t\t\t\t\t\t\t? or(...accumulatedWhereConditions)\n\t\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\twhere: combinedWhere,\n\t\t\t\t\t\tcolumns: combinedAllowedColumns,\n\t\t\t\t\t\tlimit: highestLimit,\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tfor (const entityKey of Object.keys(db.query) as DBEntityKey[]) {\n\t\t\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t\t\t}\n\n\t\t\treturn builder;\n\t\t},\n\t};\n};\n","import SchemaBuilder from \"@pothos/core\";\nimport DrizzlePlugin from \"@pothos/plugin-drizzle\";\nimport { type YogaServerOptions, createYoga } from \"graphql-yoga\";\nimport { createAbilityBuilder } from \"../abilities/builder\";\nimport type { GenericDrizzleDbTypeConstraints } from \"../types/genericDrizzleDbType\";\n\nexport const rumble = async <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tRequestEvent extends Record<string, any>,\n>({\n\tdb,\n\tnativeServerOptions,\n\tcontext: makeUserContext,\n}: {\n\t/**\n\t * Your drizzle database instance\n\t */\n\tdb: DB;\n\t/**\n\t * Optional options for the native GraphQL Yoga server\n\t */\n\tnativeServerOptions?:\n\t\t| Omit<YogaServerOptions<RequestEvent, any>, \"schema\" | \"context\">\n\t\t| undefined;\n\t/**\n\t * A function for providing context for each request based on the incoming HTTP Request.\n\t * The type of the parameter equals the HTTPRequest type of your chosen server.\n\t */\n\tcontext?:\n\t\t| ((event: RequestEvent) => Promise<UserContext> | UserContext)\n\t\t| undefined;\n}) => {\n\tconst abilityBuilder = createAbilityBuilder<UserContext, DB>({\n\t\tdb,\n\t});\n\n\tconst makeContext = async (req: RequestEvent) => {\n\t\tconst userContext = makeUserContext\n\t\t\t? await makeUserContext(req)\n\t\t\t: ({} as UserContext);\n\t\treturn {\n\t\t\t...userContext,\n\t\t\tabilities: abilityBuilder.buildWithUserContext(userContext),\n\t\t};\n\t};\n\n\tconst nativeBuilder = new SchemaBuilder<{\n\t\tContext: Awaited<ReturnType<typeof makeContext>>;\n\t\t// Scalars: Scalars<Prisma.Decimal, Prisma.InputJsonValue | null, Prisma.InputJsonValue> & {\n\t\t// \tFile: {\n\t\t// \t\tInput: File;\n\t\t// \t\tOutput: never;\n\t\t// \t};\n\t\t// \tJSONObject: {\n\t\t// \t\tInput: any;\n\t\t// \t\tOutput: any;\n\t\t// \t};\n\t\t// };\n\t\tDrizzleSchema: DB[\"_\"][\"fullSchema\"];\n\t\tDefaultFieldNullability: false;\n\t\tDefaultArgumentNullability: false;\n\t\tDefaultInputFieldRequiredness: true;\n\t}>({\n\t\tplugins: [DrizzlePlugin],\n\t\tdrizzle: {\n\t\t\tclient: db,\n\t\t},\n\t\tdefaultFieldNullability: false,\n\t\tdefaultInputFieldRequiredness: true,\n\t});\n\n\tnativeBuilder.queryType({});\n\tnativeBuilder.mutationType({});\n\n\treturn {\n\t\t/**\n * The ability builder. Use it to declare whats allowed for each entity in your DB.\n * \n * @example\n * \n * ```ts\n * // users can edit themselves\n abilityBuilder.users\n .allow([\"read\", \"update\", \"delete\"])\n .when(({ userId }) => ({ where: eq(schema.users.id, userId) }));\n \n // everyone can read posts\n abilityBuilder.posts.allow(\"read\");\n * \n * ```\n */\n\t\tabilityBuilder,\n\t\t/**\n\t\t * The pothos schema builder. See https://pothos-graphql.dev/docs/plugins/drizzle\n\t\t */\n\t\tschemaBuilder: nativeBuilder,\n\t\t/**\n * The native yoga instance. Can be used to run an actual HTTP server.\n * \n * @example\n * \n * ```ts\n import { createServer } from \"node:http\";\n * const server = createServer(yoga());\n server.listen(3000, () => {\n console.log(\"Visit http://localhost:3000/graphql\");\n });\n * ```\n */\n\t\tyoga: () =>\n\t\t\tcreateYoga<RequestEvent>({\n\t\t\t\t...nativeServerOptions,\n\t\t\t\tschema: nativeBuilder.toSchema(),\n\t\t\t\tcontext: makeContext,\n\t\t\t}),\n\t};\n};\n","export class RumbleError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"RumbleError\";\n\t}\n}\n","import { RumbleError } from \"./rumbleError\";\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n * schemaBuilder.queryFields((t) => {\n return {\n findFirstUser: t.drizzleField({\n type: UserRef,\n resolve: (query, root, args, ctx, info) => {\n return (\n db.query.users\n .findFirst({\n ...query,\n where: ctx.abilities.users.filter(\"read\").where,\n })\n // note that we need to manually raise an error if the value is not found\n .then(assertFindFirstExists)\n );\n },\n }),\n };\n });\n * ```\n */\nexport const assertFindFirstExists = <T>(value: T | undefined): T => {\n\tif (!value) throw new RumbleError(\"Value not found but required (findFirst)\");\n\treturn value;\n};\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n schemaBuilder.mutationFields((t) => {\n return {\n updateUsername: t.drizzleField({\n type: UserRef,\n args: {\n userId: t.arg.int({ required: true }),\n newName: t.arg.string({ required: true }),\n },\n resolve: (query, root, args, ctx, info) => {\n return db\n .update(schema.users)\n .set({\n name: args.newName,\n })\n .where(\n and(\n eq(schema.users.id, args.userId),\n ctx.abilities.users.filter(\"update\").where\n )\n )\n .returning({ id: schema.users.id, name: schema.users.name })\n // note that we need to manually raise an error if the value is not found\n .then(assertFirstEntryExists);\n },\n }),\n };\n });\n * ```\n */\nexport const assertFirstEntryExists = <T>(value: T[]): T => {\n\tconst v = value.at(0);\n\tif (!v) throw new RumbleError(\"Value not found but required (firstEntry)\");\n\treturn v;\n};\n"]}
|
1
|
+
{"version":3,"sources":["../lib/abilities/builder.ts","../lib/helpers/rumbleError.ts","../lib/gql/builder.ts","../lib/helpers/helper.ts"],"names":["isSimpleCondition","condition","isSyncFunctionCondition","createAbilityBuilder","db","builder","registeredConditions","createEntityObject","entityKey","action","conditionsPerEntity","actions","conditionsPerEntityAndAction","userContext","simpleConditions","syncFunctionConditions","allConditionObjects","highestLimit","conditionObject","combinedAllowedColumns","accumulatedWhereConditions","o","or","RumbleError","message","rumble","nativeServerOptions","makeUserContext","onlyQuery","abilityBuilder","makeContext","req","nativeBuilder","SchemaBuilder","DrizzlePlugin","createYoga","tableName","name","readAction","schema","t","mapSQLTypeStringToExposedPothosType","sqlType","columnName","fields","acc","key","value","relations","_args","ctx","assertFindFirstExists","assertFirstEntryExists","v"],"mappings":"qIAwBA,SAASA,CACRC,CAAAA,CAAAA,CAC6C,CAC7C,OAAO,OAAOA,CAAAA,EAAc,UAC7B,CAEA,SAASC,CACRD,CAAAA,CAAAA,CACgE,CAChE,OACC,OAAOA,CAAc,EAAA,UAAA,EACrBA,CAAU,CAAA,WAAA,CAAY,OAAS,eAEjC,CAWO,IAAME,CAAAA,CAAuB,CAIlC,CACD,EAAAC,CAAAA,CACD,IAEM,CAIL,IAAMC,CAEF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KAAO,CAAA,CAACG,CAAYC,CAAAA,CAAAA,GACnBA,CAAI,CAAA,SAAA,CAAUd,CAAgB,CAAE,CAAA,MAAA,CAAOE,CAAU,CACnD,CAAQ,CAAA,CACDO,CAER,CAAA,CAAA,EAID,CAEA,CAAA,OAAO,CACN,GAAGD,EACH,GAAGI,CACJ,CACD,CACD,CAAC,CACF,CA+CA,CACD,ECxLaG,IAAAA,CAAAA,CAA4BJ,CAA4B,EAAA,CACpE,GAAI,CAACA,CAAAA,CAAO,MAAM,IAAIxB,CAAY,CAAA,0CAA0C,CAC5E,CAAA,OAAOwB,CACR,CAyCaK,CAAAA,CAAAA,CAA6BL,CAAkB,EAAA,CAC3D,IAAMM,CAAAA,CAAIN,CAAM,CAAA,EAAA,CAAG,CAAC,CACpB,CAAA,GAAI,CAACM,CAAAA,CAAG,MAAM,IAAI9B,CAAAA,CAAY,2CAA2C,CAAA,CACzE,OAAO8B,CACR","file":"index.js","sourcesContent":["import { SQL, or } from \"drizzle-orm\";\nimport type { ExcludeFromUnionIfNotOnlyType } from \"../helpers/unionTypeExclude\";\nimport type { ExcludeFirst } from \"../helpers/unionTypeExtractor\";\nimport type {\n\tGenericDrizzleDbTypeConstraints,\n\tQueryConditionObject,\n} from \"../types/genericDrizzleDbType\";\n\nexport type AbilityBuilder = ReturnType<typeof createAbilityBuilder>;\n\ntype Condition<DBParameters, UserContext> =\n\t| SimpleCondition<DBParameters>\n\t| SyncFunctionCondition<DBParameters, UserContext>;\n// | AsyncFunctionCondition<DBParameters, UserContext>;\n\ntype SimpleCondition<DBParameters> = DBParameters;\ntype SyncFunctionCondition<DBParameters, UserContext> = (\n\tcontext: UserContext,\n) => DBParameters;\n// type AsyncFunctionCondition<DBParameters, UserContext> = (\n// \tcontext: UserContext,\n// ) => Promise<DBParameters>;\n\n// type guards for the condition types\nfunction isSimpleCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SimpleCondition<DBParameters> {\n\treturn typeof condition !== \"function\";\n}\n\nfunction isSyncFunctionCondition<DBParameters, UserContext>(\n\tcondition: Condition<DBParameters, UserContext>,\n): condition is SyncFunctionCondition<DBParameters, UserContext> {\n\treturn (\n\t\ttypeof condition === \"function\" &&\n\t\tcondition.constructor.name !== \"AsyncFunction\"\n\t);\n}\n\n// function isAsyncFunctionCondition<DBParameters, UserContext>(\n// \tcondition: Condition<DBParameters, UserContext>,\n// ): condition is AsyncFunctionCondition<DBParameters, UserContext> {\n// \treturn (\n// \t\ttypeof condition === \"function\" &&\n// \t\tcondition.constructor.name === \"AsyncFunction\"\n// \t);\n// }\n\nexport const createAbilityBuilder = <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tAction extends string,\n>({\n\tdb,\n}: {\n\tdb: DB;\n}) => {\n\ttype DBQueryKey = keyof DB[\"query\"];\n\ttype DBParameters = Parameters<DB[\"query\"][DBQueryKey][\"findMany\"]>[0];\n\n\tconst builder: {\n\t\t[key in DBQueryKey]: ReturnType<typeof createEntityObject>;\n\t} = {} as any;\n\n\tconst registeredConditions: {\n\t\t[key in DBQueryKey]: {\n\t\t\t[key in Action[number]]: (\n\t\t\t\t| QueryConditionObject\n\t\t\t\t| ((context: UserContext) => QueryConditionObject)\n\t\t\t)[];\n\t\t\t// | ((context: UserContext) => Promise<QueryConditionObject>)\n\t\t};\n\t} = {} as any;\n\n\tconst createEntityObject = (entityKey: DBQueryKey) => ({\n\t\tallow: (action: Action | Action[]) => {\n\t\t\tlet conditionsPerEntity = registeredConditions[entityKey];\n\t\t\tif (!conditionsPerEntity) {\n\t\t\t\tconditionsPerEntity = {} as any;\n\t\t\t\tregisteredConditions[entityKey] = conditionsPerEntity;\n\t\t\t}\n\n\t\t\tconst actions = Array.isArray(action) ? action : [action];\n\t\t\tfor (const action of actions) {\n\t\t\t\tlet conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\tconditionsPerEntityAndAction = [];\n\t\t\t\t\tconditionsPerEntity[action] = conditionsPerEntityAndAction;\n\t\t\t\t}\n\t\t\t}\n\n\t\t\treturn {\n\t\t\t\twhen: (condition: Condition<DBParameters, UserContext>) => {\n\t\t\t\t\tfor (const action of actions) {\n\t\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\t\tconditionsPerEntityAndAction.push(condition);\n\t\t\t\t\t}\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\n\tfor (const entityKey of Object.keys(db.query) as DBQueryKey[]) {\n\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t}\n\treturn {\n\t\t...builder,\n\t\tregisteredConditions,\n\t\tbuildWithUserContext: (userContext: UserContext) => {\n\t\t\tconst builder: {\n\t\t\t\t[key in DBQueryKey]: ReturnType<typeof createEntityObject>;\n\t\t\t} = {} as any;\n\n\t\t\tconst createEntityObject = (entityKey: DBQueryKey) => ({\n\t\t\t\tfilter: (action: Action) => {\n\t\t\t\t\tconst conditionsPerEntity = registeredConditions[entityKey];\n\t\t\t\t\tif (!conditionsPerEntity) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #1\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst conditionsPerEntityAndAction = conditionsPerEntity[action];\n\t\t\t\t\tif (!conditionsPerEntityAndAction) {\n\t\t\t\t\t\tthrow \"TODO (No allowed entry found for this condition) #2\";\n\t\t\t\t\t}\n\n\t\t\t\t\tconst simpleConditions =\n\t\t\t\t\t\tconditionsPerEntityAndAction.filter(isSimpleCondition);\n\n\t\t\t\t\tconst syncFunctionConditions = conditionsPerEntityAndAction\n\t\t\t\t\t\t.filter(isSyncFunctionCondition)\n\t\t\t\t\t\t.map((condition) => condition(userContext));\n\n\t\t\t\t\t// const asyncFunctionConditions = await Promise.all(\n\t\t\t\t\t// \tconditionsPerEntityAndAction\n\t\t\t\t\t// \t\t.filter(isAsyncFunctionCondition)\n\t\t\t\t\t// \t\t.map((condition) => condition(userContext)),\n\t\t\t\t\t// );\n\n\t\t\t\t\tconst allConditionObjects = [\n\t\t\t\t\t\t...simpleConditions,\n\t\t\t\t\t\t...syncFunctionConditions,\n\t\t\t\t\t\t// ...asyncFunctionConditions,\n\t\t\t\t\t];\n\n\t\t\t\t\tlet highestLimit = undefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.limit) {\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\thighestLimit === undefined ||\n\t\t\t\t\t\t\t\tconditionObject.limit > highestLimit\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\thighestLimit = conditionObject.limit;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tlet combinedAllowedColumns: Record<string, any> | undefined =\n\t\t\t\t\t\tundefined;\n\t\t\t\t\tfor (const conditionObject of allConditionObjects) {\n\t\t\t\t\t\tif (conditionObject.columns) {\n\t\t\t\t\t\t\tif (combinedAllowedColumns === undefined) {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = conditionObject.columns;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcombinedAllowedColumns = {\n\t\t\t\t\t\t\t\t\t...combinedAllowedColumns,\n\t\t\t\t\t\t\t\t\t...conditionObject.columns,\n\t\t\t\t\t\t\t\t};\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\n\t\t\t\t\tconst accumulatedWhereConditions = allConditionObjects\n\t\t\t\t\t\t.filter((o) => o.where)\n\t\t\t\t\t\t.map((o) => o.where);\n\n\t\t\t\t\tconst combinedWhere =\n\t\t\t\t\t\taccumulatedWhereConditions.length > 0\n\t\t\t\t\t\t\t? or(...accumulatedWhereConditions)\n\t\t\t\t\t\t\t: undefined;\n\n\t\t\t\t\treturn {\n\t\t\t\t\t\twhere: combinedWhere,\n\t\t\t\t\t\tcolumns: combinedAllowedColumns,\n\t\t\t\t\t\tlimit: highestLimit,\n\t\t\t\t\t};\n\t\t\t\t},\n\t\t\t});\n\n\t\t\tfor (const entityKey of Object.keys(db.query) as DBQueryKey[]) {\n\t\t\t\tbuilder[entityKey] = createEntityObject(entityKey);\n\t\t\t}\n\n\t\t\treturn builder;\n\t\t},\n\t};\n};\n","export class RumbleError extends Error {\n\tconstructor(message: string) {\n\t\tsuper(message);\n\t\tthis.name = \"RumbleError\";\n\t}\n}\n","import SchemaBuilder from \"@pothos/core\";\nimport DrizzlePlugin from \"@pothos/plugin-drizzle\";\nimport { type YogaServerOptions, createYoga } from \"graphql-yoga\";\nimport { createAbilityBuilder } from \"../abilities/builder\";\nimport { RumbleError } from \"../helpers/rumbleError\";\nimport type { GenericDrizzleDbTypeConstraints } from \"../types/genericDrizzleDbType\";\n\nexport const rumble = <\n\tUserContext extends Record<string, any>,\n\tDB extends GenericDrizzleDbTypeConstraints,\n\tRequestEvent extends Record<string, any>,\n\tAction extends string = \"create\" | \"read\" | \"update\" | \"delete\",\n>({\n\tdb,\n\tnativeServerOptions,\n\tcontext: makeUserContext,\n\tonlyQuery = false,\n\tactions = [\"create\", \"read\", \"update\", \"delete\"] as Action[],\n}: {\n\t/**\n\t * Your drizzle database instance\n\t */\n\tdb: DB;\n\t/**\n\t * Optional options for the native GraphQL Yoga server\n\t */\n\tnativeServerOptions?:\n\t\t| Omit<YogaServerOptions<RequestEvent, any>, \"schema\" | \"context\">\n\t\t| undefined;\n\t/**\n\t * A function for providing context for each request based on the incoming HTTP Request.\n\t * The type of the parameter equals the HTTPRequest type of your chosen server.\n\t */\n\tcontext?:\n\t\t| ((event: RequestEvent) => Promise<UserContext> | UserContext)\n\t\t| undefined;\n\t/**\n\t * If you only want to create queries and do not need mutations, enable this\n\t */\n\tonlyQuery?: boolean;\n\t/**\n\t * The actions that are available\n\t */\n\tactions?: Action[];\n}) => {\n\ttype DBQueryKey = keyof DB[\"query\"];\n\tconst abilityBuilder = createAbilityBuilder<UserContext, DB, Action>({\n\t\tdb,\n\t});\n\n\tconst makeContext = async (req: RequestEvent) => {\n\t\tconst userContext = makeUserContext\n\t\t\t? await makeUserContext(req)\n\t\t\t: ({} as UserContext);\n\t\treturn {\n\t\t\t...userContext,\n\t\t\tabilities: abilityBuilder.buildWithUserContext(userContext),\n\t\t};\n\t};\n\n\tconst nativeBuilder = new SchemaBuilder<{\n\t\tContext: Awaited<ReturnType<typeof makeContext>>;\n\t\t// Scalars: Scalars<Prisma.Decimal, Prisma.InputJsonValue | null, Prisma.InputJsonValue> & {\n\t\t// \tFile: {\n\t\t// \t\tInput: File;\n\t\t// \t\tOutput: never;\n\t\t// \t};\n\t\t// \tJSONObject: {\n\t\t// \t\tInput: any;\n\t\t// \t\tOutput: any;\n\t\t// \t};\n\t\t// };\n\t\tDrizzleSchema: DB[\"_\"][\"fullSchema\"];\n\t\tDefaultFieldNullability: false;\n\t\tDefaultArgumentNullability: false;\n\t\tDefaultInputFieldRequiredness: true;\n\t}>({\n\t\tplugins: [DrizzlePlugin],\n\t\tdrizzle: {\n\t\t\tclient: db,\n\t\t},\n\t\tdefaultFieldNullability: false,\n\t\tdefaultInputFieldRequiredness: true,\n\t});\n\n\tnativeBuilder.queryType({});\n\n\tif (!onlyQuery) {\n\t\tnativeBuilder.mutationType({});\n\t}\n\n\tconst implementDefaultObject = <\n\t\tExplicitTableName extends DBQueryKey,\n\t\tRefName extends string,\n\t>({\n\t\ttableName,\n\t\tname,\n\t\treadAction = \"read\" as Action,\n\t}: {\n\t\ttableName: ExplicitTableName;\n\t\tname: RefName;\n\t\treadAction?: Action;\n\t}) => {\n\t\tconst schema = db._.schema![tableName as any];\n\t\treturn nativeBuilder.drizzleObject(tableName as any, {\n\t\t\tname,\n\t\t\tfields: (t) => {\n\t\t\t\tconst mapSQLTypeStringToExposedPothosType = (\n\t\t\t\t\tsqlType: string,\n\t\t\t\t\tcolumnName: string,\n\t\t\t\t) => {\n\t\t\t\t\tswitch (sqlType) {\n\t\t\t\t\t\tcase \"serial\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeInt(columnName);\n\t\t\t\t\t\tcase \"int\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeInt(columnName);\n\t\t\t\t\t\tcase \"string\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeString(columnName);\n\t\t\t\t\t\tcase \"text\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeString(columnName);\n\t\t\t\t\t\tcase \"boolean\":\n\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\treturn t.exposeBoolean(columnName);\n\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\tthrow new RumbleError(\n\t\t\t\t\t\t\t\t`Unknown SQL type: ${sqlType}. Please open an issue so it can be added.`,\n\t\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t};\n\n\t\t\t\tconst fields = Object.entries(schema.columns).reduce(\n\t\t\t\t\t(acc, [key, value]) => {\n\t\t\t\t\t\tacc[key] = mapSQLTypeStringToExposedPothosType(\n\t\t\t\t\t\t\tvalue.getSQLType(),\n\t\t\t\t\t\t\tvalue.name,\n\t\t\t\t\t\t);\n\t\t\t\t\t\treturn acc;\n\t\t\t\t\t},\n\t\t\t\t\t{} as Record<\n\t\t\t\t\t\tstring,\n\t\t\t\t\t\tReturnType<typeof mapSQLTypeStringToExposedPothosType>\n\t\t\t\t\t>,\n\t\t\t\t);\n\n\t\t\t\tconst relations = Object.entries(schema.relations).reduce(\n\t\t\t\t\t(acc, [key, value]) => {\n\t\t\t\t\t\tacc[key] = t.relation(key, {\n\t\t\t\t\t\t\tquery: (_args: any, ctx: any) =>\n\t\t\t\t\t\t\t\tctx.abilities[tableName as any].filter(readAction),\n\t\t\t\t\t\t} as any) as any;\n\t\t\t\t\t\treturn acc;\n\t\t\t\t\t},\n\t\t\t\t\t{} as Record<\n\t\t\t\t\t\tstring,\n\t\t\t\t\t\tReturnType<typeof mapSQLTypeStringToExposedPothosType>\n\t\t\t\t\t>,\n\t\t\t\t);\n\n\t\t\t\treturn {\n\t\t\t\t\t...fields,\n\t\t\t\t\t...relations,\n\t\t\t\t} as any;\n\t\t\t},\n\t\t});\n\t};\n\n\treturn {\n\t\t/**\n * The ability builder. Use it to declare whats allowed for each entity in your DB.\n * \n * @example\n * \n * ```ts\n * // users can edit themselves\n abilityBuilder.users\n .allow([\"read\", \"update\", \"delete\"])\n .when(({ userId }) => ({ where: eq(schema.users.id, userId) }));\n \n // everyone can read posts\n abilityBuilder.posts.allow(\"read\");\n * \n * ```\n */\n\t\tabilityBuilder,\n\t\t/**\n\t\t * The pothos schema builder. See https://pothos-graphql.dev/docs/plugins/drizzle\n\t\t */\n\t\tschemaBuilder: nativeBuilder,\n\t\t/**\n * The native yoga instance. Can be used to run an actual HTTP server.\n * \n * @example\n * \n * ```ts\n import { createServer } from \"node:http\";\n * const server = createServer(yoga());\n server.listen(3000, () => {\n console.log(\"Visit http://localhost:3000/graphql\");\n });\n * ```\n */\n\t\tyoga: () =>\n\t\t\tcreateYoga<RequestEvent>({\n\t\t\t\t...nativeServerOptions,\n\t\t\t\tschema: nativeBuilder.toSchema(),\n\t\t\t\tcontext: makeContext,\n\t\t\t}),\n\t\t/**\n\t\t * A function for creating default objects for your schema\n\t\t */\n\t\timplementDefaultObject,\n\t};\n};\n","import { RumbleError } from \"./rumbleError\";\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n * schemaBuilder.queryFields((t) => {\n return {\n findFirstUser: t.drizzleField({\n type: UserRef,\n resolve: (query, root, args, ctx, info) => {\n return (\n db.query.users\n .findFirst({\n ...query,\n where: ctx.abilities.users.filter(\"read\").where,\n })\n // note that we need to manually raise an error if the value is not found\n .then(assertFindFirstExists)\n );\n },\n }),\n };\n });\n * ```\n */\nexport const assertFindFirstExists = <T>(value: T | undefined): T => {\n\tif (!value) throw new RumbleError(\"Value not found but required (findFirst)\");\n\treturn value;\n};\n\n/**\n * \n * Helper function to map a drizzle findFirst query result,\n * which may be optional, to a correct drizzle type.\n * \n * @throws RumbleError\n * \n * @example\n * \n * ```ts\n schemaBuilder.mutationFields((t) => {\n return {\n updateUsername: t.drizzleField({\n type: UserRef,\n args: {\n userId: t.arg.int({ required: true }),\n newName: t.arg.string({ required: true }),\n },\n resolve: (query, root, args, ctx, info) => {\n return db\n .update(schema.users)\n .set({\n name: args.newName,\n })\n .where(\n and(\n eq(schema.users.id, args.userId),\n ctx.abilities.users.filter(\"update\").where\n )\n )\n .returning({ id: schema.users.id, name: schema.users.name })\n // note that we need to manually raise an error if the value is not found\n .then(assertFirstEntryExists);\n },\n }),\n };\n });\n * ```\n */\nexport const assertFirstEntryExists = <T>(value: T[]): T => {\n\tconst v = value.at(0);\n\tif (!v) throw new RumbleError(\"Value not found but required (firstEntry)\");\n\treturn v;\n};\n"]}
|
package/package.json
CHANGED
@@ -1,24 +1 @@
|
|
1
|
-
{
|
2
|
-
"name": "@m1212e/rumble",
|
3
|
-
"module": "index.ts",
|
4
|
-
"type": "module",
|
5
|
-
"peerDependencies": {
|
6
|
-
"typescript": "^5.7.2",
|
7
|
-
"drizzle-orm": "^0.38.3"
|
8
|
-
},
|
9
|
-
"dependencies": {
|
10
|
-
"@pothos/plugin-drizzle": "^0.5.3",
|
11
|
-
"graphql-yoga": "^5.10.8",
|
12
|
-
"@pothos/core": "^4.3.0"
|
13
|
-
},
|
14
|
-
"version": "0.0.1",
|
15
|
-
"exports": {
|
16
|
-
"./package.json": "./package.json",
|
17
|
-
".": {
|
18
|
-
"require": "./index.cjs",
|
19
|
-
"import": "./index.js",
|
20
|
-
"node": "./index.cjs",
|
21
|
-
"default": "./index.cjs"
|
22
|
-
}
|
23
|
-
}
|
24
|
-
}
|
1
|
+
{"name":"@m1212e/rumble","module":"index.ts","type":"module","peerDependencies":{"typescript":"^5.7.2","drizzle-orm":"^0.38.3"},"dependencies":{"@pothos/plugin-drizzle":"^0.5.3","graphql-yoga":"^5.10.8","@pothos/core":"^4.3.0"},"version":"0.0.3","exports":{"./package.json":"./package.json",".":{"require":"./index.cjs","import":"./index.js","node":"./index.cjs","default":"./index.cjs"}}}
|