Skip to content

Commit 5699205

Browse files
authored
Merge pull request #24 from octet-stream/test/better-auth
2 parents 2ee001a + 2fd766a commit 5699205

File tree

11 files changed

+220
-6
lines changed

11 files changed

+220
-6
lines changed

.changeset/three-rabbits-fall.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"better-auth-mikro-orm": patch
3+
---
4+
5+
Remove fields filtering from output normalization

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"@vitest/ui": "3.1.1",
7171
"better-auth": "1.2.9",
7272
"del-cli": "6.0.0",
73+
"es-toolkit": "1.39.7",
7374
"husky": "9.1.7",
7475
"is-in-ci": "1.0.0",
7576
"tsup": "8.4.0",

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/utils/adapterUtils.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,7 @@ export function createAdapterUtils(orm: MikroORM): AdapterUtils {
246246

247247
const normalizeOutput: AdapterUtils["normalizeOutput"] = (
248248
metadata,
249-
output,
250-
select
249+
output
251250
) => {
252251
output = serialize(output)
253252

@@ -260,7 +259,6 @@ export function createAdapterUtils(orm: MikroORM): AdapterUtils {
260259
),
261260
value
262261
}))
263-
.filter(({path}) => (select ? select.includes(path) : true))
264262
.forEach(({path, value}) => dset(result, path, value))
265263

266264
return result
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export {Sessions} from "./better-auth-test-suite/Session.js"
2+
export {Address} from "./better-auth-test-suite/Address.js"
3+
export {User} from "./better-auth-test-suite/User.js"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Embeddable, Property} from "@mikro-orm/core"
2+
3+
@Embeddable()
4+
export class Address {
5+
@Property({type: "string"})
6+
street!: string
7+
8+
@Property({type: "string"})
9+
city!: string
10+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {Entity, ManyToOne, Property, Unique} from "@mikro-orm/core"
2+
import type {Session as DatabaseSession} from "better-auth"
3+
4+
import {Base} from "../shared/Base.js"
5+
import {User} from "./User.js"
6+
7+
@Entity()
8+
export class Sessions extends Base implements Omit<DatabaseSession, "userId"> {
9+
@Property({type: "string"})
10+
@Unique()
11+
token!: string
12+
13+
@Property({type: Date})
14+
expiresAt!: Date
15+
16+
@Property({type: "string", nullable: true, default: null})
17+
ipAddress?: string | null | undefined
18+
19+
@Property({type: "string", nullable: true, default: null})
20+
userAgent?: string | null | undefined
21+
22+
@ManyToOne(() => User)
23+
user!: User
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import {
2+
Collection,
3+
Embedded,
4+
Entity,
5+
OneToMany,
6+
type Opt,
7+
Property,
8+
Unique
9+
} from "@mikro-orm/core"
10+
11+
import {Base} from "../shared/Base.js"
12+
import {Address} from "./Address.js"
13+
import {Sessions} from "./Session.js"
14+
15+
@Entity()
16+
export class User extends Base {
17+
@Property({type: "string"})
18+
@Unique()
19+
email_address!: string
20+
21+
@Property({type: "boolean"})
22+
emailVerified: Opt<boolean> = false
23+
24+
@Property({type: "string", nullable: true})
25+
test?: string
26+
27+
@Property({type: "string"})
28+
name!: string
29+
30+
@OneToMany(() => Sessions, "user")
31+
sessions = new Collection<Sessions, this>(this)
32+
33+
@Embedded(() => Address, {object: true, nullable: true})
34+
address?: Address
35+
}

tests/fixtures/orm.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import {v7} from "uuid"
1111
import {afterAll, beforeAll, beforeEach} from "vitest"
1212

1313
interface CreateOrmParams {
14+
refreshOnEachTest?: boolean
1415
entities: Array<
1516
| EntityClass<Partial<any>>
1617
| EntityClassGroup<Partial<any>>
1718
| EntitySchema<Partial<any>>
1819
>
1920
}
2021

21-
export function createOrm({entities}: CreateOrmParams): MikroORM {
22+
export function createOrm({
23+
entities,
24+
refreshOnEachTest = true
25+
}: CreateOrmParams): MikroORM {
2226
const dbName = join(import.meta.dirname, `${v7()}.sqlite`)
2327

2428
const orm = MikroORM.initSync({
@@ -30,7 +34,11 @@ export function createOrm({entities}: CreateOrmParams): MikroORM {
3034

3135
beforeAll(async () => await orm.connect())
3236

33-
beforeEach(async () => await orm.getSchemaGenerator().refreshDatabase())
37+
if (refreshOnEachTest) {
38+
beforeEach(async () => await orm.getSchemaGenerator().refreshDatabase())
39+
} else {
40+
beforeAll(async () => await orm.getSchemaGenerator().refreshDatabase())
41+
}
3442

3543
afterAll(async () => {
3644
await orm.close()

tests/node/adapter.test.ts

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
User as DatabaseUser
44
} from "better-auth"
55
import {generateId} from "better-auth"
6-
import {validate} from "uuid"
6+
import {NIL, validate} from "uuid"
77
import {expect, suite, test} from "vitest"
88

99
import {mikroOrmAdapter} from "../../src/index.js"
@@ -325,6 +325,63 @@ suite("findMany", () => {
325325
})
326326
})
327327

328+
suite("update", () => {
329+
test("updates matched row", async () => {
330+
const user = await randomUsers.createAndFlushOne()
331+
332+
const actual = await adapter.update<DatabaseUser>({
333+
model: "user",
334+
where: [
335+
{
336+
field: "id",
337+
value: user.id
338+
}
339+
],
340+
update: {
341+
emailVerified: true
342+
}
343+
})
344+
345+
expect(actual?.emailVerified).toBe(true)
346+
})
347+
348+
test("returns null when no row found", async () => {
349+
const actual = await adapter.update<DatabaseUser>({
350+
model: "user",
351+
where: [
352+
{
353+
field: "id",
354+
value: NIL
355+
}
356+
],
357+
update: {
358+
emailVerified: true
359+
}
360+
})
361+
362+
expect(actual).toBe(null)
363+
})
364+
365+
test("updates Identity Map", async () => {
366+
const user = await randomUsers.createAndFlushOne()
367+
368+
await adapter.update<DatabaseUser>({
369+
model: "user",
370+
where: [
371+
{
372+
field: "id",
373+
value: user.id
374+
}
375+
],
376+
update: {
377+
emailVerified: true
378+
}
379+
})
380+
381+
expect(user.emailVerified).toBe(true)
382+
})
383+
})
384+
328385
suite("updateMany", () => {
329386
test("updates matched rows", async () => {
330387
const [user1, user2, user3] = await randomUsers.createAndFlushMany(3)
@@ -394,6 +451,26 @@ suite("updateMany", () => {
394451
})
395452
})
396453

454+
suite("delete", () => {
455+
test("removes matched row", async () => {
456+
const user = await randomUsers.createAndFlushOne()
457+
458+
await adapter.delete({
459+
model: "user",
460+
where: [
461+
{
462+
field: "id",
463+
value: user.id
464+
}
465+
]
466+
})
467+
468+
const promise = orm.em.findOne(entities.User, user.id)
469+
470+
await expect(promise).resolves.toBe(null)
471+
})
472+
})
473+
397474
suite("deleteMany", () => {
398475
test("deletes multiple rows", async () => {
399476
const users = await randomUsers.createAndFlushMany(3)

0 commit comments

Comments
 (0)