updated automigrate templates, added js bindings tests and updated models IsNew behavior
This commit is contained in:
+16
-11
@@ -31,7 +31,7 @@ type Model interface {
|
||||
TableName() string
|
||||
IsNew() bool
|
||||
MarkAsNew()
|
||||
UnmarkAsNew()
|
||||
MarkAsNotNew()
|
||||
HasId() bool
|
||||
GetId() string
|
||||
SetId(id string)
|
||||
@@ -48,7 +48,7 @@ type Model interface {
|
||||
|
||||
// BaseModel defines common fields and methods used by all other models.
|
||||
type BaseModel struct {
|
||||
isNewFlag bool
|
||||
isNotNew bool
|
||||
|
||||
Id string `db:"id" json:"id"`
|
||||
Created types.DateTime `db:"created" json:"created"`
|
||||
@@ -70,20 +70,20 @@ func (m *BaseModel) SetId(id string) {
|
||||
m.Id = id
|
||||
}
|
||||
|
||||
// MarkAsNew sets the model isNewFlag enforcing [m.IsNew()] to be true.
|
||||
// MarkAsNew marks the model as "new" (aka. enforces m.IsNew() to be true).
|
||||
func (m *BaseModel) MarkAsNew() {
|
||||
m.isNewFlag = true
|
||||
m.isNotNew = false
|
||||
}
|
||||
|
||||
// UnmarkAsNew resets the model isNewFlag.
|
||||
func (m *BaseModel) UnmarkAsNew() {
|
||||
m.isNewFlag = false
|
||||
// MarkAsNotNew marks the model as "not new" (aka. enforces m.IsNew() to be false)
|
||||
func (m *BaseModel) MarkAsNotNew() {
|
||||
m.isNotNew = true
|
||||
}
|
||||
|
||||
// IsNew indicates what type of db query (insert or update)
|
||||
// should be used with the model instance.
|
||||
func (m *BaseModel) IsNew() bool {
|
||||
return m.isNewFlag || !m.HasId()
|
||||
return !m.isNotNew
|
||||
}
|
||||
|
||||
// GetCreated returns the model Created datetime.
|
||||
@@ -100,9 +100,6 @@ func (m *BaseModel) GetUpdated() types.DateTime {
|
||||
//
|
||||
// The generated id is a cryptographically random 15 characters length string.
|
||||
func (m *BaseModel) RefreshId() {
|
||||
if m.Id == "" { // no previous id
|
||||
m.MarkAsNew()
|
||||
}
|
||||
m.Id = security.RandomStringWithAlphabet(DefaultIdLength, DefaultIdAlphabet)
|
||||
}
|
||||
|
||||
@@ -115,3 +112,11 @@ func (m *BaseModel) RefreshCreated() {
|
||||
func (m *BaseModel) RefreshUpdated() {
|
||||
m.Updated = types.NowDateTime()
|
||||
}
|
||||
|
||||
// PostScan implements the [dbx.PostScanner] interface.
|
||||
//
|
||||
// It is executed right after the model was populated with the db row values.
|
||||
func (m *BaseModel) PostScan() error {
|
||||
m.MarkAsNotNew()
|
||||
return nil
|
||||
}
|
||||
|
||||
+13
-7
@@ -58,15 +58,19 @@ func TestBaseModelIsNew(t *testing.T) {
|
||||
m1 := models.BaseModel{Id: ""}
|
||||
m2 := models.BaseModel{Id: "test"}
|
||||
m3 := models.BaseModel{}
|
||||
m3.MarkAsNew()
|
||||
m3.MarkAsNotNew()
|
||||
m4 := models.BaseModel{Id: "test"}
|
||||
m4.MarkAsNew()
|
||||
m4.MarkAsNotNew()
|
||||
m5 := models.BaseModel{Id: "test"}
|
||||
m5.MarkAsNew()
|
||||
m5.UnmarkAsNew()
|
||||
// check if MarkAsNew will be called on initial RefreshId()
|
||||
m5.MarkAsNotNew()
|
||||
m6 := models.BaseModel{}
|
||||
m6.RefreshId()
|
||||
m7 := models.BaseModel{}
|
||||
m7.MarkAsNotNew()
|
||||
m7.RefreshId()
|
||||
m8 := models.BaseModel{}
|
||||
m8.PostScan()
|
||||
|
||||
scenarios := []struct {
|
||||
model models.BaseModel
|
||||
@@ -74,11 +78,13 @@ func TestBaseModelIsNew(t *testing.T) {
|
||||
}{
|
||||
{m0, true},
|
||||
{m1, true},
|
||||
{m2, false},
|
||||
{m3, true},
|
||||
{m4, true},
|
||||
{m2, true},
|
||||
{m3, false},
|
||||
{m4, false},
|
||||
{m5, false},
|
||||
{m6, true},
|
||||
{m7, false},
|
||||
{m8, false},
|
||||
}
|
||||
|
||||
for i, s := range scenarios {
|
||||
|
||||
@@ -79,6 +79,7 @@ func NewRecordFromNullStringMap(collection *Collection, data dbx.NullStringMap)
|
||||
record := NewRecord(collection)
|
||||
|
||||
record.Load(resultMap)
|
||||
record.PostScan()
|
||||
|
||||
return record
|
||||
}
|
||||
@@ -111,6 +112,10 @@ func (m *Record) OriginalCopy() *Record {
|
||||
newRecord := NewRecord(m.collection)
|
||||
newRecord.Load(m.originalData)
|
||||
|
||||
if !m.IsNew() {
|
||||
newRecord.MarkAsNotNew()
|
||||
}
|
||||
|
||||
return newRecord
|
||||
}
|
||||
|
||||
|
||||
@@ -195,7 +195,14 @@ func (s *Schema) UnmarshalJSON(data []byte) error {
|
||||
s.AddField(f)
|
||||
}
|
||||
|
||||
return s.InitFieldsOptions()
|
||||
for _, field := range s.fields {
|
||||
if err := field.InitOptions(); err != nil {
|
||||
// ignore the error and remove the invalid field
|
||||
s.RemoveField(field.Id)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Value implements the [driver.Valuer] interface.
|
||||
|
||||
Reference in New Issue
Block a user