Documentation
¶
Index ¶
- Constants
- Variables
- func ColDesc(cols ...string) string
- type Option
- type Roach
- func (r *Roach) APIKeysByUserID(usrID string, offset, count int64) ([]api.Key, error)
- func (r *Roach) DeleteEmailTokensAtomic(tx *sql.Tx, email string) error
- func (r *Roach) DeletePhoneTokensAtomic(tx *sql.Tx, phone string) error
- func (r *Roach) EmailTokens(userID string, offset, count int64) ([]model.DBToken, error)
- func (r *Roach) ExecuteTx(fn func(*sql.Tx) error) error
- func (r *Roach) GetSMTPConfig(conf interface{}) error
- func (r *Roach) Group(id string) (*model.Group, error)
- func (r *Roach) GroupByName(name string) (*model.Group, error)
- func (r *Roach) Groups(offset, count int64) ([]model.Group, error)
- func (r *Roach) GroupsByUserID(usrID string) ([]model.Group, error)
- func (r *Roach) HasUsers(groupID string) error
- func (r *Roach) InitDBIfNot() error
- func (r *Roach) InsertAPIKey(userID, key string) (*api.Key, error)
- func (r *Roach) InsertEmailToken(userID, email string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)
- func (r *Roach) InsertEmailTokenAtomic(tx *sql.Tx, userID, email string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)
- func (r *Roach) InsertGroup(name string, acl float32) (*model.Group, error)
- func (r *Roach) InsertPhoneToken(userID, phone string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)
- func (r *Roach) InsertPhoneTokenAtomic(tx *sql.Tx, userID, phone string, dbt []byte, isUsed bool, expiry time.Time) (*model.DBToken, error)
- func (r *Roach) InsertUserAtomic(tx *sql.Tx, t model.UserType, g model.Group, password []byte) (*model.User, error)
- func (r *Roach) InsertUserDeviceAtomic(tx *sql.Tx, userID, devID string) (*model.Device, error)
- func (r *Roach) InsertUserEmail(userID, email string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) InsertUserEmailAtomic(tx *sql.Tx, userID, email string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) InsertUserFbIDAtomic(tx *sql.Tx, userID, fbID string, verified bool) (*model.Facebook, error)
- func (r *Roach) InsertUserName(userID, username string) (*model.Username, error)
- func (r *Roach) InsertUserNameAtomic(tx *sql.Tx, userID, username string) (*model.Username, error)
- func (r *Roach) InsertUserPhone(userID, phone string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) InsertUserPhoneAtomic(tx *sql.Tx, userID, phone string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) InsertUserType(name string) (*model.UserType, error)
- func (r *Roach) PhoneTokens(userID string, offset, count int64) ([]model.DBToken, error)
- func (r *Roach) SetEmailTokenUsedAtomic(tx *sql.Tx, id string) error
- func (r *Roach) SetPhoneTokenUsedAtomic(tx *sql.Tx, id string) error
- func (r *Roach) SetUserGroup(userID, groupID string) error
- func (r *Roach) UpdatePassword(userID string, password []byte) error
- func (r *Roach) UpdatePasswordAtomic(tx *sql.Tx, userID string, password []byte) error
- func (r *Roach) UpdateUserEmail(userID, email string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) UpdateUserEmailAtomic(tx *sql.Tx, userID, email string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) UpdateUserPhone(userID, phone string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) UpdateUserPhoneAtomic(tx *sql.Tx, userID, phone string, verified bool) (*model.VerifLogin, error)
- func (r *Roach) UpdateUsername(userID, username string) (*model.Username, error)
- func (r *Roach) UpsertSMTPConfig(conf interface{}) error
- func (r *Roach) User(id string) (*model.User, []byte, error)
- func (r *Roach) UserByDeviceID(devID string) (*model.User, []byte, error)
- func (r *Roach) UserByEmail(email string) (*model.User, []byte, error)
- func (r *Roach) UserByFacebook(fbID string) (*model.User, error)
- func (r *Roach) UserByPhone(phone string) (*model.User, []byte, error)
- func (r *Roach) UserByUsername(username string) (*model.User, []byte, error)
- func (r *Roach) UserDevicesByUserID(usrID string) ([]model.Device, error)
- func (r *Roach) UserTypeByName(name string) (*model.UserType, error)
- func (r *Roach) Users(uq model.UsersQuery, offset, count int64) ([]model.User, error)
Constants ¶
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 ); ` )
const TimeFormat = ""
Variables ¶
var AllTableDescs = []string{ TblDescConfigurations, TblDescUserTypes, TblDescGroups, TblDescUsers, TblDescAPIKeys, TblDescDeviceIDs, TblDescUserNames, TblDescEmails, TblDescEmailTokens, TblDescPhones, TblDescPhoneTokens, TblDescFacebookIDs, TblDescRefreshTokens, }
AllTableDescs lists all CREATE TABLE DESCRIPTIONS in order of dependency (tables with foreign key references listed after parent table descriptions).
var AllTableNames = []string{ TblConfigurations, TblUserTypes, TblGroups, TblUsers, TblAPIKeys, TblDeviceIDs, TblUserNames, TblEmails, TblEmailTokens, TblPhones, TblPhoneTokens, TblFacebookIDs, TblRefreshTokens, }
AllTableNames lists all table names in order of dependency (tables with foreign key references listed after parent table descriptions).
Functions ¶
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 ¶
WithDBName sets the name of the cockroach database 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 ¶
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 ¶
APIKeysByUserID returns API keys for the provided userID starting with the newest.
func (*Roach) DeleteEmailTokensAtomic ¶
func (*Roach) DeletePhoneTokensAtomic ¶
func (*Roach) EmailTokens ¶
EmailTokens fetches email tokens for userID starting with the newest.
func (*Roach) ExecuteTx ¶
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 ¶
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) GroupByName ¶
Group fetches a group by name.
func (*Roach) GroupsByUserID ¶
GroupsByUserID fetches a group having id.
func (*Roach) InitDBIfNot ¶
InitDBIfNot connects to and sets up the DB; creating it and tables if necessary.
func (*Roach) InsertAPIKey ¶
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 ¶
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 (*Roach) InsertUserEmail ¶
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 (*Roach) InsertUserName ¶
InsertUserType inserts into the database returning calculated values.
func (*Roach) InsertUserNameAtomic ¶
InsertUserType inserts through tx returning calculated values.
func (*Roach) InsertUserPhone ¶
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 ¶
InsertUserType inserts into the database returning calculated values.
func (*Roach) PhoneTokens ¶
PhoneTokens fetches phone tokens for userID starting with the none-used, newest.
func (*Roach) SetEmailTokenUsedAtomic ¶
func (*Roach) SetPhoneTokenUsedAtomic ¶
func (*Roach) SetUserGroup ¶
SetUserGroup associates groupID (from TblGroups) with userID if not already associated, otherwise returns an error.
func (*Roach) UpdatePassword ¶
UpdatePassword stores the new password for userID' account.
func (*Roach) UpdatePasswordAtomic ¶
UpdatePasswordAtomic stores the new password for userID' account using tx.
func (*Roach) UpdateUserEmail ¶
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 ¶
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 ¶
UpdateUsername sets the new username for userID.
func (*Roach) UpsertSMTPConfig ¶
UpsertSMTPConfig upserts SMTP config values into the db.
func (*Roach) UserByDeviceID ¶
UserByDeviceID fetches User and password for account with devID.
func (*Roach) UserByEmail ¶
UserByEmail fetches User and password for account with email.
func (*Roach) UserByFacebook ¶
UserByFacebook fetches User and password for account with fbID.
func (*Roach) UserByPhone ¶
UserByPhone fetches User and password for account with phone.
func (*Roach) UserByUsername ¶
UserByUsername fetches User and password for account with username.