Expo-SQlite atom with initial load and live-updates #2680
Replies: 1 comment
-
Whatever got to me: This was a stupid idea 🤦 Here is an example: export const testAtom4 = atomWithStorage(
'foo',
'bar',
{
getItem: async (key, initialValue) => {
const someColumn = await db.query.someTable.findFirst({ columns: { someColumn: true } })
return someColumn?.someColumn ?? initialValue
},
setItem: async (key, value) => {
db.update(someTable).set({ someColumn: value }).where(eq(someTable.id, 1)).execute()
},
removeItem: async (key) => {
db.delete(timerEventTable).where(eq(timerEventTable.id, 1)).execute()
},
subscribe: (key, callback, initialValue) => {
const listener = addDatabaseChangeListener(async ({ tableName, rowId }) => {
const someColumn = await db.query.someTable.findFirst({ columns: { someColumn: true } }) // typically the same query as for getItem
callback(someColumn?.someColumn ?? initialValue)
})
return () => {
listener.remove()
}
},
},
{ getOnInit: true },
) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
please see my comment below
--
Hi all,
I'm writing a react-native app with expo-sqlite.
Inspired by drizzles useLiveQuery, I wanted an atom with the following features:
I use drizzle as my ORM, but it can be replaced by any other solution.
Feel free to use it and please, feel free to give any feedback!
Beta Was this translation helpful? Give feedback.
All reactions