Instance to wrappedrow

This commit is contained in:
Endeavorance 2025-01-01 11:53:02 -05:00
parent 47d0ec687b
commit 13c3d86a10
6 changed files with 59 additions and 73 deletions

View file

@ -1,53 +0,0 @@
import { Database } from "bun:sqlite";
import { expect, test } from "bun:test";
import { Instance, Prequel, Table } from "../src/index";
interface User {
id: number;
name: string;
}
interface SerializedUser {
name: string;
}
const db = new Database();
const table = new Table<User>(db, "Users", {
id: {
type: "INTEGER",
primary: true,
},
name: "TEXT",
});
class UserInstance extends Instance<typeof table, SerializedUser> {
get name(): string {
return this.row.name;
}
set name(val: string) {
this.row.name = val;
}
serialize(): SerializedUser {
return {
name: this.row.name,
};
}
}
test("setting values on an instance", () => {
table.insert({
id: 1,
name: "Alice",
});
const alice = table.findOneByIdOrFail(1);
const inst = new UserInstance(table, alice);
inst.name = "Bob";
inst.save();
const bob = table.findOneByIdOrFail(1);
expect(bob.name).toEqual("Bob");
});