@convex-dev/better-auth 0.7.13 → 0.8.0-alpha.0

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.
@@ -45,37 +45,45 @@ export const getAdapter = (t: ReturnType<typeof convexTest>) => async () => {
45
45
  } satisfies Adapter;
46
46
  };
47
47
 
48
- describe("convex adapter", async () => {
48
+ describe("Better Auth Adapter Tests", async () => {
49
49
  const _t = convexTest(schema, import.meta.glob("../component/**/*.*s"));
50
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
51
+ const activeTests = [
52
+ "CREATE_MODEL",
53
+ "CREATE_MODEL_SHOULD_ALWAYS_RETURN_AN_ID",
54
+ "FIND_MODEL",
55
+ "FIND_MODEL_WITHOUT_ID",
56
+ "FIND_MODEL_WITH_SELECT",
57
+ "FIND_MODEL_WITH_MODIFIED_FIELD_NAME",
58
+ "UPDATE_MODEL",
59
+ "SHOULD_FIND_MANY",
60
+ "SHOULD_FIND_MANY_WITH_WHERE",
61
+ "SHOULD_FIND_MANY_WITH_OPERATORS",
62
+ "SHOULD_WORK_WITH_REFERENCE_FIELDS",
63
+ "SHOULD_FIND_MANY_WITH_SORT_BY",
64
+ "SHOULD_FIND_MANY_WITH_LIMIT",
65
+ "SHOULD_UPDATE_WITH_MULTIPLE_WHERE",
66
+ "DELETE_MODEL",
67
+ "SHOULD_DELETE_MANY",
68
+ "SHOULD_NOT_THROW_ON_DELETE_RECORD_NOT_FOUND",
69
+ "SHOULD_NOT_THROW_ON_RECORD_NOT_FOUND",
70
+ "SHOULD_FIND_MANY_WITH_CONTAINS_OPERATOR",
71
+ "SHOULD_SEARCH_USERS_WITH_STARTS_WITH",
72
+ "SHOULD_SEARCH_USERS_WITH_ENDS_WITH",
73
+ ];
74
+ const inactiveTests = [
75
+ // not supported
76
+ "SHOULD_FIND_MANY_WITH_OFFSET",
77
+ "SHOULD_PREFER_GENERATE_ID_IF_PROVIDED",
78
+ ];
79
+
50
80
  await runAdapterTest({
51
81
  getAdapter: getAdapter(_t),
52
- disableTests: {
53
- CREATE_MODEL: false,
54
- CREATE_MODEL_SHOULD_ALWAYS_RETURN_AN_ID: false,
55
- FIND_MODEL: false,
56
- FIND_MODEL_WITHOUT_ID: false,
57
- FIND_MODEL_WITH_SELECT: false,
58
- FIND_MODEL_WITH_MODIFIED_FIELD_NAME: false,
59
- UPDATE_MODEL: false,
60
- SHOULD_FIND_MANY: false,
61
- SHOULD_FIND_MANY_WITH_WHERE: false,
62
- SHOULD_FIND_MANY_WITH_OPERATORS: false,
63
- SHOULD_WORK_WITH_REFERENCE_FIELDS: false,
64
- SHOULD_FIND_MANY_WITH_SORT_BY: false,
65
- SHOULD_FIND_MANY_WITH_LIMIT: false,
66
- SHOULD_FIND_MANY_WITH_OFFSET: true, // disabled for now
67
- SHOULD_UPDATE_WITH_MULTIPLE_WHERE: false,
68
- DELETE_MODEL: false,
69
- SHOULD_DELETE_MANY: false,
70
- SHOULD_NOT_THROW_ON_DELETE_RECORD_NOT_FOUND: false,
71
- SHOULD_NOT_THROW_ON_RECORD_NOT_FOUND: false,
72
- SHOULD_FIND_MANY_WITH_CONTAINS_OPERATOR: false,
73
- SHOULD_SEARCH_USERS_WITH_STARTS_WITH: false,
74
- SHOULD_SEARCH_USERS_WITH_ENDS_WITH: false,
75
- SHOULD_PREFER_GENERATE_ID_IF_PROVIDED: true,
76
- },
82
+ disableTests: Object.fromEntries(inactiveTests.map((test) => [test, true])),
77
83
  });
84
+ });
78
85
 
86
+ describe("Convex Adapter Tests", async () => {
79
87
  test("should handle lone range operators", async () => {
80
88
  const t = convexTest(schema, import.meta.glob("../component/**/*.*s"));
81
89
  const adapter = await getAdapter(t)();
@@ -39,8 +39,8 @@ export const {
39
39
  isAuthenticated,
40
40
  } = betterAuthComponent.createAuthFunctions({
41
41
  onCreateUser: async () => {
42
- // noop
43
- // not required for the adapter test
42
+ // return a random string as a userId
43
+ return Math.random().toString(36).substring(2, 15);
44
44
  },
45
45
  });
46
46
 
@@ -252,13 +252,34 @@ const checkUniqueFields = async (
252
252
  }
253
253
  };
254
254
 
255
- const selectFields = <T extends TableNames, D extends Doc<T>>(
255
+ // This handles basic select (stripping out the other fields if there
256
+ // is a select arg), but also swaps in the app userId for any user id
257
+ // reference fields.
258
+ const selectFields = async <T extends TableNames, D extends Doc<T>>(
259
+ ctx: QueryCtx,
256
260
  doc: D | null,
257
261
  select?: string[]
258
262
  ) => {
259
263
  if (!doc) {
260
264
  return null;
261
265
  }
266
+ const modelSpecialFields =
267
+ specialFields[doc._table as keyof typeof specialFields];
268
+ const userIdField = modelSpecialFields
269
+ ? Object.entries(modelSpecialFields).find(
270
+ ([_, field]) => field.references?.model === "user"
271
+ )
272
+ : undefined;
273
+ if (userIdField) {
274
+ const userIdFieldName = userIdField[0] as keyof typeof doc;
275
+ const userId = doc[userIdFieldName];
276
+ if (userId) {
277
+ const user = await ctx.db.get(userId as unknown as Id<"user">);
278
+ if (user) {
279
+ (doc as any)[userIdFieldName] = user.userId;
280
+ }
281
+ }
282
+ }
262
283
  if (!select?.length) {
263
284
  return doc;
264
285
  }
@@ -458,7 +479,7 @@ const paginate = async (
458
479
 
459
480
  if (filterByWhere(doc, args.where, (w) => w !== uniqueWhere)) {
460
481
  return {
461
- page: [selectFields(doc, args.select)].filter(Boolean),
482
+ page: [await selectFields(ctx, doc, args.select)].filter(Boolean),
462
483
  isDone: true,
463
484
  continueCursor: "",
464
485
  };
@@ -552,7 +573,9 @@ const paginate = async (
552
573
  .paginate(paginationOpts);
553
574
  return {
554
575
  ...result,
555
- page: result.page.map((doc) => selectFields(doc, args.select)),
576
+ page: await asyncMap(result.page, (doc) =>
577
+ selectFields(ctx, doc, args.select)
578
+ ),
556
579
  };
557
580
  }
558
581
 
@@ -560,7 +583,9 @@ const paginate = async (
560
583
  const result = await query.paginate(paginationOpts);
561
584
  return {
562
585
  ...result,
563
- page: result.page.map((doc) => selectFields(doc, args.select)),
586
+ page: await asyncMap(result.page, (doc) =>
587
+ selectFields(ctx, doc, args.select)
588
+ ),
564
589
  };
565
590
  };
566
591
 
@@ -610,7 +635,7 @@ export const create = mutation({
610
635
  export const findOne = query({
611
636
  args: adapterArgsValidator,
612
637
  handler: async (ctx, args) => {
613
- return await listOne(ctx, args);
638
+ return listOne(ctx, args);
614
639
  },
615
640
  });
616
641