Documentation ¶
Overview ¶
All the error codes can be found here: https://www.postgresql.org/docs/current/errcodes-appendix.html
Index ¶
- Constants
- Variables
- func Close() error
- func Connect(dbUrl string) error
- func DeleteBentoPermissionById(permsId int64) error
- func DeleteEmailVerificationWithUserId(uid string) (sql.Result, error)
- func DeleteIngridient(bentoId, name string) error
- func DeleteTokensOwnedByUser(tx *sql.Tx, userId string) (sql.Result, error)
- func ExistsBentoPermissionByUserBentoId(userId, bentoId string) (bool, error)
- func ExistsUserWithEmail(email string) (bool, error)
- func IsTokenValid(tokenId, tokenType string) error
- func Ping() error
- func RenameIngridient(bentoId, oldName, newName string) error
- func ReseasonIngridient(bentoId, name, value string) error
- func Rollback(tx *sql.Tx, requestId string) error
- func SaveBentoEntryBatch(entries []BentoEntry) error
- func StartTx() (*sql.Tx, error)
- type Bento
- type BentoEntry
- type BentoPermission
- type EmailVerification
- type Model
- type PasswordResetCode
- type User
Constants ¶
const ( O_NO_PERMS int = 0b0000_0000_0000_0000 O_WRITE int = 0b0000_0000_0000_0001 O_SHARE int = 0b0000_0000_0000_0010 O_GRANT_SHARE int = 0b0000_0000_0000_0100 O_DELETE int = 0b0000_0000_0000_1000 O_WRITE_INGRIDIENT int = 0b0000_0000_0001_0000 O_DELETE_INGRIDIENT int = 0b0000_0000_0010_0000 O_RENAME_INGRIDIENT int = 0b0000_0000_0100_0000 O_RENAME_BENTO int = 0b0000_0000_1000_0000 O_REVOKE_SHARE int = 0b0000_0001_0000_0000 O_OWNER int = 0b1000_0000_0000_0000 // This represents all the permissions a requesting user can grant to a target user. S_ALL string = "all" S_WRITE string = "write" S_DELETE string = "delete" S_SHARE string = "share" S_RENAME_BENTO string = "rename_bento" S_RENAME_INGRIDIENT string = "rename_ingridient" S_WRITE_INGRIDIENT string = "write_ingridient" S_DELETE_INGRIDIENT string = "delete_ingridient" S_REVOKE_SHARE string = "revoke_share" )
const ( EMAIL_VERIFICATION_CODE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" EMAIL_VERIFICATION_CODE_LENGTH = 20 )
const (
PG_ERR_UNIQUE_VIOLATION = "unique_violation"
)
Class 23 - Integrity Constraint Violation
Variables ¶
var TextToBinPerms map[string]int = map[string]int{ S_WRITE: O_WRITE, S_DELETE: O_DELETE, S_SHARE: O_SHARE, S_RENAME_BENTO: O_RENAME_BENTO, S_RENAME_INGRIDIENT: O_RENAME_INGRIDIENT, S_WRITE_INGRIDIENT: O_WRITE_INGRIDIENT, S_DELETE_INGRIDIENT: O_DELETE_INGRIDIENT, S_REVOKE_SHARE: O_REVOKE_SHARE, }
An map to take the integer permission value using text
Functions ¶
func Close ¶
func Close() error
Close closes the database connection. Not intended to be used in production, but makes testing easier.
func Connect ¶
Connect establishes a connection with the given postgresql database with the given url.
func DeleteBentoPermissionById ¶
Deletes an entry in bento_permissions by matching the given id.
IMPORTANT: THIS PROCESS IS NOT REVERSABLE!
func DeleteEmailVerificationWithUserId ¶
DeleteEmailVerificationWithUserId tries to removes any email verification code that has the given user id. IMPORTANT: this method does not use a transaction so deletion are UNSAFE.
func DeleteIngridient ¶
Deletes an ingridient from a bento. IMPORTANT: This method does not check for permissions before executing. Make sure to check before calling.
func DeleteTokensOwnedByUser ¶
Deletes all the tokens, access and refresh tokens owned by the given user with the userId.
func ExistsBentoPermissionByUserBentoId ¶
Checks if there already exists a bento permission for the given user and bento.
func ExistsUserWithEmail ¶
ExistsUserWithEmail checks if there is a user with the given email stored in the database.
func IsTokenValid ¶
Checks if the token with the given tokenId exists and its valid in the database. This is because users can invalidate all access tokens for a given account and when resetting passwords, the tokens are invalidated too.
func Ping ¶
func Ping() error
Ping makes sure that connection is still alives. It context.Background and timeouts in 5 seconds.
func RenameIngridient ¶
Rename an ingridient in a bento. The ingridient and bento must exists or an error will be returned.
func ReseasonIngridient ¶
Re-seaons, aka, change the value of the ingridient. IMPORTANT: This method does not check for permissions before executing. Make sure to check before calling it.
func Rollback ¶
Rollback is a helper function to rollback if tx.Commit() returns an error and will log if rollback results in an error.
func SaveBentoEntryBatch ¶
func SaveBentoEntryBatch(entries []BentoEntry) error
SaveBentoEntryBatch will save a batch of BentoEntry at once. The entries will be updated in place with the corresponding id, created_at and updated_at from the database.
Types ¶
type Bento ¶
type Bento struct { Id string Name string OwnerId string PubKey string CreatedAt time.Time UpdatedAt time.Time }
Bento represents a bento object that has a slice with all the entries as well.
func GetBentoWithId ¶
GetBentoWithId retrieves a bento with the given id.
func NewBentoTx ¶
NewBentoTx will create and save a new bento into the database with the given information. This method will return an error if there is another bento with the same name from the same user. All bentos belonging to one user should have unique names.
IMPORTANT: You must call tx.Commit() afterwards for changes to take effect.
func (*Bento) Delete ¶
Delete removes the bento from the database.
You must call tx.Commit for it to take effect.
type BentoEntry ¶
type BentoEntry struct { Id int64 Name string Value string BentoId string CreatedAt time.Time UpdatedAt time.Time }
BentoEntry represents an entry (secret) of a already preprared bento in the database.
func GetEntriesForBento ¶
func GetEntriesForBento(bentoId string) ([]BentoEntry, error)
GetEntriesForBento will get all the entries for a given bento.
func NewBentoEntry ¶
func NewBentoEntry(name, value, bentoId string) BentoEntry
NewBentoEntry just creates a new BentoEntry struct and it DOES NOT saves it in the database. This allows any procedure to create multiple BentoEntry if needed and save them in a batch.
type BentoPermission ¶
type BentoPermission struct { Id int64 UserId string BentoId string Permissions int CreatedAt time.Time UpdatedAt time.Time }
BentoPermission represents an entry for a bento permission in the database. It has a set of methods that help with the retrieval and manipulation of the bento permission.
func GetBentoPermissionByUserBentoId ¶
func GetBentoPermissionByUserBentoId(userId, bentoId string) (*BentoPermission, error)
GetBentoPermissionByUserBentoId gets a bento permission that matches the given user and bento id.
func NewBentoPermissionTx ¶
func NewBentoPermissionTx(tx *sql.Tx, userId, bentoId string, permissions int) (*BentoPermission, error)
This will create a new bento permission in the database using the transaction provided.
IMPORTANT: You must call tx.Commit() afterwards for changes to take effect.
type EmailVerification ¶
type EmailVerification struct { Id int64 Code string UserId string ExpiresAt time.Time CreatedAt time.Time }
EmailVerification represents an email verification record in the database.
func GetEmailVerificationWithCode ¶
func GetEmailVerificationWithCode(code string) (*EmailVerification, error)
GetEmailVerificationWithCode tries to retrieves an email verification record from the database with the given code.
func NewEmailVerification ¶
func NewEmailVerification(userId string) (*EmailVerification, error)
NewEmailVerification creates a new email verification record.
type PasswordResetCode ¶
type PasswordResetCode struct { Id int64 Code string UserId string ExpiresAt time.Time CreatedAt time.Time UpdatedAt time.Time }
Represents a row in the db for password reset codes.
func GetPasswordResetCodeByUserId ¶
func GetPasswordResetCodeByUserId(uid string) (*PasswordResetCode, error)
Get a password reset code by using the given user id.
func NewOrUpdatePasswordResetCode ¶
func NewOrUpdatePasswordResetCode(uid string) (*PasswordResetCode, error)
type User ¶
type User struct { Id string Email string Password string Name string EmailVerified bool CreatedAt time.Time UpdatedAt time.Time }
User represents a real user store in the database.
func GetUserWithEmail ¶
GetUserWithEmail retrieves a user in the database with the given email.
func GetUserWithEmailAndPassword ¶
GetUserWithEmailAndPassword tries to match a user with the given email and password. Usage for signining a user. Its better to use psql to match the password in the query.
func GetUserWithId ¶
GetUserWithId retrieves a user with the given id. An error is returned if nothing is found.
func NewUser ¶
NewUser creates a new user with the given information. This function will save the new user in the database.
func (*User) Delete ¶
Delete removes a user from the database using the id.
You must call tx.Commit for it to take effect.
func (*User) SetEmailVerifiedTx ¶
Sets the value for the email_verified column in the users table. Will set the User.EmailVerified field to the boolean value 'v'.
You must call tx.Commit() for it to take effect.
func (*User) Update ¶
DEPRACATED: this method will try to update the columns in users and rehash the password while doing so, which causes issues since the password is not the same anymore. This leads to users unable to access their accounts unless they reset their password.
Update updates the user in the database with the current field values of the struct.
This does not update the Id, CreatedAt, and UpdatedAt fields.
You must call tx.Commit for it to take effect.