mirror of
https://github.com/pocketbase/pocketbase.git
synced 2025-03-19 22:19:23 +02:00
normalized Collection struct methods receiver
This commit is contained in:
parent
bc83fddaf2
commit
a446290a7a
@ -660,13 +660,15 @@ func onCollectionDeleteExecute(e *CollectionEvent) error {
|
|||||||
// -------------------------------------------------------------------
|
// -------------------------------------------------------------------
|
||||||
|
|
||||||
func (c *Collection) initDefaultId() {
|
func (c *Collection) initDefaultId() {
|
||||||
if c.Id == "" {
|
if c.Id != "" {
|
||||||
if c.System && c.Name != "" {
|
return // already set
|
||||||
// for system collections we use crc32 checksum for consistency because they cannot be renamed
|
}
|
||||||
c.Id = "_pbc_" + crc32Checksum(c.Name)
|
|
||||||
} else {
|
if c.System && c.Name != "" {
|
||||||
c.Id = "_pbc_" + security.RandomStringWithAlphabet(10, DefaultIdAlphabet)
|
// for system collections we use crc32 checksum for consistency because they cannot be renamed
|
||||||
}
|
c.Id = "_pbc_" + crc32Checksum(c.Name)
|
||||||
|
} else {
|
||||||
|
c.Id = "_pbc_" + security.RandomStringWithAlphabet(10, DefaultIdAlphabet)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -791,24 +793,24 @@ func onCollectionSaveExecute(e *CollectionEvent) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initDefaultFields() {
|
func (c *Collection) initDefaultFields() {
|
||||||
switch m.Type {
|
switch c.Type {
|
||||||
case CollectionTypeBase:
|
case CollectionTypeBase:
|
||||||
m.initIdField()
|
c.initIdField()
|
||||||
case CollectionTypeAuth:
|
case CollectionTypeAuth:
|
||||||
m.initIdField()
|
c.initIdField()
|
||||||
m.initPasswordField()
|
c.initPasswordField()
|
||||||
m.initTokenKeyField()
|
c.initTokenKeyField()
|
||||||
m.initEmailField()
|
c.initEmailField()
|
||||||
m.initEmailVisibilityField()
|
c.initEmailVisibilityField()
|
||||||
m.initVerifiedField()
|
c.initVerifiedField()
|
||||||
case CollectionTypeView:
|
case CollectionTypeView:
|
||||||
// view fields are autogenerated
|
// view fields are autogenerated
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initIdField() {
|
func (c *Collection) initIdField() {
|
||||||
field, _ := m.Fields.GetByName(FieldNameId).(*TextField)
|
field, _ := c.Fields.GetByName(FieldNameId).(*TextField)
|
||||||
if field == nil {
|
if field == nil {
|
||||||
// create default field
|
// create default field
|
||||||
field = &TextField{
|
field = &TextField{
|
||||||
@ -823,7 +825,7 @@ func (m *Collection) initIdField() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// prepend it
|
// prepend it
|
||||||
m.Fields = NewFieldsList(append([]Field{field}, m.Fields...)...)
|
c.Fields = NewFieldsList(append([]Field{field}, c.Fields...)...)
|
||||||
} else {
|
} else {
|
||||||
// enforce system defaults
|
// enforce system defaults
|
||||||
field.System = true
|
field.System = true
|
||||||
@ -833,11 +835,11 @@ func (m *Collection) initIdField() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initPasswordField() {
|
func (c *Collection) initPasswordField() {
|
||||||
field, _ := m.Fields.GetByName(FieldNamePassword).(*PasswordField)
|
field, _ := c.Fields.GetByName(FieldNamePassword).(*PasswordField)
|
||||||
if field == nil {
|
if field == nil {
|
||||||
// load default field
|
// load default field
|
||||||
m.Fields.Add(&PasswordField{
|
c.Fields.Add(&PasswordField{
|
||||||
Name: FieldNamePassword,
|
Name: FieldNamePassword,
|
||||||
System: true,
|
System: true,
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
@ -852,11 +854,11 @@ func (m *Collection) initPasswordField() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initTokenKeyField() {
|
func (c *Collection) initTokenKeyField() {
|
||||||
field, _ := m.Fields.GetByName(FieldNameTokenKey).(*TextField)
|
field, _ := c.Fields.GetByName(FieldNameTokenKey).(*TextField)
|
||||||
if field == nil {
|
if field == nil {
|
||||||
// load default field
|
// load default field
|
||||||
m.Fields.Add(&TextField{
|
c.Fields.Add(&TextField{
|
||||||
Name: FieldNameTokenKey,
|
Name: FieldNameTokenKey,
|
||||||
System: true,
|
System: true,
|
||||||
Hidden: true,
|
Hidden: true,
|
||||||
@ -873,21 +875,21 @@ func (m *Collection) initTokenKeyField() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ensure that there is a unique index for the field
|
// ensure that there is a unique index for the field
|
||||||
if !dbutils.HasSingleColumnUniqueIndex(FieldNameTokenKey, m.Indexes) {
|
if !dbutils.HasSingleColumnUniqueIndex(FieldNameTokenKey, c.Indexes) {
|
||||||
m.Indexes = append(m.Indexes, fmt.Sprintf(
|
c.Indexes = append(c.Indexes, fmt.Sprintf(
|
||||||
"CREATE UNIQUE INDEX `%s` ON `%s` (`%s`)",
|
"CREATE UNIQUE INDEX `%s` ON `%s` (`%s`)",
|
||||||
m.fieldIndexName(FieldNameTokenKey),
|
c.fieldIndexName(FieldNameTokenKey),
|
||||||
m.Name,
|
c.Name,
|
||||||
FieldNameTokenKey,
|
FieldNameTokenKey,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initEmailField() {
|
func (c *Collection) initEmailField() {
|
||||||
field, _ := m.Fields.GetByName(FieldNameEmail).(*EmailField)
|
field, _ := c.Fields.GetByName(FieldNameEmail).(*EmailField)
|
||||||
if field == nil {
|
if field == nil {
|
||||||
// load default field
|
// load default field
|
||||||
m.Fields.Add(&EmailField{
|
c.Fields.Add(&EmailField{
|
||||||
Name: FieldNameEmail,
|
Name: FieldNameEmail,
|
||||||
System: true,
|
System: true,
|
||||||
Required: true,
|
Required: true,
|
||||||
@ -899,22 +901,22 @@ func (m *Collection) initEmailField() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ensure that there is a unique index for the email field
|
// ensure that there is a unique index for the email field
|
||||||
if !dbutils.HasSingleColumnUniqueIndex(FieldNameEmail, m.Indexes) {
|
if !dbutils.HasSingleColumnUniqueIndex(FieldNameEmail, c.Indexes) {
|
||||||
m.Indexes = append(m.Indexes, fmt.Sprintf(
|
c.Indexes = append(c.Indexes, fmt.Sprintf(
|
||||||
"CREATE UNIQUE INDEX `%s` ON `%s` (`%s`) WHERE `%s` != ''",
|
"CREATE UNIQUE INDEX `%s` ON `%s` (`%s`) WHERE `%s` != ''",
|
||||||
m.fieldIndexName(FieldNameEmail),
|
c.fieldIndexName(FieldNameEmail),
|
||||||
m.Name,
|
c.Name,
|
||||||
FieldNameEmail,
|
FieldNameEmail,
|
||||||
FieldNameEmail,
|
FieldNameEmail,
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initEmailVisibilityField() {
|
func (c *Collection) initEmailVisibilityField() {
|
||||||
field, _ := m.Fields.GetByName(FieldNameEmailVisibility).(*BoolField)
|
field, _ := c.Fields.GetByName(FieldNameEmailVisibility).(*BoolField)
|
||||||
if field == nil {
|
if field == nil {
|
||||||
// load default field
|
// load default field
|
||||||
m.Fields.Add(&BoolField{
|
c.Fields.Add(&BoolField{
|
||||||
Name: FieldNameEmailVisibility,
|
Name: FieldNameEmailVisibility,
|
||||||
System: true,
|
System: true,
|
||||||
})
|
})
|
||||||
@ -924,11 +926,11 @@ func (m *Collection) initEmailVisibilityField() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) initVerifiedField() {
|
func (c *Collection) initVerifiedField() {
|
||||||
field, _ := m.Fields.GetByName(FieldNameVerified).(*BoolField)
|
field, _ := c.Fields.GetByName(FieldNameVerified).(*BoolField)
|
||||||
if field == nil {
|
if field == nil {
|
||||||
// load default field
|
// load default field
|
||||||
m.Fields.Add(&BoolField{
|
c.Fields.Add(&BoolField{
|
||||||
Name: FieldNameVerified,
|
Name: FieldNameVerified,
|
||||||
System: true,
|
System: true,
|
||||||
})
|
})
|
||||||
@ -938,13 +940,13 @@ func (m *Collection) initVerifiedField() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Collection) fieldIndexName(field string) string {
|
func (c *Collection) fieldIndexName(field string) string {
|
||||||
name := "idx_" + field + "_"
|
name := "idx_" + field + "_"
|
||||||
|
|
||||||
if m.Id != "" {
|
if c.Id != "" {
|
||||||
name += m.Id
|
name += c.Id
|
||||||
} else if m.Name != "" {
|
} else if c.Name != "" {
|
||||||
name += m.Name
|
name += c.Name
|
||||||
} else {
|
} else {
|
||||||
name += security.PseudorandomString(10)
|
name += security.PseudorandomString(10)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user