Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
"@commitlint/config-conventional": "19.8.0",
"@commitlint/types": "19.8.0",
"@faker-js/faker": "9.7.0",
"@mikro-orm/better-sqlite": "6.4.13",
"@mikro-orm/core": "6.4.13",
"@mikro-orm/core": "^7.0.1",
"@mikro-orm/decorators": "^7.0.1",
"@mikro-orm/sqlite": "^7.0.1",
"@types/node": "22.14.1",
"@types/uuid": "10.0.0",
"@vitest/coverage-v8": "3.1.1",
Expand All @@ -73,6 +74,7 @@
"es-toolkit": "1.39.7",
"husky": "9.1.7",
"is-in-ci": "1.0.0",
"reflect-metadata": "^0.2.2",
"tsup": "8.4.0",
"typescript": "5.8.3",
"uuid": "11.1.0",
Expand All @@ -82,7 +84,7 @@
"dset": "3.1.4"
},
"peerDependencies": {
"@mikro-orm/core": "^6.0.0",
"@mikro-orm/core": "^7.0.0",
"better-auth": "^1.0.0"
}
}
396 changes: 85 additions & 311 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const mikroOrmAdapter = (

const entity = orm.em.create(metadata.class, input)

await orm.em.persistAndFlush(entity)
await orm.em.persist(entity).flush()

return normalizeOutput(metadata, entity, select) as any
},
Expand Down Expand Up @@ -166,7 +166,7 @@ export const mikroOrmAdapter = (
)

if (entity) {
await orm.em.removeAndFlush(entity)
await orm.em.remove(entity).flush()
}
},

Expand Down
4 changes: 2 additions & 2 deletions src/utils/adapterUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ export function createAdapterUtils(orm: MikroORM): AdapterUtils {
) => {
entityName = normalizeEntityName(entityName)

if (!metadata.has(entityName)) {
if (!metadata.getByClassName(entityName, false)) {
createAdapterError(
`Cannot find metadata for "${entityName}" entity. Make sure it defined and listed in your Mikro ORM config.`
)
}

return metadata.get(entityName)
return metadata.getByClassName(entityName)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/entities/better-auth-test-suite/Address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Embeddable, Property} from "@mikro-orm/core"
import {Embeddable, Property} from "@mikro-orm/decorators/legacy"

@Embeddable()
export class Address {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/entities/better-auth-test-suite/Session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity, ManyToOne, Property, Unique} from "@mikro-orm/core"
import {Entity, ManyToOne, Property, Unique} from "@mikro-orm/decorators/legacy"
import type {Session as DatabaseSession} from "better-auth"

import {Base} from "../shared/Base.js"
Expand Down
5 changes: 2 additions & 3 deletions tests/fixtures/entities/better-auth-test-suite/User.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {Collection, type Opt} from "@mikro-orm/core"
import {
Collection,
Embedded,
Entity,
OneToMany,
type Opt,
Property,
Unique
} from "@mikro-orm/core"
} from "@mikro-orm/decorators/legacy"

import {Base} from "../shared/Base.js"
import {Address} from "./Address.js"
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/entities/custom-entity-name/CustomUser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity, type Opt, Property, Unique} from "@mikro-orm/core"
import type {Opt} from "@mikro-orm/core"
import {Entity, Property, Unique} from "@mikro-orm/decorators/legacy"
import type {User as DatabaseUser} from "better-auth"

import {Base} from "../shared/Base.js"
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/entities/custom-field-name-snake-case/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity, type Opt, Property, Unique} from "@mikro-orm/core"
import type {Opt} from "@mikro-orm/core"
import {Entity, Property, Unique} from "@mikro-orm/decorators/legacy"
import type {User as DatabaseUser} from "better-auth"

import {Base} from "../shared/Base.js"
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/entities/custom-field-name/User.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {Entity, type Opt, Property, Unique} from "@mikro-orm/core"
import type {Opt} from "@mikro-orm/core"
import {Entity, Property, Unique} from "@mikro-orm/decorators/legacy"
import type {User as DatabaseUser} from "better-auth"

import {Base} from "../shared/Base.js"
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/entities/defaults/Address.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Embeddable, Property} from "@mikro-orm/core"
import {Embeddable, Property} from "@mikro-orm/decorators/legacy"

@Embeddable()
export class Address {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/entities/defaults/Session.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Entity, ManyToOne, Property, Unique} from "@mikro-orm/core"
import {Entity, ManyToOne, Property, Unique} from "@mikro-orm/decorators/legacy"
import type {Session as DatabaseSession} from "better-auth"

import {Base} from "../shared/Base.js"
Expand Down
5 changes: 2 additions & 3 deletions tests/fixtures/entities/defaults/User.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import {Collection, type Opt} from "@mikro-orm/core"
import {
Collection,
Embedded,
Entity,
OneToMany,
type Opt,
Property,
Unique
} from "@mikro-orm/core"
} from "@mikro-orm/decorators/legacy"
import type {User as DatabaseUser} from "better-auth"

import {Base} from "../shared/Base.js"
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/entities/shared/Base.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {type Opt, PrimaryKey, Property} from "@mikro-orm/core"
import type {Opt} from "@mikro-orm/core"
import {PrimaryKey, Property} from "@mikro-orm/decorators/legacy"
import {v7} from "uuid"

export abstract class Base {
Expand Down
23 changes: 7 additions & 16 deletions tests/fixtures/orm.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,22 @@
import {rm} from "node:fs/promises"
import {join} from "node:path"

import {
type EntityClass,
type EntityClassGroup,
type EntitySchema,
MikroORM
} from "@mikro-orm/better-sqlite"
import {type EntityClass, type EntitySchema, MikroORM} from "@mikro-orm/sqlite"
import {v7} from "uuid"
import {afterAll, beforeAll, beforeEach} from "vitest"

interface CreateOrmParams {
refreshOnEachTest?: boolean
entities: Array<
| EntityClass<Partial<any>>
| EntityClassGroup<Partial<any>>
| EntitySchema<Partial<any>>
>
entities: Array<EntityClass<Partial<any>> | EntitySchema<Partial<any>>>
}

export function createOrm({
export async function createOrm({
entities,
refreshOnEachTest = true
}: CreateOrmParams): MikroORM {
}: CreateOrmParams): Promise<MikroORM> {
const dbName = join(import.meta.dirname, `${v7()}.sqlite`)

const orm = MikroORM.initSync({
const orm = await MikroORM.init({
dbName,
entities: entities,
ensureDatabase: true,
Expand All @@ -35,9 +26,9 @@ export function createOrm({
beforeAll(async () => await orm.connect())

if (refreshOnEachTest) {
beforeEach(async () => await orm.getSchemaGenerator().refreshDatabase())
beforeEach(async () => await orm.schema.refresh())
} else {
beforeAll(async () => await orm.getSchemaGenerator().refreshDatabase())
beforeAll(async () => await orm.schema.refresh())
}

afterAll(async () => {
Expand Down
2 changes: 1 addition & 1 deletion tests/fixtures/randomUsers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {faker} from "@faker-js/faker"
import type {MikroORM} from "@mikro-orm/better-sqlite"
import type {MikroORM} from "@mikro-orm/sqlite"

import type {UserInput} from "../utils/types.js"

Expand Down
2 changes: 1 addition & 1 deletion tests/node/adapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import type {SessionInput, UserInput} from "../utils/types.js"

import * as entities from "../fixtures/entities/defaults.js"

const orm = createOrm({entities: Object.values(entities)})
const orm = await createOrm({entities: Object.values(entities)})

const randomUsers = createRandomUsersUtils(orm)

Expand Down
2 changes: 1 addition & 1 deletion tests/node/better-auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import * as entities from "../fixtures/entities/better-auth-test-suite.js"
import {mikroOrmAdapter} from "../../src/adapter.js"
import {createOrm} from "../fixtures/orm.js"

const orm = createOrm({
const orm = await createOrm({
entities: Object.values(entities),
refreshOnEachTest: false
})
Expand Down
8 changes: 4 additions & 4 deletions tests/node/customization.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {UserInput} from "../utils/types.js"

suite("custom entity (model) names", async () => {
const entities = await import("../fixtures/entities/custom-entity-name.js")
const orm = createOrm({
const orm = await createOrm({
entities: Object.values(entities)
})

Expand All @@ -35,7 +35,7 @@ suite("custom entity (model) names", async () => {
suite("custom field names", () => {
suite("camelCase", async () => {
const entities = await import("../fixtures/entities/custom-field-name.js")
const orm = createOrm({entities: Object.values(entities)})
const orm = await createOrm({entities: Object.values(entities)})

test("with camelCase", async () => {
const randomUsers = createRandomUsersUtils(orm)
Expand Down Expand Up @@ -63,9 +63,9 @@ suite("custom field names", () => {
"../fixtures/entities/custom-field-name-snake-case.js"
)

const orm = createOrm({entities: Object.values(entities)})
const orm = await createOrm({entities: Object.values(entities)})

test("with sname_case", async () => {
test("with snake_case", async () => {
const randomUsers = createRandomUsersUtils(orm)
const adapter = mikroOrmAdapter(orm)({
user: {
Expand Down
Loading