This was originally posted on mygarden.richardhaines.dev
While still a work in progress, learning in public is fun! With that in mind id
like to share v1 of myFaunaDB CRUD hooks. Ive tested them
locally and they work. They provide simple abstractions though
Fauna Query Language (FQL)
enables so much more and the hooks will need refactoring, but for a first
version im relatively pleased! 😃
useFaunaGetAll
exportconstuseFaunaGetAll=(collectionIndex,limit)=>{constfauna=React.useContext(FaunaContext);const{client,q}=fauna;const[response,setResponse]=React.useState(null);const[error,setError]=React.useState(null);const[isLoading,setIsLoading]=React.useState(false);React.useEffect(()=>{constgetAll=async()=>{setIsLoading(true);try{client.query(q.Map(q.Paginate(q.Match(q.Index(collectionIndex)),{size:limit}),q.Lambda("ref",q.Select(["data"],q.Get(q.Var("ref")))))).then(result=>{setResponse(result.data);setIsLoading(false);});}catch(error){setError(error);}setIsLoading(false);};getAll();},[collectionIndex]);return{response,error,isLoading};};
useFaunaGetSingle
exportconstuseFaunaGetSingle=(collectionIndex,id)=>{constfauna=React.useContext(FaunaContext);const{client,q}=fauna;const[response,setResponse]=React.useState(null);const[error,setError]=React.useState(null);const[isLoading,setIsLoading]=React.useState(false);React.useEffect(()=>{constgetSingle=async()=>{setIsLoading(true);try{client.query(q.Get(q.Match(q.Index(collectionIndex),id))).then(result=>{setResponse(result.data);setIsLoading(false);});}catch(error){setError(error);}setIsLoading(false);};getSingle();},[id]);return{response,error,isLoading};};
useFaunaCreate
exportconstuseFaunaCreate=(collectionIndex,data,customId)=>{constfauna=React.useContext(FaunaContext);const{client,q}=fauna;const[response,setResponse]=React.useState(null);const[error,setError]=React.useState(null);const[isLoading,setIsLoading]=React.useState(false);React.useEffect(()=>{constcreate=async()=>{setIsLoading(true);try{customId?client.query(q.Create(q.Ref(q.Collection(collectionIndex),customId),{data})).then(result=>{setResponse(result.data);setIsLoading(false);}):client.query(q.Create(q.Collection(collectionIndex),{data})).then(result=>{setResponse(result.data);setIsLoading(false);});}catch(error){setError(error);}setIsLoading(false);};create();},[]);return{response,error,isLoading};};
useFaunaUpdate
exportconstuseFaunaUpdate=(collectionIndex,data,id)=>{constfauna=React.useContext(FaunaContext);const{client,q}=fauna;const[response,setResponse]=React.useState(null);const[error,setError]=React.useState(null);const[isLoading,setIsLoading]=React.useState(false);React.useEffect(()=>{constupdate=async()=>{setIsLoading(true);try{client.query(q.Update(q.Ref(q.Collection(collectionIndex),id),{data})).then(result=>{setResponse(result.data);setIsLoading(false);});}catch(error){setError(error);}setIsLoading(false);};update();},[id]);return{response,error,isLoading};};
useFaunaReplace
exportconstuseFaunaReplace=(collectionIndex,data,id)=>{constfauna=React.useContext(FaunaContext);const{client,q}=fauna;const[response,setResponse]=React.useState(null);const[error,setError]=React.useState(null);const[isLoading,setIsLoading]=React.useState(false);React.useEffect(()=>{constreplace=async()=>{setIsLoading(true);try{client.query(q.Replace(q.Ref(q.Collection(collectionIndex),id),{data})).then(result=>{setResponse(result.data);setIsLoading(false);});}catch(error){setError(error);}setIsLoading(false);};replace();},[id]);return{response,error,isLoading};};
useFaunaDelete
exportconstuseFaunaDelete=(collectionIndex,id)=>{constfauna=React.useContext(FaunaContext);const{client,q}=fauna;const[response,setResponse]=React.useState(null);const[error,setError]=React.useState(null);const[isLoading,setIsLoading]=React.useState(false);React.useEffect(()=>{constdeleteIndex=async()=>{setIsLoading(true);try{client.query(q.Delete(q.Ref(q.Collection(collectionIndex),id))).then(result=>{setResponse(result.data);setIsLoading(false);});}catch(error){setError(error);}setIsLoading(false);};deleteIndex();},[id]);return{response,error,isLoading};};
Top comments(0)
Subscribe
For further actions, you may consider blocking this person and/orreporting abuse