Skip to content

Commit 9aae7bb

Browse files
kripodbalazsorban44ndom91
committedFeb 1, 2021
refactor(db): update Prisma calls to support 2.12+ (#881)
Co-authored-by: Balázs Orbán <info@balazsorban.com> Co-authored-by: Nico Domino <yo@ndo.dev>
1 parent a84fe59 commit 9aae7bb

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
"mysql": "^2.18.1",
6666
"mssql": "^6.2.1",
6767
"pg": "^8.2.1",
68-
"@prisma/client": "^2.3.0"
68+
"@prisma/client": "^2.12.0"
6969
},
7070
"devDependencies": {
7171
"@babel/cli": "^7.8.4",

‎src/adapters/prisma/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const Adapter = (config) => {
5757
async function getUser (id) {
5858
debug('GET_USER', id)
5959
try {
60-
return prisma[User].findOne({ where: { id } })
60+
return prisma[User].findUnique({ where: { id } })
6161
} catch (error) {
6262
logger.error('GET_USER_BY_ID_ERROR', error)
6363
return Promise.reject(new Error('GET_USER_BY_ID_ERROR', error))
@@ -68,7 +68,7 @@ const Adapter = (config) => {
6868
debug('GET_USER_BY_EMAIL', email)
6969
try {
7070
if (!email) { return Promise.resolve(null) }
71-
return prisma[User].findOne({ where: { email } })
71+
return prisma[User].findUnique({ where: { email } })
7272
} catch (error) {
7373
logger.error('GET_USER_BY_EMAIL_ERROR', error)
7474
return Promise.reject(new Error('GET_USER_BY_EMAIL_ERROR', error))
@@ -78,9 +78,9 @@ const Adapter = (config) => {
7878
async function getUserByProviderAccountId (providerId, providerAccountId) {
7979
debug('GET_USER_BY_PROVIDER_ACCOUNT_ID', providerId, providerAccountId)
8080
try {
81-
const account = await prisma[Account].findOne({ where: { compoundId: getCompoundId(providerId, providerAccountId) } })
81+
const account = await prisma[Account].findUnique({ where: { compoundId: getCompoundId(providerId, providerAccountId) } })
8282
if (!account) { return null }
83-
return prisma[User].findOne({ where: { id: account.userId } })
83+
return prisma[User].findUnique({ where: { id: account.userId } })
8484
} catch (error) {
8585
logger.error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error)
8686
return Promise.reject(new Error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error))
@@ -174,7 +174,7 @@ const Adapter = (config) => {
174174
async function getSession (sessionToken) {
175175
debug('GET_SESSION', sessionToken)
176176
try {
177-
const session = await prisma[Session].findOne({ where: { sessionToken } })
177+
const session = await prisma[Session].findUnique({ where: { sessionToken } })
178178

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

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

‎src/adapters/typeorm/index.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
144144
}
145145

146146
try {
147-
return manager.findOne(User, { [idKey]: id })
147+
return manager.findUnique(User, { [idKey]: id })
148148
} catch (error) {
149149
logger.error('GET_USER_BY_ID_ERROR', error)
150150
return Promise.reject(new Error('GET_USER_BY_ID_ERROR', error))
@@ -155,7 +155,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
155155
debug('GET_USER_BY_EMAIL', email)
156156
try {
157157
if (!email) { return Promise.resolve(null) }
158-
return manager.findOne(User, { email })
158+
return manager.findUnique(User, { email })
159159
} catch (error) {
160160
logger.error('GET_USER_BY_EMAIL_ERROR', error)
161161
return Promise.reject(new Error('GET_USER_BY_EMAIL_ERROR', error))
@@ -165,9 +165,9 @@ const Adapter = (typeOrmConfig, options = {}) => {
165165
async function getUserByProviderAccountId (providerId, providerAccountId) {
166166
debug('GET_USER_BY_PROVIDER_ACCOUNT_ID', providerId, providerAccountId)
167167
try {
168-
const account = await manager.findOne(Account, { providerId, providerAccountId })
168+
const account = await manager.findUnique(Account, { providerId, providerAccountId })
169169
if (!account) { return null }
170-
return manager.findOne(User, { [idKey]: account.userId })
170+
return manager.findUnique(User, { [idKey]: account.userId })
171171
} catch (error) {
172172
logger.error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error)
173173
return Promise.reject(new Error('GET_USER_BY_PROVIDER_ACCOUNT_ID_ERROR', error))
@@ -227,7 +227,7 @@ const Adapter = (typeOrmConfig, options = {}) => {
227227
async function getSession (sessionToken) {
228228
debug('GET_SESSION', sessionToken)
229229
try {
230-
const session = await manager.findOne(Session, { sessionToken })
230+
const session = await manager.findUnique(Session, { sessionToken })
231231

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.