@onmax/nuxt-better-auth 0.0.2-alpha.21 → 0.0.2-alpha.23

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.
@@ -1,213 +0,0 @@
1
- import { createAdapterFactory } from "better-auth/adapters";
2
- import { ConvexHttpClient } from "convex/browser";
3
- function parseWhere(where) {
4
- if (!where)
5
- return [];
6
- const whereArray = Array.isArray(where) ? where : [where];
7
- return whereArray.map((w) => {
8
- if (w.value instanceof Date)
9
- return { ...w, value: w.value.getTime() };
10
- return w;
11
- });
12
- }
13
- async function handlePagination(next, { limit } = {}) {
14
- const state = { isDone: false, cursor: null, docs: [], count: 0 };
15
- const onResult = (result) => {
16
- state.cursor = result.pageStatus === "SplitRecommended" || result.pageStatus === "SplitRequired" ? result.splitCursor ?? result.continueCursor : result.continueCursor;
17
- if (result.page) {
18
- state.docs.push(...result.page);
19
- state.isDone = limit && state.docs.length >= limit || result.isDone;
20
- return;
21
- }
22
- if (result.count) {
23
- state.count += result.count;
24
- state.isDone = limit && state.count >= limit || result.isDone;
25
- return;
26
- }
27
- state.isDone = result.isDone;
28
- };
29
- do {
30
- const result = await next({
31
- paginationOpts: {
32
- numItems: Math.min(200, (limit ?? 200) - state.docs.length, 200),
33
- cursor: state.cursor
34
- }
35
- });
36
- onResult(result);
37
- } while (!state.isDone);
38
- return state;
39
- }
40
- export function createConvexHttpAdapter(options) {
41
- if (!options.url.startsWith("https://") || !options.url.includes(".convex.")) {
42
- throw new Error(`Invalid Convex URL: ${options.url}. Expected format: https://your-app.convex.cloud`);
43
- }
44
- const client = new ConvexHttpClient(options.url);
45
- return createAdapterFactory({
46
- config: {
47
- adapterId: "convex-http",
48
- adapterName: "Convex HTTP Adapter",
49
- debugLogs: options.debugLogs ?? false,
50
- disableIdGeneration: true,
51
- transaction: false,
52
- supportsNumericIds: false,
53
- supportsJSON: false,
54
- supportsDates: false,
55
- supportsArrays: true,
56
- usePlural: false,
57
- mapKeysTransformInput: { id: "_id" },
58
- mapKeysTransformOutput: { _id: "id" },
59
- customTransformInput: ({ data, fieldAttributes }) => {
60
- if (data && fieldAttributes.type === "date")
61
- return new Date(data).getTime();
62
- return data;
63
- },
64
- customTransformOutput: ({ data, fieldAttributes }) => {
65
- if (data && fieldAttributes.type === "date")
66
- return new Date(data).getTime();
67
- return data;
68
- }
69
- },
70
- adapter: ({ options: authOptions }) => {
71
- authOptions.telemetry = { enabled: false };
72
- return {
73
- id: "convex-http",
74
- create: async ({ model, data, select }) => {
75
- return client.mutation(options.api.create, {
76
- input: { model, data },
77
- select
78
- });
79
- },
80
- findOne: async (data) => {
81
- if (data.where?.every((w) => w.connector === "OR")) {
82
- for (const w of data.where) {
83
- const result = await client.query(options.api.findOne, {
84
- ...data,
85
- model: data.model,
86
- where: parseWhere(w)
87
- });
88
- if (result)
89
- return result;
90
- }
91
- return null;
92
- }
93
- return client.query(options.api.findOne, {
94
- ...data,
95
- model: data.model,
96
- where: parseWhere(data.where)
97
- });
98
- },
99
- findMany: async (data) => {
100
- if (data.offset)
101
- throw new Error("offset not supported");
102
- if (data.where?.some((w) => w.connector === "OR")) {
103
- const results = await Promise.all(
104
- data.where.map(
105
- async (w) => handlePagination(async ({ paginationOpts }) => {
106
- return client.query(options.api.findMany, {
107
- ...data,
108
- model: data.model,
109
- where: parseWhere(w),
110
- paginationOpts
111
- });
112
- }, { limit: data.limit })
113
- )
114
- );
115
- const allDocs = results.flatMap((r) => r.docs);
116
- const uniqueDocs = [...new Map(allDocs.map((d) => [d._id, d])).values()];
117
- if (data.sortBy) {
118
- return uniqueDocs.sort((a, b) => {
119
- const aVal = a[data.sortBy.field];
120
- const bVal = b[data.sortBy.field];
121
- const cmp = aVal < bVal ? -1 : aVal > bVal ? 1 : 0;
122
- return data.sortBy.direction === "asc" ? cmp : -cmp;
123
- });
124
- }
125
- return uniqueDocs;
126
- }
127
- const result = await handlePagination(
128
- async ({ paginationOpts }) => client.query(options.api.findMany, {
129
- ...data,
130
- model: data.model,
131
- where: parseWhere(data.where),
132
- paginationOpts
133
- }),
134
- { limit: data.limit }
135
- );
136
- return result.docs;
137
- },
138
- // Note: Convex doesn't have a native count query, so we fetch all docs and count client-side.
139
- // This is inefficient for large datasets but acceptable for auth tables (typically small).
140
- count: async (data) => {
141
- if (data.where?.some((w) => w.connector === "OR")) {
142
- const results = await Promise.all(
143
- data.where.map(
144
- async (w) => handlePagination(async ({ paginationOpts }) => {
145
- return client.query(options.api.findMany, {
146
- ...data,
147
- model: data.model,
148
- where: parseWhere(w),
149
- paginationOpts
150
- });
151
- })
152
- )
153
- );
154
- const allDocs = results.flatMap((r) => r.docs);
155
- const uniqueDocs = [...new Map(allDocs.map((d) => [d._id, d])).values()];
156
- return uniqueDocs.length;
157
- }
158
- const result = await handlePagination(async ({ paginationOpts }) => client.query(options.api.findMany, {
159
- ...data,
160
- model: data.model,
161
- where: parseWhere(data.where),
162
- paginationOpts
163
- }));
164
- return result.docs.length;
165
- },
166
- // Supports single eq or multiple AND-connected conditions (Better Auth's common patterns)
167
- update: async (data) => {
168
- const hasOrConnector = data.where?.some((w) => w.connector === "OR");
169
- if (hasOrConnector) {
170
- throw new Error("update() does not support OR conditions - use updateMany() or split into multiple calls");
171
- }
172
- return client.mutation(options.api.updateOne, {
173
- input: {
174
- model: data.model,
175
- where: parseWhere(data.where),
176
- update: data.update
177
- }
178
- });
179
- },
180
- updateMany: async (data) => {
181
- const result = await handlePagination(async ({ paginationOpts }) => client.mutation(options.api.updateMany, {
182
- input: {
183
- ...data,
184
- model: data.model,
185
- where: parseWhere(data.where)
186
- },
187
- paginationOpts
188
- }));
189
- return result.count;
190
- },
191
- delete: async (data) => {
192
- await client.mutation(options.api.deleteOne, {
193
- input: {
194
- model: data.model,
195
- where: parseWhere(data.where)
196
- }
197
- });
198
- },
199
- deleteMany: async (data) => {
200
- const result = await handlePagination(async ({ paginationOpts }) => client.mutation(options.api.deleteMany, {
201
- input: {
202
- ...data,
203
- model: data.model,
204
- where: parseWhere(data.where)
205
- },
206
- paginationOpts
207
- }));
208
- return result.count;
209
- }
210
- };
211
- }
212
- });
213
- }