add Count function to Model

begin work on tracking populated/unpopulated fields
This commit is contained in:
☙◦ The Tablet ❀ GamerGirlandCo ◦❧ 2025-04-05 23:28:34 -04:00
parent 93b81308cb
commit 1f9d32193f
Signed by: tablet
GPG Key ID: 924A5F6AF051E87C
3 changed files with 21 additions and 5 deletions

@ -7,12 +7,13 @@ import (
type Document struct {
// Created time. updated/added automatically.
Created time.Time `bson:"createdAt" json:"createdAt"`
Created time.Time `bson:"createdAt" json:"createdAt" tstype:"Date"`
// Modified time. updated/added automatically.
Modified time.Time `bson:"updatedAt" json:"updatedAt"`
model *Model `bson:"-"`
exists bool `bson:"-"`
self any `bson:"-"`
Modified time.Time `bson:"updatedAt" json:"updatedAt" tstype:"Date"`
model *Model `bson:"-"`
exists bool `bson:"-"`
self any `bson:"-"`
populatedFields map[string]bool `bson:"-"`
}
type IDocument interface {
Append(field string, a ...interface{}) error
@ -32,6 +33,8 @@ type IDocument interface {
serializeToStore() any
getModel() *Model
setModel(m Model)
markPopulated(field string)
markDepopulated(field string)
}
type SaveOptions struct {

@ -266,3 +266,11 @@ func incrementAll(item interface{}) {
default:
}
}
func (d *Document) markPopulated(field string) {
d.populatedFields[field] = true
}
func (d *Document) markDepopulated(field string) {
d.populatedFields[field] = false
}

@ -193,6 +193,11 @@ func (m *Model) FindOne(query interface{}, options *options.FindOneOptionsBuilde
return qq, err
}
func (m *Model) Count(query interface{}, options *options.CountOptionsBuilder) (int64, error) {
coll := m.getColl()
return coll.CountDocuments(context.TODO(), query, options)
}
func createBase(d any) (reflect.Value, int, string) {
var n string
var ri *Model