Skip to content

Commit

Permalink
feat(db): make Fauna DB collections & indexes configurable (#968)
Browse files Browse the repository at this point in the history
* Add collections & indexes overrides for Fauna DB

* Fix the name of the verification token index

Co-authored-by: Florian Michaut <florian@coding-days.com>
  • Loading branch information
2 people authored and balazsorban44 committed Feb 1, 2021
1 parent 94054db commit 751fd7b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
50 changes: 32 additions & 18 deletions src/adapters/fauna/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ import { createHash, randomBytes } from 'crypto'
import logger from '../../lib/logger'

const Adapter = (config, options = {}) => {
const { faunaClient } = config
const {
faunaClient,
collections = {
User: 'user',
Account: 'account',
Session: 'session',
VerificationRequest: 'verification_request'
},
indexes = {
Account: 'account_by_provider_account_id',
User: 'user_by_email',
Session: 'session_by_token',
VerificationRequest: 'verification_request_by_token'
}
} = config

async function getAdapter (appOptions) {
function _debug (debugCode, ...args) {
Expand All @@ -23,7 +37,7 @@ const Adapter = (config, options = {}) => {

const timestamp = new Date().toISOString()
const FQL = q.Create(
q.Collection('user'), {
q.Collection(collections.User), {
data: {
name: profile.name,
email: profile.email,
Expand Down Expand Up @@ -51,7 +65,7 @@ const Adapter = (config, options = {}) => {
_debug('getUser', id)

const FQL = q.Get(
q.Ref(q.Collection('user'), id)
q.Ref(q.Collection(collections.User), id)
)

try {
Expand All @@ -74,7 +88,7 @@ const Adapter = (config, options = {}) => {

const FQL = q.Let(
{
ref: q.Match(q.Index('user_by_email'), email)
ref: q.Match(q.Index(indexes.User), email)
},
q.If(
q.Exists(q.Var('ref')),
Expand Down Expand Up @@ -104,15 +118,15 @@ const Adapter = (config, options = {}) => {
const FQL = q.Let(
{
ref: q.Match(
q.Index('account_by_provider_account_id'),
q.Index(indexes.Account),
[providerId, providerAccountId]
)
},
q.If(
q.Exists(q.Var('ref')),
q.Get(
q.Ref(
q.Collection('user'),
q.Collection(collections.User),
q.Select(['data', 'userId'],
q.Get(q.Var('ref'))
)
Expand Down Expand Up @@ -143,7 +157,7 @@ const Adapter = (config, options = {}) => {

const timestamp = new Date().toISOString()
const FQL = q.Update(
q.Ref(q.Collection('user'), user.id),
q.Ref(q.Collection(collections.User), user.id),
{
data: {
name: user.name,
Expand All @@ -170,7 +184,7 @@ const Adapter = (config, options = {}) => {
_debug('deleteUser', userId)

const FQL = q.Delete(
q.Ref(q.Collection('user'), userId)
q.Ref(q.Collection(collections.User), userId)
)

try {
Expand All @@ -187,7 +201,7 @@ const Adapter = (config, options = {}) => {
try {
const timestamp = new Date().toISOString()
const account = await faunaClient.query(
q.Create(q.Collection('account'), {
q.Create(q.Collection(collections.Account), {
data: {
userId: userId,
providerId: providerId,
Expand Down Expand Up @@ -216,7 +230,7 @@ const Adapter = (config, options = {}) => {
q.Select('ref',
q.Get(
q.Match(
q.Index('account_by_provider_account_id'),
q.Index(indexes.Account),
[providerId, providerAccountId]
)
)
Expand All @@ -243,7 +257,7 @@ const Adapter = (config, options = {}) => {

const timestamp = new Date().toISOString()
const FQL =
q.Create(q.Collection('session'), {
q.Create(q.Collection(collections.Session), {
data: {
userId: user.id,
expires: q.Time(expires),
Expand All @@ -270,10 +284,10 @@ const Adapter = (config, options = {}) => {
_debug('getSession', sessionToken)

try {
var session = await faunaClient.query(
const session = await faunaClient.query(
q.Get(
q.Match(
q.Index('session_by_token'),
q.Index(indexes.Session),
sessionToken
)
)
Expand Down Expand Up @@ -325,7 +339,7 @@ const Adapter = (config, options = {}) => {

const updatedSession = await faunaClient.query(
q.Update(
q.Ref(q.Collection('session'), session.id),
q.Ref(q.Collection(collections.Session), session.id),
{
data: {
expires: q.Time(newExpiryDate.toISOString()),
Expand All @@ -349,7 +363,7 @@ const Adapter = (config, options = {}) => {
q.Select('ref',
q.Get(
q.Match(
q.Index('session_by_token'),
q.Index(indexes.Session),
sessionToken
)
)
Expand Down Expand Up @@ -391,7 +405,7 @@ const Adapter = (config, options = {}) => {

const timestamp = new Date().toISOString()
const FQL = q.Create(
q.Collection('verification_request'), {
q.Collection(collections.VerificationRequest), {
data: {
identifier: identifier,
token: hashedToken,
Expand Down Expand Up @@ -422,7 +436,7 @@ const Adapter = (config, options = {}) => {
const hashedToken = createHash('sha256').update(`${token}${secret}`).digest('hex')
const FQL = q.Let(
{
ref: q.Match(q.Index('vertification_request_by_token'), hashedToken)
ref: q.Match(q.Index(indexes.VerificationRequest), hashedToken)
},
q.If(
q.Exists(q.Var('ref')),
Expand Down Expand Up @@ -462,7 +476,7 @@ const Adapter = (config, options = {}) => {
q.Select('ref',
q.Get(
q.Match(
q.Index('vertification_request_by_token'), hashedToken
q.Index(indexes.VerificationRequest), hashedToken
)
)
)
Expand Down
2 changes: 1 addition & 1 deletion test/fauna.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const InitialiseDb = async () => {
}));

await client.query(q.CreateIndex({
name: 'vertification_request_by_token',
name: 'verification_request_by_token',
source: q.Collection('verification_request'),
unique: true,
terms: [
Expand Down

0 comments on commit 751fd7b

Please sign in to comment.