@better-auth/memory-adapter 1.5.0-beta.9 → 1.5.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.
- package/README.md +17 -0
- package/dist/index.d.mts +2 -1
- package/dist/index.mjs +25 -20
- package/dist/index.mjs.map +1 -0
- package/package.json +24 -7
- package/.turbo/turbo-build.log +0 -13
- package/src/index.ts +0 -5
- package/src/memory-adapter.ts +0 -355
- package/tsconfig.json +0 -9
- package/tsdown.config.ts +0 -7
- package/vitest.config.ts +0 -3
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# Better Auth Memory Adapter
|
|
2
|
+
|
|
3
|
+
In-memory adapter for [Better Auth](https://www.better-auth.com) — useful for development and testing.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @better-auth/memory-adapter
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
For full documentation, visit [better-auth.com](https://www.better-auth.com).
|
|
14
|
+
|
|
15
|
+
## License
|
|
16
|
+
|
|
17
|
+
MIT
|
package/dist/index.d.mts
CHANGED
|
@@ -11,4 +11,5 @@ interface MemoryAdapterConfig {
|
|
|
11
11
|
}
|
|
12
12
|
declare const memoryAdapter: (db: MemoryDB, config?: MemoryAdapterConfig | undefined) => (options: BetterAuthOptions) => _better_auth_core_db_adapter0.DBAdapter<BetterAuthOptions>;
|
|
13
13
|
//#endregion
|
|
14
|
-
export { type MemoryAdapterConfig, type MemoryDB, memoryAdapter };
|
|
14
|
+
export { type MemoryAdapterConfig, type MemoryDB, memoryAdapter };
|
|
15
|
+
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -12,7 +12,7 @@ const memoryAdapter = (db, config) => {
|
|
|
12
12
|
debugLogs: config?.debugLogs || false,
|
|
13
13
|
supportsArrays: true,
|
|
14
14
|
customTransformInput(props) {
|
|
15
|
-
if (
|
|
15
|
+
if (props.options.advanced?.database?.generateId === "serial" && props.field === "id" && props.action === "create") return db[props.model].length + 1;
|
|
16
16
|
return props.data;
|
|
17
17
|
},
|
|
18
18
|
transaction: async (cb) => {
|
|
@@ -27,7 +27,7 @@ const memoryAdapter = (db, config) => {
|
|
|
27
27
|
}
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
|
-
adapter: ({ getFieldName, options, getModelName }) => {
|
|
30
|
+
adapter: ({ getFieldName, getDefaultFieldName, options, getModelName }) => {
|
|
31
31
|
const applySortToRecords = (records, sortBy, model) => {
|
|
32
32
|
if (!sortBy) return records;
|
|
33
33
|
return records.sort((a, b) => {
|
|
@@ -49,12 +49,12 @@ const memoryAdapter = (db, config) => {
|
|
|
49
49
|
return sortBy.direction === "asc" ? comparison : -comparison;
|
|
50
50
|
});
|
|
51
51
|
};
|
|
52
|
-
function convertWhereClause(where, model, join) {
|
|
53
|
-
const
|
|
54
|
-
const table = db[model
|
|
52
|
+
function convertWhereClause(where, model, join, select) {
|
|
53
|
+
const baseRecords = (() => {
|
|
54
|
+
const table = db[model];
|
|
55
55
|
if (!table) {
|
|
56
|
-
logger.error(`[MemoryAdapter] Model ${model
|
|
57
|
-
throw new Error(`Model ${model
|
|
56
|
+
logger.error(`[MemoryAdapter] Model ${model} not found in the DB`, Object.keys(db));
|
|
57
|
+
throw new Error(`Model ${model} not found`);
|
|
58
58
|
}
|
|
59
59
|
const evalClause = (record, clause) => {
|
|
60
60
|
const { field, value, operator } = clause;
|
|
@@ -76,19 +76,23 @@ const memoryAdapter = (db, config) => {
|
|
|
76
76
|
default: return record[field] === value;
|
|
77
77
|
}
|
|
78
78
|
};
|
|
79
|
-
|
|
80
|
-
if (!where
|
|
81
|
-
let result = evalClause(record, where
|
|
82
|
-
for (const clause of where
|
|
79
|
+
let records = table.filter((record) => {
|
|
80
|
+
if (!where.length || where.length === 0) return true;
|
|
81
|
+
let result = evalClause(record, where[0]);
|
|
82
|
+
for (const clause of where) {
|
|
83
83
|
const clauseResult = evalClause(record, clause);
|
|
84
84
|
if (clause.connector === "OR") result = result || clauseResult;
|
|
85
85
|
else result = result && clauseResult;
|
|
86
86
|
}
|
|
87
87
|
return result;
|
|
88
88
|
});
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
if (select?.length && select.length > 0) records = records.map((record) => Object.fromEntries(Object.entries(record).filter(([key]) => select.includes(getDefaultFieldName({
|
|
90
|
+
model,
|
|
91
|
+
field: key
|
|
92
|
+
})))));
|
|
93
|
+
return records;
|
|
94
|
+
})();
|
|
95
|
+
if (!join) return baseRecords;
|
|
92
96
|
const grouped = /* @__PURE__ */ new Map();
|
|
93
97
|
const seenIds = /* @__PURE__ */ new Map();
|
|
94
98
|
for (const baseRecord of baseRecords) {
|
|
@@ -134,13 +138,13 @@ const memoryAdapter = (db, config) => {
|
|
|
134
138
|
}
|
|
135
139
|
return {
|
|
136
140
|
create: async ({ model, data }) => {
|
|
137
|
-
if (options.advanced?.database?.
|
|
141
|
+
if (options.advanced?.database?.generateId === "serial") data.id = db[getModelName(model)].length + 1;
|
|
138
142
|
if (!db[model]) db[model] = [];
|
|
139
143
|
db[model].push(data);
|
|
140
144
|
return data;
|
|
141
145
|
},
|
|
142
|
-
findOne: async ({ model, where, join }) => {
|
|
143
|
-
const res = convertWhereClause(where, model, join);
|
|
146
|
+
findOne: async ({ model, where, select, join }) => {
|
|
147
|
+
const res = convertWhereClause(where, model, join, select);
|
|
144
148
|
if (join) {
|
|
145
149
|
const resArray = res;
|
|
146
150
|
if (!resArray.length) return null;
|
|
@@ -148,8 +152,8 @@ const memoryAdapter = (db, config) => {
|
|
|
148
152
|
}
|
|
149
153
|
return res[0] || null;
|
|
150
154
|
},
|
|
151
|
-
findMany: async ({ model, where, sortBy, limit, offset, join }) => {
|
|
152
|
-
const res = convertWhereClause(where || [], model, join);
|
|
155
|
+
findMany: async ({ model, where, sortBy, limit, select, offset, join }) => {
|
|
156
|
+
const res = convertWhereClause(where || [], model, join, select);
|
|
153
157
|
if (join) {
|
|
154
158
|
const resArray = res;
|
|
155
159
|
if (!resArray.length) return [];
|
|
@@ -210,4 +214,5 @@ const memoryAdapter = (db, config) => {
|
|
|
210
214
|
};
|
|
211
215
|
|
|
212
216
|
//#endregion
|
|
213
|
-
export { memoryAdapter };
|
|
217
|
+
export { memoryAdapter };
|
|
218
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../src/memory-adapter.ts"],"sourcesContent":["import type { BetterAuthOptions } from \"@better-auth/core\";\nimport type {\n\tCleanedWhere,\n\tDBAdapterDebugLogOption,\n\tJoinConfig,\n} from \"@better-auth/core/db/adapter\";\nimport { createAdapterFactory } from \"@better-auth/core/db/adapter\";\nimport { logger } from \"@better-auth/core/env\";\n\nexport interface MemoryDB {\n\t[key: string]: any[];\n}\n\nexport interface MemoryAdapterConfig {\n\tdebugLogs?: DBAdapterDebugLogOption | undefined;\n}\n\nexport const memoryAdapter = (\n\tdb: MemoryDB,\n\tconfig?: MemoryAdapterConfig | undefined,\n) => {\n\tlet lazyOptions: BetterAuthOptions | null = null;\n\tconst adapterCreator = createAdapterFactory({\n\t\tconfig: {\n\t\t\tadapterId: \"memory\",\n\t\t\tadapterName: \"Memory Adapter\",\n\t\t\tusePlural: false,\n\t\t\tdebugLogs: config?.debugLogs || false,\n\t\t\tsupportsArrays: true,\n\t\t\tcustomTransformInput(props) {\n\t\t\t\tconst useNumberId =\n\t\t\t\t\tprops.options.advanced?.database?.generateId === \"serial\";\n\t\t\t\tif (useNumberId && props.field === \"id\" && props.action === \"create\") {\n\t\t\t\t\treturn db[props.model]!.length + 1;\n\t\t\t\t}\n\t\t\t\treturn props.data;\n\t\t\t},\n\t\t\ttransaction: async (cb) => {\n\t\t\t\tconst clone = structuredClone(db);\n\t\t\t\ttry {\n\t\t\t\t\tconst r = await cb(adapterCreator(lazyOptions!));\n\t\t\t\t\treturn r;\n\t\t\t\t} catch (error) {\n\t\t\t\t\t// Rollback changes\n\t\t\t\t\tObject.keys(db).forEach((key) => {\n\t\t\t\t\t\tdb[key] = clone[key]!;\n\t\t\t\t\t});\n\t\t\t\t\tthrow error;\n\t\t\t\t}\n\t\t\t},\n\t\t},\n\t\tadapter: ({ getFieldName, getDefaultFieldName, options, getModelName }) => {\n\t\t\tconst applySortToRecords = (\n\t\t\t\trecords: any[],\n\t\t\t\tsortBy: { field: string; direction: \"asc\" | \"desc\" } | undefined,\n\t\t\t\tmodel: string,\n\t\t\t) => {\n\t\t\t\tif (!sortBy) return records;\n\t\t\t\treturn records.sort((a: any, b: any) => {\n\t\t\t\t\tconst field = getFieldName({ model, field: sortBy.field });\n\t\t\t\t\tconst aValue = a[field];\n\t\t\t\t\tconst bValue = b[field];\n\n\t\t\t\t\tlet comparison = 0;\n\n\t\t\t\t\t// Handle null/undefined values\n\t\t\t\t\tif (aValue == null && bValue == null) {\n\t\t\t\t\t\tcomparison = 0;\n\t\t\t\t\t} else if (aValue == null) {\n\t\t\t\t\t\tcomparison = -1;\n\t\t\t\t\t} else if (bValue == null) {\n\t\t\t\t\t\tcomparison = 1;\n\t\t\t\t\t}\n\t\t\t\t\t// Handle string comparison\n\t\t\t\t\telse if (typeof aValue === \"string\" && typeof bValue === \"string\") {\n\t\t\t\t\t\tcomparison = aValue.localeCompare(bValue);\n\t\t\t\t\t}\n\t\t\t\t\t// Handle date comparison\n\t\t\t\t\telse if (aValue instanceof Date && bValue instanceof Date) {\n\t\t\t\t\t\tcomparison = aValue.getTime() - bValue.getTime();\n\t\t\t\t\t}\n\t\t\t\t\t// Handle numeric comparison\n\t\t\t\t\telse if (typeof aValue === \"number\" && typeof bValue === \"number\") {\n\t\t\t\t\t\tcomparison = aValue - bValue;\n\t\t\t\t\t}\n\t\t\t\t\t// Handle boolean comparison\n\t\t\t\t\telse if (typeof aValue === \"boolean\" && typeof bValue === \"boolean\") {\n\t\t\t\t\t\tcomparison = aValue === bValue ? 0 : aValue ? 1 : -1;\n\t\t\t\t\t}\n\t\t\t\t\t// Fallback to string comparison\n\t\t\t\t\telse {\n\t\t\t\t\t\tcomparison = String(aValue).localeCompare(String(bValue));\n\t\t\t\t\t}\n\n\t\t\t\t\treturn sortBy.direction === \"asc\" ? comparison : -comparison;\n\t\t\t\t});\n\t\t\t};\n\n\t\t\tfunction convertWhereClause(\n\t\t\t\twhere: CleanedWhere[],\n\t\t\t\tmodel: string,\n\t\t\t\tjoin?: JoinConfig,\n\t\t\t\tselect?: string[],\n\t\t\t): any[] {\n\t\t\t\tconst baseRecords = (() => {\n\t\t\t\t\tconst table = db[model];\n\t\t\t\t\tif (!table) {\n\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t`[MemoryAdapter] Model ${model} not found in the DB`,\n\t\t\t\t\t\t\tObject.keys(db),\n\t\t\t\t\t\t);\n\t\t\t\t\t\tthrow new Error(`Model ${model} not found`);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst evalClause = (record: any, clause: CleanedWhere): boolean => {\n\t\t\t\t\t\tconst { field, value, operator } = clause;\n\t\t\t\t\t\tswitch (operator) {\n\t\t\t\t\t\t\tcase \"in\":\n\t\t\t\t\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\t\t\t\t\tthrow new Error(\"Value must be an array\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\treturn value.includes(record[field]);\n\t\t\t\t\t\t\tcase \"not_in\":\n\t\t\t\t\t\t\t\tif (!Array.isArray(value)) {\n\t\t\t\t\t\t\t\t\tthrow new Error(\"Value must be an array\");\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\t\t\treturn !value.includes(record[field]);\n\t\t\t\t\t\t\tcase \"contains\":\n\t\t\t\t\t\t\t\treturn record[field].includes(value);\n\t\t\t\t\t\t\tcase \"starts_with\":\n\t\t\t\t\t\t\t\treturn record[field].startsWith(value);\n\t\t\t\t\t\t\tcase \"ends_with\":\n\t\t\t\t\t\t\t\treturn record[field].endsWith(value);\n\t\t\t\t\t\t\tcase \"ne\":\n\t\t\t\t\t\t\t\treturn record[field] !== value;\n\t\t\t\t\t\t\tcase \"gt\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] > value);\n\t\t\t\t\t\t\tcase \"gte\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] >= value);\n\t\t\t\t\t\t\tcase \"lt\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] < value);\n\t\t\t\t\t\t\tcase \"lte\":\n\t\t\t\t\t\t\t\treturn value != null && Boolean(record[field] <= value);\n\t\t\t\t\t\t\tdefault:\n\t\t\t\t\t\t\t\treturn record[field] === value;\n\t\t\t\t\t\t}\n\t\t\t\t\t};\n\n\t\t\t\t\tlet records = table.filter((record: any) => {\n\t\t\t\t\t\tif (!where.length || where.length === 0) {\n\t\t\t\t\t\t\treturn true;\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tlet result = evalClause(record, where[0]!);\n\t\t\t\t\t\tfor (const clause of where) {\n\t\t\t\t\t\t\tconst clauseResult = evalClause(record, clause);\n\n\t\t\t\t\t\t\tif (clause.connector === \"OR\") {\n\t\t\t\t\t\t\t\tresult = result || clauseResult;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tresult = result && clauseResult;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t});\n\t\t\t\t\tif (select?.length && select.length > 0) {\n\t\t\t\t\t\trecords = records.map((record: any) =>\n\t\t\t\t\t\t\tObject.fromEntries(\n\t\t\t\t\t\t\t\tObject.entries(record).filter(([key]) =>\n\t\t\t\t\t\t\t\t\tselect.includes(getDefaultFieldName({ model, field: key })),\n\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t),\n\t\t\t\t\t\t);\n\t\t\t\t\t}\n\t\t\t\t\treturn records;\n\t\t\t\t})();\n\n\t\t\t\tif (!join) return baseRecords;\n\n\t\t\t\t// Group results by base model and nest joined data as arrays\n\t\t\t\tconst grouped = new Map<string, any>();\n\t\t\t\t// Track seen IDs per joined model for O(1) deduplication\n\t\t\t\tconst seenIds = new Map<string, Set<string>>();\n\n\t\t\t\tfor (const baseRecord of baseRecords) {\n\t\t\t\t\tconst baseId = String(baseRecord.id);\n\n\t\t\t\t\tif (!grouped.has(baseId)) {\n\t\t\t\t\t\tconst nested: Record<string, any> = { ...baseRecord };\n\n\t\t\t\t\t\t// Initialize joined data structures based on isUnique\n\t\t\t\t\t\tfor (const [joinModel, joinAttr] of Object.entries(join)) {\n\t\t\t\t\t\t\tconst joinModelName = getModelName(joinModel);\n\t\t\t\t\t\t\tif (joinAttr.relation === \"one-to-one\") {\n\t\t\t\t\t\t\t\tnested[joinModelName] = null;\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tnested[joinModelName] = [];\n\t\t\t\t\t\t\t\tseenIds.set(`${baseId}-${joinModel}`, new Set());\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tgrouped.set(baseId, nested);\n\t\t\t\t\t}\n\n\t\t\t\t\tconst nestedEntry = grouped.get(baseId)!;\n\n\t\t\t\t\t// Add joined data\n\t\t\t\t\tfor (const [joinModel, joinAttr] of Object.entries(join)) {\n\t\t\t\t\t\tconst joinModelName = getModelName(joinModel);\n\t\t\t\t\t\tconst joinTable = db[joinModelName];\n\t\t\t\t\t\tif (!joinTable) {\n\t\t\t\t\t\t\tlogger.error(\n\t\t\t\t\t\t\t\t`[MemoryAdapter] JoinOption model ${joinModelName} not found in the DB`,\n\t\t\t\t\t\t\t\tObject.keys(db),\n\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\tthrow new Error(`JoinOption model ${joinModelName} not found`);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tconst matchingRecords = joinTable.filter(\n\t\t\t\t\t\t\t(joinRecord: any) =>\n\t\t\t\t\t\t\t\tjoinRecord[joinAttr.on.to] === baseRecord[joinAttr.on.from],\n\t\t\t\t\t\t);\n\n\t\t\t\t\t\tif (joinAttr.relation === \"one-to-one\") {\n\t\t\t\t\t\t\t// For unique relationships, store a single object (or null)\n\t\t\t\t\t\t\tnestedEntry[joinModelName] = matchingRecords[0] || null;\n\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t// For non-unique relationships, store array with limit\n\t\t\t\t\t\t\tconst seenSet = seenIds.get(`${baseId}-${joinModel}`)!;\n\t\t\t\t\t\t\tconst limit = joinAttr.limit ?? 100;\n\t\t\t\t\t\t\tlet count = 0;\n\n\t\t\t\t\t\t\tfor (const matchingRecord of matchingRecords) {\n\t\t\t\t\t\t\t\tif (count >= limit) break;\n\t\t\t\t\t\t\t\tif (!seenSet.has(matchingRecord.id)) {\n\t\t\t\t\t\t\t\t\tnestedEntry[joinModelName].push(matchingRecord);\n\t\t\t\t\t\t\t\t\tseenSet.add(matchingRecord.id);\n\t\t\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\treturn Array.from(grouped.values());\n\t\t\t}\n\t\t\treturn {\n\t\t\t\tcreate: async ({ model, data }) => {\n\t\t\t\t\tconst useNumberId =\n\t\t\t\t\t\toptions.advanced?.database?.generateId === \"serial\";\n\t\t\t\t\tif (useNumberId) {\n\t\t\t\t\t\t// @ts-expect-error\n\t\t\t\t\t\tdata.id = db[getModelName(model)]!.length + 1;\n\t\t\t\t\t}\n\t\t\t\t\tif (!db[model]) {\n\t\t\t\t\t\tdb[model] = [];\n\t\t\t\t\t}\n\t\t\t\t\tdb[model]!.push(data);\n\t\t\t\t\treturn data;\n\t\t\t\t},\n\t\t\t\tfindOne: async ({ model, where, select, join }) => {\n\t\t\t\t\tconst res = convertWhereClause(where, model, join, select);\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\t// When join is present, res is an array of nested objects\n\t\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\t\tif (!resArray.length) {\n\t\t\t\t\t\t\treturn null;\n\t\t\t\t\t\t}\n\t\t\t\t\t\t// Return the first nested object\n\t\t\t\t\t\treturn resArray[0];\n\t\t\t\t\t}\n\t\t\t\t\t// Without join, res is an array\n\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\tconst record = resArray[0] || null;\n\t\t\t\t\treturn record;\n\t\t\t\t},\n\t\t\t\tfindMany: async ({\n\t\t\t\t\tmodel,\n\t\t\t\t\twhere,\n\t\t\t\t\tsortBy,\n\t\t\t\t\tlimit,\n\t\t\t\t\tselect,\n\t\t\t\t\toffset,\n\t\t\t\t\tjoin,\n\t\t\t\t}) => {\n\t\t\t\t\tconst res = convertWhereClause(where || [], model, join, select);\n\n\t\t\t\t\tif (join) {\n\t\t\t\t\t\t// When join is present, res is an array of nested objects\n\t\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\t\tif (!resArray.length) {\n\t\t\t\t\t\t\treturn [];\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\t// Apply sorting to nested objects\n\t\t\t\t\t\tapplySortToRecords(resArray, sortBy, model);\n\n\t\t\t\t\t\t// Apply offset and limit\n\t\t\t\t\t\tlet paginatedRecords = resArray;\n\t\t\t\t\t\tif (offset !== undefined) {\n\t\t\t\t\t\t\tpaginatedRecords = paginatedRecords.slice(offset);\n\t\t\t\t\t\t}\n\t\t\t\t\t\tif (limit !== undefined) {\n\t\t\t\t\t\t\tpaginatedRecords = paginatedRecords.slice(0, limit);\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\treturn paginatedRecords;\n\t\t\t\t\t}\n\n\t\t\t\t\t// Without join - original logic\n\t\t\t\t\tconst resArray = res as any[];\n\t\t\t\t\tlet table = applySortToRecords(resArray, sortBy, model);\n\t\t\t\t\tif (offset !== undefined) {\n\t\t\t\t\t\ttable = table!.slice(offset);\n\t\t\t\t\t}\n\t\t\t\t\tif (limit !== undefined) {\n\t\t\t\t\t\ttable = table!.slice(0, limit);\n\t\t\t\t\t}\n\t\t\t\t\treturn table || [];\n\t\t\t\t},\n\t\t\t\tcount: async ({ model, where }) => {\n\t\t\t\t\tif (where) {\n\t\t\t\t\t\tconst filteredRecords = convertWhereClause(where, model);\n\t\t\t\t\t\treturn filteredRecords.length;\n\t\t\t\t\t}\n\t\t\t\t\treturn db[model]!.length;\n\t\t\t\t},\n\t\t\t\tupdate: async ({ model, where, update }) => {\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tres.forEach((record) => {\n\t\t\t\t\t\tObject.assign(record, update);\n\t\t\t\t\t});\n\t\t\t\t\treturn res[0] || null;\n\t\t\t\t},\n\t\t\t\tdelete: async ({ model, where }) => {\n\t\t\t\t\tconst table = db[model]!;\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tdb[model] = table.filter((record) => !res.includes(record));\n\t\t\t\t},\n\t\t\t\tdeleteMany: async ({ model, where }) => {\n\t\t\t\t\tconst table = db[model]!;\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tlet count = 0;\n\t\t\t\t\tdb[model] = table.filter((record) => {\n\t\t\t\t\t\tif (res.includes(record)) {\n\t\t\t\t\t\t\tcount++;\n\t\t\t\t\t\t\treturn false;\n\t\t\t\t\t\t}\n\t\t\t\t\t\treturn !res.includes(record);\n\t\t\t\t\t});\n\t\t\t\t\treturn count;\n\t\t\t\t},\n\t\t\t\tupdateMany({ model, where, update }) {\n\t\t\t\t\tconst res = convertWhereClause(where, model);\n\t\t\t\t\tres.forEach((record) => {\n\t\t\t\t\t\tObject.assign(record, update);\n\t\t\t\t\t});\n\t\t\t\t\treturn res[0] || null;\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\t});\n\treturn (options: BetterAuthOptions) => {\n\t\tlazyOptions = options;\n\t\treturn adapterCreator(options);\n\t};\n};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memory-adapter",
|
|
3
|
-
"version": "1.5.0
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Memory adapter for Better Auth",
|
|
5
5
|
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"homepage": "https://www.better-auth.com",
|
|
6
8
|
"repository": {
|
|
7
9
|
"type": "git",
|
|
8
10
|
"url": "git+https://github.com/better-auth/better-auth.git",
|
|
9
11
|
"directory": "packages/memory-adapter"
|
|
10
12
|
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"auth",
|
|
15
|
+
"memory",
|
|
16
|
+
"adapter",
|
|
17
|
+
"typescript",
|
|
18
|
+
"better-auth"
|
|
19
|
+
],
|
|
20
|
+
"publishConfig": {
|
|
21
|
+
"access": "public"
|
|
22
|
+
},
|
|
23
|
+
"files": [
|
|
24
|
+
"dist"
|
|
25
|
+
],
|
|
11
26
|
"main": "./dist/index.mjs",
|
|
12
27
|
"module": "./dist/index.mjs",
|
|
13
28
|
"types": "./dist/index.d.mts",
|
|
@@ -20,18 +35,20 @@
|
|
|
20
35
|
},
|
|
21
36
|
"peerDependencies": {
|
|
22
37
|
"@better-auth/utils": "^0.3.0",
|
|
23
|
-
"@better-auth/core": "1.5.0
|
|
38
|
+
"@better-auth/core": "1.5.0"
|
|
24
39
|
},
|
|
25
40
|
"devDependencies": {
|
|
26
|
-
"@better-auth/utils": "^0.3.
|
|
27
|
-
"tsdown": "^0.
|
|
41
|
+
"@better-auth/utils": "^0.3.1",
|
|
42
|
+
"tsdown": "^0.20.3",
|
|
28
43
|
"typescript": "^5.9.3",
|
|
29
|
-
"@better-auth/core": "1.5.0
|
|
44
|
+
"@better-auth/core": "1.5.0"
|
|
30
45
|
},
|
|
31
46
|
"scripts": {
|
|
32
47
|
"build": "tsdown",
|
|
33
48
|
"dev": "tsdown --watch",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
49
|
+
"lint:package": "publint run --strict",
|
|
50
|
+
"lint:types": "attw --profile esm-only --pack .",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"test": "vitest"
|
|
36
53
|
}
|
|
37
54
|
}
|
package/.turbo/turbo-build.log
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @better-auth/memory-adapter@1.5.0-beta.9 build /home/runner/work/better-auth/better-auth/packages/memory-adapter
|
|
3
|
-
> tsdown
|
|
4
|
-
|
|
5
|
-
[34mℹ[39m tsdown [2mv0.19.0[22m powered by rolldown [2mv1.0.0-beta.59[22m
|
|
6
|
-
[34mℹ[39m config file: [4m/home/runner/work/better-auth/better-auth/packages/memory-adapter/tsdown.config.ts[24m
|
|
7
|
-
[34mℹ[39m entry: [34msrc/index.ts[39m
|
|
8
|
-
[34mℹ[39m tsconfig: [34mtsconfig.json[39m
|
|
9
|
-
[34mℹ[39m Build start
|
|
10
|
-
[34mℹ[39m [2mdist/[22m[1mindex.mjs[22m [2m8.16 kB[22m [2m│ gzip: 2.21 kB[22m
|
|
11
|
-
[34mℹ[39m [2mdist/[22m[32m[1mindex.d.mts[22m[39m [2m0.63 kB[22m [2m│ gzip: 0.29 kB[22m
|
|
12
|
-
[34mℹ[39m 2 files, total: 8.79 kB
|
|
13
|
-
[32m✔[39m Build complete in [32m7750ms[39m
|
package/src/index.ts
DELETED
package/src/memory-adapter.ts
DELETED
|
@@ -1,355 +0,0 @@
|
|
|
1
|
-
import type { BetterAuthOptions } from "@better-auth/core";
|
|
2
|
-
import type {
|
|
3
|
-
CleanedWhere,
|
|
4
|
-
DBAdapterDebugLogOption,
|
|
5
|
-
JoinConfig,
|
|
6
|
-
} from "@better-auth/core/db/adapter";
|
|
7
|
-
import { createAdapterFactory } from "@better-auth/core/db/adapter";
|
|
8
|
-
import { logger } from "@better-auth/core/env";
|
|
9
|
-
|
|
10
|
-
export interface MemoryDB {
|
|
11
|
-
[key: string]: any[];
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
export interface MemoryAdapterConfig {
|
|
15
|
-
debugLogs?: DBAdapterDebugLogOption | undefined;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export const memoryAdapter = (
|
|
19
|
-
db: MemoryDB,
|
|
20
|
-
config?: MemoryAdapterConfig | undefined,
|
|
21
|
-
) => {
|
|
22
|
-
let lazyOptions: BetterAuthOptions | null = null;
|
|
23
|
-
const adapterCreator = createAdapterFactory({
|
|
24
|
-
config: {
|
|
25
|
-
adapterId: "memory",
|
|
26
|
-
adapterName: "Memory Adapter",
|
|
27
|
-
usePlural: false,
|
|
28
|
-
debugLogs: config?.debugLogs || false,
|
|
29
|
-
supportsArrays: true,
|
|
30
|
-
customTransformInput(props) {
|
|
31
|
-
const useNumberId =
|
|
32
|
-
props.options.advanced?.database?.useNumberId ||
|
|
33
|
-
props.options.advanced?.database?.generateId === "serial";
|
|
34
|
-
if (useNumberId && props.field === "id" && props.action === "create") {
|
|
35
|
-
return db[props.model]!.length + 1;
|
|
36
|
-
}
|
|
37
|
-
return props.data;
|
|
38
|
-
},
|
|
39
|
-
transaction: async (cb) => {
|
|
40
|
-
const clone = structuredClone(db);
|
|
41
|
-
try {
|
|
42
|
-
const r = await cb(adapterCreator(lazyOptions!));
|
|
43
|
-
return r;
|
|
44
|
-
} catch (error) {
|
|
45
|
-
// Rollback changes
|
|
46
|
-
Object.keys(db).forEach((key) => {
|
|
47
|
-
db[key] = clone[key]!;
|
|
48
|
-
});
|
|
49
|
-
throw error;
|
|
50
|
-
}
|
|
51
|
-
},
|
|
52
|
-
},
|
|
53
|
-
adapter: ({ getFieldName, options, getModelName }) => {
|
|
54
|
-
const applySortToRecords = (
|
|
55
|
-
records: any[],
|
|
56
|
-
sortBy: { field: string; direction: "asc" | "desc" } | undefined,
|
|
57
|
-
model: string,
|
|
58
|
-
) => {
|
|
59
|
-
if (!sortBy) return records;
|
|
60
|
-
return records.sort((a: any, b: any) => {
|
|
61
|
-
const field = getFieldName({ model, field: sortBy.field });
|
|
62
|
-
const aValue = a[field];
|
|
63
|
-
const bValue = b[field];
|
|
64
|
-
|
|
65
|
-
let comparison = 0;
|
|
66
|
-
|
|
67
|
-
// Handle null/undefined values
|
|
68
|
-
if (aValue == null && bValue == null) {
|
|
69
|
-
comparison = 0;
|
|
70
|
-
} else if (aValue == null) {
|
|
71
|
-
comparison = -1;
|
|
72
|
-
} else if (bValue == null) {
|
|
73
|
-
comparison = 1;
|
|
74
|
-
}
|
|
75
|
-
// Handle string comparison
|
|
76
|
-
else if (typeof aValue === "string" && typeof bValue === "string") {
|
|
77
|
-
comparison = aValue.localeCompare(bValue);
|
|
78
|
-
}
|
|
79
|
-
// Handle date comparison
|
|
80
|
-
else if (aValue instanceof Date && bValue instanceof Date) {
|
|
81
|
-
comparison = aValue.getTime() - bValue.getTime();
|
|
82
|
-
}
|
|
83
|
-
// Handle numeric comparison
|
|
84
|
-
else if (typeof aValue === "number" && typeof bValue === "number") {
|
|
85
|
-
comparison = aValue - bValue;
|
|
86
|
-
}
|
|
87
|
-
// Handle boolean comparison
|
|
88
|
-
else if (typeof aValue === "boolean" && typeof bValue === "boolean") {
|
|
89
|
-
comparison = aValue === bValue ? 0 : aValue ? 1 : -1;
|
|
90
|
-
}
|
|
91
|
-
// Fallback to string comparison
|
|
92
|
-
else {
|
|
93
|
-
comparison = String(aValue).localeCompare(String(bValue));
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
return sortBy.direction === "asc" ? comparison : -comparison;
|
|
97
|
-
});
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
function convertWhereClause(
|
|
101
|
-
where: CleanedWhere[],
|
|
102
|
-
model: string,
|
|
103
|
-
join?: JoinConfig,
|
|
104
|
-
): any[] {
|
|
105
|
-
const execute = (where: CleanedWhere[], model: string) => {
|
|
106
|
-
const table = db[model];
|
|
107
|
-
if (!table) {
|
|
108
|
-
logger.error(
|
|
109
|
-
`[MemoryAdapter] Model ${model} not found in the DB`,
|
|
110
|
-
Object.keys(db),
|
|
111
|
-
);
|
|
112
|
-
throw new Error(`Model ${model} not found`);
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
const evalClause = (record: any, clause: CleanedWhere): boolean => {
|
|
116
|
-
const { field, value, operator } = clause;
|
|
117
|
-
switch (operator) {
|
|
118
|
-
case "in":
|
|
119
|
-
if (!Array.isArray(value)) {
|
|
120
|
-
throw new Error("Value must be an array");
|
|
121
|
-
}
|
|
122
|
-
// @ts-expect-error
|
|
123
|
-
return value.includes(record[field]);
|
|
124
|
-
case "not_in":
|
|
125
|
-
if (!Array.isArray(value)) {
|
|
126
|
-
throw new Error("Value must be an array");
|
|
127
|
-
}
|
|
128
|
-
// @ts-expect-error
|
|
129
|
-
return !value.includes(record[field]);
|
|
130
|
-
case "contains":
|
|
131
|
-
return record[field].includes(value);
|
|
132
|
-
case "starts_with":
|
|
133
|
-
return record[field].startsWith(value);
|
|
134
|
-
case "ends_with":
|
|
135
|
-
return record[field].endsWith(value);
|
|
136
|
-
case "ne":
|
|
137
|
-
return record[field] !== value;
|
|
138
|
-
case "gt":
|
|
139
|
-
return value != null && Boolean(record[field] > value);
|
|
140
|
-
case "gte":
|
|
141
|
-
return value != null && Boolean(record[field] >= value);
|
|
142
|
-
case "lt":
|
|
143
|
-
return value != null && Boolean(record[field] < value);
|
|
144
|
-
case "lte":
|
|
145
|
-
return value != null && Boolean(record[field] <= value);
|
|
146
|
-
default:
|
|
147
|
-
return record[field] === value;
|
|
148
|
-
}
|
|
149
|
-
};
|
|
150
|
-
|
|
151
|
-
return table.filter((record: any) => {
|
|
152
|
-
if (!where.length || where.length === 0) {
|
|
153
|
-
return true;
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
let result = evalClause(record, where[0]!);
|
|
157
|
-
for (const clause of where) {
|
|
158
|
-
const clauseResult = evalClause(record, clause);
|
|
159
|
-
|
|
160
|
-
if (clause.connector === "OR") {
|
|
161
|
-
result = result || clauseResult;
|
|
162
|
-
} else {
|
|
163
|
-
result = result && clauseResult;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return result;
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
|
|
171
|
-
if (!join) return execute(where, model);
|
|
172
|
-
|
|
173
|
-
const baseRecords = execute(where, model);
|
|
174
|
-
|
|
175
|
-
// Group results by base model and nest joined data as arrays
|
|
176
|
-
const grouped = new Map<string, any>();
|
|
177
|
-
// Track seen IDs per joined model for O(1) deduplication
|
|
178
|
-
const seenIds = new Map<string, Set<string>>();
|
|
179
|
-
|
|
180
|
-
for (const baseRecord of baseRecords) {
|
|
181
|
-
const baseId = String(baseRecord.id);
|
|
182
|
-
|
|
183
|
-
if (!grouped.has(baseId)) {
|
|
184
|
-
const nested: Record<string, any> = { ...baseRecord };
|
|
185
|
-
|
|
186
|
-
// Initialize joined data structures based on isUnique
|
|
187
|
-
for (const [joinModel, joinAttr] of Object.entries(join)) {
|
|
188
|
-
const joinModelName = getModelName(joinModel);
|
|
189
|
-
if (joinAttr.relation === "one-to-one") {
|
|
190
|
-
nested[joinModelName] = null;
|
|
191
|
-
} else {
|
|
192
|
-
nested[joinModelName] = [];
|
|
193
|
-
seenIds.set(`${baseId}-${joinModel}`, new Set());
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
grouped.set(baseId, nested);
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
const nestedEntry = grouped.get(baseId)!;
|
|
201
|
-
|
|
202
|
-
// Add joined data
|
|
203
|
-
for (const [joinModel, joinAttr] of Object.entries(join)) {
|
|
204
|
-
const joinModelName = getModelName(joinModel);
|
|
205
|
-
const joinTable = db[joinModelName];
|
|
206
|
-
if (!joinTable) {
|
|
207
|
-
logger.error(
|
|
208
|
-
`[MemoryAdapter] JoinOption model ${joinModelName} not found in the DB`,
|
|
209
|
-
Object.keys(db),
|
|
210
|
-
);
|
|
211
|
-
throw new Error(`JoinOption model ${joinModelName} not found`);
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
const matchingRecords = joinTable.filter(
|
|
215
|
-
(joinRecord: any) =>
|
|
216
|
-
joinRecord[joinAttr.on.to] === baseRecord[joinAttr.on.from],
|
|
217
|
-
);
|
|
218
|
-
|
|
219
|
-
if (joinAttr.relation === "one-to-one") {
|
|
220
|
-
// For unique relationships, store a single object (or null)
|
|
221
|
-
nestedEntry[joinModelName] = matchingRecords[0] || null;
|
|
222
|
-
} else {
|
|
223
|
-
// For non-unique relationships, store array with limit
|
|
224
|
-
const seenSet = seenIds.get(`${baseId}-${joinModel}`)!;
|
|
225
|
-
const limit = joinAttr.limit ?? 100;
|
|
226
|
-
let count = 0;
|
|
227
|
-
|
|
228
|
-
for (const matchingRecord of matchingRecords) {
|
|
229
|
-
if (count >= limit) break;
|
|
230
|
-
if (!seenSet.has(matchingRecord.id)) {
|
|
231
|
-
nestedEntry[joinModelName].push(matchingRecord);
|
|
232
|
-
seenSet.add(matchingRecord.id);
|
|
233
|
-
count++;
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
|
|
240
|
-
return Array.from(grouped.values());
|
|
241
|
-
}
|
|
242
|
-
return {
|
|
243
|
-
create: async ({ model, data }) => {
|
|
244
|
-
const useNumberId =
|
|
245
|
-
options.advanced?.database?.useNumberId ||
|
|
246
|
-
options.advanced?.database?.generateId === "serial";
|
|
247
|
-
if (useNumberId) {
|
|
248
|
-
// @ts-expect-error
|
|
249
|
-
data.id = db[getModelName(model)]!.length + 1;
|
|
250
|
-
}
|
|
251
|
-
if (!db[model]) {
|
|
252
|
-
db[model] = [];
|
|
253
|
-
}
|
|
254
|
-
db[model]!.push(data);
|
|
255
|
-
return data;
|
|
256
|
-
},
|
|
257
|
-
findOne: async ({ model, where, join }) => {
|
|
258
|
-
const res = convertWhereClause(where, model, join);
|
|
259
|
-
if (join) {
|
|
260
|
-
// When join is present, res is an array of nested objects
|
|
261
|
-
const resArray = res as any[];
|
|
262
|
-
if (!resArray.length) {
|
|
263
|
-
return null;
|
|
264
|
-
}
|
|
265
|
-
// Return the first nested object
|
|
266
|
-
return resArray[0];
|
|
267
|
-
}
|
|
268
|
-
// Without join, res is an array
|
|
269
|
-
const resArray = res as any[];
|
|
270
|
-
const record = resArray[0] || null;
|
|
271
|
-
return record;
|
|
272
|
-
},
|
|
273
|
-
findMany: async ({ model, where, sortBy, limit, offset, join }) => {
|
|
274
|
-
const res = convertWhereClause(where || [], model, join);
|
|
275
|
-
|
|
276
|
-
if (join) {
|
|
277
|
-
// When join is present, res is an array of nested objects
|
|
278
|
-
const resArray = res as any[];
|
|
279
|
-
if (!resArray.length) {
|
|
280
|
-
return [];
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// Apply sorting to nested objects
|
|
284
|
-
applySortToRecords(resArray, sortBy, model);
|
|
285
|
-
|
|
286
|
-
// Apply offset and limit
|
|
287
|
-
let paginatedRecords = resArray;
|
|
288
|
-
if (offset !== undefined) {
|
|
289
|
-
paginatedRecords = paginatedRecords.slice(offset);
|
|
290
|
-
}
|
|
291
|
-
if (limit !== undefined) {
|
|
292
|
-
paginatedRecords = paginatedRecords.slice(0, limit);
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
return paginatedRecords;
|
|
296
|
-
}
|
|
297
|
-
|
|
298
|
-
// Without join - original logic
|
|
299
|
-
const resArray = res as any[];
|
|
300
|
-
let table = applySortToRecords(resArray, sortBy, model);
|
|
301
|
-
if (offset !== undefined) {
|
|
302
|
-
table = table!.slice(offset);
|
|
303
|
-
}
|
|
304
|
-
if (limit !== undefined) {
|
|
305
|
-
table = table!.slice(0, limit);
|
|
306
|
-
}
|
|
307
|
-
return table || [];
|
|
308
|
-
},
|
|
309
|
-
count: async ({ model, where }) => {
|
|
310
|
-
if (where) {
|
|
311
|
-
const filteredRecords = convertWhereClause(where, model);
|
|
312
|
-
return filteredRecords.length;
|
|
313
|
-
}
|
|
314
|
-
return db[model]!.length;
|
|
315
|
-
},
|
|
316
|
-
update: async ({ model, where, update }) => {
|
|
317
|
-
const res = convertWhereClause(where, model);
|
|
318
|
-
res.forEach((record) => {
|
|
319
|
-
Object.assign(record, update);
|
|
320
|
-
});
|
|
321
|
-
return res[0] || null;
|
|
322
|
-
},
|
|
323
|
-
delete: async ({ model, where }) => {
|
|
324
|
-
const table = db[model]!;
|
|
325
|
-
const res = convertWhereClause(where, model);
|
|
326
|
-
db[model] = table.filter((record) => !res.includes(record));
|
|
327
|
-
},
|
|
328
|
-
deleteMany: async ({ model, where }) => {
|
|
329
|
-
const table = db[model]!;
|
|
330
|
-
const res = convertWhereClause(where, model);
|
|
331
|
-
let count = 0;
|
|
332
|
-
db[model] = table.filter((record) => {
|
|
333
|
-
if (res.includes(record)) {
|
|
334
|
-
count++;
|
|
335
|
-
return false;
|
|
336
|
-
}
|
|
337
|
-
return !res.includes(record);
|
|
338
|
-
});
|
|
339
|
-
return count;
|
|
340
|
-
},
|
|
341
|
-
updateMany({ model, where, update }) {
|
|
342
|
-
const res = convertWhereClause(where, model);
|
|
343
|
-
res.forEach((record) => {
|
|
344
|
-
Object.assign(record, update);
|
|
345
|
-
});
|
|
346
|
-
return res[0] || null;
|
|
347
|
-
},
|
|
348
|
-
};
|
|
349
|
-
},
|
|
350
|
-
});
|
|
351
|
-
return (options: BetterAuthOptions) => {
|
|
352
|
-
lazyOptions = options;
|
|
353
|
-
return adapterCreator(options);
|
|
354
|
-
};
|
|
355
|
-
};
|
package/tsconfig.json
DELETED
package/tsdown.config.ts
DELETED
package/vitest.config.ts
DELETED