Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Richard Haines
Richard Haines

Posted on • Edited on

     

FaunaDB CRUD hooks (WIP)

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};};
Enter fullscreen modeExit fullscreen mode

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};};
Enter fullscreen modeExit fullscreen mode

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};};
Enter fullscreen modeExit fullscreen mode

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};};
Enter fullscreen modeExit fullscreen mode

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};};
Enter fullscreen modeExit fullscreen mode

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};};
Enter fullscreen modeExit fullscreen mode

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Writing docs @prisma I love to create content and fun experiences while exploring new technologies
  • Location
    Örnsköldsvik
  • Work
    Tech Writer at Prisma
  • Joined

More fromRichard Haines

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp