Skip to content
Snippets Groups Projects
Commit c8265900 authored by Jake Taylor's avatar Jake Taylor
Browse files

added basic model.go

parent 06ad0e9c
No related branches found
No related tags found
2 merge requests!60Revert "Fail a test to be sure it works",!4Xx 4114/index db
...@@ -17,16 +17,19 @@ import ( ...@@ -17,16 +17,19 @@ import (
"syscall/js" "syscall/js"
"gitlab.com/elixxir/client/channels" "gitlab.com/elixxir/client/channels"
"gitlab.com/xx_network/primitives/id"
) )
// currentVersion of the IndexDb runtime. Used for migration purposes. const (
const currentVersion uint = 1 // databaseSuffix to be appended to the name of the database
databaseSuffix = "_messenger"
// currentVersion of the IndexDb runtime. Used for migration purposes.
currentVersion uint = 1
)
// NewWasmEventModel returns a [channels.EventModel] backed by a wasmModel // NewWasmEventModel returns a [channels.EventModel] backed by a wasmModel
func NewWasmEventModel(receptionId *id.ID) (channels.EventModel, error) { func NewWasmEventModel(username string) (channels.EventModel, error) {
ctx := context.Background() ctx := context.Background()
databaseName := receptionId.String() databaseName := username + databaseSuffix
// Attempt to open database object // Attempt to open database object
openRequest, _ := idb.Global().Open(ctx, databaseName, currentVersion, openRequest, _ := idb.Global().Open(ctx, databaseName, currentVersion,
...@@ -47,6 +50,7 @@ func NewWasmEventModel(receptionId *id.ID) (channels.EventModel, error) { ...@@ -47,6 +50,7 @@ func NewWasmEventModel(receptionId *id.ID) (channels.EventModel, error) {
return errors.Errorf("Invalid version upgrade path: v%d -> v%d", return errors.Errorf("Invalid version upgrade path: v%d -> v%d",
oldVersion, newVersion) oldVersion, newVersion)
}) })
// Wait for database open to finish // Wait for database open to finish
db, err := openRequest.Await(ctx) db, err := openRequest.Await(ctx)
......
////////////////////////////////////////////////////////////////////////////////
// Copyright © 2022 Privategrity Corporation /
// /
// All rights reserved. /
////////////////////////////////////////////////////////////////////////////////
//go:build js && wasm
// +build js,wasm
package indexedDb
import (
"time"
)
const (
// Text representation of primary key value (keyPath).
pkeyName = "id"
// Text representation of the names of the various [idb.ObjectStore].
messageStoreName = "messages"
userStoreName = "users"
channelsStoreName = "channels"
// Message index names.
messageStoreChannelIndex = "channel_id_index"
messageStoreParentIndex = "parent_message_id_index"
messageStoreTimestampIndex = "timestamp_index"
messageStorePinnedIndex = "pinned_index"
// Message keyPath names (must match json struct tags).
messageStoreChannel = "channel_id"
messageStoreParent = "parent_message_id"
messageStoreTimestamp = "timestamp"
messageStorePinned = "pinned"
)
// Message defines the IndexedDb representation of a single Message.
// A Message belongs to one User (Sender).
// A Message belongs to one Channel.
// A Message may belong to one Message (Parent).
type Message struct {
Id []byte `json:"id"` // Matches pkeyName
SenderId []byte `json:"sender_id"`
ChannelId []byte `json:"channel_id"` // Index
ParentMessageId []byte `json:"parent_message_id"` // Index
Timestamp time.Time `json:"timestamp"` // Index
Hidden bool `json:"hidden"`
Pinned bool `json:"pinned"` // Index
}
// User defines the IndexedDb representation of a single User.
// A User sends many Message.
type User struct {
Id []byte `json:"id"` // Matches pkeyName
Username string `json:"username"`
}
// Channel defines the IndexedDb representation of a single Channel
// A Channel has many Message.
type Channel struct {
Id []byte `json:"id"` // Matches pkeyName
Name string `json:"name"`
Description string `json:"description"`
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment