AdminQuery returns a new Admin select query.
CanAccessRecord checks if a record is allowed to be accessed by the specified requestInfo and accessRule.
Rule and db checks are ignored in case requestInfo.Admin is set.
The returned error indicate that something unexpected happened during the check (eg. invalid rule or db error).
The method always return false on invalid access rule or db error.
Example:
requestInfo := apis.RequestInfo(c /* echo.Context */)
record, _ := dao.FindRecordById("example", "RECORD_ID")
rule := types.Pointer("@request.auth.id != '' || status = 'public'")
// ... or use one of the record collection's rule, eg. record.Collection().ViewRule
if ok, _ := dao.CanAccessRecord(record, requestInfo, rule); ok { ... }
CollectionQuery returns a new Collection select query.
CreateViewSchema creates a new view schema from the provided select query.
There are some caveats:
Delete deletes the provided model.
DeleteCollection deletes the provided Collection model. This method automatically deletes the related collection records table.
NB! The collection cannot be deleted, if:
DeleteExternalAuth deletes the provided ExternalAuth model.
DeleteOldLogs delete all requests that are created before createdBefore.
DeleteParam deletes the provided Param model.
DeleteRecord deletes the provided Record model.
This method will also cascade the delete operation to all linked relational records (delete or unset, depending on the rel settings).
The delete operation may fail if the record is part of a required reference in another record (aka. cannot be deleted or unset).
DeleteTable drops the specified table.
This method is a no-op if a table with the provided name doesn't exist.
Be aware that this method is vulnerable to SQL injection and the "tableName" argument must come only from trusted input!
ExpandRecord expands the relations of a single Record model.
If optFetchFunc is not set, then a default function will be used that returns all relation records.
Returns a map with the failed expand parameters and their errors.
ExpandRecords expands the relations of the provided Record models list.
If optFetchFunc is not set, then a default function will be used that returns all relation records.
Returns a map with the failed expand parameters and their errors.
ExternalAuthQuery returns a new ExternalAuth select query.
FindAllExternalAuthsByRecord returns all ExternalAuth models linked to the provided auth record.
FindAuthRecordByUsername finds the auth record associated with the provided username (case insensitive).
Returns an error if it is not an auth collection or the record is not found.
FindById finds a single db record with the specified id and scans the result into m.
FindCollectionByNameOrId finds a single collection by its name (case insensitive) or id.
FindCollectionReferences returns information for all relation schema fields referencing the provided collection.
If the provided collection has reference to itself then it will be also included in the result. To exclude it, pass the collection id as the excludeId argument.
Rest
...excludeIds: string[]FindCollectionsByType finds all collections by the given type.
FindExternalAuthByRecordAndProvider returns the first available ExternalAuth model for the specified record data and provider.
FindFirstExternalAuthByExpr returns the first available ExternalAuth model that satisfies the non-nil expression.
FindFirstRecordByFilter returns the first available record matching the provided filter.
NB! Use the last params argument to bind untrusted user variables!
Example:
dao.FindFirstRecordByFilter("posts", "slug={:slug} && status='public'", dbx.Params{"slug": "test"})
Rest
...params: Params[]FindRecordsByExpr finds all records by the specified db expression.
Returns all collection records if no expressions are provided.
Returns an empty slice if no records are found.
Example:
expr1 := dbx.HashExp{"email": "test@example.com"}
expr2 := dbx.NewExp("LOWER(username) = {:username}", dbx.Params{"username": "test"})
dao.FindRecordsByExpr("example", expr1, expr2)
Rest
...exprs: Expression[]FindRecordsByFilter returns limit number of records matching the provided string filter.
NB! Use the last "params" argument to bind untrusted user variables!
The sort argument is optional and can be empty string OR the same format used in the web APIs, eg. "-created,title".
If the limit argument is <= 0, no limit is applied to the query and all matching records are returned.
Example:
dao.FindRecordsByFilter(
"posts",
"title ~ {:title} && visible = {:visible}",
"-created",
10,
0,
dbx.Params{"title": "lorem ipsum", "visible": true}
)
Rest
...params: Params[]FindRecordsByIds finds all Record models by the provided ids. If no records are found, returns an empty slice.
Rest
...optFilters: ((q) => void)[]FindSettings returns and decode the serialized app settings param value.
The method will first try to decode the param value without decryption. If it fails and optEncryptionKey is set, it will try again by first decrypting the value and then decode it again.
Returns an error if it fails to decode the stored serialized param value.
Rest
...optEncryptionKey: string[]ImportCollections imports the provided collections list within a single transaction.
NB1! If deleteMissing is set, all local collections and schema fields, that are not present in the imported configuration, WILL BE DELETED (including their related records data).
NB2! This method doesn't perform validations on the imported collections data! If you need validations, use [forms.CollectionsImport].
IsCollectionNameUnique checks that there is no existing collection with the provided name (case insensitive!).
Note: case insensitive check because the name is used also as a table name for the records.
Rest
...excludeIds: string[]IsRecordValueUnique checks if the provided key-value pair is a unique Record value.
For correctness, if the collection is "auth" and the key is "username", the unique check will be case insensitive.
NB! Array values (eg. from multiple select fields) are matched
as a serialized json strings (eg. ["a","b"]
), so the value uniqueness
depends on the elements order. Or in other words the following values
are considered different: []string{"a","b"}
and []string{"b","a"}
Rest
...excludeIds: string[]LogQuery returns a new Log select query.
LogsStats returns hourly grouped requests logs statistics.
ModelQuery creates a new preconfigured select query with preset SELECT, FROM and other common fields based on the provided model.
ParamQuery returns a new Param select query.
RecordQuery returns a new Record select query from a collection model, id or name.
In case a collection id or name is provided and that collection doesn't actually exists, the generated query will be created with a cancelled context and will fail once an executor (Row(), One(), All(), etc.) is called.
Save persists the provided model in the database.
If m.IsNew() is true, the method will perform a create, otherwise an update. To explicitly mark a model for update you can use m.MarkAsNotNew().
SaveCollection persists the provided Collection model and updates its related records table schema.
If collection.IsNew() is true, the method will perform a create, otherwise an update. To explicitly mark a collection for update you can use collection.MarkAsNotNew().
SaveExternalAuth upserts the provided ExternalAuth model.
SaveLog upserts the provided Log model.
SaveParam creates or updates a Param model by the provided key-value pair. The value argument will be encoded as json string.
If optEncryptionKey
is provided it will encrypt the value before storing it.
Rest
...optEncryptionKey: string[]SaveSettings persists the specified settings configuration.
If optEncryptionKey is set, then the stored serialized value will be encrypted with it.
Rest
...optEncryptionKey: string[]SuggestUniqueAuthRecordUsername checks if the provided username is unique and return a new "unique" username with appended random numeric part (eg. "existingName" -> "existingName583").
The same username will be returned if the provided string is already unique.
Rest
...excludeIds: string[]SyncRecordTableSchema compares the two provided collections and applies the necessary related record table changes.
If oldCollection
is null, then only newCollection
is used to create the record table.
TableIndexes returns a name grouped map with all non empty index of the specified table.
Note: This method doesn't return an error on nonexisting table.
TableInfo returns the table_info
pragma result for the specified table.
MaxLockRetries specifies the default max "database is locked" auto retry attempts.
ModelQueryTimeout is the default max duration of a running ModelQuery().
This field has no effect if an explicit query context is already specified.
Generated using TypeDoc
Dao handles various db operations.
You can think of Dao as a repository and service layer in one.