@proofkit/better-auth 0.3.1-beta.1 → 0.4.0-beta.2

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,12 +1,9 @@
1
+ import { Database } from '../../fmodata/src.js';
1
2
  import { CleanedWhere, DBAdapterDebugLogOption } from 'better-auth/adapters';
2
- import { FmOdataConfig } from './odata.js';
3
3
  export interface FileMakerAdapterConfig {
4
4
  debugLogs?: DBAdapterDebugLogOption;
5
5
  usePlural?: boolean;
6
- odata: FmOdataConfig;
7
- }
8
- export interface AdapterOptions {
9
- config: FileMakerAdapterConfig;
6
+ database: Database;
10
7
  }
11
8
  export declare function parseWhere(where?: CleanedWhere[]): string;
12
- export declare const FileMakerAdapter: (_config?: FileMakerAdapterConfig) => import('better-auth/adapters').AdapterFactory;
9
+ export declare const FileMakerAdapter: (config: FileMakerAdapterConfig) => import('better-auth/adapters').AdapterFactory;
@@ -1,26 +1,5 @@
1
1
  import { logger } from "better-auth";
2
2
  import { createAdapter } from "better-auth/adapters";
3
- import buildQuery from "odata-query";
4
- import { z, prettifyError } from "zod/v4";
5
- import { createRawFetch } from "./odata/index.js";
6
- const configSchema = z.object({
7
- debugLogs: z.unknown().optional(),
8
- usePlural: z.boolean().optional(),
9
- odata: z.object({
10
- serverUrl: z.url(),
11
- auth: z.union([z.object({ username: z.string(), password: z.string() }), z.object({ apiKey: z.string() })]),
12
- database: z.string().endsWith(".fmp12")
13
- })
14
- });
15
- const defaultConfig = {
16
- debugLogs: false,
17
- usePlural: false,
18
- odata: {
19
- serverUrl: "",
20
- auth: { username: "", password: "" },
21
- database: ""
22
- }
23
- };
24
3
  const FIELD_SPECIAL_CHARS_REGEX = /[\s_]/;
25
4
  const ISO_DATE_REGEX = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z?$/;
26
5
  function parseWhere(where) {
@@ -107,43 +86,48 @@ function parseWhere(where) {
107
86
  }
108
87
  return clauses.join(" ");
109
88
  }
110
- const FileMakerAdapter = (_config = defaultConfig) => {
111
- const parsed = configSchema.loose().safeParse(_config);
112
- if (!parsed.success) {
113
- throw new Error(`Invalid configuration: ${prettifyError(parsed.error)}`);
89
+ function buildQueryString(params) {
90
+ var _a;
91
+ const parts = [];
92
+ if (params.top !== void 0) {
93
+ parts.push(`$top=${params.top}`);
114
94
  }
115
- const config = parsed.data;
116
- const { fetch } = createRawFetch({
117
- ...config.odata,
118
- logging: config.debugLogs ? "verbose" : "none"
119
- });
95
+ if (params.skip !== void 0) {
96
+ parts.push(`$skip=${params.skip}`);
97
+ }
98
+ if (params.filter) {
99
+ parts.push(`$filter=${encodeURIComponent(params.filter)}`);
100
+ }
101
+ if (params.orderBy) {
102
+ parts.push(`$orderby=${encodeURIComponent(params.orderBy)}`);
103
+ }
104
+ if ((_a = params.select) == null ? void 0 : _a.length) {
105
+ parts.push(`$select=${params.select.map(encodeURIComponent).join(",")}`);
106
+ }
107
+ return parts.length > 0 ? `?${parts.join("&")}` : "";
108
+ }
109
+ const FileMakerAdapter = (config) => {
110
+ if (!config.database || typeof config.database !== "object") {
111
+ throw new Error("FileMakerAdapter requires a `database` (fmodata Database instance).");
112
+ }
113
+ const db = config.database;
120
114
  const adapterFactory = createAdapter({
121
115
  config: {
122
116
  adapterId: "filemaker",
123
117
  adapterName: "FileMaker",
124
118
  usePlural: config.usePlural ?? false,
125
- // Whether the table names in the schema are plural.
126
119
  debugLogs: config.debugLogs ?? false,
127
- // Whether to enable debug logs.
128
120
  supportsJSON: false,
129
- // Whether the database supports JSON. (Default: false)
130
121
  supportsDates: false,
131
- // Whether the database supports dates. (Default: true)
132
122
  supportsBooleans: false,
133
- // Whether the database supports booleans. (Default: true)
134
123
  supportsNumericIds: false
135
- // Whether the database supports auto-incrementing numeric IDs. (Default: true)
136
124
  },
137
125
  adapter: () => {
138
126
  return {
139
127
  create: async ({ data, model }) => {
140
- if (model === "session") {
141
- console.log("session", data);
142
- }
143
- const result = await fetch(`/${model}`, {
128
+ const result = await db._makeRequest(`/${model}`, {
144
129
  method: "POST",
145
- body: data,
146
- output: z.looseObject({ id: z.string() })
130
+ body: JSON.stringify(data)
147
131
  });
148
132
  if (result.error) {
149
133
  throw new Error("Failed to create record");
@@ -154,14 +138,11 @@ const FileMakerAdapter = (_config = defaultConfig) => {
154
138
  var _a;
155
139
  const filter = parseWhere(where);
156
140
  logger.debug("$filter", filter);
157
- const query = buildQuery({
141
+ const query = buildQueryString({
158
142
  filter: filter.length > 0 ? filter : void 0
159
143
  });
160
- const result = await fetch(`/${model}/$count${query}`, {
161
- method: "GET",
162
- output: z.object({ value: z.number() })
163
- });
164
- if (!result.data) {
144
+ const result = await db._makeRequest(`/${model}/$count${query}`);
145
+ if (result.error) {
165
146
  throw new Error("Failed to count records");
166
147
  }
167
148
  return ((_a = result.data) == null ? void 0 : _a.value) ?? 0;
@@ -170,14 +151,11 @@ const FileMakerAdapter = (_config = defaultConfig) => {
170
151
  var _a, _b;
171
152
  const filter = parseWhere(where);
172
153
  logger.debug("$filter", filter);
173
- const query = buildQuery({
154
+ const query = buildQueryString({
174
155
  top: 1,
175
156
  filter: filter.length > 0 ? filter : void 0
176
157
  });
177
- const result = await fetch(`/${model}${query}`, {
178
- method: "GET",
179
- output: z.object({ value: z.array(z.any()) })
180
- });
158
+ const result = await db._makeRequest(`/${model}${query}`);
181
159
  if (result.error) {
182
160
  throw new Error("Failed to find record");
183
161
  }
@@ -187,17 +165,14 @@ const FileMakerAdapter = (_config = defaultConfig) => {
187
165
  var _a;
188
166
  const filter = parseWhere(where);
189
167
  logger.debug("FIND MANY", { where, filter });
190
- const query = buildQuery({
168
+ const query = buildQueryString({
191
169
  top: limit,
192
170
  skip: offset,
193
171
  orderBy: sortBy ? `${sortBy.field} ${sortBy.direction ?? "asc"}` : void 0,
194
172
  filter: filter.length > 0 ? filter : void 0
195
173
  });
196
174
  logger.debug("QUERY", query);
197
- const result = await fetch(`/${model}${query}`, {
198
- method: "GET",
199
- output: z.object({ value: z.array(z.any()) })
200
- });
175
+ const result = await db._makeRequest(`/${model}${query}`);
201
176
  logger.debug("RESULT", result);
202
177
  if (result.error) {
203
178
  throw new Error("Failed to find records");
@@ -207,45 +182,36 @@ const FileMakerAdapter = (_config = defaultConfig) => {
207
182
  delete: async ({ model, where }) => {
208
183
  var _a, _b, _c;
209
184
  const filter = parseWhere(where);
210
- console.log("DELETE", { model, where, filter });
211
185
  logger.debug("$filter", filter);
212
- const query = buildQuery({
186
+ const query = buildQueryString({
213
187
  top: 1,
214
188
  select: [`"id"`],
215
189
  filter: filter.length > 0 ? filter : void 0
216
190
  });
217
- const toDelete = await fetch(`/${model}${query}`, {
218
- method: "GET",
219
- output: z.object({ value: z.array(z.object({ id: z.string() })) })
220
- });
191
+ const toDelete = await db._makeRequest(`/${model}${query}`);
221
192
  const id = (_c = (_b = (_a = toDelete.data) == null ? void 0 : _a.value) == null ? void 0 : _b[0]) == null ? void 0 : _c.id;
222
193
  if (!id) {
223
194
  return;
224
195
  }
225
- const result = await fetch(`/${model}('${id}')`, {
196
+ const result = await db._makeRequest(`/${model}('${id}')`, {
226
197
  method: "DELETE"
227
198
  });
228
199
  if (result.error) {
229
- console.log("DELETE ERROR", result.error);
230
200
  throw new Error("Failed to delete record");
231
201
  }
232
202
  },
233
203
  deleteMany: async ({ model, where }) => {
234
204
  var _a, _b;
235
205
  const filter = parseWhere(where);
236
- console.log("DELETE MANY", { model, where, filter });
237
- const query = buildQuery({
206
+ const query = buildQueryString({
238
207
  select: [`"id"`],
239
208
  filter: filter.length > 0 ? filter : void 0
240
209
  });
241
- const rows = await fetch(`/${model}${query}`, {
242
- method: "GET",
243
- output: z.object({ value: z.array(z.object({ id: z.string() })) })
244
- });
210
+ const rows = await db._makeRequest(`/${model}${query}`);
245
211
  const ids = ((_b = (_a = rows.data) == null ? void 0 : _a.value) == null ? void 0 : _b.map((r) => r.id)) ?? [];
246
212
  let deleted = 0;
247
213
  for (const id of ids) {
248
- const res = await fetch(`/${model}('${id}')`, {
214
+ const res = await db._makeRequest(`/${model}('${id}')`, {
249
215
  method: "DELETE"
250
216
  });
251
217
  if (!res.error) {
@@ -259,51 +225,42 @@ const FileMakerAdapter = (_config = defaultConfig) => {
259
225
  const filter = parseWhere(where);
260
226
  logger.debug("UPDATE", { model, where, update });
261
227
  logger.debug("$filter", filter);
262
- const query = buildQuery({
228
+ const query = buildQueryString({
263
229
  select: [`"id"`],
264
230
  filter: filter.length > 0 ? filter : void 0
265
231
  });
266
- const existing = await fetch(`/${model}${query}`, {
267
- method: "GET",
268
- output: z.object({ value: z.array(z.object({ id: z.string() })) })
269
- });
232
+ const existing = await db._makeRequest(`/${model}${query}`);
270
233
  logger.debug("EXISTING", existing.data);
271
234
  const id = (_c = (_b = (_a = existing.data) == null ? void 0 : _a.value) == null ? void 0 : _b[0]) == null ? void 0 : _c.id;
272
235
  if (!id) {
273
236
  return null;
274
237
  }
275
- const patchRes = await fetch(`/${model}('${id}')`, {
238
+ const patchRes = await db._makeRequest(`/${model}('${id}')`, {
276
239
  method: "PATCH",
277
- body: update
240
+ body: JSON.stringify(update)
278
241
  });
279
242
  logger.debug("PATCH RES", patchRes.data);
280
243
  if (patchRes.error) {
281
244
  return null;
282
245
  }
283
- const readBack = await fetch(`/${model}('${id}')`, {
284
- method: "GET",
285
- output: z.record(z.string(), z.unknown())
286
- });
246
+ const readBack = await db._makeRequest(`/${model}('${id}')`);
287
247
  logger.debug("READ BACK", readBack.data);
288
248
  return readBack.data ?? null;
289
249
  },
290
250
  updateMany: async ({ model, where, update }) => {
291
251
  var _a, _b;
292
252
  const filter = parseWhere(where);
293
- const query = buildQuery({
253
+ const query = buildQueryString({
294
254
  select: [`"id"`],
295
255
  filter: filter.length > 0 ? filter : void 0
296
256
  });
297
- const rows = await fetch(`/${model}${query}`, {
298
- method: "GET",
299
- output: z.object({ value: z.array(z.object({ id: z.string() })) })
300
- });
257
+ const rows = await db._makeRequest(`/${model}${query}`);
301
258
  const ids = ((_b = (_a = rows.data) == null ? void 0 : _a.value) == null ? void 0 : _b.map((r) => r.id)) ?? [];
302
259
  let updated = 0;
303
260
  for (const id of ids) {
304
- const res = await fetch(`/${model}('${id}')`, {
261
+ const res = await db._makeRequest(`/${model}('${id}')`, {
305
262
  method: "PATCH",
306
- body: update
263
+ body: JSON.stringify(update)
307
264
  });
308
265
  if (!res.error) {
309
266
  updated++;
@@ -314,8 +271,14 @@ const FileMakerAdapter = (_config = defaultConfig) => {
314
271
  };
315
272
  }
316
273
  });
317
- adapterFactory.filemakerConfig = config;
318
- return adapterFactory;
274
+ const originalFactory = adapterFactory;
275
+ const wrappedFactory = ((options) => {
276
+ const adapter = originalFactory(options);
277
+ adapter.database = db;
278
+ return adapter;
279
+ });
280
+ wrappedFactory.database = db;
281
+ return wrappedFactory;
319
282
  };
320
283
  export {
321
284
  FileMakerAdapter,
@@ -1 +1 @@
1
- {"version":3,"file":"adapter.js","sources":["../../src/adapter.ts"],"sourcesContent":["/** biome-ignore-all lint/suspicious/noExplicitAny: library code */\nimport { logger } from \"better-auth\";\nimport { type CleanedWhere, createAdapter, type DBAdapterDebugLogOption } from \"better-auth/adapters\";\nimport buildQuery from \"odata-query\";\nimport { prettifyError, z } from \"zod/v4\";\nimport { createRawFetch, type FmOdataConfig } from \"./odata\";\n\nconst configSchema = z.object({\n debugLogs: z.unknown().optional(),\n usePlural: z.boolean().optional(),\n odata: z.object({\n serverUrl: z.url(),\n auth: z.union([z.object({ username: z.string(), password: z.string() }), z.object({ apiKey: z.string() })]),\n database: z.string().endsWith(\".fmp12\"),\n }),\n});\n\nexport interface FileMakerAdapterConfig {\n /**\n * Helps you debug issues with the adapter.\n */\n debugLogs?: DBAdapterDebugLogOption;\n /**\n * If the table names in the schema are plural.\n */\n usePlural?: boolean;\n\n /**\n * Connection details for the FileMaker server.\n */\n odata: FmOdataConfig;\n}\n\nexport interface AdapterOptions {\n config: FileMakerAdapterConfig;\n}\n\nconst defaultConfig: Required<FileMakerAdapterConfig> = {\n debugLogs: false,\n usePlural: false,\n odata: {\n serverUrl: \"\",\n auth: { username: \"\", password: \"\" },\n database: \"\",\n },\n};\n\n// Regex patterns for field validation and ISO date detection\nconst FIELD_SPECIAL_CHARS_REGEX = /[\\s_]/;\nconst ISO_DATE_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?Z?$/;\n\n/**\n * Parse the where clause to an OData filter string.\n * @param where - The where clause to parse.\n * @returns The OData filter string.\n * @internal\n */\nexport function parseWhere(where?: CleanedWhere[]): string {\n if (!where || where.length === 0) {\n return \"\";\n }\n\n // Helper to quote field names with special chars or if field is 'id'\n function quoteField(field: string, value?: any) {\n // Never quote for null or date values (per test expectations)\n if (value === null || value instanceof Date) {\n return field;\n }\n // Always quote if field is 'id' or has space or underscore\n if (field === \"id\" || FIELD_SPECIAL_CHARS_REGEX.test(field)) {\n return `\"${field}\"`;\n }\n return field;\n }\n\n // Helper to format values for OData\n function formatValue(value: any): string {\n if (value === null) {\n return \"null\";\n }\n if (typeof value === \"boolean\") {\n return value ? \"true\" : \"false\";\n }\n if (value instanceof Date) {\n return value.toISOString();\n }\n if (Array.isArray(value)) {\n return `(${value.map(formatValue).join(\",\")})`;\n }\n\n // Handle strings - check if it's an ISO date string first\n if (typeof value === \"string\") {\n // Check if it's an ISO date string (YYYY-MM-DDTHH:mm:ss.sssZ format)\n if (ISO_DATE_REGEX.test(value)) {\n return value; // Return ISO date strings without quotes\n }\n return `'${value.replace(/'/g, \"''\")}'`; // Regular strings get quotes\n }\n\n return value?.toString() ?? \"\";\n }\n\n // Map our operators to OData\n const opMap: Record<string, string> = {\n eq: \"eq\",\n ne: \"ne\",\n lt: \"lt\",\n lte: \"le\",\n gt: \"gt\",\n gte: \"ge\",\n };\n\n // Build each clause\n const clauses: string[] = [];\n for (let i = 0; i < where.length; i++) {\n const cond = where[i];\n if (!cond) {\n continue;\n }\n const field = quoteField(cond.field, cond.value);\n let clause = \"\";\n switch (cond.operator) {\n case \"eq\":\n case \"ne\":\n case \"lt\":\n case \"lte\":\n case \"gt\":\n case \"gte\":\n clause = `${field} ${opMap[cond.operator]} ${formatValue(cond.value)}`;\n break;\n case \"in\":\n if (Array.isArray(cond.value)) {\n clause = cond.value.map((v) => `${field} eq ${formatValue(v)}`).join(\" or \");\n clause = `(${clause})`;\n }\n break;\n case \"contains\":\n clause = `contains(${field}, ${formatValue(cond.value)})`;\n break;\n case \"starts_with\":\n clause = `startswith(${field}, ${formatValue(cond.value)})`;\n break;\n case \"ends_with\":\n clause = `endswith(${field}, ${formatValue(cond.value)})`;\n break;\n default:\n clause = `${field} eq ${formatValue(cond.value)}`;\n }\n clauses.push(clause);\n // Add connector if not last\n if (i < where.length - 1) {\n clauses.push((cond.connector || \"and\").toLowerCase());\n }\n }\n return clauses.join(\" \");\n}\n\nexport const FileMakerAdapter = (_config: FileMakerAdapterConfig = defaultConfig) => {\n const parsed = configSchema.loose().safeParse(_config);\n\n if (!parsed.success) {\n throw new Error(`Invalid configuration: ${prettifyError(parsed.error)}`);\n }\n const config = parsed.data;\n\n const { fetch } = createRawFetch({\n ...config.odata,\n logging: config.debugLogs ? \"verbose\" : \"none\",\n });\n\n const adapterFactory = createAdapter({\n config: {\n adapterId: \"filemaker\",\n adapterName: \"FileMaker\",\n usePlural: config.usePlural ?? false, // Whether the table names in the schema are plural.\n debugLogs: config.debugLogs ?? false, // Whether to enable debug logs.\n supportsJSON: false, // Whether the database supports JSON. (Default: false)\n supportsDates: false, // Whether the database supports dates. (Default: true)\n supportsBooleans: false, // Whether the database supports booleans. (Default: true)\n supportsNumericIds: false, // Whether the database supports auto-incrementing numeric IDs. (Default: true)\n },\n adapter: () => {\n return {\n create: async ({ data, model }) => {\n if (model === \"session\") {\n console.log(\"session\", data);\n }\n\n const result = await fetch(`/${model}`, {\n method: \"POST\",\n body: data,\n output: z.looseObject({ id: z.string() }),\n });\n\n if (result.error) {\n throw new Error(\"Failed to create record\");\n }\n\n return result.data as any;\n },\n count: async ({ model, where }) => {\n const filter = parseWhere(where);\n logger.debug(\"$filter\", filter);\n\n const query = buildQuery({\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const result = await fetch(`/${model}/$count${query}`, {\n method: \"GET\",\n output: z.object({ value: z.number() }),\n });\n if (!result.data) {\n throw new Error(\"Failed to count records\");\n }\n return (result.data?.value as any) ?? 0;\n },\n findOne: async ({ model, where }) => {\n const filter = parseWhere(where);\n logger.debug(\"$filter\", filter);\n\n const query = buildQuery({\n top: 1,\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const result = await fetch(`/${model}${query}`, {\n method: \"GET\",\n output: z.object({ value: z.array(z.any()) }),\n });\n if (result.error) {\n throw new Error(\"Failed to find record\");\n }\n return (result.data?.value?.[0] as any) ?? null;\n },\n findMany: async ({ model, where, limit, offset, sortBy }) => {\n const filter = parseWhere(where);\n logger.debug(\"FIND MANY\", { where, filter });\n\n const query = buildQuery({\n top: limit,\n skip: offset,\n orderBy: sortBy ? `${sortBy.field} ${sortBy.direction ?? \"asc\"}` : undefined,\n filter: filter.length > 0 ? filter : undefined,\n });\n logger.debug(\"QUERY\", query);\n\n const result = await fetch(`/${model}${query}`, {\n method: \"GET\",\n output: z.object({ value: z.array(z.any()) }),\n });\n logger.debug(\"RESULT\", result);\n\n if (result.error) {\n throw new Error(\"Failed to find records\");\n }\n\n return (result.data?.value as any) ?? [];\n },\n delete: async ({ model, where }) => {\n const filter = parseWhere(where);\n console.log(\"DELETE\", { model, where, filter });\n logger.debug(\"$filter\", filter);\n\n // Find a single id matching the filter\n const query = buildQuery({\n top: 1,\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const toDelete = await fetch(`/${model}${query}`, {\n method: \"GET\",\n output: z.object({ value: z.array(z.object({ id: z.string() })) }),\n });\n\n const id = toDelete.data?.value?.[0]?.id;\n if (!id) {\n // Nothing to delete\n return;\n }\n\n const result = await fetch(`/${model}('${id}')`, {\n method: \"DELETE\",\n });\n if (result.error) {\n console.log(\"DELETE ERROR\", result.error);\n throw new Error(\"Failed to delete record\");\n }\n },\n deleteMany: async ({ model, where }) => {\n const filter = parseWhere(where);\n console.log(\"DELETE MANY\", { model, where, filter });\n\n // Find all ids matching the filter\n const query = buildQuery({\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const rows = await fetch(`/${model}${query}`, {\n method: \"GET\",\n output: z.object({ value: z.array(z.object({ id: z.string() })) }),\n });\n\n const ids = rows.data?.value?.map((r: any) => r.id) ?? [];\n let deleted = 0;\n for (const id of ids) {\n const res = await fetch(`/${model}('${id}')`, {\n method: \"DELETE\",\n });\n if (!res.error) {\n deleted++;\n }\n }\n return deleted;\n },\n update: async ({ model, where, update }) => {\n const filter = parseWhere(where);\n logger.debug(\"UPDATE\", { model, where, update });\n logger.debug(\"$filter\", filter);\n // Find one id to update\n const query = buildQuery({\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const existing = await fetch(`/${model}${query}`, {\n method: \"GET\",\n output: z.object({ value: z.array(z.object({ id: z.string() })) }),\n });\n logger.debug(\"EXISTING\", existing.data);\n\n const id = existing.data?.value?.[0]?.id;\n if (!id) {\n return null;\n }\n\n const patchRes = await fetch(`/${model}('${id}')`, {\n method: \"PATCH\",\n body: update,\n });\n logger.debug(\"PATCH RES\", patchRes.data);\n if (patchRes.error) {\n return null;\n }\n\n // Read back the updated record\n const readBack = await fetch(`/${model}('${id}')`, {\n method: \"GET\",\n output: z.record(z.string(), z.unknown()),\n });\n logger.debug(\"READ BACK\", readBack.data);\n return (readBack.data as any) ?? null;\n },\n updateMany: async ({ model, where, update }) => {\n const filter = parseWhere(where);\n // Find all ids matching the filter\n const query = buildQuery({\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const rows = await fetch(`/${model}${query}`, {\n method: \"GET\",\n output: z.object({ value: z.array(z.object({ id: z.string() })) }),\n });\n\n const ids = rows.data?.value?.map((r: any) => r.id) ?? [];\n let updated = 0;\n for (const id of ids) {\n const res = await fetch(`/${model}('${id}')`, {\n method: \"PATCH\",\n body: update,\n });\n if (!res.error) {\n updated++;\n }\n }\n return updated as any;\n },\n };\n },\n });\n\n // Expose the FileMaker config for CLI access\n (adapterFactory as any).filemakerConfig = config as FileMakerAdapterConfig;\n return adapterFactory;\n};\n"],"names":[],"mappings":";;;;;AAOA,MAAM,eAAe,EAAE,OAAO;AAAA,EAC5B,WAAW,EAAE,QAAA,EAAU,SAAA;AAAA,EACvB,WAAW,EAAE,QAAA,EAAU,SAAA;AAAA,EACvB,OAAO,EAAE,OAAO;AAAA,IACd,WAAW,EAAE,IAAA;AAAA,IACb,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,UAAU,EAAE,OAAA,GAAU,UAAU,EAAE,SAAO,CAAG,GAAG,EAAE,OAAO,EAAE,QAAQ,EAAE,SAAO,CAAG,CAAC,CAAC;AAAA,IAC1G,UAAU,EAAE,OAAA,EAAS,SAAS,QAAQ;AAAA,EAAA,CACvC;AACH,CAAC;AAsBD,MAAM,gBAAkD;AAAA,EACtD,WAAW;AAAA,EACX,WAAW;AAAA,EACX,OAAO;AAAA,IACL,WAAW;AAAA,IACX,MAAM,EAAE,UAAU,IAAI,UAAU,GAAA;AAAA,IAChC,UAAU;AAAA,EAAA;AAEd;AAGA,MAAM,4BAA4B;AAClC,MAAM,iBAAiB;AAQhB,SAAS,WAAW,OAAgC;AACzD,MAAI,CAAC,SAAS,MAAM,WAAW,GAAG;AAChC,WAAO;AAAA,EACT;AAGA,WAAS,WAAW,OAAe,OAAa;AAE9C,QAAI,UAAU,QAAQ,iBAAiB,MAAM;AAC3C,aAAO;AAAA,IACT;AAEA,QAAI,UAAU,QAAQ,0BAA0B,KAAK,KAAK,GAAG;AAC3D,aAAO,IAAI,KAAK;AAAA,IAClB;AACA,WAAO;AAAA,EACT;AAGA,WAAS,YAAY,OAAoB;AACvC,QAAI,UAAU,MAAM;AAClB,aAAO;AA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O,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,OAAA,GAAU,CAAC,GAAG;AAAA,UAAA,CAClE;AAED,gBAAM,QAAM,gBAAK,SAAL,mBAAW,UAAX,mBAAkB,IAAI,CAAC,MAAW,EAAE,QAAO,CAAA;AACvD,cAAI,UAAU;AACd,qBAAW,MAAM,KAAK;AACpB,kBAAM,MAAM,MAAM,MAAM,IAAI,KAAK,KAAK,EAAE,MAAM;AAAA,cAC5C,QAAQ;AAAA,cACR,MAAM;AAAA,YAAA,CACP;AACD,gBAAI,CAAC,IAAI,OAAO;AACd;AAAA,YACF;AAAA,UACF;AACA,iBAAO;AAAA,QACT;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA,CACD;AAGA,iBAAuB,kBAAkB;AAC1C,SAAO;AACT;"}
1
+ {"version":3,"file":"adapter.js","sources":["../../src/adapter.ts"],"sourcesContent":["/** biome-ignore-all lint/suspicious/noExplicitAny: library code */\nimport type { Database } from \"@proofkit/fmodata\";\nimport { logger } from \"better-auth\";\nimport { type CleanedWhere, createAdapter, type DBAdapterDebugLogOption } from \"better-auth/adapters\";\n\nexport interface FileMakerAdapterConfig {\n /**\n * Helps you debug issues with the adapter.\n */\n debugLogs?: DBAdapterDebugLogOption;\n /**\n * If the table names in the schema are plural.\n */\n usePlural?: boolean;\n /**\n * The fmodata Database instance to use for all OData requests.\n */\n database: Database;\n}\n\n// Regex patterns for field validation and ISO date detection\nconst FIELD_SPECIAL_CHARS_REGEX = /[\\s_]/;\nconst ISO_DATE_REGEX = /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?Z?$/;\n\n/**\n * Parse the where clause to an OData filter string.\n * @param where - The where clause to parse.\n * @returns The OData filter string.\n * @internal\n */\nexport function parseWhere(where?: CleanedWhere[]): string {\n if (!where || where.length === 0) {\n return \"\";\n }\n\n // Helper to quote field names with special chars or if field is 'id'\n function quoteField(field: string, value?: any) {\n // Never quote for null or date values (per test expectations)\n if (value === null || value instanceof Date) {\n return field;\n }\n // Always quote if field is 'id' or has space or underscore\n if (field === \"id\" || FIELD_SPECIAL_CHARS_REGEX.test(field)) {\n return `\"${field}\"`;\n }\n return field;\n }\n\n // Helper to format values for OData\n function formatValue(value: any): string {\n if (value === null) {\n return \"null\";\n }\n if (typeof value === \"boolean\") {\n return value ? \"true\" : \"false\";\n }\n if (value instanceof Date) {\n return value.toISOString();\n }\n if (Array.isArray(value)) {\n return `(${value.map(formatValue).join(\",\")})`;\n }\n\n // Handle strings - check if it's an ISO date string first\n if (typeof value === \"string\") {\n // Check if it's an ISO date string (YYYY-MM-DDTHH:mm:ss.sssZ format)\n if (ISO_DATE_REGEX.test(value)) {\n return value; // Return ISO date strings without quotes\n }\n return `'${value.replace(/'/g, \"''\")}'`; // Regular strings get quotes\n }\n\n return value?.toString() ?? \"\";\n }\n\n // Map our operators to OData\n const opMap: Record<string, string> = {\n eq: \"eq\",\n ne: \"ne\",\n lt: \"lt\",\n lte: \"le\",\n gt: \"gt\",\n gte: \"ge\",\n };\n\n // Build each clause\n const clauses: string[] = [];\n for (let i = 0; i < where.length; i++) {\n const cond = where[i];\n if (!cond) {\n continue;\n }\n const field = quoteField(cond.field, cond.value);\n let clause = \"\";\n switch (cond.operator) {\n case \"eq\":\n case \"ne\":\n case \"lt\":\n case \"lte\":\n case \"gt\":\n case \"gte\":\n clause = `${field} ${opMap[cond.operator]} ${formatValue(cond.value)}`;\n break;\n case \"in\":\n if (Array.isArray(cond.value)) {\n clause = cond.value.map((v) => `${field} eq ${formatValue(v)}`).join(\" or \");\n clause = `(${clause})`;\n }\n break;\n case \"contains\":\n clause = `contains(${field}, ${formatValue(cond.value)})`;\n break;\n case \"starts_with\":\n clause = `startswith(${field}, ${formatValue(cond.value)})`;\n break;\n case \"ends_with\":\n clause = `endswith(${field}, ${formatValue(cond.value)})`;\n break;\n default:\n clause = `${field} eq ${formatValue(cond.value)}`;\n }\n clauses.push(clause);\n // Add connector if not last\n if (i < where.length - 1) {\n clauses.push((cond.connector || \"and\").toLowerCase());\n }\n }\n return clauses.join(\" \");\n}\n\n/**\n * Build an OData query string from parameters.\n */\nfunction buildQueryString(params: {\n top?: number;\n skip?: number;\n filter?: string;\n orderBy?: string;\n select?: string[];\n}): string {\n const parts: string[] = [];\n if (params.top !== undefined) {\n parts.push(`$top=${params.top}`);\n }\n if (params.skip !== undefined) {\n parts.push(`$skip=${params.skip}`);\n }\n if (params.filter) {\n parts.push(`$filter=${encodeURIComponent(params.filter)}`);\n }\n if (params.orderBy) {\n parts.push(`$orderby=${encodeURIComponent(params.orderBy)}`);\n }\n if (params.select?.length) {\n parts.push(`$select=${params.select.map(encodeURIComponent).join(\",\")}`);\n }\n return parts.length > 0 ? `?${parts.join(\"&\")}` : \"\";\n}\n\nexport const FileMakerAdapter = (config: FileMakerAdapterConfig) => {\n if (!config.database || typeof config.database !== \"object\") {\n throw new Error(\"FileMakerAdapter requires a `database` (fmodata Database instance).\");\n }\n\n const db = config.database;\n\n const adapterFactory = createAdapter({\n config: {\n adapterId: \"filemaker\",\n adapterName: \"FileMaker\",\n usePlural: config.usePlural ?? false,\n debugLogs: config.debugLogs ?? false,\n supportsJSON: false,\n supportsDates: false,\n supportsBooleans: false,\n supportsNumericIds: false,\n },\n adapter: () => {\n return {\n create: async ({ data, model }) => {\n const result = await db._makeRequest<Record<string, any>>(`/${model}`, {\n method: \"POST\",\n body: JSON.stringify(data),\n });\n\n if (result.error) {\n throw new Error(\"Failed to create record\");\n }\n\n return result.data as any;\n },\n count: async ({ model, where }) => {\n const filter = parseWhere(where);\n logger.debug(\"$filter\", filter);\n\n const query = buildQueryString({\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const result = await db._makeRequest<{ value: number }>(`/${model}/$count${query}`);\n if (result.error) {\n throw new Error(\"Failed to count records\");\n }\n return (result.data?.value as any) ?? 0;\n },\n findOne: async ({ model, where }) => {\n const filter = parseWhere(where);\n logger.debug(\"$filter\", filter);\n\n const query = buildQueryString({\n top: 1,\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const result = await db._makeRequest<{ value: any[] }>(`/${model}${query}`);\n if (result.error) {\n throw new Error(\"Failed to find record\");\n }\n return (result.data?.value?.[0] as any) ?? null;\n },\n findMany: async ({ model, where, limit, offset, sortBy }) => {\n const filter = parseWhere(where);\n logger.debug(\"FIND MANY\", { where, filter });\n\n const query = buildQueryString({\n top: limit,\n skip: offset,\n orderBy: sortBy ? `${sortBy.field} ${sortBy.direction ?? \"asc\"}` : undefined,\n filter: filter.length > 0 ? filter : undefined,\n });\n logger.debug(\"QUERY\", query);\n\n const result = await db._makeRequest<{ value: any[] }>(`/${model}${query}`);\n logger.debug(\"RESULT\", result);\n\n if (result.error) {\n throw new Error(\"Failed to find records\");\n }\n\n return (result.data?.value as any) ?? [];\n },\n delete: async ({ model, where }) => {\n const filter = parseWhere(where);\n logger.debug(\"$filter\", filter);\n\n // Find a single id matching the filter\n const query = buildQueryString({\n top: 1,\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const toDelete = await db._makeRequest<{ value: { id: string }[] }>(`/${model}${query}`);\n\n const id = toDelete.data?.value?.[0]?.id;\n if (!id) {\n return;\n }\n\n const result = await db._makeRequest(`/${model}('${id}')`, {\n method: \"DELETE\",\n });\n if (result.error) {\n throw new Error(\"Failed to delete record\");\n }\n },\n deleteMany: async ({ model, where }) => {\n const filter = parseWhere(where);\n\n // Find all ids matching the filter\n const query = buildQueryString({\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const rows = await db._makeRequest<{ value: { id: string }[] }>(`/${model}${query}`);\n\n const ids = rows.data?.value?.map((r: any) => r.id) ?? [];\n let deleted = 0;\n for (const id of ids) {\n const res = await db._makeRequest(`/${model}('${id}')`, {\n method: \"DELETE\",\n });\n if (!res.error) {\n deleted++;\n }\n }\n return deleted;\n },\n update: async ({ model, where, update }) => {\n const filter = parseWhere(where);\n logger.debug(\"UPDATE\", { model, where, update });\n logger.debug(\"$filter\", filter);\n\n // Find one id to update\n const query = buildQueryString({\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const existing = await db._makeRequest<{ value: { id: string }[] }>(`/${model}${query}`);\n logger.debug(\"EXISTING\", existing.data);\n\n const id = existing.data?.value?.[0]?.id;\n if (!id) {\n return null;\n }\n\n const patchRes = await db._makeRequest(`/${model}('${id}')`, {\n method: \"PATCH\",\n body: JSON.stringify(update),\n });\n logger.debug(\"PATCH RES\", patchRes.data);\n if (patchRes.error) {\n return null;\n }\n\n // Read back the updated record\n const readBack = await db._makeRequest<Record<string, unknown>>(`/${model}('${id}')`);\n logger.debug(\"READ BACK\", readBack.data);\n return (readBack.data as any) ?? null;\n },\n updateMany: async ({ model, where, update }) => {\n const filter = parseWhere(where);\n\n // Find all ids matching the filter\n const query = buildQueryString({\n select: [`\"id\"`],\n filter: filter.length > 0 ? filter : undefined,\n });\n\n const rows = await db._makeRequest<{ value: { id: string }[] }>(`/${model}${query}`);\n\n const ids = rows.data?.value?.map((r: any) => r.id) ?? [];\n let updated = 0;\n for (const id of ids) {\n const res = await db._makeRequest(`/${model}('${id}')`, {\n method: \"PATCH\",\n body: JSON.stringify(update),\n });\n if (!res.error) {\n updated++;\n }\n }\n return updated as any;\n },\n };\n },\n });\n\n // Expose the Database instance for CLI access.\n // Set on both the factory function (for pre-getAdapter extraction)\n // and the returned adapter (for post-getAdapter extraction).\n const originalFactory = adapterFactory;\n const wrappedFactory = ((options: unknown) => {\n const adapter = (originalFactory as (opts: unknown) => Record<string, unknown>)(options);\n adapter.database = db;\n return adapter;\n }) as typeof adapterFactory;\n (wrappedFactory as unknown as { database: Database }).database = db;\n return wrappedFactory;\n};\n"],"names":[],"mappings":";;AAqBA,MAAM,4BAA4B;AAClC,MAAM,iBAAiB;AAQhB,SAAS,WAAW,OAAgC;AACzD,MAAI,CAAC,SAAS,MAAM,WAAW,GAAG;AAChC,WAAO;AAAA,EACT;AAGA,WAAS,WAA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D;AAKD,QAAM,kBAAkB;AACxB,QAAM,kBAAkB,CAAC,YAAqB;AAC5C,UAAM,UAAW,gBAA+D,OAAO;AACvF,YAAQ,WAAW;AACnB,WAAO;AAAA,EACT;AACC,iBAAqD,WAAW;AACjE,SAAO;AACT;"}
@@ -1,5 +1,6 @@
1
1
  #!/usr/bin/env node --no-warnings
2
2
  import { Command } from "@commander-js/extra-typings";
3
+ import { FMServerConnection } from "@proofkit/fmodata";
3
4
  import { logger } from "better-auth";
4
5
  import { getAdapter, getSchema } from "better-auth/db";
5
6
  import chalk from "chalk";
@@ -7,11 +8,11 @@ import fs from "fs-extra";
7
8
  import prompts from "prompts";
8
9
  import { getConfig } from "../better-auth-cli/utils/get-config.js";
9
10
  import { planMigration, prettyPrintMigrationPlan, executeMigration } from "../migrate.js";
10
- import { createRawFetch } from "../odata/index.js";
11
11
  import "dotenv/config";
12
12
  async function main() {
13
13
  const program = new Command();
14
14
  program.command("migrate", { isDefault: true }).option("--cwd <path>", "Path to the current working directory", process.cwd()).option("--config <path>", "Path to the config file").option("-u, --username <username>", "Full Access Username").option("-p, --password <password>", "Full Access Password").option("-y, --yes", "Skip confirmation", false).action(async (options) => {
15
+ var _a, _b, _c, _d;
15
16
  const cwd = options.cwd;
16
17
  if (!fs.existsSync(cwd)) {
17
18
  logger.error(`The directory "${cwd}" does not exist.`);
@@ -36,26 +37,53 @@ async function main() {
36
37
  return;
37
38
  }
38
39
  const betterAuthSchema = getSchema(config);
39
- const adapterConfig = adapter.filemakerConfig;
40
- const { fetch } = createRawFetch({
41
- ...adapterConfig.odata,
42
- auth: (
43
- // If the username and password are provided in the CLI, use them to authenticate instead of what's in the config file.
44
- options.username && options.password ? {
40
+ const configDb = adapter.database ?? ((_a = config.database) == null ? void 0 : _a.database);
41
+ if (!configDb || typeof configDb !== "object" || !("schema" in configDb)) {
42
+ logger.error(
43
+ "Could not extract Database instance from adapter. Ensure your auth.ts uses FileMakerAdapter with an fmodata Database."
44
+ );
45
+ process.exit(1);
46
+ }
47
+ let db = configDb;
48
+ const dbObj = configDb;
49
+ const dbName = dbObj._getDatabaseName ?? dbObj.databaseName ?? "";
50
+ const baseUrl = (_c = (_b = dbObj.context) == null ? void 0 : _b._getBaseUrl) == null ? void 0 : _c.call(_b);
51
+ const serverUrl = baseUrl ? new URL(baseUrl).origin : void 0;
52
+ if (options.username && options.password) {
53
+ if (!dbName) {
54
+ logger.error("Could not determine database filename from adapter config.");
55
+ process.exit(1);
56
+ }
57
+ if (!baseUrl) {
58
+ logger.error(
59
+ "Could not determine server URL from adapter config. Ensure your auth.ts uses FMServerConnection."
60
+ );
61
+ process.exit(1);
62
+ }
63
+ const fetchClientOptions = (_d = dbObj.context) == null ? void 0 : _d._fetchClientOptions;
64
+ const connection = new FMServerConnection({
65
+ serverUrl,
66
+ auth: {
45
67
  username: options.username,
46
68
  password: options.password
47
- } : adapterConfig.odata.auth
48
- ),
49
- logging: "verbose"
50
- // Enable logging for CLI operations
51
- });
52
- const migrationPlan = await planMigration(fetch, betterAuthSchema, adapterConfig.odata.database);
69
+ },
70
+ fetchClientOptions
71
+ });
72
+ db = connection.database(dbName);
73
+ }
74
+ let migrationPlan;
75
+ try {
76
+ migrationPlan = await planMigration(db, betterAuthSchema);
77
+ } catch (err) {
78
+ logger.error(`Failed to read database schema: ${err instanceof Error ? err.message : err}`);
79
+ process.exit(1);
80
+ }
53
81
  if (migrationPlan.length === 0) {
54
82
  logger.info("No changes to apply. Database is up to date.");
55
83
  return;
56
84
  }
57
85
  if (!options.yes) {
58
- prettyPrintMigrationPlan(migrationPlan);
86
+ prettyPrintMigrationPlan(migrationPlan, { serverUrl, fileName: dbName });
59
87
  if (migrationPlan.length > 0) {
60
88
  console.log(chalk.gray("💡 Tip: You can use the --yes flag to skip this confirmation."));
61
89
  }
@@ -69,11 +97,18 @@ async function main() {
69
97
  return;
70
98
  }
71
99
  }
72
- await executeMigration(fetch, migrationPlan);
73
- logger.info("Migration applied successfully.");
100
+ try {
101
+ await executeMigration(db, migrationPlan);
102
+ logger.info("Migration applied successfully.");
103
+ } catch {
104
+ process.exit(1);
105
+ }
74
106
  });
75
107
  await program.parseAsync(process.argv);
76
108
  process.exit(0);
77
109
  }
78
- main().catch(console.error);
110
+ main().catch((err) => {
111
+ logger.error(err.message ?? err);
112
+ process.exit(1);
113
+ });
79
114
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sources":["../../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node --no-warnings\nimport { Command } from \"@commander-js/extra-typings\";\nimport { logger } from \"better-auth\";\nimport { getAdapter, getSchema } from \"better-auth/db\";\nimport chalk from \"chalk\";\nimport fs from \"fs-extra\";\nimport prompts from \"prompts\";\nimport type { FileMakerAdapterConfig } from \"../adapter\";\nimport { getConfig } from \"../better-auth-cli/utils/get-config\";\nimport { executeMigration, planMigration, prettyPrintMigrationPlan } from \"../migrate\";\nimport { createRawFetch } from \"../odata\";\nimport \"dotenv/config\";\n\nasync function main() {\n const program = new Command();\n\n program\n .command(\"migrate\", { isDefault: true })\n .option(\"--cwd <path>\", \"Path to the current working directory\", process.cwd())\n .option(\"--config <path>\", \"Path to the config file\")\n .option(\"-u, --username <username>\", \"Full Access Username\")\n .option(\"-p, --password <password>\", \"Full Access Password\")\n .option(\"-y, --yes\", \"Skip confirmation\", false)\n\n .action(async (options) => {\n const cwd = options.cwd;\n if (!fs.existsSync(cwd)) {\n logger.error(`The directory \"${cwd}\" does not exist.`);\n process.exit(1);\n }\n\n const config = await getConfig({\n cwd,\n configPath: options.config,\n });\n if (!config) {\n logger.error(\n \"No configuration file found. Add a `auth.ts` file to your project or pass the path to the configuration file using the `--config` flag.\",\n );\n return;\n }\n\n const adapter = await getAdapter(config).catch((e) => {\n logger.error(e.message);\n process.exit(1);\n });\n\n if (adapter.id !== \"filemaker\") {\n logger.error(\"This generator is only compatible with the FileMaker adapter.\");\n return;\n }\n\n const betterAuthSchema = getSchema(config);\n\n const adapterConfig = (adapter as unknown as { filemakerConfig: FileMakerAdapterConfig }).filemakerConfig;\n const { fetch } = createRawFetch({\n ...adapterConfig.odata,\n auth:\n // If the username and password are provided in the CLI, use them to authenticate instead of what's in the config file.\n options.username && options.password\n ? {\n username: options.username,\n password: options.password,\n }\n : adapterConfig.odata.auth,\n logging: \"verbose\", // Enable logging for CLI operations\n });\n\n const migrationPlan = await planMigration(fetch, betterAuthSchema, adapterConfig.odata.database);\n\n if (migrationPlan.length === 0) {\n logger.info(\"No changes to apply. Database is up to date.\");\n return;\n }\n\n if (!options.yes) {\n prettyPrintMigrationPlan(migrationPlan);\n\n if (migrationPlan.length > 0) {\n console.log(chalk.gray(\"💡 Tip: You can use the --yes flag to skip this confirmation.\"));\n }\n\n const { confirm } = await prompts({\n type: \"confirm\",\n name: \"confirm\",\n message: \"Apply the above changes to your database?\",\n });\n if (!confirm) {\n logger.error(\"Schema changes not applied.\");\n return;\n }\n }\n\n await executeMigration(fetch, migrationPlan);\n\n logger.info(\"Migration applied successfully.\");\n });\n await program.parseAsync(process.argv);\n process.exit(0);\n}\n\nmain().catch(console.error);\n"],"names":[],"mappings":";;;;;;;;;;;AAaA,eAAe,OAAO;AACpB,QAAM,UAAU,IAAI,QAAA;AAEpB,UACG,QAAQ,WAAW,EAAE,WAAW,MAAM,EACtC,OAAO,gBAAgB,yCAAyC,QAAQ,IAAA,CAAK,EAC7E,OAAO,mBAAmB,yBAAyB,EACnD,OAAO,6BAA6B,sBAAsB,EAC1D,OAAO,6BAA6B,sBAAsB,EAC1D,OAAO,aAAa,qBAAqB,KAAK,EAE9C,OAAO,OAAO,YAAY;AACzB,UAAM,MAAM,QAAQ;AACpB,QAAI,CAAC,GAAG,WAAW,GAAG,GAAG;AACvB,aAAO,MAAM,kBAAkB,GAAG,mBAAmB;AACrD,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,UAAM,SAAS,MAAM,UAAU;AAAA,MAC7B;AAAA,MACA,YAAY,QAAQ;AAAA,IAAA,CACrB;A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,KAAK;"}
1
+ {"version":3,"file":"index.js","sources":["../../../src/cli/index.ts"],"sourcesContent":["#!/usr/bin/env node --no-warnings\nimport { Command } from \"@commander-js/extra-typings\";\nimport type { Database, FFetchOptions } from \"@proofkit/fmodata\";\nimport { FMServerConnection } from \"@proofkit/fmodata\";\nimport { logger } from \"better-auth\";\nimport { getAdapter, getSchema } from \"better-auth/db\";\nimport chalk from \"chalk\";\nimport fs from \"fs-extra\";\nimport prompts from \"prompts\";\nimport { getConfig } from \"../better-auth-cli/utils/get-config\";\nimport { executeMigration, planMigration, prettyPrintMigrationPlan } from \"../migrate\";\nimport \"dotenv/config\";\n\nasync function main() {\n const program = new Command();\n\n program\n .command(\"migrate\", { isDefault: true })\n .option(\"--cwd <path>\", \"Path to the current working directory\", process.cwd())\n .option(\"--config <path>\", \"Path to the config file\")\n .option(\"-u, --username <username>\", \"Full Access Username\")\n .option(\"-p, --password <password>\", \"Full Access Password\")\n .option(\"-y, --yes\", \"Skip confirmation\", false)\n\n .action(async (options) => {\n const cwd = options.cwd;\n if (!fs.existsSync(cwd)) {\n logger.error(`The directory \"${cwd}\" does not exist.`);\n process.exit(1);\n }\n\n const config = await getConfig({\n cwd,\n configPath: options.config,\n });\n if (!config) {\n logger.error(\n \"No configuration file found. Add a `auth.ts` file to your project or pass the path to the configuration file using the `--config` flag.\",\n );\n return;\n }\n\n const adapter = await getAdapter(config).catch((e) => {\n logger.error(e.message);\n process.exit(1);\n });\n\n if (adapter.id !== \"filemaker\") {\n logger.error(\"This generator is only compatible with the FileMaker adapter.\");\n return;\n }\n\n const betterAuthSchema = getSchema(config);\n\n // Extract Database from the adapter factory or resolved adapter.\n // config.database is the FileMakerAdapter factory function (has .database set on it).\n // adapter is the resolved adapter after getAdapter() calls the factory (also has .database).\n // Try both: adapter first (post-call), then config.database (pre-call / factory function).\n const configDb =\n (adapter as unknown as { database?: Database }).database ??\n (config.database as unknown as { database?: Database } | undefined)?.database;\n if (!configDb || typeof configDb !== \"object\" || !(\"schema\" in configDb)) {\n logger.error(\n \"Could not extract Database instance from adapter. Ensure your auth.ts uses FileMakerAdapter with an fmodata Database.\",\n );\n process.exit(1);\n }\n let db: Database = configDb;\n\n // Extract database name and server URL for display.\n // Try the public getter first (_getDatabaseName), fall back to the private field (databaseName).\n const dbObj = configDb as unknown as {\n _getDatabaseName?: string;\n databaseName?: string;\n context?: { _getBaseUrl?: () => string; _fetchClientOptions?: unknown };\n };\n const dbName: string = dbObj._getDatabaseName ?? dbObj.databaseName ?? \"\";\n const baseUrl: string | undefined = dbObj.context?._getBaseUrl?.();\n const serverUrl = baseUrl ? new URL(baseUrl).origin : undefined;\n\n // If CLI credential overrides are provided, construct a new connection\n if (options.username && options.password) {\n if (!dbName) {\n logger.error(\"Could not determine database filename from adapter config.\");\n process.exit(1);\n }\n\n if (!baseUrl) {\n logger.error(\n \"Could not determine server URL from adapter config. Ensure your auth.ts uses FMServerConnection.\",\n );\n process.exit(1);\n }\n\n const fetchClientOptions = dbObj.context?._fetchClientOptions as FFetchOptions | undefined;\n const connection = new FMServerConnection({\n serverUrl: serverUrl as string,\n auth: {\n username: options.username,\n password: options.password,\n },\n fetchClientOptions,\n });\n\n db = connection.database(dbName);\n }\n\n let migrationPlan: Awaited<ReturnType<typeof planMigration>>;\n try {\n migrationPlan = await planMigration(db, betterAuthSchema);\n } catch (err) {\n logger.error(`Failed to read database schema: ${err instanceof Error ? err.message : err}`);\n process.exit(1);\n }\n\n if (migrationPlan.length === 0) {\n logger.info(\"No changes to apply. Database is up to date.\");\n return;\n }\n\n if (!options.yes) {\n prettyPrintMigrationPlan(migrationPlan, { serverUrl, fileName: dbName });\n\n if (migrationPlan.length > 0) {\n console.log(chalk.gray(\"💡 Tip: You can use the --yes flag to skip this confirmation.\"));\n }\n\n const { confirm } = await prompts({\n type: \"confirm\",\n name: \"confirm\",\n message: \"Apply the above changes to your database?\",\n });\n if (!confirm) {\n logger.error(\"Schema changes not applied.\");\n return;\n }\n }\n\n try {\n await executeMigration(db, migrationPlan);\n logger.info(\"Migration applied successfully.\");\n } catch {\n process.exit(1);\n }\n });\n await program.parseAsync(process.argv);\n process.exit(0);\n}\n\nmain().catch((err) => {\n logger.error(err.message ?? err);\n process.exit(1);\n});\n"],"names":[],"mappings":";;;;;;;;;;;AAaA,eAAe,OAAO;AACpB,QAAM,UAAU,IAAI,QAAA;AAEpB,UACG,QAAQ,WAAW,EAAE,WAAW,MAAM,EACtC,OAAO,gBAAgB,yCAAyC,QAAQ,IAAA,CAAK,EAC7E,OAAO,mBAAmB,yBAAyB,EACnD,OAAO,6BAA6B,sBAAsB,EAC1D,OAAO,6BAA6B,sBAAsB,EAC1D,OAAO,aAAa,qBAAqB,KAAK,EAE9C,OAAO,OAAO,YAAY;;AACzB,UAAM,MAAM,QAAQ;AACpB,QAAI,CAAC,GAAG,WAAW,GAAG,GAAG;AACvB,aAAO,MAAM,kBAAkB,GAAG,mBAAmB;AACrD,cAAQ,KAAK,CAAC;AAAA,IAChB;AAEA,UAAM,SAAS,MAAM,UAAU;AAAA,MAC7B;AAAA,MACA,YAAY,QAAQ;AAAA,IAAA,CACrB;AACD,QAAI,CAAC,QAAQ;AACX,aAAO;AAAA,QACL;AAAA,MAAA;AAEF;AAAA,IACF;AAEA,UAAM,UAAU,MAAM,WAAW,MAAM,EAAE,MAAM,CAAC,MAAM;AACpD,aAAO,MAAM,EAAE,OAAO;AACtB,cAAQ,KAAK,CAAC;AAAA,IAChB,CAAC;AAED,QAAI,QAAQ,OAAO,aAAa;AAC9B,aAAO,MAAM,+DAA+D;AAC5E;AAAA,IACF;AAEA,UAAM,mBAAmB,UAAU,MAAM;AAMzC,UAAM,WACH,QAA+C,cAC/C,YAAO,aAAP,mBAAoE;AACvE,QAAI,CAAC,YAAY,OAAO,aAAa,YAAY,EAAE,YAAY,WAAW;AACxE,aAAO;AAAA,QACL;AAAA,MAAA;AAEF,cAAQ,KAAK,CAAC;AAAA,IAChB;AACA,QAAI,KAAe;AAInB,UAAM,QAAQ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