Documentation ¶
Overview ¶
Weasel is an all-purpose, powerful ORM for Golang built on Generics. It has the feel of Active Record, and much of the same functionality. See the README for a detailed explanation of Weasel and its abilities, or you can check out the test suite to see examples of many of its functions.
Weasel currently supports Models, Documents, Groups, Relations, Model properties, Validations, Query building, Error handling, and many common utilities.
Index ¶
- type And
- type BelongsTo
- type Connection
- type Document
- func (d *Document[Doc]) AddError(es error)
- func (d Document[Doc]) AllErrors() []error
- func (d Document[Doc]) Conn() Connection
- func (d *Document[Doc]) Create(doc Doc, model *Model[Doc])
- func (d Document[Doc]) Delete() error
- func (d Document[Doc]) Error(id int) error
- func (d Document[Doc]) Get(name string) any
- func (d Document[Doc]) GetModel() *Model[Doc]
- func (d Document[Doc]) Init()
- func (d Document[Doc]) IsInvalid() bool
- func (d Document[Doc]) IsValid() bool
- func (d Document[Doc]) PrimaryKey() string
- func (d *Document[Doc]) RemoveError(id int)
- func (d Document[Doc]) Save() error
- func (d Document[Doc]) Set(name string, value any)
- func (d *Document[Doc]) SetErrors(errs []error)
- func (d Document[Doc]) Table() string
- func (d Document[Doc]) ToJSON() (string, error)
- func (d Document[Doc]) ToMap() map[string]any
- func (d *Document[Doc]) Use(m Middleware)
- type DocumentBase
- type Eq
- type Field
- type Group
- func (m Group[Doc]) All() SelectManyQuery[Doc]
- func (m Group[Doc]) Count() (int, error)
- func (m Group[Doc]) Create(d Doc) (Doc, error)
- func (m *Group[Doc]) CreateGroup(name string, expr whereable)
- func (m Group[Doc]) Exists(id any) (bool, error)
- func (m Group[Doc]) Fifth() (Doc, error)
- func (m Group[Doc]) Find(value any) (Doc, error)
- func (m Group[Doc]) FindBy(name string, value any) (Doc, error)
- func (m Group[Doc]) First() (Doc, error)
- func (m Group[Doc]) Fourth() (Doc, error)
- func (m Group[Doc]) FromGroup(name string) *Group[Doc]
- func (m *Group[Doc]) GetOrder() string
- func (m Group[Doc]) Last() (Doc, error)
- func (m Group[Doc]) Nth(id int) (Doc, error)
- func (m Group[Doc]) NthToLast(id int) (Doc, error)
- func (m *Group[Doc]) Order(by string)
- func (m Group[Doc]) Second() (Doc, error)
- func (m Group[Doc]) SecondToLast() (Doc, error)
- func (m Group[Doc]) Third() (Doc, error)
- type Gt
- type GtOrEq
- type HasMany
- type HasOne
- type ILike
- type Init
- type InsertQuery
- type Like
- type Lt
- type LtOrEq
- type Middleware
- type Model
- type NotEq
- type NotILike
- type NotLike
- type Opts
- type Or
- type Relation
- type SelectManyQuery
- func (s SelectManyQuery[Doc]) Column(column any, args ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Columns(columns ...string) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) CrossJoin(join string, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Distinct() SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Exec() ([]Doc, error)
- func (s SelectManyQuery[Doc]) GroupBy(groupBys ...string) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Having(pred any, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) InnerJoin(join string, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Join(join string, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) JoinClause(pred any, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) LeftJoin(join string, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Limit(limit uint64) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Offset(offset uint64) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Options(options ...string) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) OrderBy(orderBys ...string) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) OrderByClause(pred any, args ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) RightJoin(join string, rest ...any) SelectManyQuery[Doc]
- func (s SelectManyQuery[Doc]) Where(pred any, args ...any) SelectManyQuery[Doc]
- type SelectQuery
- func (s SelectQuery[Doc]) Column(column any, args ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Columns(columns ...string) SelectQuery[Doc]
- func (s SelectQuery[Doc]) CrossJoin(join string, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Distinct() SelectQuery[Doc]
- func (s SelectQuery[Doc]) Exec() (Doc, error)
- func (s SelectQuery[Doc]) GroupBy(groupBys ...string) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Having(pred any, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) InnerJoin(join string, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Join(join string, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) JoinClause(pred any, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) LeftJoin(join string, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Limit(limit uint64) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Offset(offset uint64) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Options(options ...string) SelectQuery[Doc]
- func (s SelectQuery[Doc]) OrderBy(orderBys ...string) SelectQuery[Doc]
- func (s SelectQuery[Doc]) OrderByClause(pred any, args ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) RightJoin(join string, rest ...any) SelectQuery[Doc]
- func (s SelectQuery[Doc]) Where(pred any, args ...any) SelectQuery[Doc]
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BelongsTo ¶ added in v0.3.0
type BelongsTo[Doc document[Doc]] func() (Doc, error)
Type BelongsTo is the type used to represent the flipside of a one-to-many relationship in a schema. Use the following struct tags to give it more information:
- key: default is the primary key. This is the column that the foreign key points to.
- fk: the name of the foreign key column.
- belongsto: the table that it belongs to.
type Connection ¶
type Connection struct { Builder sq.StatementBuilderType DB *sqlx.DB // contains filtered or unexported fields }
Connection holds all of the connection information, to be used in the models. It also contains the configured raw query builder and DB API, which both are not recommended to directly be used, but are useful if you need a complex query that is not officially supported. The query builder type comes from Squirrel, and the DB type is *sqlx.DB.
func Connect ¶
func Connect(driver string, options Opts) Connection
Connect creates a connection to the database. The opts string as the second parameter is a wrapper of sqlx.Connect but uses a custom Opts struct that it parses into a DSN.
type Document ¶ added in v0.2.0
type Document[Doc document[Doc]] struct { Model *Model[Doc] Errors []error // contains filtered or unexported fields }
Document provides a struct to extend your schemas. It contains errors and model information and extends DocumentBase. You typically will not have to use Document except for in defining your schema, for example:
type PersonSchema struct { weasel.Document[*PersonSchema] }
func (*Document[Doc]) AddError ¶ added in v0.4.0
You can use AddError to append an error to the list. This is very useful in middleware.
func (Document[Doc]) Conn ¶ added in v0.4.0
func (d Document[Doc]) Conn() Connection
Conn returns the current connection. See the connection docs.
func (*Document[Doc]) Create ¶ added in v0.3.0
This is an internal function, exported only for use with reflect Do not use.
func (Document[Doc]) Delete ¶ added in v0.2.0
Delete completely removes the document from the database.
func (Document[Doc]) Get ¶ added in v0.2.0
Get returns a property (DB or struct) on the document. You may need to use type assertion.
func (Document[Doc]) Init ¶ added in v0.2.0
func (d Document[Doc]) Init()
You can define a custom Init function to run on document creation.
func (Document[Doc]) IsValid ¶ added in v0.3.2
IsValid checks that the document does not contain any errors.
func (Document[Doc]) PrimaryKey ¶ added in v0.4.0
PrimaryKey returns the table's primary key. This is useful for middleware.
func (*Document[Doc]) RemoveError ¶ added in v0.5.1
RemoveError takes the index of an error and removes it from the list.
func (Document[Doc]) Save ¶ added in v0.2.0
Save saves the document's changes, changed either by Set or manually.
func (*Document[Doc]) SetErrors ¶ added in v0.5.1
SetErrors completely sets all of the errors. It is not recommended to use unless you really need to or want to reset all of the errors.
func (Document[Doc]) Table ¶ added in v0.4.0
Table returns the table name of the document. This is useful for middleware.
func (Document[Doc]) ToJSON ¶ added in v0.6.3
ToJSON returns a JSON string of all of the document's fields. This is useful for serialization in HTTP responses.
func (Document[Doc]) ToMap ¶ added in v0.6.3
ToMap returns a map of the document. This is useful for custom serialization.
func (*Document[Doc]) Use ¶ added in v0.4.0
func (d *Document[Doc]) Use(m Middleware)
Use takes middleware and runs it on document creation.
type DocumentBase ¶ added in v0.4.0
type DocumentBase interface { Delete() error Save() error ToJSON() (string, error) ToMap() map[string]any Get(string) any Set(string, any) AllErrors() []error AddError(error) SetErrors([]error) RemoveError(int) Error(int) error PrimaryKey() string Init() IsValid() bool IsInvalid() bool Table() string Conn() Connection Use(Middleware) }
DocumentBase provides an interface to be used for times when you may not know the schema type. All documents conform to it; it is used as the constraint for type parameters. You may find yourself using DocumentBase if you want to write custom validation and middleware.
type Field ¶
type Field struct { Name string DBName string Type string Default string NotNil bool PrimaryKey bool }
Type field represents the field structure used internally for field metadata, provided by struct tags.
type Group ¶ added in v0.5.0
type Group[Doc DocumentBase] struct { Where whereable Model *Model[Doc] // contains filtered or unexported fields }
Group is the foundation on both Model and Group functionality. It has many of the methods used in the model, and contains all of the querying utilities.
func NewGroupWith ¶ added in v0.7.0
func (Group[Doc]) All ¶ added in v0.5.0
func (m Group[Doc]) All() SelectManyQuery[Doc]
All returns all of the documents in the group or model. It returns a query builder that contains functions including Where, OrderBy, GroupBy. For more information and functions, see SelectManyQuery and its methods.
func (Group[Doc]) Count ¶ added in v0.6.0
Count returns the number of documents in the group or model.
func (Group[Doc]) Create ¶ added in v0.5.2
Create creates a document and adds it to the database TODO: Make create conform to `where` clause
func (*Group[Doc]) CreateGroup ¶ added in v0.6.0
CreateGroup adds a group to the model or group that can be accessed by FromGroup. It respects the current group's where clause and contains all of the querying functionality and utilities.
func (Group[Doc]) Exists ¶ added in v0.6.0
Exists checks if the document with the given primary key exists.
func (Group[Doc]) Fifth ¶ added in v0.6.0
Fifth returns the fifth document. See Nth for more information.
func (Group[Doc]) Find ¶ added in v0.5.0
Find takes the primary key value and finds the corresponding document.
func (Group[Doc]) FindBy ¶ added in v0.5.0
FindBy takes a column name and value and finds the corresponding document. If you want to find multiple, use All().Where(weasel.Eq{key: value}).
func (Group[Doc]) First ¶ added in v0.6.0
First returns the first document from the table, via the set order clause. See Nth for more information.
func (Group[Doc]) Fourth ¶ added in v0.6.0
Fourth returns the fourth document. See Nth for more information.
func (Group[Doc]) FromGroup ¶ added in v0.6.0
FromGroup returns the group that the name parameter points to. See CreateGroup() and Group for more information.
func (*Group[Doc]) GetOrder ¶ added in v0.6.2
Get order returns the order of the documents used when queried, for example "id ASC"
func (Group[Doc]) Last ¶ added in v0.6.0
Last returns the last document in the table, via the opposite of order. See NthToLast for more information.
func (Group[Doc]) Nth ¶ added in v0.6.0
Nth returns the document at the given index. For example:
Person.Nth(6) // Returns the sixth document.
func (Group[Doc]) NthToLast ¶ added in v0.6.0
NthToLast returns the last document at the given index. For example:
Person.NthToLast(3) // Returns the third to last document.
func (*Group[Doc]) Order ¶ added in v0.6.0
Order sets the order that the documents should be sorted by when queried.
func (Group[Doc]) Second ¶ added in v0.6.0
Second returns the second document. See Nth for more information.
func (Group[Doc]) SecondToLast ¶ added in v0.6.0
SecondToLast returns the second to last document in the table. See NthToLast for more information.
type HasMany ¶ added in v0.3.0
type HasMany[Doc document[Doc]] func() *Group[Doc]
Type HasMany is the type used to represent a one-to-many or many-to-many relationship in a schema. Use the following struct tags to give it more information:
- key: default is the primary key. This is the column that the foreign key points to.
- fk: the name of the foreign key column.
- through: the join table for many-to-many relationships.
- hasmany: the table that it has many of.
type HasOne ¶ added in v0.3.0
type HasOne[Doc document[Doc]] func() (Doc, error)
Type HasMany is the type used to represent a one-to-one relationship in a schema. Use the following struct tags to give it more information:
- key: default is the primary key. This is the column that the foreign key points to.
- fk: the name of the foreign key column.
- hasone: the table that it has one of.
type Init ¶ added in v0.6.0
type Init[Doc document[Doc]] func(*Model[Doc])
Type Init represents the init function passed to Create
type InsertQuery ¶
type InsertQuery[Doc DocumentBase] struct { // contains filtered or unexported fields }
InsertQuery builds an insert sql query. See squirrel's docs for some of the functions. Note: InsertQuery does not include all of squirrel's functions. It handles some of the internally.
func Insert ¶
func Insert[Doc DocumentBase](model *Model[Doc]) InsertQuery[Doc]
Insert takes a model and builds an insert query. You can set the columns and values. Finally, call Exec() to run the query.
func (InsertQuery[Doc]) Columns ¶
func (i InsertQuery[Doc]) Columns(columns ...string) InsertQuery[Doc]
func (InsertQuery[Doc]) Exec ¶
func (i InsertQuery[Doc]) Exec() (Doc, error)
func (InsertQuery[Doc]) Options ¶
func (i InsertQuery[Doc]) Options(options ...string) InsertQuery[Doc]
func (InsertQuery[Doc]) Values ¶
func (i InsertQuery[Doc]) Values(values ...any) InsertQuery[Doc]
type Middleware ¶ added in v0.4.0
type Middleware func(DocumentBase)
Middleware is a type that all middleware (passed to the Use function) should be/return. it takes an argument in DocumentBase and can use `Get` and `Set` to get and change properties.
type Model ¶
type Model[Doc DocumentBase] struct { *Group[Doc] Conn Connection // contains filtered or unexported fields }
Model is the model itself. It extends Group and has all of Group's functionality, and more. It also provides the table metadata to Group.
func Create ¶
func Create[Doc document[Doc]](conn Connection, ex Doc, name string, inits ...Init[Doc]) *Model[Doc]
Create creates a model from the given connection, document, table name, and initializers.
type Opts ¶ added in v0.8.0
type Opts struct { // Common options User string // Username for authentication Password string // Password for authentication Host string // Hostname or IP Port int // Port number Database string // Database name Charset string // Character set (e.g., utf8) TLS string // TLS mode (e.g., "true", "false", "skip-verify") Timeout int // Connection timeout in seconds ReadTimeout int // Read timeout in seconds WriteTimeout int // Write timeout in seconds // MySQL-specific options ParseTime bool // Parse time values into time.Time Collation string // Collation to use (e.g., "utf8_general_ci") AllowNativePasswords bool // Allow native password authentication MultiStatements bool // Allow multiple statements in one query // PostgreSQL-specific options SSLMode string // SSL mode (e.g., "disable", "require") SearchPath string // Schema search path ApplicationName string // Name of the application FallbackApplicationName string // Fallback app name ConnectTimeout int // Connection timeout in seconds SSLRootCert string // Path to root certificate file SSLKey string // Path to SSL key file SSLCert string // Path to SSL certificate file // SQLite-specific options Mode string // SQLite mode (e.g., "memory", "ro", "rw", "rwc") Cache string // Cache mode (e.g., "shared", "private") JournalMode string // Journal mode (e.g., "delete", "truncate", "persist") Synchronous string // Synchronization mode (e.g., "off", "normal", "full") // Additional custom parameters for flexibility CustomParams map[string]string // Any additional parameters }
Opts represents a generalized connection options structure for the Connect function. It is formatted automatically into a DSN based on the provided driver.
type Relation ¶ added in v0.3.0
type Relation struct { Name string Variant string Key string ForeignKey string Table string Through string }
Type relation represents a relation's metadata, provided by struct tags.
type SelectManyQuery ¶
type SelectManyQuery[Doc DocumentBase] struct { // contains filtered or unexported fields }
SelectManyQuery builds an select sql query. See squirrel's docs for some of the functions. Note: SelectManyQuery does not include all of squirrel's functions. It handles some of the internally.
func SelectMany ¶
func SelectMany[Doc DocumentBase](columns []string, model *Model[Doc]) SelectManyQuery[Doc]
SelectMany builds a select query. Pass in the columns and model, and when you are done building the query, call the Exec() function to run it.
func (SelectManyQuery[Doc]) Column ¶
func (s SelectManyQuery[Doc]) Column(column any, args ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Columns ¶
func (s SelectManyQuery[Doc]) Columns(columns ...string) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) CrossJoin ¶
func (s SelectManyQuery[Doc]) CrossJoin(join string, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Distinct ¶
func (s SelectManyQuery[Doc]) Distinct() SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Exec ¶
func (s SelectManyQuery[Doc]) Exec() ([]Doc, error)
func (SelectManyQuery[Doc]) GroupBy ¶
func (s SelectManyQuery[Doc]) GroupBy(groupBys ...string) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Having ¶
func (s SelectManyQuery[Doc]) Having(pred any, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) InnerJoin ¶
func (s SelectManyQuery[Doc]) InnerJoin(join string, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Join ¶
func (s SelectManyQuery[Doc]) Join(join string, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) JoinClause ¶
func (s SelectManyQuery[Doc]) JoinClause(pred any, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) LeftJoin ¶
func (s SelectManyQuery[Doc]) LeftJoin(join string, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Limit ¶
func (s SelectManyQuery[Doc]) Limit(limit uint64) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Offset ¶
func (s SelectManyQuery[Doc]) Offset(offset uint64) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Options ¶
func (s SelectManyQuery[Doc]) Options(options ...string) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) OrderBy ¶
func (s SelectManyQuery[Doc]) OrderBy(orderBys ...string) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) OrderByClause ¶
func (s SelectManyQuery[Doc]) OrderByClause(pred any, args ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) RightJoin ¶
func (s SelectManyQuery[Doc]) RightJoin(join string, rest ...any) SelectManyQuery[Doc]
func (SelectManyQuery[Doc]) Where ¶
func (s SelectManyQuery[Doc]) Where(pred any, args ...any) SelectManyQuery[Doc]
type SelectQuery ¶
type SelectQuery[Doc DocumentBase] struct { // contains filtered or unexported fields }
SelectQuery builds an select sql query. See squirrel's docs for some of the functions. Note: SelectQuery does not include all of squirrel's functions. It handles some of the internally.
func Select ¶
func Select[Doc DocumentBase](columns []string, model *Model[Doc]) SelectQuery[Doc]
Select starts a select one query. It handles the internal table and column logic. You can pass the columns and model. When you are done building the query, call the Exec() function to run it.
func (SelectQuery[Doc]) Column ¶
func (s SelectQuery[Doc]) Column(column any, args ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) Columns ¶
func (s SelectQuery[Doc]) Columns(columns ...string) SelectQuery[Doc]
func (SelectQuery[Doc]) CrossJoin ¶
func (s SelectQuery[Doc]) CrossJoin(join string, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) Distinct ¶
func (s SelectQuery[Doc]) Distinct() SelectQuery[Doc]
func (SelectQuery[Doc]) Exec ¶
func (s SelectQuery[Doc]) Exec() (Doc, error)
func (SelectQuery[Doc]) GroupBy ¶
func (s SelectQuery[Doc]) GroupBy(groupBys ...string) SelectQuery[Doc]
func (SelectQuery[Doc]) Having ¶
func (s SelectQuery[Doc]) Having(pred any, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) InnerJoin ¶
func (s SelectQuery[Doc]) InnerJoin(join string, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) Join ¶
func (s SelectQuery[Doc]) Join(join string, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) JoinClause ¶
func (s SelectQuery[Doc]) JoinClause(pred any, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) LeftJoin ¶
func (s SelectQuery[Doc]) LeftJoin(join string, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) Limit ¶
func (s SelectQuery[Doc]) Limit(limit uint64) SelectQuery[Doc]
func (SelectQuery[Doc]) Offset ¶
func (s SelectQuery[Doc]) Offset(offset uint64) SelectQuery[Doc]
func (SelectQuery[Doc]) Options ¶
func (s SelectQuery[Doc]) Options(options ...string) SelectQuery[Doc]
func (SelectQuery[Doc]) OrderBy ¶
func (s SelectQuery[Doc]) OrderBy(orderBys ...string) SelectQuery[Doc]
func (SelectQuery[Doc]) OrderByClause ¶
func (s SelectQuery[Doc]) OrderByClause(pred any, args ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) RightJoin ¶
func (s SelectQuery[Doc]) RightJoin(join string, rest ...any) SelectQuery[Doc]
func (SelectQuery[Doc]) Where ¶
func (s SelectQuery[Doc]) Where(pred any, args ...any) SelectQuery[Doc]
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package use provides common utilities and validations for document, along with middleware for relations, to be used in the `Init` function of the schema.
|
Package use provides common utilities and validations for document, along with middleware for relations, to be used in the `Init` function of the schema. |