Public
Back

Version 23

1/31/2025
import { sqlite } from "https://esm.town/v/std/sqlite";
import hash from "npm:hash-it";

export default async function newUniq(ns: string): Promise<Uniq> {
let u = new Uniq(ns);
await u.init();
return u;
}

class Uniq {
private tbl: string;

public constructor(ns: string) {
this.tbl = `uniq_${ns}`;
}

public async init() {
await sqlite.execute(
`CREATE TABLE IF NOT EXISTS ${this.tbl} (hash INT PRIMARY KEY)`,
);
}

public async add(obj: any): Promise<boolean> {
const result = await sqlite.execute({
sql: `INSERT OR IGNORE INTO ${this.tbl} (hash) VALUES (:hash)`,
args: { hash: hash(obj) },
});

return result.rowsAffected !== 0;
}
public async clear() {
await sqlite.execute(`DELETE FROM ${this.tbl};`);
}
}

Updated: February 1, 2025