db

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 23, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Internal package for handling database methods by listening for changes and dispatching updates to clients with the internal websocket package.

This package is used in the following way:

  1. Create a new DB type with Connect().
  2. Listen for changes with Listen().
  3. Disconnect from the database with Disconnect().

No need to call these methods exclusively, they are automatically called and are executed synchronously in the socketeer.go file.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CreateEvent

type CreateEvent struct {
	OperationType string `bson:"operationType"`
	FullDocument  bson.M `bson:"fullDocument"`
}

CreateEvent is a struct for handling mongo create events from the database.

  • OperationType is the type of operation, which is always "insert".
  • FullDocument is a struct for handling the full document.

type DB

type DB struct {
	Client *mongo.Client
	DB     *mongo.Database
	Coll   *mongo.Collection
}

DB is an interface for handling database methods.

  • Client is a mongo client.
  • DB is a mongo database.
  • Coll is a mongo collection.

func Connect

func Connect(uriString string, dbName string, collName string) (*DB, error)

Connect returns a new DB type by connecting to the database with the uri, database name, and collection name provided.

This method is utilized to create a new DB type and is called internally when the socketeer is started.

Parameters:

  • uriString (string): the uri string to connect to the database, example: mongodb://localhost:27017
  • dbName (string): the name of the database to connect to, example: mydb
  • collName (string): the name of the collection to connect to, example: mycollection

Example:

db.Connect("mongodb://localhost:27017", "mydb", "mycollection")

func (*DB) Disconnect

func (d *DB) Disconnect() error

Disconnect ends the connection to the database.

This method is called internally when the socketeer is stopped.

Example:

db.Disconnect()

func (*DB) Listen

func (d *DB) Listen(ws *ws.WebSocket, keys []string) error

Listen listens for changes in the database by the mongo watch & changeStream methods and dispatches updates to clients with the internal websocket package.

This method is called internally when the socketeer is started.

Parameters:

  • ws (WebSocket): the WebSocket type to dispatch updates to.
  • keys ([]string): the keys in the documents of the collection to listen for changes on.

Example:

db.Listen(ws, []string{"displayName", "email"})

type UpdateEvent

type UpdateEvent struct {
	OperationType     string `bson:"operationType"`
	UpdateDescription struct {
		UpdatedFields bson.M `bson:"updatedFields"`
	} `bson:"updateDescription"`
}

UpdateEvent is a struct for handling mongo update events from the database.

  • OperationType is the type of operation, which is always "update".
  • UpdateDescription is a struct for handling the updated fields.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL