@vllnt/convex-slugs 0.1.0-canary.2051641
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 +138 -0
- package/dist/client/index.d.ts +90 -0
- package/dist/client/index.d.ts.map +1 -0
- package/dist/client/index.js +102 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client/types.d.ts +27 -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 +67 -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 +133 -0
- package/dist/component/_generated/server.d.ts.map +1 -0
- package/dist/component/_generated/server.js +80 -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 +102 -0
- package/dist/component/mutations.d.ts.map +1 -0
- package/dist/component/mutations.js +191 -0
- package/dist/component/mutations.js.map +1 -0
- package/dist/component/queries.d.ts +44 -0
- package/dist/component/queries.d.ts.map +1 -0
- package/dist/component/queries.js +75 -0
- package/dist/component/queries.js.map +1 -0
- package/dist/component/schema.d.ts +36 -0
- package/dist/component/schema.d.ts.map +1 -0
- package/dist/component/schema.js +27 -0
- package/dist/component/schema.js.map +1 -0
- package/dist/component/validators.d.ts +30 -0
- package/dist/component/validators.d.ts.map +1 -0
- package/dist/component/validators.js +25 -0
- package/dist/component/validators.js.map +1 -0
- package/dist/react/index.d.ts +55 -0
- package/dist/react/index.d.ts.map +1 -0
- package/dist/react/index.js +40 -0
- package/dist/react/index.js.map +1 -0
- package/dist/shared.d.ts +49 -0
- package/dist/shared.d.ts.map +1 -0
- package/dist/shared.js +52 -0
- package/dist/shared.js.map +1 -0
- package/package.json +115 -0
- package/src/client/index.ts +213 -0
- package/src/client/types.ts +29 -0
- package/src/component/_generated/api.ts +54 -0
- package/src/component/_generated/component.ts +90 -0
- package/src/component/_generated/dataModel.ts +60 -0
- package/src/component/_generated/server.ts +169 -0
- package/src/component/component.test.ts +115 -0
- package/src/component/convex.config.ts +9 -0
- package/src/component/mutations.ts +212 -0
- package/src/component/queries.ts +84 -0
- package/src/component/schema.ts +27 -0
- package/src/component/validators.ts +32 -0
- package/src/react/index.test.tsx +110 -0
- package/src/react/index.tsx +79 -0
- package/src/shared.ts +78 -0
- package/src/test.ts +12 -0
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
FunctionArgs,
|
|
3
|
+
FunctionReference,
|
|
4
|
+
FunctionReturnType,
|
|
5
|
+
} from "convex/server";
|
|
6
|
+
import type { SlugsOptions, SlugWriteResult } from "./types.js";
|
|
7
|
+
import {
|
|
8
|
+
DEFAULT_MAX_LENGTH,
|
|
9
|
+
DEFAULT_MIN_LENGTH,
|
|
10
|
+
DEFAULT_SCOPE,
|
|
11
|
+
normalizeSlug,
|
|
12
|
+
validateSlug,
|
|
13
|
+
} from "../shared.js";
|
|
14
|
+
import type { SlugConstraints } from "../shared.js";
|
|
15
|
+
|
|
16
|
+
/** Numeric length backstop forwarded to the component on each write. */
|
|
17
|
+
interface LengthBounds {
|
|
18
|
+
minLength: number;
|
|
19
|
+
maxLength: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* The slug component's function references, as exposed on the host via
|
|
24
|
+
* `components.slugs`.
|
|
25
|
+
*/
|
|
26
|
+
export interface SlugsComponent {
|
|
27
|
+
mutations: {
|
|
28
|
+
reserve: FunctionReference<
|
|
29
|
+
"mutation",
|
|
30
|
+
"internal",
|
|
31
|
+
{ scope: string; slug: string; resourceRef: string; bounds: LengthBounds },
|
|
32
|
+
SlugWriteResult
|
|
33
|
+
>;
|
|
34
|
+
release: FunctionReference<
|
|
35
|
+
"mutation",
|
|
36
|
+
"internal",
|
|
37
|
+
{ scope: string; slug: string },
|
|
38
|
+
null
|
|
39
|
+
>;
|
|
40
|
+
rename: FunctionReference<
|
|
41
|
+
"mutation",
|
|
42
|
+
"internal",
|
|
43
|
+
{ scope: string; fromSlug: string; toSlug: string; bounds: LengthBounds },
|
|
44
|
+
SlugWriteResult
|
|
45
|
+
>;
|
|
46
|
+
};
|
|
47
|
+
queries: {
|
|
48
|
+
resolve: FunctionReference<
|
|
49
|
+
"query",
|
|
50
|
+
"internal",
|
|
51
|
+
{ scope: string; slug: string },
|
|
52
|
+
string | null
|
|
53
|
+
>;
|
|
54
|
+
redirectFor: FunctionReference<
|
|
55
|
+
"query",
|
|
56
|
+
"internal",
|
|
57
|
+
{ scope: string; slug: string },
|
|
58
|
+
string | null
|
|
59
|
+
>;
|
|
60
|
+
slugForResource: FunctionReference<
|
|
61
|
+
"query",
|
|
62
|
+
"internal",
|
|
63
|
+
{ scope: string; resourceRef: string },
|
|
64
|
+
string | null
|
|
65
|
+
>;
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
interface RunQueryCtx {
|
|
70
|
+
runQuery<Q extends FunctionReference<"query", "internal">>(
|
|
71
|
+
reference: Q,
|
|
72
|
+
args: FunctionArgs<Q>,
|
|
73
|
+
): Promise<FunctionReturnType<Q>>;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
interface RunMutationCtx {
|
|
77
|
+
runMutation<M extends FunctionReference<"mutation", "internal">>(
|
|
78
|
+
reference: M,
|
|
79
|
+
args: FunctionArgs<M>,
|
|
80
|
+
): Promise<FunctionReturnType<M>>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Consumer-facing client for the unique slug / handle registry. The host owns
|
|
85
|
+
* meaning and auth; it passes an opaque `resourceRef` and an optional `scope`.
|
|
86
|
+
*
|
|
87
|
+
* Slug validation (length, charset, reserved words) runs here against the
|
|
88
|
+
* resolved {@link SlugsOptions}; the component re-guards length at the trust
|
|
89
|
+
* boundary. An invalid write short-circuits to a typed result with no round-trip.
|
|
90
|
+
*/
|
|
91
|
+
export class Slugs {
|
|
92
|
+
private readonly defaultScope: string;
|
|
93
|
+
private readonly foldCase: boolean;
|
|
94
|
+
private readonly constraints: SlugConstraints;
|
|
95
|
+
|
|
96
|
+
constructor(
|
|
97
|
+
private readonly component: SlugsComponent,
|
|
98
|
+
options: SlugsOptions = {},
|
|
99
|
+
) {
|
|
100
|
+
this.defaultScope = options.defaultScope ?? DEFAULT_SCOPE;
|
|
101
|
+
this.foldCase = options.foldCase ?? true;
|
|
102
|
+
this.constraints = {
|
|
103
|
+
minLength: options.minLength ?? DEFAULT_MIN_LENGTH,
|
|
104
|
+
maxLength: options.maxLength ?? DEFAULT_MAX_LENGTH,
|
|
105
|
+
pattern: options.pattern,
|
|
106
|
+
reservedWords: options.reservedWords ?? [],
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
private norm(slug: string): string {
|
|
111
|
+
return normalizeSlug(slug, this.foldCase);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
private scopeOf(scope: string | undefined): string {
|
|
115
|
+
return scope ?? this.defaultScope;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private get bounds(): LengthBounds {
|
|
119
|
+
return {
|
|
120
|
+
minLength: this.constraints.minLength,
|
|
121
|
+
maxLength: this.constraints.maxLength,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Reserve `slug` for `resourceRef`. Returns `{ ok: false, reason }` when the
|
|
127
|
+
* slug is invalid (`SLUG_INVALID`), reserved (`SLUG_RESERVED`), or already held
|
|
128
|
+
* (`SLUG_TAKEN`).
|
|
129
|
+
*/
|
|
130
|
+
reserve(
|
|
131
|
+
ctx: RunMutationCtx,
|
|
132
|
+
slug: string,
|
|
133
|
+
resourceRef: string,
|
|
134
|
+
scope?: string,
|
|
135
|
+
): Promise<SlugWriteResult> {
|
|
136
|
+
const normalized = this.norm(slug);
|
|
137
|
+
const reason = validateSlug(normalized, this.constraints);
|
|
138
|
+
if (reason !== null) {
|
|
139
|
+
return Promise.resolve({ ok: false, reason });
|
|
140
|
+
}
|
|
141
|
+
return ctx.runMutation(this.component.mutations.reserve, {
|
|
142
|
+
scope: this.scopeOf(scope),
|
|
143
|
+
slug: normalized,
|
|
144
|
+
resourceRef,
|
|
145
|
+
bounds: this.bounds,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Release `slug` (idempotent — releasing an unheld slug is a no-op). */
|
|
150
|
+
release(ctx: RunMutationCtx, slug: string, scope?: string): Promise<null> {
|
|
151
|
+
return ctx.runMutation(this.component.mutations.release, {
|
|
152
|
+
scope: this.scopeOf(scope),
|
|
153
|
+
slug: this.norm(slug),
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Rename `fromSlug` to `toSlug`, recording a redirect. The `toSlug` is
|
|
159
|
+
* validated; an invalid target short-circuits to `{ ok: false, reason }`.
|
|
160
|
+
*/
|
|
161
|
+
rename(
|
|
162
|
+
ctx: RunMutationCtx,
|
|
163
|
+
fromSlug: string,
|
|
164
|
+
toSlug: string,
|
|
165
|
+
scope?: string,
|
|
166
|
+
): Promise<SlugWriteResult> {
|
|
167
|
+
const normalizedTo = this.norm(toSlug);
|
|
168
|
+
const reason = validateSlug(normalizedTo, this.constraints);
|
|
169
|
+
if (reason !== null) {
|
|
170
|
+
return Promise.resolve({ ok: false, reason });
|
|
171
|
+
}
|
|
172
|
+
return ctx.runMutation(this.component.mutations.rename, {
|
|
173
|
+
scope: this.scopeOf(scope),
|
|
174
|
+
fromSlug: this.norm(fromSlug),
|
|
175
|
+
toSlug: normalizedTo,
|
|
176
|
+
bounds: this.bounds,
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Resolve `slug` to its `resourceRef`, or `null`. */
|
|
181
|
+
resolve(ctx: RunQueryCtx, slug: string, scope?: string): Promise<string | null> {
|
|
182
|
+
return ctx.runQuery(this.component.queries.resolve, {
|
|
183
|
+
scope: this.scopeOf(scope),
|
|
184
|
+
slug: this.norm(slug),
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/** Where a renamed `slug` now points (`toSlug`), or `null`. */
|
|
189
|
+
redirectFor(
|
|
190
|
+
ctx: RunQueryCtx,
|
|
191
|
+
slug: string,
|
|
192
|
+
scope?: string,
|
|
193
|
+
): Promise<string | null> {
|
|
194
|
+
return ctx.runQuery(this.component.queries.redirectFor, {
|
|
195
|
+
scope: this.scopeOf(scope),
|
|
196
|
+
slug: this.norm(slug),
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** The slug currently held by `resourceRef`, or `null`. */
|
|
201
|
+
slugForResource(
|
|
202
|
+
ctx: RunQueryCtx,
|
|
203
|
+
resourceRef: string,
|
|
204
|
+
scope?: string,
|
|
205
|
+
): Promise<string | null> {
|
|
206
|
+
return ctx.runQuery(this.component.queries.slugForResource, {
|
|
207
|
+
scope: this.scopeOf(scope),
|
|
208
|
+
resourceRef,
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
export type { SlugsOptions, SlugWriteResult };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/** Public TypeScript surface for the slugs client. */
|
|
2
|
+
|
|
3
|
+
import type { SlugReason } from "../shared.js";
|
|
4
|
+
|
|
5
|
+
/** Result of a slug write that may be rejected. */
|
|
6
|
+
export interface SlugWriteResult {
|
|
7
|
+
ok: boolean;
|
|
8
|
+
/**
|
|
9
|
+
* Stable code-tag when `ok` is false:
|
|
10
|
+
* `SLUG_TAKEN` | `SLUG_NOT_FOUND` | `SLUG_INVALID` | `SLUG_RESERVED`.
|
|
11
|
+
*/
|
|
12
|
+
reason?: SlugReason;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** Construction options for the {@link Slugs} client. */
|
|
16
|
+
export interface SlugsOptions {
|
|
17
|
+
/** Namespace applied when a call omits `scope`. Default `"global"`. */
|
|
18
|
+
defaultScope?: string;
|
|
19
|
+
/** Lowercase slugs before store/lookup (case-insensitive handles). Default `true`. */
|
|
20
|
+
foldCase?: boolean;
|
|
21
|
+
/** Minimum slug length after normalization. Default `1`. */
|
|
22
|
+
minLength?: number;
|
|
23
|
+
/** Maximum slug length after normalization. Default `256`. */
|
|
24
|
+
maxLength?: number;
|
|
25
|
+
/** Allowed charset; a slug failing it is rejected `SLUG_INVALID`. Default: none. */
|
|
26
|
+
pattern?: RegExp;
|
|
27
|
+
/** Slugs rejected with `SLUG_RESERVED` (matched on the normalized form). Default `[]`. */
|
|
28
|
+
reservedWords?: string[];
|
|
29
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `api` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type * as mutations from "../mutations.js";
|
|
12
|
+
import type * as queries from "../queries.js";
|
|
13
|
+
import type * as validators from "../validators.js";
|
|
14
|
+
|
|
15
|
+
import type {
|
|
16
|
+
ApiFromModules,
|
|
17
|
+
FilterApi,
|
|
18
|
+
FunctionReference,
|
|
19
|
+
} from "convex/server";
|
|
20
|
+
import { anyApi, componentsGeneric } from "convex/server";
|
|
21
|
+
|
|
22
|
+
const fullApi: ApiFromModules<{
|
|
23
|
+
mutations: typeof mutations;
|
|
24
|
+
queries: typeof queries;
|
|
25
|
+
validators: typeof validators;
|
|
26
|
+
}> = anyApi as any;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A utility for referencing Convex functions in your app's public API.
|
|
30
|
+
*
|
|
31
|
+
* Usage:
|
|
32
|
+
* ```js
|
|
33
|
+
* const myFunctionReference = api.myModule.myFunction;
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export const api: FilterApi<
|
|
37
|
+
typeof fullApi,
|
|
38
|
+
FunctionReference<any, "public">
|
|
39
|
+
> = anyApi as any;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* A utility for referencing Convex functions in your app's internal API.
|
|
43
|
+
*
|
|
44
|
+
* Usage:
|
|
45
|
+
* ```js
|
|
46
|
+
* const myFunctionReference = internal.myModule.myFunction;
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
export const internal: FilterApi<
|
|
50
|
+
typeof fullApi,
|
|
51
|
+
FunctionReference<any, "internal">
|
|
52
|
+
> = anyApi as any;
|
|
53
|
+
|
|
54
|
+
export const components = componentsGeneric() as unknown as {};
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
/**
|
|
3
|
+
* Generated `ComponentApi` utility.
|
|
4
|
+
*
|
|
5
|
+
* THIS CODE IS AUTOMATICALLY GENERATED.
|
|
6
|
+
*
|
|
7
|
+
* To regenerate, run `npx convex dev`.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import type { FunctionReference } from "convex/server";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* A utility for referencing a Convex component's exposed API.
|
|
15
|
+
*
|
|
16
|
+
* Useful when expecting a parameter like `components.myComponent`.
|
|
17
|
+
* Usage:
|
|
18
|
+
* ```ts
|
|
19
|
+
* async function myFunction(ctx: QueryCtx, component: ComponentApi) {
|
|
20
|
+
* return ctx.runQuery(component.someFile.someQuery, { ...args });
|
|
21
|
+
* }
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
export type ComponentApi<Name extends string | undefined = string | undefined> =
|
|
25
|
+
{
|
|
26
|
+
mutations: {
|
|
27
|
+
release: FunctionReference<
|
|
28
|
+
"mutation",
|
|
29
|
+
"internal",
|
|
30
|
+
{ scope: string; slug: string },
|
|
31
|
+
null,
|
|
32
|
+
Name
|
|
33
|
+
>;
|
|
34
|
+
rename: FunctionReference<
|
|
35
|
+
"mutation",
|
|
36
|
+
"internal",
|
|
37
|
+
{
|
|
38
|
+
bounds: { maxLength: number; minLength: number };
|
|
39
|
+
fromSlug: string;
|
|
40
|
+
scope: string;
|
|
41
|
+
toSlug: string;
|
|
42
|
+
},
|
|
43
|
+
{
|
|
44
|
+
ok: boolean;
|
|
45
|
+
reason?:
|
|
46
|
+
"SLUG_TAKEN" | "SLUG_NOT_FOUND" | "SLUG_INVALID" | "SLUG_RESERVED";
|
|
47
|
+
},
|
|
48
|
+
Name
|
|
49
|
+
>;
|
|
50
|
+
reserve: FunctionReference<
|
|
51
|
+
"mutation",
|
|
52
|
+
"internal",
|
|
53
|
+
{
|
|
54
|
+
bounds: { maxLength: number; minLength: number };
|
|
55
|
+
resourceRef: string;
|
|
56
|
+
scope: string;
|
|
57
|
+
slug: string;
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
ok: boolean;
|
|
61
|
+
reason?:
|
|
62
|
+
"SLUG_TAKEN" | "SLUG_NOT_FOUND" | "SLUG_INVALID" | "SLUG_RESERVED";
|
|
63
|
+
},
|
|
64
|
+
Name
|
|
65
|
+
>;
|
|
66
|
+
};
|
|
67
|
+
queries: {
|
|
68
|
+
redirectFor: FunctionReference<
|
|
69
|
+
"query",
|
|
70
|
+
"internal",
|
|
71
|
+
{ scope: string; slug: string },
|
|
72
|
+
null | string,
|
|
73
|
+
Name
|
|
74
|
+
>;
|
|
75
|
+
resolve: FunctionReference<
|
|
76
|
+
"query",
|
|
77
|
+
"internal",
|
|
78
|
+
{ scope: string; slug: string },
|
|
79
|
+
null | string,
|
|
80
|
+
Name
|
|
81
|
+
>;
|
|
82
|
+
slugForResource: FunctionReference<
|
|
83
|
+
"query",
|
|
84
|
+
"internal",
|
|
85
|
+
{ resourceRef: string; scope: string },
|
|
86
|
+
null | string,
|
|
87
|
+
Name
|
|
88
|
+
>;
|
|
89
|
+
};
|
|
90
|
+
};
|
|
@@ -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,169 @@
|
|
|
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
|
+
* Typesafe environment variables.
|
|
35
|
+
*
|
|
36
|
+
* This includes platform-provided env vars and any variables declared in
|
|
37
|
+
* `convex.config.ts`.
|
|
38
|
+
*/
|
|
39
|
+
type Env = {
|
|
40
|
+
readonly CONVEX_CLOUD_URL: string;
|
|
41
|
+
readonly CONVEX_SITE_URL: string;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Define a query in this Convex app's public API.
|
|
46
|
+
*
|
|
47
|
+
* This function will be allowed to read your Convex database and will be accessible from the client.
|
|
48
|
+
*
|
|
49
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
50
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
51
|
+
*/
|
|
52
|
+
export const query: QueryBuilder<DataModel, "public"> = queryGeneric;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Define a query that is only accessible from other Convex functions (but not from the client).
|
|
56
|
+
*
|
|
57
|
+
* This function will be allowed to read from your Convex database. It will not be accessible from the client.
|
|
58
|
+
*
|
|
59
|
+
* @param func - The query function. It receives a {@link QueryCtx} as its first argument.
|
|
60
|
+
* @returns The wrapped query. Include this as an `export` to name it and make it accessible.
|
|
61
|
+
*/
|
|
62
|
+
export const internalQuery: QueryBuilder<DataModel, "internal"> =
|
|
63
|
+
internalQueryGeneric;
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Define a mutation in this Convex app's public API.
|
|
67
|
+
*
|
|
68
|
+
* This function will be allowed to modify your Convex database and will be accessible from the client.
|
|
69
|
+
*
|
|
70
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
71
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
72
|
+
*/
|
|
73
|
+
export const mutation: MutationBuilder<DataModel, "public"> = mutationGeneric;
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Define a mutation that is only accessible from other Convex functions (but not from the client).
|
|
77
|
+
*
|
|
78
|
+
* This function will be allowed to modify your Convex database. It will not be accessible from the client.
|
|
79
|
+
*
|
|
80
|
+
* @param func - The mutation function. It receives a {@link MutationCtx} as its first argument.
|
|
81
|
+
* @returns The wrapped mutation. Include this as an `export` to name it and make it accessible.
|
|
82
|
+
*/
|
|
83
|
+
export const internalMutation: MutationBuilder<DataModel, "internal"> =
|
|
84
|
+
internalMutationGeneric;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Define an action in this Convex app's public API.
|
|
88
|
+
*
|
|
89
|
+
* An action is a function which can execute any JavaScript code, including non-deterministic
|
|
90
|
+
* code and code with side-effects, like calling third-party services.
|
|
91
|
+
* They can be run in Convex's JavaScript environment or in Node.js using the "use node" directive.
|
|
92
|
+
* They can interact with the database indirectly by calling queries and mutations using the {@link ActionCtx}.
|
|
93
|
+
*
|
|
94
|
+
* @param func - The action. It receives an {@link ActionCtx} as its first argument.
|
|
95
|
+
* @returns The wrapped action. Include this as an `export` to name it and make it accessible.
|
|
96
|
+
*/
|
|
97
|
+
export const action: ActionBuilder<DataModel, "public"> = actionGeneric;
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Define an action that is only accessible from other Convex functions (but not from the client).
|
|
101
|
+
*
|
|
102
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
|
|
103
|
+
* @returns The wrapped function. Include this as an `export` to name it and make it accessible.
|
|
104
|
+
*/
|
|
105
|
+
export const internalAction: ActionBuilder<DataModel, "internal"> =
|
|
106
|
+
internalActionGeneric;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Define an HTTP action.
|
|
110
|
+
*
|
|
111
|
+
* The wrapped function will be used to respond to HTTP requests received
|
|
112
|
+
* by a Convex deployment if the requests matches the path and method where
|
|
113
|
+
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
|
|
114
|
+
*
|
|
115
|
+
* @param func - The function. It receives an {@link ActionCtx} as its first argument
|
|
116
|
+
* and a Fetch API `Request` object as its second.
|
|
117
|
+
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
|
|
118
|
+
*/
|
|
119
|
+
export const httpAction: HttpActionBuilder = httpActionGeneric;
|
|
120
|
+
export const env: Env = (globalThis as unknown as { process: { env: Env } })
|
|
121
|
+
.process.env;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* A set of services for use within Convex query functions.
|
|
125
|
+
*
|
|
126
|
+
* The query context is passed as the first argument to any Convex query
|
|
127
|
+
* function run on the server.
|
|
128
|
+
*
|
|
129
|
+
* If you're using code generation, use the `QueryCtx` type in `convex/_generated/server.d.ts` instead.
|
|
130
|
+
*/
|
|
131
|
+
export type QueryCtx = GenericQueryCtx<DataModel>;
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* A set of services for use within Convex mutation functions.
|
|
135
|
+
*
|
|
136
|
+
* The mutation context is passed as the first argument to any Convex mutation
|
|
137
|
+
* function run on the server.
|
|
138
|
+
*
|
|
139
|
+
* If you're using code generation, use the `MutationCtx` type in `convex/_generated/server.d.ts` instead.
|
|
140
|
+
*/
|
|
141
|
+
export type MutationCtx = GenericMutationCtx<DataModel>;
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* A set of services for use within Convex action functions.
|
|
145
|
+
*
|
|
146
|
+
* The action context is passed as the first argument to any Convex action
|
|
147
|
+
* function run on the server.
|
|
148
|
+
*/
|
|
149
|
+
export type ActionCtx = GenericActionCtx<DataModel>;
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* An interface to read from the database within Convex query functions.
|
|
153
|
+
*
|
|
154
|
+
* The two entry points are {@link DatabaseReader.get}, which fetches a single
|
|
155
|
+
* document by its {@link Id}, or {@link DatabaseReader.query}, which starts
|
|
156
|
+
* building a query.
|
|
157
|
+
*/
|
|
158
|
+
export type DatabaseReader = GenericDatabaseReader<DataModel>;
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* An interface to read from and write to the database within Convex mutation
|
|
162
|
+
* functions.
|
|
163
|
+
*
|
|
164
|
+
* Convex guarantees that all writes within a single mutation are
|
|
165
|
+
* executed atomically, so you never have to worry about partial writes leaving
|
|
166
|
+
* your data in an inconsistent state. See [the Convex Guide](https://docs.convex.dev/understanding/convex-fundamentals/functions#atomicity-and-optimistic-concurrency-control)
|
|
167
|
+
* for the guarantees Convex provides your functions.
|
|
168
|
+
*/
|
|
169
|
+
export type DatabaseWriter = GenericDatabaseWriter<DataModel>;
|