create-z3 0.0.1

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.
Files changed (65) hide show
  1. package/dist/index.d.ts +1 -0
  2. package/dist/index.js +50 -0
  3. package/package.json +54 -0
  4. package/templates/tanstack-start/.env.example +45 -0
  5. package/templates/tanstack-start/.prettierignore +18 -0
  6. package/templates/tanstack-start/README.md +221 -0
  7. package/templates/tanstack-start/components.json +24 -0
  8. package/templates/tanstack-start/convex/_generated/api.d.ts +65 -0
  9. package/templates/tanstack-start/convex/_generated/api.js +23 -0
  10. package/templates/tanstack-start/convex/_generated/dataModel.d.ts +60 -0
  11. package/templates/tanstack-start/convex/_generated/server.d.ts +143 -0
  12. package/templates/tanstack-start/convex/_generated/server.js +93 -0
  13. package/templates/tanstack-start/convex/auth/adapter/index.ts +222 -0
  14. package/templates/tanstack-start/convex/auth/adapter/utils.ts +597 -0
  15. package/templates/tanstack-start/convex/auth/api.ts +8 -0
  16. package/templates/tanstack-start/convex/auth/config.ts +8 -0
  17. package/templates/tanstack-start/convex/auth/db.ts +299 -0
  18. package/templates/tanstack-start/convex/auth/index.ts +62 -0
  19. package/templates/tanstack-start/convex/auth/plugins/index.ts +7 -0
  20. package/templates/tanstack-start/convex/auth/sessions.ts +60 -0
  21. package/templates/tanstack-start/convex/http.ts +44 -0
  22. package/templates/tanstack-start/convex/schema.ts +69 -0
  23. package/templates/tanstack-start/eslint.config.js +77 -0
  24. package/templates/tanstack-start/package.json +74 -0
  25. package/templates/tanstack-start/prettier.config.js +35 -0
  26. package/templates/tanstack-start/public/favicon.ico +0 -0
  27. package/templates/tanstack-start/public/logo192.png +0 -0
  28. package/templates/tanstack-start/public/logo512.png +0 -0
  29. package/templates/tanstack-start/public/manifest.json +25 -0
  30. package/templates/tanstack-start/public/robots.txt +3 -0
  31. package/templates/tanstack-start/public/tanstack-circle-logo.png +0 -0
  32. package/templates/tanstack-start/public/tanstack-word-logo-white.svg +1 -0
  33. package/templates/tanstack-start/src/components/component-example.tsx +470 -0
  34. package/templates/tanstack-start/src/components/example.tsx +52 -0
  35. package/templates/tanstack-start/src/components/ui/alert-dialog.tsx +160 -0
  36. package/templates/tanstack-start/src/components/ui/badge.tsx +49 -0
  37. package/templates/tanstack-start/src/components/ui/button.tsx +58 -0
  38. package/templates/tanstack-start/src/components/ui/card.tsx +92 -0
  39. package/templates/tanstack-start/src/components/ui/combobox.tsx +271 -0
  40. package/templates/tanstack-start/src/components/ui/dropdown-menu.tsx +244 -0
  41. package/templates/tanstack-start/src/components/ui/field.tsx +222 -0
  42. package/templates/tanstack-start/src/components/ui/input-group.tsx +146 -0
  43. package/templates/tanstack-start/src/components/ui/input.tsx +20 -0
  44. package/templates/tanstack-start/src/components/ui/label.tsx +20 -0
  45. package/templates/tanstack-start/src/components/ui/select.tsx +189 -0
  46. package/templates/tanstack-start/src/components/ui/separator.tsx +19 -0
  47. package/templates/tanstack-start/src/components/ui/textarea.tsx +18 -0
  48. package/templates/tanstack-start/src/db/constants/auth.ts +41 -0
  49. package/templates/tanstack-start/src/db/constants/index.ts +8 -0
  50. package/templates/tanstack-start/src/env.ts +94 -0
  51. package/templates/tanstack-start/src/lib/auth/client.ts +18 -0
  52. package/templates/tanstack-start/src/lib/auth/server.ts +14 -0
  53. package/templates/tanstack-start/src/lib/utils.ts +6 -0
  54. package/templates/tanstack-start/src/logo.svg +12 -0
  55. package/templates/tanstack-start/src/providers.tsx +38 -0
  56. package/templates/tanstack-start/src/routeTree.gen.ts +127 -0
  57. package/templates/tanstack-start/src/router.tsx +41 -0
  58. package/templates/tanstack-start/src/routes/__root.tsx +92 -0
  59. package/templates/tanstack-start/src/routes/account/$accountView.tsx +15 -0
  60. package/templates/tanstack-start/src/routes/api/auth/$.tsx +11 -0
  61. package/templates/tanstack-start/src/routes/auth/$authView.tsx +15 -0
  62. package/templates/tanstack-start/src/routes/index.tsx +9 -0
  63. package/templates/tanstack-start/src/styles.css +156 -0
  64. package/templates/tanstack-start/tsconfig.json +42 -0
  65. package/templates/tanstack-start/vite.config.ts +32 -0
@@ -0,0 +1,597 @@
1
+ import type { BetterAuthDBSchema } from "better-auth/db";
2
+ import type {
3
+ DocumentByName,
4
+ GenericDataModel,
5
+ GenericQueryCtx,
6
+ PaginationOptions,
7
+ PaginationResult,
8
+ SchemaDefinition,
9
+ TableNamesInDataModel,
10
+ } from "convex/server";
11
+ import type { GenericId } from "convex/values";
12
+
13
+ import { asyncMap } from "convex-helpers";
14
+ import { mergedStream, stream } from "convex-helpers/server/stream";
15
+
16
+ export type QueryArgs = {
17
+ limit?: number;
18
+ model: string;
19
+ offset?: number;
20
+ select?: string[];
21
+ sortBy?: {
22
+ direction: "asc" | "desc";
23
+ field: string;
24
+ };
25
+ where?: WhereClause[];
26
+ };
27
+
28
+ export type WhereClause = {
29
+ connector?: "AND" | "OR";
30
+ field: string;
31
+ operator?:
32
+ | "contains"
33
+ | "ends_with"
34
+ | "eq"
35
+ | "gt"
36
+ | "gte"
37
+ | "in"
38
+ | "lt"
39
+ | "lte"
40
+ | "ne"
41
+ | "not_in"
42
+ | "starts_with";
43
+ value: boolean | null | number | number[] | string | string[];
44
+ };
45
+
46
+ export const isUniqueField = (
47
+ betterAuthSchema: BetterAuthDBSchema,
48
+ model: string,
49
+ field: string
50
+ ): boolean => {
51
+ const fields =
52
+ betterAuthSchema[model]?.fields;
53
+ if (!fields) {
54
+ return false;
55
+ }
56
+ return Object.entries(fields)
57
+ .filter(([, value]) => value.unique)
58
+ .map(([key]) => key)
59
+ .includes(field);
60
+ };
61
+
62
+ export const hasUniqueFields = (
63
+ betterAuthSchema: BetterAuthDBSchema,
64
+ model: string,
65
+ input: Record<string, any>
66
+ ): boolean => {
67
+ for (const field of Object.keys(input)) {
68
+ if (isUniqueField(betterAuthSchema, model, field)) {
69
+ return true;
70
+ }
71
+ }
72
+ return false;
73
+ };
74
+
75
+ export const findIndex = (
76
+ schema: SchemaDefinition<any, any>,
77
+ args: QueryArgs
78
+ ) => {
79
+ if (
80
+ (args.where?.length ?? 0) > 1 &&
81
+ args.where?.some((w) => w.connector === "OR")
82
+ ) {
83
+ throw new Error(
84
+ `OR connector not supported with multiple where statements in findIndex, split up the where statements before calling findIndex: ${JSON.stringify(args.where)}`
85
+ );
86
+ }
87
+ const where = args.where?.filter((w) => {
88
+ return (
89
+ (!w.operator ||
90
+ ["eq", "gt", "gte", "in", "lt", "lte", "not_in"].includes(
91
+ w.operator
92
+ )) &&
93
+ w.field !== "_id"
94
+ );
95
+ });
96
+ if (!where?.length && !args.sortBy) {
97
+ return;
98
+ }
99
+ const lowerBounds =
100
+ where?.filter((w) => w.operator === "lt" || w.operator === "lte") ?? [];
101
+ if (lowerBounds.length > 1) {
102
+ throw new Error(
103
+ `cannot have more than one lower bound where clause: ${JSON.stringify(where)}`
104
+ );
105
+ }
106
+ const upperBounds =
107
+ where?.filter((w) => w.operator === "gt" || w.operator === "gte") ?? [];
108
+ if (upperBounds.length > 1) {
109
+ throw new Error(
110
+ `cannot have more than one upper bound where clause: ${JSON.stringify(where)}`
111
+ );
112
+ }
113
+ const lowerBound = lowerBounds[0];
114
+ const upperBound = upperBounds[0];
115
+ if (lowerBound && upperBound && lowerBound.field !== upperBound.field) {
116
+ throw new Error(
117
+ `lower bound and upper bound must have the same field: ${JSON.stringify(where)}`
118
+ );
119
+ }
120
+ const boundField = lowerBound?.field ?? upperBound?.field;
121
+ if (
122
+ boundField &&
123
+ where?.some(
124
+ (w) => w.field === boundField && w !== lowerBound && w !== upperBound
125
+ )
126
+ ) {
127
+ throw new Error(
128
+ `too many where clauses on the bound field: ${JSON.stringify(where)}`
129
+ );
130
+ }
131
+ const indexEqFields =
132
+ where
133
+ ?.filter((w) => !w.operator || w.operator === "eq")
134
+ .sort((a, b) => {
135
+ return a.field.localeCompare(b.field);
136
+ })
137
+ .map((w) => [w.field, w.value]) ?? [];
138
+ if (!indexEqFields?.length && !boundField && !args.sortBy) {
139
+ return;
140
+ }
141
+ const table = schema.tables[args.model as keyof typeof schema.tables];
142
+ if (!table) {
143
+ throw new Error(`Table ${args.model} not found`);
144
+ }
145
+ const indexes = table[" indexes"]();
146
+ const sortField = args.sortBy?.field;
147
+
148
+ // We internally use _creationTime in place of Better Auth's createdAt
149
+ const indexFields = indexEqFields
150
+ .map(([field]) => field)
151
+ .concat(
152
+ boundField && boundField !== "createdAt"
153
+ ? `${indexEqFields.length ? "_" : ""}${boundField}`
154
+ : ""
155
+ )
156
+ .concat(
157
+ sortField && sortField !== "createdAt" && boundField !== sortField
158
+ ? `${indexEqFields.length || boundField ? "_" : ""}${sortField}`
159
+ : ""
160
+ )
161
+ .filter(Boolean);
162
+ if (!indexFields.length && !boundField && !sortField) {
163
+ return;
164
+ }
165
+ // Use the built in _creationTime index if bounding or sorting by createdAt
166
+ // with no other fields
167
+ const index = !indexFields.length
168
+ ? {
169
+ fields: [],
170
+ indexDescriptor: "by_creation_time",
171
+ }
172
+ : indexes.find(({ fields }: { fields: string[] }) => {
173
+ const fieldsMatch = indexFields.every(
174
+ (field, idx) => field === fields[idx]
175
+ );
176
+ // If sorting by createdAt, no intermediate fields can be on the index
177
+ // as they may override the createdAt sort order.
178
+ const boundFieldMatch =
179
+ boundField === "createdAt" || sortField === "createdAt"
180
+ ? indexFields.length === fields.length
181
+ : true;
182
+ return fieldsMatch && boundFieldMatch;
183
+ });
184
+ if (!index) {
185
+ return { indexFields };
186
+ }
187
+ return {
188
+ boundField,
189
+ index: {
190
+ fields: [...index.fields, "_creationTime"],
191
+ indexDescriptor: index.indexDescriptor,
192
+ },
193
+ sortField,
194
+ values: {
195
+ eq: indexEqFields.map(([, value]) => value),
196
+ gt: upperBound?.operator === "gt" ? upperBound.value : undefined,
197
+ gte: upperBound?.operator === "gte" ? upperBound.value : undefined,
198
+ lt: lowerBound?.operator === "lt" ? lowerBound.value : undefined,
199
+ lte: lowerBound?.operator === "lte" ? lowerBound.value : undefined,
200
+ },
201
+ };
202
+ };
203
+
204
+ export const checkUniqueFields = async <
205
+ Schema extends SchemaDefinition<any, any>,
206
+ >(
207
+ ctx: GenericQueryCtx<GenericDataModel>,
208
+ schema: Schema,
209
+ betterAuthSchema: BetterAuthDBSchema,
210
+ table: string,
211
+ input: Record<string, any>,
212
+ doc?: Record<string, any>
213
+ ) => {
214
+ if (!hasUniqueFields(betterAuthSchema, table, input)) {
215
+ return;
216
+ }
217
+ for (const field of Object.keys(input)) {
218
+ if (!isUniqueField(betterAuthSchema, table, field)) {
219
+ continue;
220
+ }
221
+ const { index } =
222
+ findIndex(schema, {
223
+ model: table,
224
+ where: [
225
+ { field, operator: "eq", value: input[field] },
226
+ ],
227
+ }) ?? {};
228
+ if (!index) {
229
+ throw new Error(`No index found for ${table}.${field}`);
230
+ }
231
+ const existingDoc = await ctx.db
232
+ .query(table as any)
233
+ .withIndex(index.indexDescriptor, (q) =>
234
+ q.eq(field, input[field])
235
+ )
236
+ .unique();
237
+ if (existingDoc && existingDoc._id !== doc?._id) {
238
+ throw new Error(`${table} ${field} already exists`);
239
+ }
240
+ }
241
+ };
242
+
243
+ export const selectFields = <
244
+ T extends TableNamesInDataModel<GenericDataModel>,
245
+ D extends DocumentByName<GenericDataModel, T>,
246
+ >(
247
+ doc: D | null,
248
+ select?: string[]
249
+ ): D | null => {
250
+ if (!doc) {
251
+ return null;
252
+ }
253
+ if (!select?.length) {
254
+ return doc;
255
+ }
256
+ return select.reduce((acc, field) => {
257
+ (acc as any)[field] = doc[field];
258
+ return acc;
259
+ }, {} as D);
260
+ };
261
+
262
+ export const filterByWhere = <
263
+ T extends TableNamesInDataModel<GenericDataModel>,
264
+ D extends DocumentByName<GenericDataModel, T>,
265
+ >(
266
+ doc: D | null,
267
+ where?: WhereClause[],
268
+ // Optionally filter which where clauses to apply.
269
+ filterWhere?: (w: WhereClause) => any
270
+ ): boolean => {
271
+ if (!doc) {
272
+ return false;
273
+ }
274
+ for (const w of where ?? []) {
275
+ if (filterWhere && !filterWhere(w)) {
276
+ continue;
277
+ }
278
+ const value = doc[w.field as keyof typeof doc] as WhereClause["value"];
279
+ const isLessThan = (val: typeof value, wVal: typeof w.value) => {
280
+ if (!wVal) {
281
+ return false;
282
+ }
283
+ if (!val) {
284
+ return true;
285
+ }
286
+ return val < wVal;
287
+ };
288
+ const isGreaterThan = (val: typeof value, wVal: typeof w.value) => {
289
+ if (!val) {
290
+ return false;
291
+ }
292
+ if (!wVal) {
293
+ return true;
294
+ }
295
+ return val > wVal;
296
+ };
297
+ const filter = (w: WhereClause) => {
298
+ switch (w.operator) {
299
+ case "contains": {
300
+ return typeof value === "string" && value.includes(w.value as string);
301
+ }
302
+ case "ends_with": {
303
+ return typeof value === "string" && value.endsWith(w.value as string);
304
+ }
305
+ case "eq":
306
+ case undefined: {
307
+ return value === w.value;
308
+ }
309
+ case "gt": {
310
+ return isGreaterThan(value, w.value);
311
+ }
312
+ case "gte": {
313
+ return value === w.value || isGreaterThan(value, w.value);
314
+ }
315
+ case "in": {
316
+ return Array.isArray(w.value) && (w.value as any[]).includes(value);
317
+ }
318
+ case "lt": {
319
+ return isLessThan(value, w.value);
320
+ }
321
+ case "lte": {
322
+ return value === w.value || isLessThan(value, w.value);
323
+ }
324
+ case "ne": {
325
+ return value !== w.value;
326
+ }
327
+ case "not_in": {
328
+ return Array.isArray(w.value) && !(w.value as any[]).includes(value);
329
+ }
330
+ case "starts_with": {
331
+ return (
332
+ typeof value === "string" && value.startsWith(w.value as string)
333
+ );
334
+ }
335
+ }
336
+ };
337
+ if (!filter(w)) {
338
+ return false;
339
+ }
340
+ }
341
+ return true;
342
+ };
343
+
344
+ const generateQuery = (
345
+ ctx: GenericQueryCtx<GenericDataModel>,
346
+ schema: SchemaDefinition<any, any>,
347
+ args: QueryArgs
348
+ ) => {
349
+ const { boundField, index, indexFields, values } =
350
+ findIndex(schema, args) ?? {};
351
+ const query = stream(ctx.db as any, schema).query(args.model as any);
352
+ const hasValues =
353
+ values?.eq?.length ??
354
+ values?.lt ??
355
+ values?.lte ??
356
+ values?.gt ??
357
+ values?.gte;
358
+ const indexedQuery =
359
+ index && index.indexDescriptor !== "by_creation_time"
360
+ ? query.withIndex(
361
+ index.indexDescriptor,
362
+ hasValues
363
+ ? (q: any) => {
364
+ for (const [idx, value] of (values?.eq ?? []).entries()) {
365
+ q = q.eq(index.fields[idx], value);
366
+ }
367
+ if (values?.lt) {
368
+ q = q.lt(boundField, values.lt);
369
+ }
370
+ if (values?.lte) {
371
+ q = q.lte(boundField, values.lte);
372
+ }
373
+ if (values?.gt) {
374
+ q = q.gt(boundField, values.gt);
375
+ }
376
+ if (values?.gte) {
377
+ q = q.gte(boundField, values.gte);
378
+ }
379
+ return q;
380
+ }
381
+ : undefined
382
+ )
383
+ : query;
384
+ const orderedQuery = args.sortBy
385
+ ? indexedQuery.order(args.sortBy.direction === "desc" ? "desc" : "asc")
386
+ : indexedQuery;
387
+ const filteredQuery = orderedQuery.filterWith(async (doc) => {
388
+ if (!index && indexFields?.length) {
389
+ console.warn(
390
+ `Querying without an index on table "${args.model}".\n` +
391
+ `This can cause performance issues, and may hit the document read limit.\n` +
392
+ `To fix, add an index that begins with the following fields in order:\n` +
393
+ `[${indexFields.join(", ")}]`
394
+ );
395
+ // No index, handle all where clauses statically.
396
+ return filterByWhere(doc, args.where);
397
+ }
398
+ return filterByWhere(
399
+ doc,
400
+ args.where,
401
+ // Index used for all eq and range clauses, apply remaining clauses
402
+ // incompatible with Convex statically.
403
+ (w) =>
404
+ w.operator &&
405
+ ["contains", "ends_with", "ne", "not_in", "starts_with"].includes(
406
+ w.operator
407
+ )
408
+ );
409
+ });
410
+ return filteredQuery;
411
+ };
412
+
413
+ export const paginate = async <
414
+ Doc extends DocumentByName<GenericDataModel, T>,
415
+ T extends TableNamesInDataModel<GenericDataModel>,
416
+ >(
417
+ ctx: GenericQueryCtx<GenericDataModel>,
418
+ schema: SchemaDefinition<any, any>,
419
+ betterAuthSchema: BetterAuthDBSchema,
420
+ args: QueryArgs & {
421
+ paginationOpts: PaginationOptions;
422
+ }
423
+ ): Promise<PaginationResult<Doc>> => {
424
+ if (args.offset) {
425
+ throw new Error(`offset not supported: ${JSON.stringify(args.offset)}`);
426
+ }
427
+ if (args.where?.some((w) => w.connector === "OR") && args.where?.length > 1) {
428
+ throw new Error(
429
+ `OR connector not supported with multiple where statements in paginate, split up the where statements before calling paginate: ${JSON.stringify(args.where)}`
430
+ );
431
+ }
432
+ if (
433
+ args.where?.some(
434
+ (w) =>
435
+ w.field === "_id" &&
436
+ w.operator &&
437
+ !["eq", "in", "not_in"].includes(w.operator)
438
+ )
439
+ ) {
440
+ throw new Error(
441
+ `_id can only be used with eq, in, or not_in operator: ${JSON.stringify(args.where)}`
442
+ );
443
+ }
444
+ // If any where clause is "eq" (or missing operator) on a unique field,
445
+ // we can only return a single document, so we get it and use any other
446
+ // where clauses as static filters.
447
+ const uniqueWhere = args.where?.find(
448
+ (w) =>
449
+ (!w.operator || w.operator === "eq") &&
450
+ (isUniqueField(betterAuthSchema, args.model, w.field) ||
451
+ w.field === "_id")
452
+ );
453
+ if (uniqueWhere) {
454
+ const { index } =
455
+ findIndex(schema, {
456
+ model: args.model,
457
+ where: [uniqueWhere],
458
+ }) ?? {};
459
+
460
+ let doc;
461
+ if (uniqueWhere.field === "_id") {
462
+ doc = await ctx.db.get(uniqueWhere.value as GenericId<T>);
463
+ } else if (index?.indexDescriptor) {
464
+ doc = await ctx.db
465
+ .query(args.model as any)
466
+ .withIndex(index.indexDescriptor, (q) =>
467
+ q.eq(index.fields[0], uniqueWhere.value)
468
+ )
469
+ .unique();
470
+ } else {
471
+ // No index found - fall back to collect() for unique field lookup
472
+ const results = await ctx.db
473
+ .query(args.model as any)
474
+ .collect();
475
+ doc = results.find((d: any) => d[uniqueWhere.field] === uniqueWhere.value) ?? null;
476
+ }
477
+
478
+ // Apply all other clauses as static filters to our 0 or 1 result.
479
+ if (filterByWhere(doc, args.where, (w) => w !== uniqueWhere)) {
480
+ return {
481
+ continueCursor: "",
482
+ isDone: true,
483
+ page: [selectFields(doc, args.select)].filter(Boolean) as Doc[],
484
+ };
485
+ }
486
+ return {
487
+ continueCursor: "",
488
+ isDone: true,
489
+ page: [],
490
+ };
491
+ }
492
+
493
+ const paginationOpts = {
494
+ ...args.paginationOpts,
495
+ // If maximumRowsRead is not at least 1 higher than numItems, bad cursors
496
+ // and incorrect paging will result (at least with convex-test).
497
+ maximumRowsRead: Math.max((args.paginationOpts.numItems ?? 0) + 1, 200),
498
+ };
499
+
500
+ // Large queries using "in" clause will crash, but these are only currently
501
+ // possible with the organization plugin listing all members with a high
502
+ // limit. For cases like this we need to create proper convex queries in
503
+ // the component as an alternative to using Better Auth api's.
504
+ const inWhere = args.where?.find((w) => w.operator === "in");
505
+ if (inWhere) {
506
+ if (!Array.isArray(inWhere.value)) {
507
+ throw new Error("in clause value must be an array");
508
+ }
509
+ // For ids, just use asyncMap + .get()
510
+ if (inWhere.field === "_id") {
511
+ const docs = await asyncMap(inWhere.value as any[], async (value) => {
512
+ return ctx.db.get(value as GenericId<T>);
513
+ });
514
+ const filteredDocs = docs
515
+ .flatMap((doc) => (doc ? [doc] : []))
516
+ .filter((doc) => filterByWhere(doc, args.where, (w) => w !== inWhere));
517
+
518
+ return {
519
+ continueCursor: "",
520
+ isDone: true,
521
+ page: filteredDocs.sort((a, b) => {
522
+ if (args.sortBy?.field === "createdAt") {
523
+ return args.sortBy.direction === "asc"
524
+ ? (a._creationTime as number) - (b._creationTime as number)
525
+ : (b._creationTime as number) - (a._creationTime as number);
526
+ }
527
+ if (args.sortBy) {
528
+ const aValue = a[args.sortBy.field];
529
+ const bValue = b[args.sortBy.field];
530
+ if (aValue === bValue) {
531
+ return 0;
532
+ }
533
+ return args.sortBy.direction === "asc"
534
+ ? aValue! > bValue!
535
+ ? 1
536
+ : -1
537
+ : aValue! > bValue!
538
+ ? -1
539
+ : 1;
540
+ }
541
+ return 0;
542
+ }) as Doc[],
543
+ };
544
+ }
545
+ const streams = inWhere.value.map((value) => {
546
+ return generateQuery(ctx, schema, {
547
+ ...args,
548
+ where: args.where?.map((w) => {
549
+ if (w === inWhere) {
550
+ return { ...w, operator: "eq" as const, value };
551
+ }
552
+ return w;
553
+ }),
554
+ });
555
+ });
556
+ const result = await mergedStream(
557
+ streams,
558
+ [
559
+ args.sortBy?.field !== "createdAt" && args.sortBy?.field,
560
+ "_creationTime",
561
+ ].flatMap((f) => (f ? [f] : []))
562
+ ).paginate(paginationOpts);
563
+ return {
564
+ ...result,
565
+ page: await asyncMap(result.page, (doc) =>
566
+ selectFields(doc, args.select)
567
+ ),
568
+ };
569
+ }
570
+
571
+ const query = generateQuery(ctx, schema, args);
572
+ const result = await query.paginate(paginationOpts);
573
+ return {
574
+ ...result,
575
+ page: await asyncMap(result.page, (doc) => selectFields(doc, args.select)),
576
+ };
577
+ };
578
+
579
+ export const listOne = async <
580
+ Doc extends DocumentByName<GenericDataModel, T>,
581
+ T extends TableNamesInDataModel<GenericDataModel>,
582
+ >(
583
+ ctx: GenericQueryCtx<GenericDataModel>,
584
+ schema: SchemaDefinition<any, any>,
585
+ betterAuthSchema: BetterAuthDBSchema,
586
+ args: QueryArgs
587
+ ): Promise<Doc | null> => {
588
+ return (
589
+ await paginate(ctx, schema, betterAuthSchema, {
590
+ ...args,
591
+ paginationOpts: {
592
+ cursor: null,
593
+ numItems: 1,
594
+ },
595
+ })
596
+ ).page[0] as Doc | null;
597
+ };
@@ -0,0 +1,8 @@
1
+ import { query } from "../_generated/server"
2
+
3
+ export const identifyCurrentUser = query({
4
+ args: {},
5
+ handler: async (ctx) => {
6
+ return ctx.auth.getUserIdentity()
7
+ }
8
+ })
@@ -0,0 +1,8 @@
1
+ import { getAuthConfigProvider } from "@convex-dev/better-auth/auth-config"
2
+ import type { AuthConfig } from "convex/server"
3
+
4
+ const config = {
5
+ providers: [getAuthConfigProvider()]
6
+ } satisfies AuthConfig
7
+
8
+ export default config