Unverified Commit df65498e authored by An Nyeong's avatar An Nyeong
Browse files

Improve docs for SqliteKvStore

- Use code-group for platform-dependent code example
- Remove outdated contents of the README
- Add docs for the class constructor
parent da9acceb
Loading
Loading
Loading
Loading
+32 −4
Original line number Diff line number Diff line
@@ -59,19 +59,47 @@ Pros
Cons
:   Limited scalability, not suitable for high-traffic production.

~~~~ typescript
# Node.js and Deno. Use `'bun:sqlite'` on Bun instead.
import { DatbaseSync } from "node:sqlite";

::: code-group

~~~~ typescript [Deno]
import { DatabaseSync } from "node:sqlite";
import { createFederation } from "@fedify/fedify";
import { SqliteKvStore } from "@fedify/sqlite";

const db = new DatabaseSync(":memory:");
const federation = createFederation<void>({
  // ...
  kv: new SqliteKvStore(db),
});
~~~~

~~~~ typescript [Node.js]
import { DatabaseSync } from "node:sqlite";
import { createFederation } from "@fedify/fedify";
import { SqliteKvStore } from "@fedify/sqlite";

const db = new DatabaseSync(:memory:);
const db = new DatabaseSync(":memory:");
const federation = createFederation<void>({
  // ...
  kv: new SqliteKvStore(db),
});
~~~~

~~~~ typescript [Bun]
import { Database } from "bun:sqlite";
import { createFederation } from "@fedify/fedify";
import { SqliteKvStore } from "@fedify/sqlite";

const db = new Database(":memory:");
const federation = createFederation<void>({
  // ...
  kv: new SqliteKvStore(db),
});
~~~~

:::

### `DenoKvStore` (Deno only)

`DenoKvStore` is a key–value store implementation for [Deno] runtime that uses
+19 −11
Original line number Diff line number Diff line
@fedify/sqlite: SQLite drivers for Fedify
==============
=========================================

This package provides a SQLite-based key–value store implementation.
[![JSR][JSR badge]][JSR]
[![npm][npm badge]][npm]

This package provides a SQLite-based [`KvStore`] implementation for [Fedify].

[JSR]: https://jsr.io/@fedify/sqlite
[JSR badge]: https://jsr.io/badges/@fedify/sqlite
[npm]: https://www.npmjs.com/package/@fedify/sqlite
[npm badge]: https://img.shields.io/npm/v/@fedify/sqlite?logo=npm
[Fedify]: https://fedify.dev/
[`KvStore`]: https://jsr.io/@fedify/fedify/doc/federation/~/KvStore

## Usage

### Node.js
### Deno

```typescript
import { DatabaseSync } from 'node:sqlite';
@@ -15,24 +25,22 @@ const db = new DatabaseSync('./data.db');
const store = new SqliteKvStore(db);
```

### Bun
### Node.js

```typescript
import { Database } from 'bun:sqlite';
import { DatabaseSync } from 'node:sqlite';
import { SqliteKvStore } from '@fedify/sqlite';

const db = new Database('./data.db');
const db = new DatabaseSync('./data.db');
const store = new SqliteKvStore(db);
```

### Deno

For Deno, you can directly import from `@fedify/sqlite` when using the import map in `deno.json`:
### Bun

```typescript
import { DB } from 'https://deno.land/x/sqlite@v3.7.0/mod.ts';
import { Database } from 'bun:sqlite';
import { SqliteKvStore } from '@fedify/sqlite';

const db = new DB('./data.db');
const db = new Database('./data.db');
const store = new SqliteKvStore(db);
```
+5 −0
Original line number Diff line number Diff line
@@ -49,6 +49,11 @@ export class SqliteKvStore implements KvStore {
  readonly #tableName: string;
  #initialized: boolean;

  /**
   * Creates a new SQLite key–value store.
   * @param db The SQLite database to use. Supports `node:sqlite` and `bun:sqlite`.
   * @param options The options for the key–value store.
   */
  constructor(
    readonly db: PlatformDatabase,
    readonly options: SqliteKvStoreOptions = {},