Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/SantaClaas/shopping-list.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";import { and, count, desc, eq, max, sql } from "drizzle-orm";import { drizzle } from "drizzle-orm/expo-sqlite";import { migrate } from "drizzle-orm/expo-sqlite/migrator";import { deleteDatabaseAsync, openDatabaseAsync } from "expo-sqlite/next";import { useEffect } from "react";import migrations from "./migrations/migrations";import { items, lists } from "./schema";import * as schema from "./schema";
async function initializeDatabase() { // if (__DEV__) await deleteDatabaseAsync(".db");
const expoDatabase = await openDatabaseAsync(".db"); // Drizzle deez database
const database = drizzle(expoDatabase, { schema, logger: __DEV__ }); database.run(sql`PRAGMA foreign_keys = ON;`);
await migrate(database, migrations); return database;}
function useDatabase() { return useQuery({ queryKey: ["database"], queryFn: initializeDatabase, // The database lives for the lifetime of the app staleTime: Infinity, gcTime: Infinity, });}export type List = typeof lists.$inferSelect & { itemsCount: number; // Can be null if there are no items itemsLastUpdatedUtc: Date | null;};
export type NewList = Required<typeof lists.$inferInsert>;const LISTS_KEY = ["lists"] as const;export function useLists() { const { data: database, error } = useDatabase();
useEffect(() => { if (!error) return; console.error("(TODO) Database Error", error); }, [error]); const query = useQuery({ queryKey: LISTS_KEY, async queryFn() { // Query is only enabled if the database is available return ( (await database! .select({ id: lists.id, name: lists.name, lastUpdatedUtc: lists.lastUpdatedUtc, createdUtc: lists.createdUtc, itemsCount: count(items.id), itemsLastUpdatedUtc: max(items.lastUpdatedUtc), }) .from(lists) .leftJoin(items, eq(lists.id, items.listId)) .orderBy( desc( // Get last update date sql`max(max(coalesce(${items.lastUpdatedUtc}, 0)),${lists.lastUpdatedUtc},${lists.createdUtc})`, ), ) // Group by is required, otherwise we get a row with everything set to null except the count which is 0 .groupBy(lists.id)) satisfies List[] ); }, // This makes the query run as soon as the database is available enabled: !!database, });
const queryClient = useQueryClient(); const createList = useMutation({ async mutationFn(list: NewList) { await database?.insert(lists).values(list); }, async onMutate(newList) { // Optimistically update the cache await queryClient.cancelQueries({ queryKey: LISTS_KEY });
// Snapshot the previous value const previousLists = queryClient.getQueryData<List[]>(LISTS_KEY);
// Optimistically update to the new value queryClient.setQueryData<List[]>(LISTS_KEY, (old) => { const list = { ...newList, itemsCount: 0, itemsLastUpdatedUtc: null, };
// The new list should be the latest so we prepend it return old ? [list, ...old] : [list]; });
// Return a context object with the snapshotted value return { previousLists }; }, onError(_error, _newList, context) { // Rollback the cache update queryClient.setQueryData(LISTS_KEY, context?.previousLists); //TODO display error to user }, // Don't refetch the query after success or failure // because this is a local database and no one else is mutating it // so we can easily and safely mirror the state });
const deleteList = useMutation({ async mutationFn(list: List) { await database?.delete(lists).where(eq(lists.id, list.id)); }, async onMutate(deletedList) { // Optimistically update the cache await queryClient.cancelQueries({ queryKey: LISTS_KEY });
// Snapshot the previous value const previousLists = queryClient.getQueryData<List[]>(LISTS_KEY);
// Optimistically update to the new value queryClient.setQueryData<List[]>(LISTS_KEY, (old) => old?.filter((list) => list.id !== deletedList.id), );
// Return a context object with the snapshotted value return { previousLists }; }, onError(_error, _deletedList, context) { // Rollback the cache update queryClient.setQueryData(LISTS_KEY, context?.previousLists); //TODO display error to user }, });
return { query, createList, deleteList };}
export type Item = typeof items.$inferSelect;// Require all fields as defaults for types are just used as fallback and values should be set from application codeexport type NewItem = Required<typeof items.$inferInsert>;
export function useList(id: string) { const { data: database } = useDatabase(); const queryKey = ["lists", id] as const;
const query = useQuery({ queryKey: queryKey, async queryFn({ queryKey: [, listId] }) { return await database!.query.lists.findFirst({ where: (lists, { eq }) => eq(lists.id, listId), }); }, enabled: !!database, });
return query;}
export function useListItems(listId: string) { const { data: database } = useDatabase(); const queryKey = ["lists", listId, "items"] as const;
const query = useQuery({ queryKey: queryKey, async queryFn({ queryKey: [, listId] }) { // Query is only enabled if the database is available return (await database! .select() .from(items) .where(eq(items.listId, listId))) satisfies Item[]; }, enabled: !!database, });
const queryClient = useQueryClient(); const insert = useMutation({ async mutationFn(item: NewItem) { await database?.insert(items).values(item); }, async onMutate(newItem) { // Optimistically update the cache await queryClient.cancelQueries({ queryKey });
// Snapshot the previous value const previousItems = queryClient.getQueryData<Item[]>(queryKey);
// Optimistically update to the new value queryClient.setQueryData<Item[]>(queryKey, (old) => old ? [...old, newItem] : [newItem], );
// Return a context object with the snapshotted value return { previousItems }; }, onError(_error, _newItem, context) { // Rollback the cache update queryClient.setQueryData(queryKey, context?.previousItems); //TODO display error to user }, // Don't refetch the query after success or failure // because this is a local database and no one else is mutating it // so we can easily and safely mirror the state onSuccess() { // Invalidate the list query to refetch last updated and items count queryClient.invalidateQueries({ queryKey: LISTS_KEY }); }, });
const deleter = useMutation({ async mutationFn(item: Item) { await database ?.delete(items) .where(and(eq(items.id, item.id), eq(items.listId, listId))); }, async onMutate(deletedItem) { // Optimistically update the cache await queryClient.cancelQueries({ queryKey });
// Snapshot the previous value const previousItems = queryClient.getQueryData<Item[]>(queryKey);
// Optimistically update to the new value queryClient.setQueryData<Item[]>(queryKey, (old) => old?.filter((item) => item.id !== deletedItem.id), );
// Return a context object with the snapshotted value return { previousItems }; }, onError(_error, _deletedItem, context) { // Rollback the cache update queryClient.setQueryData(queryKey, context?.previousItems); //TODO display error to user }, onSuccess() { // Invalidate the list query to refetch last updated and items count queryClient.invalidateQueries({ queryKey: LISTS_KEY }); }, });
const updateIsChecked = useMutation({ async mutationFn({ itemId, isChecked, }: { itemId: string; isChecked: boolean; }) { await database! .update(items) .set({ isChecked, lastUpdatedUtc: new Date(Date.now()) }) .where(and(eq(items.id, itemId), eq(items.listId, listId))); }, async onMutate({ itemId, isChecked, }: { itemId: string; isChecked: boolean; }) { // Optimistically update the cache await queryClient.cancelQueries({ queryKey });
// Snapshot the previous value const previousItems = queryClient.getQueryData<Item[]>(queryKey);
// Optimistically update to the new value queryClient.setQueryData<Item[]>(queryKey, (old) => { if (!old) return old;
const index = old?.findIndex((item) => item.id === itemId);
// Could be a warning level because updating a non-existent item is weird if (index === -1) return old;
old[index].isChecked = isChecked;
return old; });
// Return a context object with the snapshotted value return { previousItems }; }, onError(_error, _updated, context) { // Rollback the cache update queryClient.setQueryData(queryKey, context?.previousItems); //TODO display error to user }, onSuccess() { // Invalidate the general list query to refetch last updated queryClient.invalidateQueries({ queryKey: LISTS_KEY }); }, });
return { query, insert, delete: deleter, updateIsChecked };}