Skip to content

Commit

Permalink
refactor(db): update Prisma calls to support 2.12+ (#881)
Browse files Browse the repository at this point in the history
Co-authored-by: Balázs Orbán <info@balazsorban.com>
Co-authored-by: Nico Domino <yo@ndo.dev>
  • Loading branch information
3 people committed Dec 6, 2020
1 parent 9dbd372 commit ddaa830
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -65,7 +65,7 @@
"mysql": "^2.18.1",
"mssql": "^6.2.1",
"pg": "^8.2.1",
"@prisma/client": "^2.3.0"
"@prisma/client": "^2.12.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
12 changes: 6 additions & 6 deletions src/adapters/prisma/index.js
Expand Up @@ -57,7 +57,7 @@ const Adapter = (config) => {
async function getUser (id) {
debug('GET_USER', id)
try {
return prisma[User].findOne({ where: { id } })
return prisma[User].findUnique({ where: { id } })
} catch (error) {
logger.error('GET_USER_BY_ID_ERROR', error)
return Promise.reject(new Error('GET_USER_BY_ID_ERROR', error))
Expand All @@ -68,7 +68,7 @@ const Adapter = (config) => {
debug('GET_USER_BY_EMAIL', email)
try {
if (!email) { return Promise.resolve(null) }
return prisma[User].findOne({ where: { email } })
return prisma[User].findUnique({ where: { email } })
} catch (error) {
logger.error('GET_USER_BY_EMAIL_ERROR', error)
return Promise.reject(new Error('GET_USER_BY_EMAIL_ERROR', error))
Expand All @@ -78,9 +78,9 @@ const Adapter = (config) => {
async function getUserByProviderAccountId (providerId, providerAccountId) {
debug('GET_USER_BY_PROVIDER_ACCOUNT_ID', providerId, providerAccountId)
try {
const account = await prisma[Account].findOne({ where: { compoundId: getCompoundId(providerId, providerAccountId) } })
const account = await prisma[Account].findUnique({ where: { compoundId: getCompoundId(providerId, providerAccountId) } })
if (!account) { return null }
return prisma[User].findOne({ where: { id: account.userId } })
return prisma[User].findUnique({ where: { id: account.userId } })
} catch (error) {
logger.error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error)
return Promise.reject(new Error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error))
Expand Down Expand Up @@ -174,7 +174,7 @@ const Adapter = (config) => {
async function getSession (sessionToken) {
debug('GET_SESSION', sessionToken)
try {
const session = await prisma[Session].findOne({ where: { sessionToken } })
const session = await prisma[Session].findUnique({ where: { sessionToken } })

// Check session has not expired (do not return it if it has)
if (session && session.expires && new Date() > session.expires) {
Expand Down Expand Up @@ -280,7 +280,7 @@ const Adapter = (config) => {
// Hash token provided with secret before trying to match it with database
// @TODO Use bcrypt instead of salted SHA-256 hash for token
const hashedToken = createHash('sha256').update(`${token}${secret}`).digest('hex')
const verificationRequest = await prisma[VerificationRequest].findOne({ where: { token: hashedToken } })
const verificationRequest = await prisma[VerificationRequest].findUnique({ where: { token: hashedToken } })

if (verificationRequest && verificationRequest.expires && new Date() > verificationRequest.expires) {
// Delete verification entry so it cannot be used again
Expand Down
12 changes: 6 additions & 6 deletions src/adapters/typeorm/index.js
Expand Up @@ -144,7 +144,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
}

try {
return manager.findOne(User, { [idKey]: id })
return manager.findUnique(User, { [idKey]: id })
} catch (error) {
logger.error('GET_USER_BY_ID_ERROR', error)
return Promise.reject(new Error('GET_USER_BY_ID_ERROR', error))
Expand All @@ -155,7 +155,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
debug('GET_USER_BY_EMAIL', email)
try {
if (!email) { return Promise.resolve(null) }
return manager.findOne(User, { email })
return manager.findUnique(User, { email })
} catch (error) {
logger.error('GET_USER_BY_EMAIL_ERROR', error)
return Promise.reject(new Error('GET_USER_BY_EMAIL_ERROR', error))
Expand All @@ -165,9 +165,9 @@ const Adapter = (typeOrmConfig, options = {}) => {
async function getUserByProviderAccountId (providerId, providerAccountId) {
debug('GET_USER_BY_PROVIDER_ACCOUNT_ID', providerId, providerAccountId)
try {
const account = await manager.findOne(Account, { providerId, providerAccountId })
const account = await manager.findUnique(Account, { providerId, providerAccountId })
if (!account) { return null }
return manager.findOne(User, { [idKey]: account.userId })
return manager.findUnique(User, { [idKey]: account.userId })
} catch (error) {
logger.error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error)
return Promise.reject(new Error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error))
Expand Down Expand Up @@ -227,7 +227,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
async function getSession (sessionToken) {
debug('GET_SESSION', sessionToken)
try {
const session = await manager.findOne(Session, { sessionToken })
const session = await manager.findUnique(Session, { sessionToken })

// Check session has not expired (do not return it if it has)
if (session && session.expires && new Date() > new Date(session.expires)) {
Expand Down Expand Up @@ -327,7 +327,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
// Hash token provided with secret before trying to match it with database
// @TODO Use bcrypt instead of salted SHA-256 hash for token
const hashedToken = createHash('sha256').update(`${token}${secret}`).digest('hex')
const verificationRequest = await manager.findOne(VerificationRequest, { identifier, token: hashedToken })
const verificationRequest = await manager.findUnique(VerificationRequest, { identifier, token: hashedToken })

if (verificationRequest && verificationRequest.expires && new Date() > new Date(verificationRequest.expires)) {
// Delete verification entry so it cannot be used again
Expand Down

0 comments on commit ddaa830

Please sign in to comment.