db.go 3.4 KB

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