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

@ -7,6 +7,14 @@ interface UserWithID {
name: string;
}
interface UserWithBio {
user_id: number;
bio: {
name: string;
age: number;
};
}
interface Post {
post_id: number;
user_id: number;

View file

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