From 1f9d32193fd59303af02e46e69e5b64290c3f94e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=98=99=E2=97=A6=20The=20Tablet=20=E2=9D=80=20GamerGirla?= =?UTF-8?q?ndCo=20=E2=97=A6=E2=9D=A7?= Date: Sat, 5 Apr 2025 23:28:34 -0400 Subject: [PATCH] add `Count` function to Model begin work on tracking populated/unpopulated fields --- document.go | 13 ++++++++----- document_internals.go | 8 ++++++++ model.go | 5 +++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/document.go b/document.go index 67335ba..3439e2a 100644 --- a/document.go +++ b/document.go @@ -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 { diff --git a/document_internals.go b/document_internals.go index 123c8d8..9584c76 100644 --- a/document_internals.go +++ b/document_internals.go @@ -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 +} diff --git a/model.go b/model.go index 43887f5..fb25cae 100644 --- a/model.go +++ b/model.go @@ -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