30 lines
845 B
Markdown
30 lines
845 B
Markdown
# Simplistic JSON database
|
|
|
|
- Save / load all data from one JSON file
|
|
- Automatically saves data after any modifications (create / update / delete)
|
|
- Autoincremented ids
|
|
|
|
## Usage
|
|
|
|
```typescript
|
|
import { Database } from "./database.ts";
|
|
import { Table } from "./table.ts";
|
|
|
|
const database = await Database.load("./data.json", async (emptyDatabase) => {
|
|
await emptyDatabase.createTable("foo");
|
|
await emptyDatabase.createTable("bar");
|
|
await emptyDatabase.createTable("baz");
|
|
});
|
|
|
|
const fooTable = await database.getTable("foo") as Table<{
|
|
id: number;
|
|
name: string;
|
|
}>;
|
|
|
|
const insertedEntry = await fooTable.insert({ name: "Hello world" });
|
|
const updatedEntries = await fooTable.update(
|
|
(candidat) => candidat.id === insertedEntry.id,
|
|
(entry) => entry.name = "Goodbye",
|
|
);
|
|
const deletedEntries = await fooTable.delete(() => true);
|
|
``` |