From ec015b78494651af4d0cc5bd0bfdf414dcb4e58d Mon Sep 17 00:00:00 2001 From: dswbx Date: Tue, 25 Mar 2025 13:02:09 +0100 Subject: [PATCH] docs: add postgres and sqlocal instructions --- app/src/ui/hooks/use-event.ts | 17 ++++------ docs/usage/database.mdx | 63 ++++++++++++++++++++++++++++++++--- 2 files changed, 65 insertions(+), 15 deletions(-) diff --git a/app/src/ui/hooks/use-event.ts b/app/src/ui/hooks/use-event.ts index 26c39e3..23f8130 100644 --- a/app/src/ui/hooks/use-event.ts +++ b/app/src/ui/hooks/use-event.ts @@ -4,15 +4,12 @@ // there is no lifecycle or Hook in React that we can use to switch // .current at the right timing." // So we will have to make do with this "close enough" approach for now. -import { useEffect, useRef } from "react"; +import { useLayoutEffect, useRef } from "react"; +import { isDebug } from "core"; -export const useEvent = (fn: Fn | ((...args: any[]) => any) | undefined): Fn => { - const ref = useRef([fn, (...args) => ref[0](...args)]).current; - // Per Dan Abramov: useInsertionEffect executes marginally closer to the - // correct timing for ref synchronization than useLayoutEffect on React 18. - // See: https://github.com/facebook/react/pull/25881#issuecomment-1356244360 - useEffect(() => { - ref[0] = fn; - }, []); - return ref[1]; +export const useEvent = (fn: Fn): Fn => { + if (isDebug()) { + console.warn("useEvent() is deprecated"); + } + return fn; }; diff --git a/docs/usage/database.mdx b/docs/usage/database.mdx index 638abd9..0586572 100644 --- a/docs/usage/database.mdx +++ b/docs/usage/database.mdx @@ -55,12 +55,65 @@ connection object to your new database: } ``` -### Custom Connection - - Follow the progress of custom connections on its [Github Issue](https://github.com/bknd-io/bknd/issues/24). - If you're interested, make sure to upvote so it can be prioritized. - +### Cloudflare D1 +Using the [Cloudflare Adapter](/integration/cloudflare), you can choose to use a D1 database binding. To do so, you only need to add a D1 database to your `wrangler.toml` and it'll pick up automatically. To manually specify which D1 database to take, you can specify it manually: +```ts +import { serve, d1 } from "bknd/adapter/cloudflare"; + +export default serve({ + app: ({ env }) => d1({ binding: env.D1_BINDING }) +}); +``` + +### PostgreSQL +To use bknd with Postgres, you need to install the `@bknd/postgres` package. You can do so by running the following command: + +```bash +npm install @bknd/postgres +``` + +This package uses `pg` under the hood. If you'd like to see `postgres` or any other flavor, please create an [issue on Github](https://github.com/bknd-io/bknd/issues/new). + +To establish a connection to your database, you can use any connection options available on the [`pg`](https://node-postgres.com/apis/client) package. Here is a quick example using the [Node.js Adapter](http://localhost:3000/integration/node): + +```js +import { serve } from "bknd/adapter/node"; +import { PostgresConnection } from "@bknd/postgres"; + +/** @type {import("bknd/adapter/node").NodeBkndConfig} */ +const config = { + connection: new PostgresConnection({ + connectionString: + "postgresql://user:password@localhost:5432/database", + }), +}; + +serve(config); +``` + +### SQLocal +To use bknd with `sqlocal` for a offline expierence, you need to install the `@bknd/sqlocal` package. You can do so by running the following command: + +```bash +npm install @bknd/sqlocal +``` + +This package uses `sqlocal` under the hood. Consult the [sqlocal documentation](https://sqlocal.dallashoffman.com/guide/setup) for connection options: + +```js +import { createApp } from "bknd"; +import { SQLocalConnection } from "@bknd/sqlocal"; + +const app = createApp({ + connection: new SQLocalConnection({ + databasePath: ":localStorage:", + verbose: true, + }) +}); +``` + +### Custom Connection Any bknd app instantiation accepts as connection either `undefined`, a connection object like described above, or an class instance that extends from `Connection`: