1import { IndexedDb, IndexedDbTable } from "@effect/platform-browser"
2import { assert, describe, it } from "@effect/vitest"
3import { Schema } from "effect"
4
5describe("IndexedDbTable", () => {
6 it("make", () => {
7 class Table extends IndexedDbTable.make({
8 name: "todo",
9 schema: Schema.Struct({
10 id: Schema.Number,
11 title: Schema.String,
12 completed: Schema.Boolean
13 }),
14 keyPath: "id"
15 }) {}
16
17 assert.equal(Table.tableName, "todo")
18 assert.equal(Table.autoIncrement, false)
19 assert.deepStrictEqual(Table.tableSchema.fields.id, Schema.Number)
20 assert.deepStrictEqual(Table.tableSchema.fields.title, Schema.String)
21 assert.deepStrictEqual(Table.tableSchema.fields.completed, Schema.Boolean)
22 assert.equal(Table.keyPath, "id")
23 })
24
25 it("autoIncrement", () => {
26 class Table1 extends IndexedDbTable.make({
27 name: "todo",
28 schema: Schema.Struct({
29 id: IndexedDb.AutoIncrement,
30 title: Schema.String
31 }),
32 keyPath: "id",
33 autoIncrement: true
34 }) {}
35
36 class Table2 extends IndexedDbTable.make({
37 name: "todo",
38 schema: Schema.Struct({
39 id: IndexedDb.AutoIncrement,
40 title: Schema.String
41 }),
42 keyPath: "id",
43 autoIncrement: false
44 }) {}
45
46 assert.equal(Table1.tableName, "todo")
47 assert.equal(Table1.autoIncrement, true)
48 assert.deepStrictEqual(
49 Table1.tableSchema.fields.id,
50 IndexedDb.AutoIncrement
51 )
52 assert.deepStrictEqual(Table1.tableSchema.fields.title, Schema.String)
53 assert.equal(Table1.keyPath, "id")
54
55 assert.equal(Table2.tableName, "todo")
56 assert.equal(Table2.autoIncrement, false)
57 assert.deepStrictEqual(
58 Table2.tableSchema.fields.id,
59 IndexedDb.AutoIncrement
60 )
61 assert.deepStrictEqual(Table2.tableSchema.fields.title, Schema.String)
62 assert.equal(Table2.keyPath, "id")
63 })
64
65 it("multiple keyPath", () => {
66 const Table = IndexedDbTable.make({
67 name: "todo",
68 schema: Schema.Struct({
69 id: Schema.Number,
70 title: Schema.String,
71 completed: Schema.Boolean
72 }),
73 keyPath: ["id", "title"]
74 })
75
76 assert.equal(Table.tableName, "todo")
77 assert.equal(Table.autoIncrement, false)
78 assert.deepStrictEqual(Table.tableSchema.fields.id, Schema.Number)
79 assert.deepStrictEqual(Table.tableSchema.fields.title, Schema.String)
80 assert.deepStrictEqual(Table.tableSchema.fields.completed, Schema.Boolean)
81 assert.deepStrictEqual(Table.keyPath, ["id", "title"])
82 })
83
84 it("no keyPath", () => {
85 const Table = IndexedDbTable.make({
86 name: "todo",
87 schema: Schema.Struct({
88 id: Schema.Number,
89 title: Schema.String,
90 completed: Schema.Boolean
91 })
92 })
93
94 assert.equal(Table.tableName, "todo")
95 assert.equal(Table.autoIncrement, false)
96 assert.deepStrictEqual(Table.tableSchema.fields.id, Schema.Number)
97 assert.deepStrictEqual(Table.tableSchema.fields.title, Schema.String)
98 assert.deepStrictEqual(Table.tableSchema.fields.completed, Schema.Boolean)
99 assert.deepStrictEqual(Table.keyPath, undefined)
100 })
101})