1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-01-09 10:07:17 +02:00
pocketbase/forms/collection_upsert.go

535 lines
15 KiB
Go
Raw Normal View History

2022-07-06 23:19:05 +02:00
package forms
import (
2022-10-30 10:28:14 +02:00
"encoding/json"
2022-08-10 12:22:27 +02:00
"fmt"
2022-07-06 23:19:05 +02:00
"regexp"
"strconv"
2022-07-06 23:19:05 +02:00
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/pocketbase/pocketbase/core"
"github.com/pocketbase/pocketbase/daos"
2023-01-11 22:29:48 +02:00
"github.com/pocketbase/pocketbase/forms/validators"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/resolvers"
"github.com/pocketbase/pocketbase/tools/dbutils"
2022-10-30 10:28:14 +02:00
"github.com/pocketbase/pocketbase/tools/list"
2022-07-06 23:19:05 +02:00
"github.com/pocketbase/pocketbase/tools/search"
2022-10-30 10:28:14 +02:00
"github.com/pocketbase/pocketbase/tools/types"
2022-07-06 23:19:05 +02:00
)
var collectionNameRegex = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9_]*$`)
2022-10-30 10:28:14 +02:00
// CollectionUpsert is a [models.Collection] upsert (create/update) form.
2022-07-06 23:19:05 +02:00
type CollectionUpsert struct {
2022-10-30 10:28:14 +02:00
app core.App
dao *daos.Dao
2022-07-06 23:19:05 +02:00
collection *models.Collection
Id string `form:"id" json:"id"`
Type string `form:"type" json:"type"`
Name string `form:"name" json:"name"`
System bool `form:"system" json:"system"`
Schema schema.Schema `form:"schema" json:"schema"`
Indexes types.JsonArray[string] `form:"indexes" json:"indexes"`
ListRule *string `form:"listRule" json:"listRule"`
ViewRule *string `form:"viewRule" json:"viewRule"`
CreateRule *string `form:"createRule" json:"createRule"`
UpdateRule *string `form:"updateRule" json:"updateRule"`
DeleteRule *string `form:"deleteRule" json:"deleteRule"`
Options types.JsonMap `form:"options" json:"options"`
}
// NewCollectionUpsert creates a new [CollectionUpsert] form with initializer
2022-08-06 21:16:58 +02:00
// config created from the provided [core.App] and [models.Collection] instances
// (for create you could pass a pointer to an empty Collection - `&models.Collection{}`).
//
2022-10-30 10:28:14 +02:00
// If you want to submit the form as part of a transaction,
// you can change the default Dao via [SetDao()].
2022-07-06 23:19:05 +02:00
func NewCollectionUpsert(app core.App, collection *models.Collection) *CollectionUpsert {
2022-08-06 17:59:28 +02:00
form := &CollectionUpsert{
2022-10-30 10:28:14 +02:00
app: app,
dao: app.Dao(),
2022-08-06 17:59:28 +02:00
collection: collection,
}
2022-07-06 23:19:05 +02:00
// load defaults
form.Id = form.collection.Id
2022-10-30 10:28:14 +02:00
form.Type = form.collection.Type
form.Name = form.collection.Name
form.System = form.collection.System
form.Indexes = form.collection.Indexes
form.ListRule = form.collection.ListRule
form.ViewRule = form.collection.ViewRule
form.CreateRule = form.collection.CreateRule
form.UpdateRule = form.collection.UpdateRule
form.DeleteRule = form.collection.DeleteRule
2022-10-30 10:28:14 +02:00
form.Options = form.collection.Options
if form.Type == "" {
form.Type = models.CollectionTypeBase
}
clone, _ := form.collection.Schema.Clone()
2023-02-18 19:33:42 +02:00
if clone != nil && form.Type != models.CollectionTypeView {
2022-07-06 23:19:05 +02:00
form.Schema = *clone
} else {
form.Schema = schema.Schema{}
}
return form
}
2022-10-30 10:28:14 +02:00
// SetDao replaces the default form Dao instance with the provided one.
func (form *CollectionUpsert) SetDao(dao *daos.Dao) {
form.dao = dao
}
2022-07-06 23:19:05 +02:00
// Validate makes the form validatable by implementing [validation.Validatable] interface.
func (form *CollectionUpsert) Validate() error {
2022-10-30 10:28:14 +02:00
isAuth := form.Type == models.CollectionTypeAuth
2023-02-18 19:33:42 +02:00
isView := form.Type == models.CollectionTypeView
// generate schema from the query (overwriting any explicit user defined schema)
if isView {
options := models.CollectionViewOptions{}
if err := decodeOptions(form.Options, &options); err != nil {
return err
}
form.Schema, _ = form.dao.CreateViewSchema(options.Query)
}
2022-10-30 10:28:14 +02:00
2022-07-06 23:19:05 +02:00
return validation.ValidateStruct(form,
validation.Field(
&form.Id,
2022-08-06 21:16:58 +02:00
validation.When(
form.collection.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength),
2022-08-11 09:29:01 +02:00
validation.Match(idRegex),
2023-01-11 22:29:48 +02:00
validation.By(validators.UniqueId(form.dao, form.collection.TableName())),
2022-08-06 21:16:58 +02:00
).Else(validation.In(form.collection.Id)),
),
2022-07-06 23:19:05 +02:00
validation.Field(
&form.System,
validation.By(form.ensureNoSystemFlagChange),
),
2022-10-30 10:28:14 +02:00
validation.Field(
&form.Type,
validation.Required,
2023-02-18 19:33:42 +02:00
validation.In(
models.CollectionTypeBase,
models.CollectionTypeAuth,
models.CollectionTypeView,
),
2022-10-30 10:28:14 +02:00
validation.By(form.ensureNoTypeChange),
),
2022-07-06 23:19:05 +02:00
validation.Field(
&form.Name,
validation.Required,
validation.Length(1, 255),
validation.Match(collectionNameRegex),
validation.By(form.ensureNoSystemNameChange),
validation.By(form.checkUniqueName),
),
2023-02-18 19:33:42 +02:00
// validates using the type's own validation rules + some collection's specifics
2022-07-06 23:19:05 +02:00
validation.Field(
&form.Schema,
validation.By(form.checkMinSchemaFields),
2022-07-06 23:19:05 +02:00
validation.By(form.ensureNoSystemFieldsChange),
validation.By(form.ensureNoFieldsTypeChange),
validation.By(form.checkRelationFields),
2023-02-18 19:33:42 +02:00
validation.When(isAuth, validation.By(form.ensureNoAuthFieldName)),
2022-07-06 23:19:05 +02:00
),
validation.Field(&form.ListRule, validation.By(form.checkRule)),
validation.Field(&form.ViewRule, validation.By(form.checkRule)),
2023-02-18 19:33:42 +02:00
validation.Field(
&form.CreateRule,
validation.When(isView, validation.Nil),
validation.By(form.checkRule),
),
validation.Field(
&form.UpdateRule,
validation.When(isView, validation.Nil),
validation.By(form.checkRule),
),
validation.Field(
&form.DeleteRule,
validation.When(isView, validation.Nil),
validation.By(form.checkRule),
),
validation.Field(&form.Indexes, validation.By(form.checkIndexes)),
2022-10-30 10:28:14 +02:00
validation.Field(&form.Options, validation.By(form.checkOptions)),
2022-07-06 23:19:05 +02:00
)
}
func (form *CollectionUpsert) checkUniqueName(value any) error {
v, _ := value.(string)
2022-10-30 10:28:14 +02:00
// ensure unique collection name
if !form.dao.IsCollectionNameUnique(v, form.collection.Id) {
2022-07-06 23:19:05 +02:00
return validation.NewError("validation_collection_name_exists", "Collection name must be unique (case insensitive).")
}
2022-10-30 10:28:14 +02:00
// ensure that the collection name doesn't collide with the id of any collection
if form.dao.FindById(&models.Collection{}, v) == nil {
return validation.NewError("validation_collection_name_id_duplicate", "The name must not match an existing collection id.")
}
2022-07-06 23:19:05 +02:00
return nil
}
func (form *CollectionUpsert) ensureNoSystemNameChange(value any) error {
v, _ := value.(string)
2022-10-30 10:28:14 +02:00
if !form.collection.IsNew() && form.collection.System && v != form.collection.Name {
return validation.NewError("validation_collection_system_name_change", "System collections cannot be renamed.")
2022-07-06 23:19:05 +02:00
}
2022-10-30 10:28:14 +02:00
return nil
2022-07-06 23:19:05 +02:00
}
func (form *CollectionUpsert) ensureNoSystemFlagChange(value any) error {
v, _ := value.(bool)
2022-10-30 10:28:14 +02:00
if !form.collection.IsNew() && v != form.collection.System {
return validation.NewError("validation_collection_system_flag_change", "System collection state cannot be changed.")
2022-07-06 23:19:05 +02:00
}
2022-10-30 10:28:14 +02:00
return nil
}
func (form *CollectionUpsert) ensureNoTypeChange(value any) error {
v, _ := value.(string)
if !form.collection.IsNew() && v != form.collection.Type {
return validation.NewError("validation_collection_type_change", "Collection type cannot be changed.")
}
return nil
2022-07-06 23:19:05 +02:00
}
func (form *CollectionUpsert) ensureNoFieldsTypeChange(value any) error {
v, _ := value.(schema.Schema)
2022-08-10 12:22:27 +02:00
for i, field := range v.Fields() {
2022-07-06 23:19:05 +02:00
oldField := form.collection.Schema.GetFieldById(field.Id)
if oldField != nil && oldField.Type != field.Type {
2022-08-10 12:22:27 +02:00
return validation.Errors{fmt.Sprint(i): validation.NewError(
"validation_field_type_change",
"Field type cannot be changed.",
)}
}
}
return nil
}
func (form *CollectionUpsert) checkRelationFields(value any) error {
2022-08-10 12:22:27 +02:00
v, _ := value.(schema.Schema)
systemDisplayFields := schema.BaseModelFieldNames()
systemDisplayFields = append(systemDisplayFields,
schema.FieldNameUsername,
schema.FieldNameEmail,
schema.FieldNameEmailVisibility,
schema.FieldNameVerified,
)
2022-08-10 12:22:27 +02:00
for i, field := range v.Fields() {
if field.Type != schema.FieldTypeRelation {
continue
}
options, _ := field.Options.(*schema.RelationOptions)
if options == nil {
return validation.Errors{fmt.Sprint(i): validation.Errors{
"options": validation.NewError(
"validation_schema_invalid_relation_field_options",
"The relation field has invalid field options.",
)},
}
}
// prevent collectionId change
oldField := form.collection.Schema.GetFieldById(field.Id)
if oldField != nil {
oldOptions, _ := oldField.Options.(*schema.RelationOptions)
if oldOptions != nil && oldOptions.CollectionId != options.CollectionId {
return validation.Errors{fmt.Sprint(i): validation.Errors{
"options": validation.Errors{
"collectionId": validation.NewError(
"validation_field_relation_change",
"The relation collection cannot be changed.",
),
}},
}
}
2022-08-10 12:22:27 +02:00
}
collection, err := form.dao.FindCollectionByNameOrId(options.CollectionId)
// validate collectionId
if err != nil || collection.Id != options.CollectionId {
return validation.Errors{fmt.Sprint(i): validation.Errors{
"options": validation.Errors{
"collectionId": validation.NewError(
"validation_field_invalid_relation",
"The relation collection doesn't exist.",
),
}},
}
}
// validate displayFields (if any)
for _, name := range options.DisplayFields {
if collection.Schema.GetFieldByName(name) == nil && !list.ExistInSlice(name, systemDisplayFields) {
return validation.Errors{fmt.Sprint(i): validation.Errors{
"options": validation.Errors{
"displayFields": validation.NewError(
"validation_field_invalid_relation_displayFields",
fmt.Sprintf("%q does not exist in the related %q collection.", name, collection.Name),
),
}},
}
}
2022-07-06 23:19:05 +02:00
}
}
return nil
}
2022-10-30 10:28:14 +02:00
func (form *CollectionUpsert) ensureNoAuthFieldName(value any) error {
v, _ := value.(schema.Schema)
if form.Type != models.CollectionTypeAuth {
return nil // not an auth collection
}
authFieldNames := schema.AuthFieldNames()
// exclude the meta RecordUpsert form fields
authFieldNames = append(authFieldNames, "password", "passwordConfirm", "oldPassword")
errs := validation.Errors{}
for i, field := range v.Fields() {
if list.ExistInSlice(field.Name, authFieldNames) {
errs[fmt.Sprint(i)] = validation.Errors{
"name": validation.NewError(
"validation_reserved_auth_field_name",
"The field name is reserved and cannot be used.",
),
}
}
}
if len(errs) > 0 {
return errs
}
return nil
}
func (form *CollectionUpsert) checkMinSchemaFields(value any) error {
2023-02-18 19:33:42 +02:00
v, _ := value.(schema.Schema)
2023-02-18 19:33:42 +02:00
switch form.Type {
case models.CollectionTypeAuth, models.CollectionTypeView:
return nil // no schema fields constraint
default:
if len(v.Fields()) == 0 {
return validation.ErrRequired
}
}
return nil
}
2022-07-06 23:19:05 +02:00
func (form *CollectionUpsert) ensureNoSystemFieldsChange(value any) error {
v, _ := value.(schema.Schema)
for _, oldField := range form.collection.Schema.Fields() {
if !oldField.System {
continue
}
newField := v.GetFieldById(oldField.Id)
if newField == nil || oldField.String() != newField.String() {
return validation.NewError("validation_system_field_change", "System fields cannot be deleted or changed.")
}
}
return nil
}
func (form *CollectionUpsert) checkRule(value any) error {
v, _ := value.(*string)
if v == nil || *v == "" {
return nil // nothing to check
}
dummy := *form.collection
dummy.Type = form.Type
dummy.Schema = form.Schema
dummy.System = form.System
dummy.Options = form.Options
r := resolvers.NewRecordFieldResolver(form.dao, &dummy, nil, true)
2022-07-06 23:19:05 +02:00
_, err := search.FilterData(*v).BuildExpr(r)
if err != nil {
2022-10-30 10:28:14 +02:00
return validation.NewError("validation_invalid_rule", "Invalid filter rule.")
}
return nil
}
func (form *CollectionUpsert) checkIndexes(value any) error {
v, _ := value.(types.JsonArray[string])
if form.Type == models.CollectionTypeView && len(v) > 0 {
return validation.NewError(
"validation_indexes_not_supported",
2023-03-28 06:59:37 +02:00
"The collection doesn't support indexes.",
)
}
for i, rawIndex := range v {
parsed := dbutils.ParseIndex(rawIndex)
if !parsed.IsValid() {
return validation.Errors{
strconv.Itoa(i): validation.NewError(
"validation_invalid_index_expression",
2023-03-28 06:59:37 +02:00
"Invalid CREATE INDEX expression.",
),
}
}
// note: we don't check the index table because it is always
// overwritten by the daos.SyncRecordTableSchema to allow
// easier partial modifications (eg. changing only the collection name).
// if !strings.EqualFold(parsed.TableName, form.Name) {
// return validation.Errors{
// strconv.Itoa(i): validation.NewError(
// "validation_invalid_index_table",
// fmt.Sprintf("The index table must be the same as the collection name."),
// ),
// }
// }
}
return nil
}
2022-10-30 10:28:14 +02:00
func (form *CollectionUpsert) checkOptions(value any) error {
v, _ := value.(types.JsonMap)
2023-02-18 19:33:42 +02:00
switch form.Type {
case models.CollectionTypeAuth:
2022-10-30 10:28:14 +02:00
options := models.CollectionAuthOptions{}
2023-02-18 19:33:42 +02:00
if err := decodeOptions(v, &options); err != nil {
return err
2022-10-30 10:28:14 +02:00
}
// check the generic validations
if err := options.Validate(); err != nil {
return err
}
// additional form specific validations
if err := form.checkRule(options.ManageRule); err != nil {
return validation.Errors{"manageRule": err}
}
2023-02-18 19:33:42 +02:00
case models.CollectionTypeView:
options := models.CollectionViewOptions{}
if err := decodeOptions(v, &options); err != nil {
return err
}
// check the generic validations
if err := options.Validate(); err != nil {
return err
}
// check the query option
if _, err := form.dao.CreateViewSchema(options.Query); err != nil {
return validation.Errors{
"query": validation.NewError(
"validation_invalid_view_query",
fmt.Sprintf("Invalid query - %s", err.Error()),
),
}
}
}
return nil
}
func decodeOptions(options types.JsonMap, result any) error {
raw, err := options.MarshalJSON()
if err != nil {
return validation.NewError("validation_invalid_options", "Invalid options.")
}
if err := json.Unmarshal(raw, result); err != nil {
return validation.NewError("validation_invalid_options", "Invalid options.")
2022-07-06 23:19:05 +02:00
}
return nil
}
// Submit validates the form and upserts the form's Collection model.
//
// On success the related record table schema will be auto updated.
//
// You can optionally provide a list of InterceptorFunc to further
// modify the form behavior before persisting it.
func (form *CollectionUpsert) Submit(interceptors ...InterceptorFunc[*models.Collection]) error {
2022-07-06 23:19:05 +02:00
if err := form.Validate(); err != nil {
return err
}
if form.collection.IsNew() {
2022-10-30 10:28:14 +02:00
// type can be set only on create
form.collection.Type = form.Type
// system flag can be set only on create
2022-07-06 23:19:05 +02:00
form.collection.System = form.System
// custom insertion id can be set only on create
if form.Id != "" {
form.collection.MarkAsNew()
form.collection.SetId(form.Id)
}
2022-07-06 23:19:05 +02:00
}
// system collections cannot be renamed
if form.collection.IsNew() || !form.collection.System {
2022-07-06 23:19:05 +02:00
form.collection.Name = form.Name
}
// view schema is autogenerated on save and cannot have indexes
2023-02-18 19:33:42 +02:00
if !form.collection.IsView() {
form.collection.Schema = form.Schema
// normalize indexes format
form.collection.Indexes = make(types.JsonArray[string], len(form.Indexes))
for i, rawIdx := range form.Indexes {
form.collection.Indexes[i] = dbutils.ParseIndex(rawIdx).Build()
}
2023-02-18 19:33:42 +02:00
}
2022-07-06 23:19:05 +02:00
form.collection.ListRule = form.ListRule
form.collection.ViewRule = form.ViewRule
form.collection.CreateRule = form.CreateRule
form.collection.UpdateRule = form.UpdateRule
form.collection.DeleteRule = form.DeleteRule
2022-10-30 10:28:14 +02:00
form.collection.SetOptions(form.Options)
2022-07-06 23:19:05 +02:00
return runInterceptors(form.collection, func(collection *models.Collection) error {
return form.dao.SaveCollection(collection)
}, interceptors...)
2022-07-06 23:19:05 +02:00
}