Basic Usage

Each call on the client: insert, find, one, count, exists, update, and delete.

db is the client from Let's start. Each section is one call.

Insert

Insert passes title and omits id. The row that comes back has id.

notes.ts
import { db } from "./db.ts";

export async function createNote(title: string) {
  const note = await db.notes.insert({ title }); 
  return note;
}

Find

find returns a list. limit: 1 asks for one row. The row is [0].

notes.ts
import { db } from "./db.ts";

export async function readNote(id: string) {
  const found = await db.notes.find({ where: { id }, limit: 1 }); 
  return found[0]; 
}

Omit the limit and the call is OKM1101. [0] is undefined when nothing matched. .all(reason) is how you ask for the full set.

One

one returns that row, or null when nothing matched. More than one match throws not_unique.

notes.ts
import { db } from "./db.ts";

export async function readOne(id: string) {
  return db.notes.one({ where: { id } }); 
}

Count

count returns how many rows match. It does not return the rows.

notes.ts
import { db } from "./db.ts";

export async function countNotes() {
  return db.notes.count(); 
}

Exists

exists returns true when a row matches. It does not return the row.

notes.ts
import { db } from "./db.ts";

export async function hasNote(id: string) {
  return db.notes.exists({ where: { id } }); 
}

Update

where picks the row. set names the new title. A write with no where is OKM1102.

notes.ts
import { db } from "./db.ts";

export async function renameNote(id: string, title: string) {
  await db.notes.update({ where: { id }, set: { title } }); 
}

Delete

where picks the row. A write with no where is OKM1102.

notes.ts
import { db } from "./db.ts";

export async function removeNote(id: string) {
  await db.notes.delete({ where: { id } }); 
}