mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
docs: add postgres and sqlocal instructions
This commit is contained in:
@@ -4,15 +4,12 @@
|
|||||||
// there is no lifecycle or Hook in React that we can use to switch
|
// there is no lifecycle or Hook in React that we can use to switch
|
||||||
// .current at the right timing."
|
// .current at the right timing."
|
||||||
// So we will have to make do with this "close enough" approach for now.
|
// 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: Fn | ((...args: any[]) => any) | undefined): Fn => {
|
export const useEvent = <Fn>(fn: Fn): Fn => {
|
||||||
const ref = useRef([fn, (...args) => ref[0](...args)]).current;
|
if (isDebug()) {
|
||||||
// Per Dan Abramov: useInsertionEffect executes marginally closer to the
|
console.warn("useEvent() is deprecated");
|
||||||
// correct timing for ref synchronization than useLayoutEffect on React 18.
|
}
|
||||||
// See: https://github.com/facebook/react/pull/25881#issuecomment-1356244360
|
return fn;
|
||||||
useEffect(() => {
|
|
||||||
ref[0] = fn;
|
|
||||||
}, []);
|
|
||||||
return ref[1];
|
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -55,12 +55,65 @@ connection object to your new database:
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
### Custom Connection
|
### Cloudflare D1
|
||||||
<Note>
|
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:
|
||||||
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.
|
|
||||||
</Note>
|
|
||||||
|
|
||||||
|
```ts
|
||||||
|
import { serve, d1 } from "bknd/adapter/cloudflare";
|
||||||
|
|
||||||
|
export default serve<Env>({
|
||||||
|
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
|
Any bknd app instantiation accepts as connection either `undefined`, a connection object like
|
||||||
described above, or an class instance that extends from `Connection`:
|
described above, or an class instance that extends from `Connection`:
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user