1 //go:build js && wasm
2 // +build js,wasm
3
4 package idb
5
6 import (
7 "github.com/aperturerobotics/go-indexeddb/idb/internal/jscache"
8 "github.com/hack-pad/safejs"
9 )
10
11 // Database provides a connection to a database. You can use a Database object to open a transaction on your database then create, manipulate, and delete objects (data) in that database.
12 type Database struct {
13 jsDB safejs.Value
14 callStrings jscache.Strings
15 }
16
17 func wrapDatabase(jsDB safejs.Value) *Database {
18 return &Database{jsDB: jsDB}
19 }
20
21 // Name returns the name of the connected database.
22 func (db *Database) Name() (string, error) {
23 value, err := db.jsDB.Get("name")
24 if err != nil {
25 return "", err
26 }
27 return value.String()
28 }
29
30 // Version returns the version of the connected database.
31 func (db *Database) Version() (uint, error) {
32 value, err := db.jsDB.Get("version")
33 if err != nil {
34 return 0, err
35 }
36 intValue, err := value.Int()
37 return uint(intValue), err
38 }
39
40 // ObjectStoreNames returns a list of the names of the object stores currently in the connected database.
41 func (db *Database) ObjectStoreNames() ([]string, error) {
42 array, err := db.jsDB.Get("objectStoreNames")
43 if err != nil {
44 return nil, err
45 }
46 return stringsFromArray(array)
47 }
48
49 // CreateObjectStore creates and returns a new object store or index.
50 func (db *Database) CreateObjectStore(name string, options ObjectStoreOptions) (*ObjectStore, error) {
51 jsObjectStore, err := db.jsDB.Call("createObjectStore", name, map[string]interface{}{
52 "autoIncrement": options.AutoIncrement,
53 "keyPath": options.KeyPath,
54 })
55 if err != nil {
56 return nil, tryAsDOMException(err)
57 }
58 return wrapObjectStore(nil, jsObjectStore), nil
59 }
60
61 // DeleteObjectStore destroys the object store with the given name in the connected database, along with any indexes that reference it.
62 func (db *Database) DeleteObjectStore(name string) error {
63 _, err := db.jsDB.Call("deleteObjectStore", name)
64 return tryAsDOMException(err)
65 }
66
67 // Close closes the connection to a database.
68 func (db *Database) Close() error {
69 _, err := db.jsDB.Call("close")
70 return tryAsDOMException(err)
71 }
72
73 // Transaction returns a transaction object containing the Transaction.ObjectStore() method, which you can use to access your object store.
74 func (db *Database) Transaction(mode TransactionMode, objectStoreName string, objectStoreNames ...string) (_ *Transaction, err error) {
75 return db.TransactionWithOptions(TransactionOptions{Mode: mode}, objectStoreName, objectStoreNames...)
76 }
77
78 // TransactionOptions contains all available options for creating and starting a Transaction
79 type TransactionOptions struct {
80 Mode TransactionMode
81 Durability TransactionDurability
82 }
83
84 // TransactionWithOptions returns a transaction object containing the Transaction.ObjectStore() method, which you can use to access your object store.
85 func (db *Database) TransactionWithOptions(options TransactionOptions, objectStoreName string, objectStoreNames ...string) (*Transaction, error) {
86 objectStoreNames = append([]string{objectStoreName}, objectStoreNames...) // require at least one name
87
88 optionsMap := make(map[string]interface{})
89 if options.Durability != DurabilityDefault {
90 optionsMap["durability"] = options.Durability.jsValue()
91 }
92
93 args := []interface{}{sliceFromStrings(objectStoreNames), options.Mode.jsValue()}
94 if len(optionsMap) > 0 {
95 args = append(args, optionsMap)
96 }
97
98 jsTxn, err := db.jsDB.Call("transaction", args...)
99 if err != nil {
100 return nil, tryAsDOMException(err)
101 }
102 return wrapTransaction(db, jsTxn), nil
103 }
104