db

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2020 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// Database definition version
	Version = 1

	// Table names
	TblConfigurations = "configurations"
	TblUserTypes      = "userTypes"
	TblGroups         = "groups"
	TblUsers          = "users"
	TblAPIKeys        = "apiKeys"
	TblDeviceIDs      = "deviceIDs"
	TblUserNames      = "userNames"
	TblEmails         = "emails"
	TblEmailTokens    = "emailTokens"
	TblPhones         = "phones"
	TblPhoneTokens    = "phoneTokens"
	TblFacebookIDs    = "facebookIDs"
	TblRefreshTokens  = "refreshTokens"

	// DB Table Columns
	ColID          = "ID"
	ColPassword    = "password"
	ColCreateDate  = "createDate"
	ColUpdateDate  = "updateDate"
	ColName        = "name"
	ColAccessLevel = "accessLevel"
	ColUserID      = "userID"
	ColGroupID     = "groupID"
	ColUserName    = "username"
	ColEmail       = "email"
	ColPhone       = "phone"
	ColVerified    = "verified"
	ColFacebookID  = "facebookID"
	ColToken       = "token"
	ColIsUsed      = "isUsed"
	ColIssueDate   = "issueDate"
	ColExpiryDate  = "expiryDate"
	ColKey         = "key"
	ColValue       = "value"
	ColTypeID      = "typeID"
	ColDevID       = "deviceID"
	ColIsRevoked   = "isRevoked"
	ColAPIKeyID    = "apiKeyID"

	// CREATE TABLE DESCRIPTIONS
	TblDescConfigurations = `
	CREATE TABLE IF NOT EXISTS ` + TblConfigurations + ` (
		` + ColKey + ` VARCHAR(56) PRIMARY KEY NOT NULL CHECK (` + ColKey + ` != ''),
		` + ColValue + ` BYTEA NOT NULL CHECK (` + ColValue + ` != ''),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescUserTypes = `
	CREATE TABLE IF NOT EXISTS ` + TblUserTypes + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColName + ` VARCHAR(56) UNIQUE NOT NULL CHECK (` + ColName + ` != ''),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescGroups = `
	CREATE TABLE IF NOT EXISTS ` + TblGroups + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColName + ` VARCHAR(56) UNIQUE NOT NULL CHECK (` + ColName + ` != ''),
		` + ColAccessLevel + ` FLOAT NOT NULL CHECK (` + ColAccessLevel + ` BETWEEN 0 AND 10),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescUsers = `
	CREATE TABLE IF NOT EXISTS ` + TblUsers + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColTypeID + ` BIGINT NOT NULL REFERENCES ` + TblUserTypes + ` (` + ColID + `),
		` + ColGroupID + ` BIGINT NOT NULL REFERENCES ` + TblGroups + ` (` + ColID + `),
		` + ColPassword + ` BYTEA NOT NULL CHECK ( LENGTH(` + ColPassword + `) >= 8 ),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescAPIKeys = `
	CREATE TABLE IF NOT EXISTS ` + TblAPIKeys + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColUserID + ` BIGINT NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColKey + ` VARCHAR(256) NOT NULL CHECK ( LENGTH(` + ColKey + `) >= 56 ),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescDeviceIDs = `
	CREATE TABLE IF NOT EXISTS ` + TblDeviceIDs + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColDevID + ` VARCHAR(256) UNIQUE NOT NULL CHECK (` + ColDevID + ` != ''),
		` + ColUserID + ` BIGINT NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescUserNames = `
	CREATE TABLE IF NOT EXISTS ` + TblUserNames + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColUserName + ` VARCHAR(56) UNIQUE NOT NULL,
		` + ColUserID + ` BIGINT UNIQUE NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescEmails = `
	CREATE TABLE IF NOT EXISTS ` + TblEmails + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColEmail + ` VARCHAR(128) UNIQUE NOT NULL CHECK (` + ColEmail + ` != ''),
		` + ColUserID + ` BIGINT UNIQUE NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColVerified + ` BOOL NOT NULL DEFAULT FALSE,
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescEmailTokens = `
	CREATE TABLE IF NOT EXISTS ` + TblEmailTokens + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColUserID + ` BIGINT NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColEmail + ` VARCHAR(128) NOT NULL REFERENCES ` + TblEmails + ` (` + ColEmail + `),
		` + ColToken + ` BYTEA NOT NULL CHECK (LENGTH(` + ColToken + `)>0),
		` + ColIsUsed + ` BOOL NOT NULL,
		` + ColIssueDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColExpiryDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescPhones = `
	CREATE TABLE IF NOT EXISTS ` + TblPhones + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColPhone + ` VARCHAR(56) UNIQUE NOT NULL CHECK (` + ColPhone + ` != ''),
		` + ColUserID + ` BIGINT UNIQUE NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColVerified + ` BOOL NOT NULL DEFAULT FALSE,
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescPhoneTokens = `
	CREATE TABLE IF NOT EXISTS ` + TblPhoneTokens + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColUserID + ` BIGINT NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColPhone + ` VARCHAR(56) NOT NULL REFERENCES ` + TblPhones + ` (` + ColPhone + `),
		` + ColToken + ` BYTEA NOT NULL CHECK (LENGTH(` + ColToken + `)>0),
		` + ColIsUsed + ` BOOL NOT NULL,
		` + ColIssueDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColExpiryDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescFacebookIDs = `
	CREATE TABLE IF NOT EXISTS ` + TblFacebookIDs + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColFacebookID + ` VARCHAR(512) UNIQUE NOT NULL CHECK(` + ColFacebookID + ` != ''),
		` + ColUserID + ` BIGINT UNIQUE NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColVerified + ` BOOL NOT NULL DEFAULT FALSE,
		` + ColCreateDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColUpdateDate + ` TIMESTAMPTZ NOT NULL
	);
	`
	TblDescRefreshTokens = `
	CREATE TABLE IF NOT EXISTS ` + TblRefreshTokens + ` (
		` + ColID + ` BIGSERIAL PRIMARY KEY NOT NULL CHECK (` + ColID + `>0),
		` + ColUserID + ` BIGINT NOT NULL REFERENCES ` + TblUsers + ` (` + ColID + `),
		` + ColAPIKeyID + ` BIGINT NOT NULL REFERENCES ` + TblAPIKeys + ` (` + ColID + `),
		` + ColToken + ` BYTEA NOT NULL,
		` + ColIsRevoked + ` BOOL NOT NULL,
		` + ColIssueDate + ` TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
		` + ColExpiryDate + ` TIMESTAMPTZ NOT NULL
	);
	`
)
View Source
const TimeFormat = ""

Variables

AllTableDescs lists all CREATE TABLE DESCRIPTIONS in order of dependency (tables with foreign key references listed after parent table descriptions).

AllTableNames lists all table names in order of dependency (tables with foreign key references listed after parent table descriptions).

Functions

func ColDesc

func ColDesc(cols ...string) string

ColDesc returns a string containing cols in the given order separated by ",".

Types

type Option

type Option func(*Roach)

Option allows extra configuration for instantiating Roach. Use the With... functions to set options e.g.

nameOpt := WithDBName("my_app_db")

func WithDBName

func WithDBName(db string) Option

WithDBName sets the name of the cockroach database to be used by Roach.

func WithDSN

func WithDSN(dsn string) Option

WithDSN sets the DSN to be used by Roach.

type Roach

type Roach struct {
	errors.NotFoundErrCheck
	// contains filtered or unexported fields
}

Roach is a cockroach db store. Use NewRoach() to instantiate.

func NewRoach

func NewRoach(opts ...Option) *Roach

NewRoach creates an instance of *Roach. A db connection is only established when InitDBIfNot() or one of the Execute/Query methods is called.

func (*Roach) APIKeysByUserID

func (r *Roach) APIKeysByUserID(usrID string, offset, count int64) ([]api.Key, error)

APIKeysByUserID returns API keys for the provided userID starting with the newest.

func (*Roach) DeleteEmailTokensAtomic

func (r *Roach) DeleteEmailTokensAtomic(tx *sql.Tx, email string) error

func (*Roach) DeletePhoneTokensAtomic

func (r *Roach) DeletePhoneTokensAtomic(tx *sql.Tx, phone string) error

func (*Roach) EmailTokens

func (r *Roach) EmailTokens(userID string, offset, count int64) ([]model.DBToken, error)

EmailTokens fetches email tokens for userID starting with the newest.

func (*Roach) ExecuteTx

func (r *Roach) ExecuteTx(fn func(*sql.Tx) error) error

ExecuteTx prepares a transaction (with retries) for execution in fn. It commits the changes if fn returns nil, otherwise changes are rolled back.

func (*Roach) GetSMTPConfig

func (r *Roach) GetSMTPConfig(conf interface{}) error

GetSMTPConfig fetches SMTP config values from the db and unmarshals them into conf. this method fails if conf is nil or not a pointer.

func (*Roach) Group

func (r *Roach) Group(id string) (*model.Group, error)

Group fetches a group by id.

func (*Roach) GroupByName

func (r *Roach) GroupByName(name string) (*model.Group, error)

Group fetches a group by name.

func (*Roach) Groups

func (r *Roach) Groups(offset, count int64) ([]model.Group, error)

func (*Roach) GroupsByUserID

func (r *Roach) GroupsByUserID(usrID string) ([]model.Group, error)

GroupsByUserID fetches a group having id.

func (*Roach) HasUsers

func (r *Roach) HasUsers(groupID string) error

func (*Roach) InitDBIfNot

func (r *Roach) InitDBIfNot() error

InitDBIfNot connects to and sets up the DB; creating it and tables if necessary.

func (*Roach) InsertAPIKey

func (r *Roach) InsertAPIKey(userID, key string) (*api.Key, error)

InsertAPIKey inserts an API key for the userID.

func (*Roach) InsertEmailToken

func (r *Roach) InsertEmailToken(userID, email string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)

InsertEmailToken persists a token for email.

func (*Roach) InsertEmailTokenAtomic

func (r *Roach) InsertEmailTokenAtomic(tx *sql.Tx, userID, email string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)

InsertEmailTokenAtomic persists a token for email using tx.

func (*Roach) InsertGroup

func (r *Roach) InsertGroup(name string, acl float32) (*model.Group, error)

InsertGroup inserts into the database returning calculated values.

func (*Roach) InsertPhoneToken

func (r *Roach) InsertPhoneToken(userID, phone string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)

InsertPhoneToken persists a token for phone.

func (*Roach) InsertPhoneTokenAtomic

func (r *Roach) InsertPhoneTokenAtomic(tx *sql.Tx, userID, phone string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)

InsertPhoneTokenAtomic persists a token for phone using tx.

func (*Roach) InsertUserAtomic

func (r *Roach) InsertUserAtomic(tx *sql.Tx, t model.UserType, g model.Group, password []byte) (*model.User, error)

InsertUserType inserts into the database returning calculated values.

func (*Roach) InsertUserDeviceAtomic

func (r *Roach) InsertUserDeviceAtomic(tx *sql.Tx, userID, devID string) (*model.Device, error)

func (*Roach) InsertUserEmail

func (r *Roach) InsertUserEmail(userID, email string, verified bool) (*model.VerifLogin, error)

InsertUserPhone inserts email details for userID.

func (*Roach) InsertUserEmailAtomic

func (r *Roach) InsertUserEmailAtomic(tx *sql.Tx, userID, email string, verified bool) (*model.VerifLogin, error)

InsertUserEmailAtomic inserts email details for userID.

func (*Roach) InsertUserFbIDAtomic

func (r *Roach) InsertUserFbIDAtomic(tx *sql.Tx, userID, fbID string, verified bool) (*model.Facebook, error)

func (*Roach) InsertUserName

func (r *Roach) InsertUserName(userID, username string) (*model.Username, error)

InsertUserType inserts into the database returning calculated values.

func (*Roach) InsertUserNameAtomic

func (r *Roach) InsertUserNameAtomic(tx *sql.Tx, userID, username string) (*model.Username, error)

InsertUserType inserts through tx returning calculated values.

func (*Roach) InsertUserPhone

func (r *Roach) InsertUserPhone(userID, phone string, verified bool) (*model.VerifLogin, error)

InsertUserPhone inserts phone details for userID.

func (*Roach) InsertUserPhoneAtomic

func (r *Roach) InsertUserPhoneAtomic(tx *sql.Tx, userID, phone string, verified bool) (*model.VerifLogin, error)

InsertUserPhone inserts phone details for userID.

func (*Roach) InsertUserType

func (r *Roach) InsertUserType(name string) (*model.UserType, error)

InsertUserType inserts into the database returning calculated values.

func (*Roach) PhoneTokens

func (r *Roach) PhoneTokens(userID string, offset, count int64) ([]model.DBToken, error)

PhoneTokens fetches phone tokens for userID starting with the none-used, newest.

func (*Roach) SetEmailTokenUsedAtomic

func (r *Roach) SetEmailTokenUsedAtomic(tx *sql.Tx, id string) error

func (*Roach) SetPhoneTokenUsedAtomic

func (r *Roach) SetPhoneTokenUsedAtomic(tx *sql.Tx, id string) error

func (*Roach) SetUserGroup

func (r *Roach) SetUserGroup(userID, groupID string) error

SetUserGroup associates groupID (from TblGroups) with userID if not already associated, otherwise returns an error.

func (*Roach) UpdatePassword

func (r *Roach) UpdatePassword(userID string, password []byte) error

UpdatePassword stores the new password for userID' account.

func (*Roach) UpdatePasswordAtomic

func (r *Roach) UpdatePasswordAtomic(tx *sql.Tx, userID string, password []byte) error

UpdatePasswordAtomic stores the new password for userID' account using tx.

func (*Roach) UpdateUserEmail

func (r *Roach) UpdateUserEmail(userID, email string, verified bool) (*model.VerifLogin, error)

UpdateUserEmail updates email details for userID.

func (*Roach) UpdateUserEmailAtomic

func (r *Roach) UpdateUserEmailAtomic(tx *sql.Tx, userID, email string, verified bool) (*model.VerifLogin, error)

UpdateUserEmailAtomic updates email details for userID using tx.

func (*Roach) UpdateUserPhone

func (r *Roach) UpdateUserPhone(userID, phone string, verified bool) (*model.VerifLogin, error)

UpdateUserPhone updates phone details for userID.

func (*Roach) UpdateUserPhoneAtomic

func (r *Roach) UpdateUserPhoneAtomic(tx *sql.Tx, userID, phone string, verified bool) (*model.VerifLogin, error)

UpdateUserPhoneAtomic updates phone details for userID using tx.

func (*Roach) UpdateUsername

func (r *Roach) UpdateUsername(userID, username string) (*model.Username, error)

UpdateUsername sets the new username for userID.

func (*Roach) UpsertSMTPConfig

func (r *Roach) UpsertSMTPConfig(conf interface{}) error

UpsertSMTPConfig upserts SMTP config values into the db.

func (*Roach) User

func (r *Roach) User(id string) (*model.User, []byte, error)

User fetches User and password for account with id.

func (*Roach) UserByDeviceID

func (r *Roach) UserByDeviceID(devID string) (*model.User, []byte, error)

UserByDeviceID fetches User and password for account with devID.

func (*Roach) UserByEmail

func (r *Roach) UserByEmail(email string) (*model.User, []byte, error)

UserByEmail fetches User and password for account with email.

func (*Roach) UserByFacebook

func (r *Roach) UserByFacebook(fbID string) (*model.User, error)

UserByFacebook fetches User and password for account with fbID.

func (*Roach) UserByPhone

func (r *Roach) UserByPhone(phone string) (*model.User, []byte, error)

UserByPhone fetches User and password for account with phone.

func (*Roach) UserByUsername

func (r *Roach) UserByUsername(username string) (*model.User, []byte, error)

UserByUsername fetches User and password for account with username.

func (*Roach) UserDevicesByUserID

func (r *Roach) UserDevicesByUserID(usrID string) ([]model.Device, error)

func (*Roach) UserTypeByName

func (r *Roach) UserTypeByName(name string) (*model.UserType, error)

func (*Roach) Users

func (r *Roach) Users(uq model.UsersQuery, offset, count int64) ([]model.User, error)

Jump to

Keyboard shortcuts

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