import { query as q } from "faunadb" ; import client from "./client" ; import { Resource , StandardResourceType } from "../types" ; const actions = { functions : { create : q . CreateFunction , index : q . Function } , indexes : { create : q . CreateIndex , index : q . Index } , roles : { create : q . CreateRole , index : q . Role } , providers : { create : q . CreateAccessProvider , index : q . AccessProvider } } ; param { Object } resource param { String } obj . name param { Object } obj . body param { String } obj . role returns { Promise < string > } export default async function createOrUpdateResources ( resources , type ) { const result = await client . query ( q . Foreach ( resources , q . Lambda ( "resource" , q . Let ( { name : q . Select ( [ "name" ] , q . Var ( "resource" ) ) , exists : q . Exists ( actions [ type ] . index ( q . Var ( "name" ) ) ) } , q . Do ( q . If ( q . Var ( "exists" ) , q . Update ( actions [ type ] . index ( q . Var ( "name" ) ) , q . Var ( "resource" ) ) , actions [ type ] . create ( q . Var ( "resource" ) ) ) , q . If ( q . Var ( "exists" ) , "updated" , "created" ) ) ) ) ) ) ; return result ; }