Skip to content
This repository has been archived by the owner on Jul 23, 2023. It is now read-only.
/ hamster Public archive

A relational API for Deno KV

License

Notifications You must be signed in to change notification settings

vwkd/hamster

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

55 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

README

A relational API for Deno KV

Features

  • insert, read, update and delete rows of tables
  • automatic auto-incrementing IDs
  • schema-fixed, validates all input against schema
  • full TypeScript type inference

Use

import { z } from "https://raw.githubusercontent.com/vwkd/hamster/main/deps.ts";
import { openDatabase } from "https://raw.githubusercontent.com/vwkd/hamster/main/src/main.ts";

const options = {
  tables: {
    "countries": {
      "name": z.string(),
      "color": z.string().optional(),
    },
  },
};

const db = await openDatabase(options);

const res = await db
  .insert("countries", { name: "USA", color: "blue" });

if (res.ok) {
  const id = res.id;

  const a = await db
    .read("countries", { id });
  console.log(id, a);

  await db
    .update("countries", { id }, { color: "red" });

  const b = await db
    .read("countries", { id });
  console.log(id, b);

  await db
    .delete("countries", { id })

  const c = await db
    .read("countries", { id });
  console.log(id, c);
}

db.close();

TODO

  • read multiple rows at once based on condition, allow multiple IDs in where, but only makes sense for get?!
  • support secondary indeces