1.5.3 • Published 4 days ago

@jjmyers/api-store v1.5.3

Weekly downloads
-
License
ISC
Repository
github
Last release
4 days ago

API store

A react.js global state manager. Maintains relationships between objects. The idea here is that all you have to do is fetch data and then use it. Once the store is setup correctly, mutations, queries and all other actions will cause state to update wherever necessary with little to no intervention from your side.

☕ Support My Work

Hey there! 👋 If my npm libraries have made your coding journey easier or sparked creativity, consider supporting my work with a virtual coffee. Your generosity keeps the code flowing and inspires more innovations! ☕🚀

"Buy Me A Coffee"

This is an implimentation of https://www.npmjs.com/package/@jjmyers/object-relationship-store

Step 1.

Define your objects and their relationship

import {
  ORS,                          // Ref object-relationship-store
  createStore,                  // Ref object-relationship-store
  createRelationalObject,       // Ref object-relationship-store
  createRelationalObjectIndex,  // Ref object-relationship-store
  withOptions,                  // Ref object-relationship-store
  UseAPIStore,
  useStore,                     // Ref object-relationship-store ReturnType<typeof createStore>
  useStoreSelect,               // Ref object-relationship-store store.select()
  useStoreIndex,                // Ref object-relationship-store store.selectIndex()
  RelationalStoreProvider,
  useMutation,
  useQuery,
  useInfiniteQuery
} from "@jjmyers/api-store"

/**
 * To setup the store and all store related operations
 * Check object-relationship-store
 * https://www.npmjs.com/package/@jjmyers/object-relationship-store
 */

// Once you have gone through object-relationship-store and you have created a store
const store = createStore()

Step 2.

Wrap you application in RelationalStoreProvider

<RelationalStoreProvider store={store}>
  {children}
</RelationalStoreProvider>

Step 4.

You're done!

Use the following to get and set data.

type From = "user" | "wishlist" | "product"

// Example useage
const query = useQuery<From, User, User>({
  
  // Enable fetch on mount, by default it's true
  // Optional
  // Default TRUE
  enabled: true,

  // Optionally add a fetch to get data on mount
  fetch: () => GetData.request({user: 10}),

  // Select the data from the fetch result that is the object we expect
  // Optional
  getData: (fetchResult) => fetchResult.user

  /**
   * The selector here is from object-relationship-store
   * https://www.npmjs.com/package/@jjmyers/object-relationship-store
   * 
   * This is the same selector object
   */
  select: {
    from: "user",
    where: { id: 10 },
    fields: ["id", "wishlist"],
    join: [{
      on: "wishlist",
      fields: ["id", "products"],
      join: [{ on: "products", fields: "*" }]
    }]
  }
})

const {
  state,      // A piece of state that was selected (Will cause state to update if the object changes)
  result,     // The result from the fetch          (State will not change, this is just the raw result from the fetch)
  error,      // If there was an error, undefined otherwise
  isFetching, // Is fetching, Happens on mount and when refetch() is called
  refetch,    // Refetch the data
} = query


const infiniteQuery = useInfiniteQuery({
  index: "homeFeed-home",
  getData: r => r.data,
  getNextPageParams: r => r.nextParams,
  fetch: nextParams => fakePaginatingFetch(posts, nextParams),
  enabled: true // Default is true
  // select: {} // Optionally you can pass select here or it will just select the object with no joins.
})


const {
  state,          // A piece of state that was selected (Will cause state to update if the object changes)
  error,          // If there was an error, undefined otherwise
  isLoading,      // Happens only on mount
  isFetching,     // When fetchNextPage() is called
  hasNextPage,    
  nextPageParams,
  fetchNextPage,  // Fetch the next page
  refresh         // Clear the index and reinitialize the hook. Basically reset.
} = infiniteQuery;


  /**
   * NOTE: fakeFetch() returns the object that was passed in
   * 
   * Imagine a post = {id: 10}
   * 
   * const result = fakeFetch({id: 10})
   * 
   * console.log(result) // Will print "{id: 10}"
   */

  /**
   * We used __identify__ here, if you read 
   * https://www.npmjs.com/package/@jjmyers/object-relationship-store
   * You'll understand why
   * Similarly, you can add the result from the mutation to an 
   * index by passing __indexes__
   * Or you can delete the object by passing __destroy__
   */
  const updatePost = useMutation({ mutate: () => fakeFetch({ id: 10, createdAt: "Updated", __identify__: "post" }) })

  const {
    mutate,   // A function to start the mutation
    error,    // If error, otherwise undefined
    isLoading // If the mutation is fetching
  } = updatePost


  /**
   * Below is an example of a few things you can do in a mutation
   * If you read https://www.npmjs.com/package/@jjmyers/object-relationship-store, it will make more sense if it's not clear.
   */
  const updatePost = useMutation({ mutate: () => fakeFetch({ id: 10, createdAt: "Updated", __identify__: "post" }) })

  const createPost = useMutation({ mutate: () => fakeFetch({ id: 10, __identify__: "post", __indexes__: ["homeFeed-home"] }) })

  const deletePost = useMutation({ mutate: () => fakeFetch({ id: 10, __identify__: "post", __destroy__: true }) })
1.5.3

4 days ago

1.5.2

5 days ago

1.5.1

5 days ago

1.4.5

8 days ago

1.5.0

8 days ago

1.4.4

12 days ago

1.4.3

12 days ago

1.4.2

13 days ago

1.4.1

13 days ago

1.4.0

13 days ago

1.3.13

13 days ago

1.3.11

13 days ago

1.3.12

13 days ago

1.3.10

26 days ago

1.3.9

26 days ago

1.3.8

26 days ago

1.3.7

26 days ago

1.3.6

2 months ago

1.3.5

2 months ago

1.3.4

2 months ago

1.2.8

2 months ago

1.3.3

2 months ago

1.3.2

2 months ago

1.3.1

2 months ago

1.3.0

2 months ago

1.2.9

2 months ago

1.2.7

2 months ago

1.2.6

2 months ago

1.2.5

2 months ago

1.2.4

2 months ago

1.2.3

2 months ago

1.2.2

2 months ago

1.2.1

2 months ago

1.1.38

2 months ago

1.1.39

2 months ago

1.1.41

2 months ago

1.1.40

2 months ago

1.1.45

2 months ago

1.1.43

2 months ago

1.1.42

2 months ago

1.1.47

2 months ago

1.1.46

2 months ago

1.2.21

2 months ago

1.1.34

2 months ago

1.1.33

2 months ago

1.1.32

2 months ago

1.1.37

2 months ago

1.1.36

2 months ago

1.1.35

2 months ago

1.1.31

2 months ago

1.1.30

2 months ago

1.1.29

2 months ago

1.1.28

2 months ago

1.1.27

2 months ago

1.1.26

2 months ago

1.1.25

3 months ago

1.1.24

3 months ago

1.1.19

4 months ago

1.1.23

4 months ago

1.1.22

4 months ago

1.1.21

4 months ago

1.1.20

4 months ago

1.1.18

7 months ago

1.1.17

7 months ago

1.1.16

7 months ago

1.1.15

7 months ago

1.1.14

7 months ago

1.1.13

7 months ago

1.1.12

7 months ago

1.1.11

7 months ago

1.1.10

7 months ago

1.1.9

7 months ago

1.1.8

7 months ago

1.1.7

7 months ago

1.1.6

7 months ago

1.1.5

7 months ago

1.1.4

7 months ago

1.1.3

7 months ago

1.1.2

7 months ago

1.1.1

7 months ago

1.1.0

8 months ago

1.0.9

8 months ago

1.0.8

9 months ago

1.0.7

9 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

0.3.4

9 months ago

0.3.3

9 months ago

0.3.2

9 months ago

0.3.1

9 months ago