@vllnt/convex-memberships 0.1.0-canary.3fc56e6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +144 -0
- package/dist/client/index.d.ts +114 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +108 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +56 -0
- package/dist/client/types.d.ts.map +1 -0
- package/dist/client/types.js +3 -0
- package/dist/client/types.js.map +1 -0
- package/dist/component/_generated/api.d.ts +38 -0
- package/dist/component/_generated/api.d.ts.map +1 -0
- package/dist/component/_generated/api.js +31 -0
- package/dist/component/_generated/api.js.map +1 -0
- package/dist/component/_generated/component.d.ts +116 -0
- package/dist/component/_generated/component.d.ts.map +1 -0
- package/dist/component/_generated/component.js +11 -0
- package/dist/component/_generated/component.js.map +1 -0
- package/dist/component/_generated/dataModel.d.ts +46 -0
- package/dist/component/_generated/dataModel.d.ts.map +1 -0
- package/dist/component/_generated/dataModel.js +11 -0
- package/dist/component/_generated/dataModel.js.map +1 -0
- package/dist/component/_generated/server.d.ts +121 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +78 -0
- package/dist/component/_generated/server.js.map +1 -0
- package/dist/component/convex.config.d.ts +3 -0
- package/dist/component/convex.config.d.ts.map +1 -0
- package/dist/component/convex.config.js +7 -0
- package/dist/component/convex.config.js.map +1 -0
- package/dist/component/mutations.d.ts +24 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +96 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +80 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +126 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +33 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +26 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +146 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +60 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +113 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +80 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +20 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +10 -0
- package/dist/shared.js.map +1 -0
- package/package.json +113 -0
- package/src/client/index.ts +270 -0
- package/src/client/types.ts +69 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +145 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +156 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/mutations.ts +109 -0
- package/src/component/queries.ts +145 -0
- package/src/component/schema.ts +26 -0
- package/src/component/validators.ts +72 -0
- package/src/react/index.test.tsx +140 -0
- package/src/react/index.tsx +134 -0
- package/src/shared.ts +25 -0
- package/src/test.ts +12 -0
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated data model types.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
DataModelFromSchemaDefinition,
|
|
13
|
+
DocumentByName,
|
|
14
|
+
TableNamesInDataModel,
|
|
15
|
+
SystemTableNames,
|
|
16
|
+
} from "convex/server";
|
|
17
|
+
import type { GenericId } from "convex/values";
|
|
18
|
+
import schema from "../schema.js";
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The names of all of your Convex tables.
|
|
22
|
+
*/
|
|
23
|
+
export type TableNames = TableNamesInDataModel<DataModel>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The type of a document stored in Convex.
|
|
27
|
+
*
|
|
28
|
+
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
29
|
+
*/
|
|
30
|
+
export type Doc<TableName extends TableNames> = DocumentByName<
|
|
31
|
+
DataModel,
|
|
32
|
+
TableName
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* An identifier for a document in Convex.
|
|
37
|
+
*
|
|
38
|
+
* Convex documents are uniquely identified by their `Id`, which is accessible
|
|
39
|
+
* on the `_id` field. To learn more, see [Document IDs](https://docs.convex.dev/using/document-ids).
|
|
40
|
+
*
|
|
41
|
+
* Documents can be loaded using `db.get(tableName, id)` in query and mutation functions.
|
|
42
|
+
*
|
|
43
|
+
* IDs are just strings at runtime, but this type can be used to distinguish them from other
|
|
44
|
+
* strings when type checking.
|
|
45
|
+
*
|
|
46
|
+
* @typeParam TableName - A string literal type of the table name (like "users").
|
|
47
|
+
*/
|
|
48
|
+
export type Id<TableName extends TableNames | SystemTableNames> =
|
|
49
|
+
GenericId<TableName>;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* A type describing your Convex data model.
|
|
53
|
+
*
|
|
54
|
+
* This type includes information about what tables you have, the type of
|
|
55
|
+
* documents stored in those tables, and the indexes defined on them.
|
|
56
|
+
*
|
|
57
|
+
* This type is used to parameterize methods like `queryGeneric` and
|
|
58
|
+
* `mutationGeneric` to make them type-safe.
|
|
59
|
+
*/
|
|
60
|
+
export type DataModel = DataModelFromSchemaDefinition<typeof schema>;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated utilities for implementing server-side Convex query and mutation functions.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type {
|
|
12
|
+
ActionBuilder,
|
|
13
|
+
HttpActionBuilder,
|
|
14
|
+
MutationBuilder,
|
|
15
|
+
QueryBuilder,
|
|
16
|
+
GenericActionCtx,
|
|
17
|
+
GenericMutationCtx,
|
|
18
|
+
GenericQueryCtx,
|
|
19
|
+
GenericDatabaseReader,
|
|
20
|
+
GenericDatabaseWriter,
|
|
21
|
+
} from "convex/server";
|
|
22
|
+
import {
|
|
23
|
+
actionGeneric,
|
|
24
|
+
httpActionGeneric,
|
|
25
|
+
queryGeneric,
|
|
26
|
+
mutationGeneric,
|
|
27
|
+
internalActionGeneric,
|
|
28
|
+
internalMutationGeneric,
|
|
29
|
+
internalQueryGeneric,
|
|
30
|
+
} from "convex/server";
|
|
31
|
+
import type { DataModel } from "./dataModel.js";
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Define a query in this Convex app's public API.
|
|
35
|
+
*
|
|
36
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
37
|
+
*
|
|
38
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
39
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
40
|
+
*/
|
|
41
|
+
export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
45
|
+
*
|
|
46
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
47
|
+
*
|
|
48
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
49
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
50
|
+
*/
|
|
51
|
+
export const internalQuery: QueryBuilder<DataModel, "internal"> =
|
|
52
|
+
internalQueryGeneric;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Define a mutation in this Convex app's public API.
|
|
56
|
+
*
|
|
57
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
58
|
+
*
|
|
59
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
60
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
61
|
+
*/
|
|
62
|
+
export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
66
|
+
*
|
|
67
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
68
|
+
*
|
|
69
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
70
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
71
|
+
*/
|
|
72
|
+
export const internalMutation: MutationBuilder<DataModel, "internal"> =
|
|
73
|
+
internalMutationGeneric;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Define an action in this Convex app's public API.
|
|
77
|
+
*
|
|
78
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
79
|
+
* code and code with side-effects, like calling third-party services.
|
|
80
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
81
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
82
|
+
*
|
|
83
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
84
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
85
|
+
*/
|
|
86
|
+
export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
90
|
+
*
|
|
91
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
92
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
93
|
+
*/
|
|
94
|
+
export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
95
|
+
internalActionGeneric;
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Define an HTTP action.
|
|
99
|
+
*
|
|
100
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
101
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
102
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
103
|
+
*
|
|
104
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
105
|
+
* and a Fetch API `Request` object as its second.
|
|
106
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
107
|
+
*/
|
|
108
|
+
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* A set of services for use within Convex query functions.
|
|
112
|
+
*
|
|
113
|
+
* The query context is passed as the first argument to any Convex query
|
|
114
|
+
* function run on the server.
|
|
115
|
+
*
|
|
116
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
117
|
+
*/
|
|
118
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* A set of services for use within Convex mutation functions.
|
|
122
|
+
*
|
|
123
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
124
|
+
* function run on the server.
|
|
125
|
+
*
|
|
126
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
127
|
+
*/
|
|
128
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* A set of services for use within Convex action functions.
|
|
132
|
+
*
|
|
133
|
+
* The action context is passed as the first argument to any Convex action
|
|
134
|
+
* function run on the server.
|
|
135
|
+
*/
|
|
136
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* An interface to read from the database within Convex query functions.
|
|
140
|
+
*
|
|
141
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
142
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
143
|
+
* building a query.
|
|
144
|
+
*/
|
|
145
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* An interface to read from and write to the database within Convex mutation
|
|
149
|
+
* functions.
|
|
150
|
+
*
|
|
151
|
+
* Convex guarantees that all writes within a single mutation are
|
|
152
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
153
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
154
|
+
* for the guarantees Convex provides your functions.
|
|
155
|
+
*/
|
|
156
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { defineComponent } from "convex/server";
|
|
2
|
+
|
|
3
|
+
const component = defineComponent("memberships");
|
|
4
|
+
|
|
5
|
+
// Nest official child components here, e.g.:
|
|
6
|
+
// import sharded from "@convex-dev/sharded-counter/convex.config";
|
|
7
|
+
// component.use(sharded);
|
|
8
|
+
|
|
9
|
+
export default component;
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import type { Infer } from "convex/values";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { mutation } from "./_generated/server";
|
|
4
|
+
import { memberKind, removeResult, setRelationResult } from "./validators";
|
|
5
|
+
|
|
6
|
+
type SetRelationResult = Infer<typeof setRelationResult>;
|
|
7
|
+
|
|
8
|
+
export const add = mutation({
|
|
9
|
+
args: {
|
|
10
|
+
memberRef: v.string(),
|
|
11
|
+
memberKind,
|
|
12
|
+
resourceRef: v.string(),
|
|
13
|
+
relation: v.string(),
|
|
14
|
+
status: v.optional(v.string()),
|
|
15
|
+
},
|
|
16
|
+
returns: v.id("memberships"),
|
|
17
|
+
handler: async (ctx, args) => {
|
|
18
|
+
const existing = await ctx.db
|
|
19
|
+
.query("memberships")
|
|
20
|
+
.withIndex("by_tuple", (q) =>
|
|
21
|
+
q
|
|
22
|
+
.eq("memberRef", args.memberRef)
|
|
23
|
+
.eq("resourceRef", args.resourceRef)
|
|
24
|
+
.eq("relation", args.relation),
|
|
25
|
+
)
|
|
26
|
+
.first();
|
|
27
|
+
if (existing !== null) {
|
|
28
|
+
await ctx.db.patch(existing._id, {
|
|
29
|
+
memberKind: args.memberKind,
|
|
30
|
+
status: args.status,
|
|
31
|
+
});
|
|
32
|
+
return existing._id;
|
|
33
|
+
}
|
|
34
|
+
return await ctx.db.insert("memberships", {
|
|
35
|
+
memberRef: args.memberRef,
|
|
36
|
+
memberKind: args.memberKind,
|
|
37
|
+
resourceRef: args.resourceRef,
|
|
38
|
+
relation: args.relation,
|
|
39
|
+
status: args.status,
|
|
40
|
+
createdAt: Date.now(),
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
export const remove = mutation({
|
|
46
|
+
args: {
|
|
47
|
+
memberRef: v.string(),
|
|
48
|
+
resourceRef: v.string(),
|
|
49
|
+
relation: v.string(),
|
|
50
|
+
},
|
|
51
|
+
returns: removeResult,
|
|
52
|
+
handler: async (ctx, args) => {
|
|
53
|
+
const existing = await ctx.db
|
|
54
|
+
.query("memberships")
|
|
55
|
+
.withIndex("by_tuple", (q) =>
|
|
56
|
+
q
|
|
57
|
+
.eq("memberRef", args.memberRef)
|
|
58
|
+
.eq("resourceRef", args.resourceRef)
|
|
59
|
+
.eq("relation", args.relation),
|
|
60
|
+
)
|
|
61
|
+
.first();
|
|
62
|
+
if (existing === null) {
|
|
63
|
+
return { removed: false };
|
|
64
|
+
}
|
|
65
|
+
await ctx.db.delete(existing._id);
|
|
66
|
+
return { removed: true };
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
export const setRelation = mutation({
|
|
71
|
+
args: {
|
|
72
|
+
memberRef: v.string(),
|
|
73
|
+
resourceRef: v.string(),
|
|
74
|
+
fromRelation: v.string(),
|
|
75
|
+
toRelation: v.string(),
|
|
76
|
+
},
|
|
77
|
+
returns: setRelationResult,
|
|
78
|
+
handler: async (ctx, args): Promise<SetRelationResult> => {
|
|
79
|
+
const from = await ctx.db
|
|
80
|
+
.query("memberships")
|
|
81
|
+
.withIndex("by_tuple", (q) =>
|
|
82
|
+
q
|
|
83
|
+
.eq("memberRef", args.memberRef)
|
|
84
|
+
.eq("resourceRef", args.resourceRef)
|
|
85
|
+
.eq("relation", args.fromRelation),
|
|
86
|
+
)
|
|
87
|
+
.first();
|
|
88
|
+
if (from === null) {
|
|
89
|
+
return { ok: false, reason: "NOT_FOUND" };
|
|
90
|
+
}
|
|
91
|
+
if (args.fromRelation === args.toRelation) {
|
|
92
|
+
return { ok: true };
|
|
93
|
+
}
|
|
94
|
+
const taken = await ctx.db
|
|
95
|
+
.query("memberships")
|
|
96
|
+
.withIndex("by_tuple", (q) =>
|
|
97
|
+
q
|
|
98
|
+
.eq("memberRef", args.memberRef)
|
|
99
|
+
.eq("resourceRef", args.resourceRef)
|
|
100
|
+
.eq("relation", args.toRelation),
|
|
101
|
+
)
|
|
102
|
+
.first();
|
|
103
|
+
if (taken !== null) {
|
|
104
|
+
return { ok: false, reason: "EXISTS" };
|
|
105
|
+
}
|
|
106
|
+
await ctx.db.patch(from._id, { relation: args.toRelation });
|
|
107
|
+
return { ok: true };
|
|
108
|
+
},
|
|
109
|
+
});
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { paginationOptsValidator } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
import { query } from "./_generated/server";
|
|
4
|
+
import {
|
|
5
|
+
memberEntry,
|
|
6
|
+
memberEntryPage,
|
|
7
|
+
membershipDocPage,
|
|
8
|
+
resourceEntry,
|
|
9
|
+
resourceEntryPage,
|
|
10
|
+
} from "./validators";
|
|
11
|
+
|
|
12
|
+
export const listMembers = query({
|
|
13
|
+
args: {
|
|
14
|
+
resourceRef: v.string(),
|
|
15
|
+
relation: v.optional(v.string()),
|
|
16
|
+
paginationOpts: paginationOptsValidator,
|
|
17
|
+
},
|
|
18
|
+
returns: memberEntryPage,
|
|
19
|
+
handler: async (ctx, args) => {
|
|
20
|
+
const relation = args.relation;
|
|
21
|
+
const result =
|
|
22
|
+
relation === undefined
|
|
23
|
+
? await ctx.db
|
|
24
|
+
.query("memberships")
|
|
25
|
+
.withIndex("by_resource", (q) =>
|
|
26
|
+
q.eq("resourceRef", args.resourceRef),
|
|
27
|
+
)
|
|
28
|
+
.paginate(args.paginationOpts)
|
|
29
|
+
: await ctx.db
|
|
30
|
+
.query("memberships")
|
|
31
|
+
.withIndex("by_resource_relation", (q) =>
|
|
32
|
+
q.eq("resourceRef", args.resourceRef).eq("relation", relation),
|
|
33
|
+
)
|
|
34
|
+
.paginate(args.paginationOpts);
|
|
35
|
+
return {
|
|
36
|
+
page: result.page.map((row) => ({
|
|
37
|
+
memberRef: row.memberRef,
|
|
38
|
+
memberKind: row.memberKind,
|
|
39
|
+
relation: row.relation,
|
|
40
|
+
})),
|
|
41
|
+
isDone: result.isDone,
|
|
42
|
+
continueCursor: result.continueCursor,
|
|
43
|
+
};
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
export const members = query({
|
|
48
|
+
args: { memberRef: v.string(), paginationOpts: paginationOptsValidator },
|
|
49
|
+
returns: resourceEntryPage,
|
|
50
|
+
handler: async (ctx, args) => {
|
|
51
|
+
const result = await ctx.db
|
|
52
|
+
.query("memberships")
|
|
53
|
+
.withIndex("by_member", (q) => q.eq("memberRef", args.memberRef))
|
|
54
|
+
.paginate(args.paginationOpts);
|
|
55
|
+
return {
|
|
56
|
+
page: result.page.map((row) => ({
|
|
57
|
+
resourceRef: row.resourceRef,
|
|
58
|
+
relation: row.relation,
|
|
59
|
+
})),
|
|
60
|
+
isDone: result.isDone,
|
|
61
|
+
continueCursor: result.continueCursor,
|
|
62
|
+
};
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Whether `memberRef` relates to `resourceRef` under the given `relation`, or
|
|
68
|
+
* under **any** stored relation when `relation` is omitted.
|
|
69
|
+
*
|
|
70
|
+
* @remarks
|
|
71
|
+
* **Security warning — unscoped path.** When `relation` is omitted this
|
|
72
|
+
* function returns `true` for **any** relation stored between the pair
|
|
73
|
+
* (e.g. `"invited"`, `"suspended"`, `"banned"`). Do **not** use the
|
|
74
|
+
* unscoped path as an authorization gate without first enumerating every
|
|
75
|
+
* relation your policy considers sufficient for the guarded action. Use the
|
|
76
|
+
* scoped path (`relation` provided) for authz checks, or enumerate
|
|
77
|
+
* acceptable relations explicitly before calling.
|
|
78
|
+
*/
|
|
79
|
+
export const isMember = query({
|
|
80
|
+
args: {
|
|
81
|
+
memberRef: v.string(),
|
|
82
|
+
resourceRef: v.string(),
|
|
83
|
+
relation: v.optional(v.string()),
|
|
84
|
+
},
|
|
85
|
+
returns: v.boolean(),
|
|
86
|
+
handler: async (ctx, args) => {
|
|
87
|
+
const relation = args.relation;
|
|
88
|
+
if (relation !== undefined) {
|
|
89
|
+
const row = await ctx.db
|
|
90
|
+
.query("memberships")
|
|
91
|
+
.withIndex("by_tuple", (q) =>
|
|
92
|
+
q
|
|
93
|
+
.eq("memberRef", args.memberRef)
|
|
94
|
+
.eq("resourceRef", args.resourceRef)
|
|
95
|
+
.eq("relation", relation),
|
|
96
|
+
)
|
|
97
|
+
.first();
|
|
98
|
+
return row !== null;
|
|
99
|
+
}
|
|
100
|
+
const row = await ctx.db
|
|
101
|
+
.query("memberships")
|
|
102
|
+
.withIndex("by_member_resource", (q) =>
|
|
103
|
+
q.eq("memberRef", args.memberRef).eq("resourceRef", args.resourceRef),
|
|
104
|
+
)
|
|
105
|
+
.first();
|
|
106
|
+
return row !== null;
|
|
107
|
+
},
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
export const documents = query({
|
|
111
|
+
args: {
|
|
112
|
+
resourceRef: v.optional(v.string()),
|
|
113
|
+
memberRef: v.optional(v.string()),
|
|
114
|
+
paginationOpts: paginationOptsValidator,
|
|
115
|
+
},
|
|
116
|
+
returns: membershipDocPage,
|
|
117
|
+
handler: async (ctx, args) => {
|
|
118
|
+
const resourceRef = args.resourceRef;
|
|
119
|
+
const memberRef = args.memberRef;
|
|
120
|
+
const result =
|
|
121
|
+
resourceRef !== undefined
|
|
122
|
+
? await ctx.db
|
|
123
|
+
.query("memberships")
|
|
124
|
+
.withIndex("by_resource", (q) => q.eq("resourceRef", resourceRef))
|
|
125
|
+
.paginate(args.paginationOpts)
|
|
126
|
+
: memberRef !== undefined
|
|
127
|
+
? await ctx.db
|
|
128
|
+
.query("memberships")
|
|
129
|
+
.withIndex("by_member", (q) => q.eq("memberRef", memberRef))
|
|
130
|
+
.paginate(args.paginationOpts)
|
|
131
|
+
: await ctx.db.query("memberships").paginate(args.paginationOpts);
|
|
132
|
+
return {
|
|
133
|
+
page: result.page.map((row) => ({
|
|
134
|
+
memberRef: row.memberRef,
|
|
135
|
+
memberKind: row.memberKind,
|
|
136
|
+
resourceRef: row.resourceRef,
|
|
137
|
+
relation: row.relation,
|
|
138
|
+
status: row.status,
|
|
139
|
+
createdAt: row.createdAt,
|
|
140
|
+
})),
|
|
141
|
+
isDone: result.isDone,
|
|
142
|
+
continueCursor: result.continueCursor,
|
|
143
|
+
};
|
|
144
|
+
},
|
|
145
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { defineSchema, defineTable } from "convex/server";
|
|
2
|
+
import { v } from "convex/values";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Sandboxed table — the relationship graph's own concern. A row is one flat
|
|
6
|
+
* ReBAC edge `(memberRef, resourceRef, relation)`. `memberRef`, `resourceRef`,
|
|
7
|
+
* `relation`, and `memberKind` are opaque host strings (never assume their
|
|
8
|
+
* shape). `status` is an opaque, optional lifecycle marker the host may stamp
|
|
9
|
+
* (e.g. `"active"`, `"invited"`); the component never inspects it. FLAT edges
|
|
10
|
+
* only — no transitive expansion in this version.
|
|
11
|
+
*/
|
|
12
|
+
export default defineSchema({
|
|
13
|
+
memberships: defineTable({
|
|
14
|
+
memberRef: v.string(),
|
|
15
|
+
memberKind: v.string(),
|
|
16
|
+
resourceRef: v.string(),
|
|
17
|
+
relation: v.string(),
|
|
18
|
+
status: v.optional(v.string()),
|
|
19
|
+
createdAt: v.number(),
|
|
20
|
+
})
|
|
21
|
+
.index("by_member", ["memberRef"])
|
|
22
|
+
.index("by_resource", ["resourceRef"])
|
|
23
|
+
.index("by_resource_relation", ["resourceRef", "relation"])
|
|
24
|
+
.index("by_member_resource", ["memberRef", "resourceRef"])
|
|
25
|
+
.index("by_tuple", ["memberRef", "resourceRef", "relation"]),
|
|
26
|
+
});
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { v } from "convex/values";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* What a tuple's `memberRef` points at — an opaque, host-classified string
|
|
5
|
+
* (e.g. `"user"`, `"group"`, `"resource"`, or any host-defined kind). The
|
|
6
|
+
* component never interprets it. Defaults to `"user"` when the host omits it.
|
|
7
|
+
*/
|
|
8
|
+
export const memberKind = v.string();
|
|
9
|
+
|
|
10
|
+
/** A full membership edge as stored — returned by document-shaped queries. */
|
|
11
|
+
export const membershipDoc = v.object({
|
|
12
|
+
memberRef: v.string(),
|
|
13
|
+
memberKind,
|
|
14
|
+
resourceRef: v.string(),
|
|
15
|
+
relation: v.string(),
|
|
16
|
+
status: v.optional(v.string()),
|
|
17
|
+
createdAt: v.number(),
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
/** Projection returned by `listMembers` — who relates to a resource, and how. */
|
|
21
|
+
export const memberEntry = v.object({
|
|
22
|
+
memberRef: v.string(),
|
|
23
|
+
memberKind,
|
|
24
|
+
relation: v.string(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
/** Projection returned by `members` — which resources a member relates to, and how. */
|
|
28
|
+
export const resourceEntry = v.object({
|
|
29
|
+
resourceRef: v.string(),
|
|
30
|
+
relation: v.string(),
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
/** A stable code-tag explaining why a `setRelation` was rejected. */
|
|
34
|
+
export const setRelationReason = v.union(
|
|
35
|
+
v.literal("NOT_FOUND"),
|
|
36
|
+
v.literal("EXISTS"),
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Result of `setRelation`. `reason` carries a stable code-tag (`NOT_FOUND`,
|
|
41
|
+
* `EXISTS`) when `ok` is false.
|
|
42
|
+
*/
|
|
43
|
+
export const setRelationResult = v.object({
|
|
44
|
+
ok: v.boolean(),
|
|
45
|
+
reason: v.optional(setRelationReason),
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
/** Result of `remove` — `removed` is true when an edge was actually deleted. */
|
|
49
|
+
export const removeResult = v.object({
|
|
50
|
+
removed: v.boolean(),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
/** A page of `MemberEntry` rows, as returned by a paginated `listMembers`. */
|
|
54
|
+
export const memberEntryPage = v.object({
|
|
55
|
+
page: v.array(memberEntry),
|
|
56
|
+
isDone: v.boolean(),
|
|
57
|
+
continueCursor: v.string(),
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
/** A page of `ResourceEntry` rows, as returned by a paginated `members`. */
|
|
61
|
+
export const resourceEntryPage = v.object({
|
|
62
|
+
page: v.array(resourceEntry),
|
|
63
|
+
isDone: v.boolean(),
|
|
64
|
+
continueCursor: v.string(),
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
/** A page of full membership docs, as returned by the paginated `documents`. */
|
|
68
|
+
export const membershipDocPage = v.object({
|
|
69
|
+
page: v.array(membershipDoc),
|
|
70
|
+
isDone: v.boolean(),
|
|
71
|
+
continueCursor: v.string(),
|
|
72
|
+
});
|