Initial commit
This commit is contained in:
commit
47d0ec687b
15 changed files with 1704 additions and 0 deletions
53
test/instance.test.ts
Normal file
53
test/instance.test.ts
Normal file
|
@ -0,0 +1,53 @@
|
|||
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");
|
||||
});
|
Loading…
Add table
Add a link
Reference in a new issue