import faunadb , { ClientConfig , Client } from 'faunadb' ; import { unusedServerKeyRefIdLocalStorageKeyName } from './database.constants' ; const { query } = faunadb ; const collectionName = process . env . COLLECTION_NAME as string ; const indexName = process . env . INDEX_NAME as string ; interface PasswordListRaw { data : PasswordEntityRaw [ ] ; } interface PasswordEntityDatabasePayload { label : string ; value : string ; } export interface PasswordEntityRaw { ref : { id : string ; value : { id : string ; } ; } ; ts : number ; data : PasswordEntityDatabasePayload ; } const hasCollections = async ( client ) : Promise < boolean > => { const collections = await client . query ( query . Paginate ( query . Collections ( ) ) ) ; return Boolean ( collections . data . length ) ; } ; const hasAllPasswordsIndex = async ( client ) : Promise < boolean > => { const indexes = await client . query ( query . Paginate ( query . Indexes ( ) ) ) ; return Boolean ( indexes . data . length ) ; } ; const removeUnusedServerKey = async ( adminClient ) : Promise < void > => { const refId = localStorage . getItem ( unusedServerKeyRefIdLocalStorageKeyName ) ; if ( refId ) { await adminClient . query ( query . Delete ( query . Ref ( query . Keys ( ) , refId ) ) ) ; } } ; export const setupClient = async ( options ) : Promise < Client > => { const adminClient = new faunadb . Client ( options ) ; const serverKey = await adminClient . query ( query . CreateKey ( { role : 'server' } ) ) ; const client = new faunadb . Client ( { secret : serverKey . secret } ) ; if ( ! ( await hasCollections ( client ) ) ) { await client . query ( query . CreateCollection ( { name : collectionName } ) ) ; } if ( ! ( await hasAllPasswordsIndex ( client ) ) ) { await client . query ( query . CreateIndex ( { name : indexName , source : query . Collection ( collectionName ) , } ) ) ; } removeUnusedServerKey ( adminClient ) ; localStorage . setItem ( unusedServerKeyRefIdLocalStorageKeyName , serverKey . ref . id ) ; return client ; } ; export const fetchAllPasswordEntities = async ( client ) : Promise < PasswordEntityRaw [ ] > => { const paramName = 'placeholderValue' ; const response = await client . query ( query . Map ( query . Paginate ( query . Match ( query . Index ( indexName ) ) ) , query . Lambda ( paramName , query . Get ( query . Var ( paramName ) ) ) ) ) ; return response . data ; } ; export const createPasswordEntity = async ( client , encryptedEntity ) : Promise < void > => { await client . query ( query . Create ( query . Collection ( collectionName ) , { data : encryptedEntity , } ) ) ; } ; export const updatePasswordEntity = async ( client , refId , encryptedEntity ) : Promise < void > => { await client . query ( query . Update ( query . Ref ( query . Collection ( collectionName ) , refId ) , { data : encryptedEntity , } ) ) ; } ; export const deletePasswordEntity = async ( client , refId ) : Promise < void > => { await client . query ( query . Delete ( query . Ref ( query . Collection ( collectionName ) , refId ) ) ) ; } ;