-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathadapter.ts
More file actions
183 lines (147 loc) · 4.89 KB
/
adapter.ts
File metadata and controls
183 lines (147 loc) · 4.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import type {FindOptions, MikroORM} from "@mikro-orm/core"
import {type AdapterDebugLogs, createAdapter} from "better-auth/adapters"
import {dset} from "dset"
import {createAdapterUtils} from "./utils/adapterUtils.js"
export interface MikroOrmAdapterConfig {
/**
* Enable debug logs.
*
* @default false
*/
debugLogs?: AdapterDebugLogs
/**
* Indicates whether or not JSON is supported by target database.
*
* This option is enabled by default, because Mikro ORM supports JSON serialization/deserialization via [JsonType](https://mikro-orm.io/docs/custom-types#jsontype).
* See documentation for more info: https://mikro-orm.io/docs/json-properties
*
* If disabled, Better Auth will handle these transformations for you.
*
* @default true
*/
supportsJSON?: boolean
}
/**
* Creates Mikro ORM adapter for Better Auth.
*
* Current limitations:
* * No m:m and 1:m and embedded references support
* * No complex primary key support
* * No schema generation
*
* @param orm - Instance of Mikro ORM returned from `MikroORM.init` or `MikroORM.initSync` methods
* @param config - Additional configuration for Mikro ORM adapter
*/
export const mikroOrmAdapter = (
orm: MikroORM,
{debugLogs, supportsJSON = true}: MikroOrmAdapterConfig = {}
) =>
createAdapter({
config: {
debugLogs,
supportsJSON,
adapterId: "mikro-orm-adapter",
adapterName: "Mikro ORM Adapter"
},
adapter({options}) {
const {
getEntityMetadata,
getFieldPath,
normalizeInput,
normalizeOutput,
normalizeWhereClauses
} = createAdapterUtils(orm)
return {
async create({model, data, select}) {
const metadata = getEntityMetadata(model)
const input = normalizeInput(metadata, data)
// Better Auth ignores `advanced.generateId` option when it's disabled, so this needs to be taken care of (for backwards compatibility)
if (
options.advanced?.generateId === false &&
!options.advanced?.database
) {
Reflect.deleteProperty(input, "id")
}
const entity = orm.em.create(metadata.class, input)
await orm.em.persist(entity).flush()
return normalizeOutput(metadata, entity, select) as any
},
async count({model, where}): Promise<number> {
const metadata = getEntityMetadata(model)
return orm.em.count(
metadata.class,
normalizeWhereClauses(metadata, where)
)
},
async findOne({model, where, select}) {
const metadata = getEntityMetadata(model)
const entity = await orm.em.findOne(
metadata.class,
normalizeWhereClauses(metadata, where)
)
if (!entity) {
return null
}
return normalizeOutput(metadata, entity, select) as any
},
async findMany({model, where, limit, offset, sortBy}) {
const metadata = getEntityMetadata(model)
const options: FindOptions<any> = {
limit,
offset
}
if (sortBy) {
const path = getFieldPath(metadata, sortBy.field)
dset(options, ["orderBy", ...path], sortBy.direction)
}
const rows = await orm.em.find(
metadata.class,
normalizeWhereClauses(metadata, where),
options
)
return rows.map(row => normalizeOutput(metadata, row)) as any
},
async update({model, where, update}) {
const metadata = getEntityMetadata(model)
const entity = await orm.em.findOne(
metadata.class,
normalizeWhereClauses(metadata, where)
)
if (!entity) {
return null
}
orm.em.assign(entity, normalizeInput(metadata, update as any))
await orm.em.flush()
return normalizeOutput(metadata, entity) as any
},
async updateMany({model, where, update}) {
const metadata = getEntityMetadata(model)
return orm.em.nativeUpdate(
metadata.class,
normalizeWhereClauses(metadata, where),
normalizeInput(metadata, update as any)
)
},
async delete({model, where}) {
const metadata = getEntityMetadata(model)
const entity = await orm.em.findOne(
metadata.class,
normalizeWhereClauses(metadata, where),
{
fields: ["id"]
}
)
if (entity) {
await orm.em.remove(entity).flush()
}
},
async deleteMany({model, where}) {
const metadata = getEntityMetadata(model)
return orm.em.nativeDelete(
metadata.class,
normalizeWhereClauses(metadata, where)
)
}
}
}
})