Skip to content

Commit

Permalink
fix: use explicit names for Entities (#9129)
Browse files Browse the repository at this point in the history
* use entities and not names

* fix build

* fix build
  • Loading branch information
gkorland committed Nov 13, 2023
1 parent 5636f91 commit 465e882
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 25 deletions.
16 changes: 8 additions & 8 deletions packages/adapter-typeorm/src/entities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const transformer: Record<"date" | "bigint", ValueTransformer> = {
},
}

@Entity("UserEntity", { name: "users" })
@Entity({ name: "users" })
export class UserEntity {
@PrimaryGeneratedColumn("uuid")
id!: string
Expand All @@ -35,14 +35,14 @@ export class UserEntity {
@Column({ type: "varchar", nullable: true })
image!: string | null

@OneToMany<SessionEntity>("SessionEntity", (session) => session.userId)
@OneToMany(() => SessionEntity, (session) => session.userId)
sessions!: SessionEntity[]

@OneToMany<AccountEntity>("AccountEntity", (account) => account.userId)
@OneToMany(() => AccountEntity, (account) => account.userId)
accounts!: AccountEntity[]
}

@Entity("AccountEntity", { name: "accounts" })
@Entity({ name: "accounts" })
export class AccountEntity {
@PrimaryGeneratedColumn("uuid")
id!: string
Expand Down Expand Up @@ -84,13 +84,13 @@ export class AccountEntity {
@Column({ type: "varchar", nullable: true })
session_state!: string | null

@ManyToOne<UserEntity>("UserEntity", (user) => user.accounts, {
@ManyToOne(() => UserEntity, (user) => user.accounts, {
createForeignKeyConstraints: true,
})
user!: UserEntity
}

@Entity("SessionEntity", { name: "sessions" })
@Entity({ name: "sessions" })
export class SessionEntity {
@PrimaryGeneratedColumn("uuid")
id!: string
Expand All @@ -104,11 +104,11 @@ export class SessionEntity {
@Column({ transformer: transformer.date })
expires!: string

@ManyToOne<UserEntity>("UserEntity", (user) => user.sessions)
@ManyToOne(() => UserEntity, (user) => user.sessions)
user!: UserEntity
}

@Entity("VerificationTokenEntity", { name: "verification_tokens" })
@Entity({ name: "verification_tokens" })
export class VerificationTokenEntity {
@PrimaryGeneratedColumn("uuid")
id!: string
Expand Down
39 changes: 22 additions & 17 deletions packages/adapter-typeorm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ export function TypeORMAdapter(
},
}

const UserEntityName = c.entities.UserEntity.name
const AccountEntityName = c.entities.AccountEntity.name
const SessionEntityName = c.entities.SessionEntity.name
const VerificationTokenEntityName = c.entities.VerificationTokenEntity.name

return {
/**
* Method used in testing. You won't need to call this in your app.
Expand All @@ -309,28 +314,28 @@ export function TypeORMAdapter(
// @ts-expect-error
createUser: async (data) => {
const m = await getManager(c)
const user = await m.save("UserEntity", data)
const user = await m.save(UserEntityName, data)
return user
},
// @ts-expect-error
async getUser(id) {
const m = await getManager(c)
const user = await m.findOne("UserEntity", { where: { id } })
const user = await m.findOne(UserEntityName, { where: { id } })
if (!user) return null
return { ...user }
},
// @ts-expect-error
async getUserByEmail(email) {
const m = await getManager(c)
const user = await m.findOne("UserEntity", { where: { email } })
const user = await m.findOne(UserEntityName, { where: { email } })
if (!user) return null
return { ...user }
},
async getUserByAccount(provider_providerAccountId) {
const m = await getManager(c)
// @ts-expect-error
const account = await m.findOne<AdapterAccount & { user: AdapterUser }>(
"AccountEntity",
AccountEntityName,
// @ts-expect-error
{ where: provider_providerAccountId, relations: ["user"] }
)
Expand All @@ -340,68 +345,68 @@ export function TypeORMAdapter(
// @ts-expect-error
async updateUser(data) {
const m = await getManager(c)
const user = await m.save("UserEntity", data)
const user = await m.save(UserEntityName, data)
return user
},
async deleteUser(id) {
const m = await getManager(c)
await m.transaction(async (tm) => {
await tm.delete("AccountEntity", { userId: id })
await tm.delete("SessionEntity", { userId: id })
await tm.delete("UserEntity", { id })
await tm.delete(AccountEntityName, { userId: id })
await tm.delete(SessionEntityName, { userId: id })
await tm.delete(UserEntityName, { id })
})
},
async linkAccount(data) {
const m = await getManager(c)
const account = await m.save("AccountEntity", data)
const account = await m.save(AccountEntityName, data)
return account
},
async unlinkAccount(providerAccountId) {
const m = await getManager(c)
await m.delete<AdapterAccount>("AccountEntity", providerAccountId)
await m.delete<AdapterAccount>(AccountEntityName, providerAccountId)
},
async createSession(data) {
const m = await getManager(c)
const session = await m.save("SessionEntity", data)
const session = await m.save(SessionEntityName, data)
return session
},
async getSessionAndUser(sessionToken) {
const m = await getManager(c)
const sessionAndUser = await m.findOne<
AdapterSession & { user: AdapterUser }
>("SessionEntity", { where: { sessionToken }, relations: ["user"] })
>(SessionEntityName, { where: { sessionToken }, relations: ["user"] })

if (!sessionAndUser) return null
const { user, ...session } = sessionAndUser
return { session, user }
},
async updateSession(data) {
const m = await getManager(c)
await m.update("SessionEntity", { sessionToken: data.sessionToken }, data)
await m.update(SessionEntityName, { sessionToken: data.sessionToken }, data)
// TODO: Try to return?
return null
},
async deleteSession(sessionToken) {
const m = await getManager(c)
await m.delete("SessionEntity", { sessionToken })
await m.delete(SessionEntityName, { sessionToken })
},
async createVerificationToken(data) {
const m = await getManager(c)
const verificationToken = await m.save("VerificationTokenEntity", data)
const verificationToken = await m.save(VerificationTokenEntityName, data)
// @ts-expect-error
delete verificationToken.id
return verificationToken
},
// @ts-expect-error
async useVerificationToken(identifier_token) {
const m = await getManager(c)
const verificationToken = await m.findOne("VerificationTokenEntity", {
const verificationToken = await m.findOne(VerificationTokenEntityName, {
where: identifier_token,
})
if (!verificationToken) {
return null
}
await m.delete("VerificationTokenEntity", identifier_token)
await m.delete(VerificationTokenEntityName, identifier_token)
// @ts-expect-error
delete verificationToken.id
return verificationToken
Expand Down

1 comment on commit 465e882

@vercel
Copy link

@vercel vercel bot commented on 465e882 Nov 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.