const mixinafMorphia::Datastore
afMorphia::Datastore
(Service) - Wraps a MongoDB Collection, converting Fantom entities to / from BSON documents.
When injecting as a service, use the @Inject.type
attribute to state the Entity type:
@Inject { type=MyEntity# } private const Datastore myEntityDatastore
You can also autobuild a Datastore instance by passing in the entity type as a ctor param:
scope.build(Datastore#, [MyEntity#])
- collection
abstract MongoColl collection()
The underlying MongoDB collection this Datastore wraps.
- connMgr
abstract MongoConnMgr connMgr()
The backing connector manager instance.
- count
abstract Int count(|MongoQ? queryFn := null)
Returns the number of documents that would be returned by the given
query
.- db
abstract MongoDb db()
Creates an
MongoDb
instance of the associated DB.- delete
abstract Void delete(Obj entity, Bool checked := true)
Deletes the given entity from the MongoDB. Throws an
Err
ifchecked
and nothing was deleted.- deleteAll
abstract Int deleteAll()
Deletes all entities in the Datastore. Returns the number of entities deleted.
Note this is MUCH quicker than dropping the Collection.
- deleteById
abstract Void deleteById(Obj id, Bool checked := true)
Deletes entity with the given Id. Throws an
Err
ifchecked
and nothing was deleted.- drop
abstract This drop(Bool force := false)
Drops the underlying MongoDB collection.
Note that deleting all documents is MUCH quicker than dropping the Collection.
- exists
abstract Bool exists()
Returns
true
if the underlying MongoDB collection exists.- find
abstract MorphiaCur find([Str:Obj?]? query := null, |MongoCmd? optsFn := null)
A general purpose
find()
method whose cursor returns converted entity objects.find(["rick":"morty"]) { it->sort = ["fieldName":1] it->hint = "_indexName_" it->skip = 50 it->limit = 100 it->projection = ["_id":1, "name":1] it->batchSize = 101 it->singleBatch = true it->collation = [...] }.toList
The given query may generated from
query()
.- findAll
abstract Obj[] findAll(Obj? sort := null, |MongoQ? queryFn := null)
Returns a list of entities that match the given
query
.Use
find()
if you need to set any options, likelimit
orskip
.sort
may one of:Str
- the name an index to be used as a hintStr:Obj?
- a ordered sort document of field names with the standard Mongo1
and-1
values for ascending / descendingField
- the field to use for an (ascending) sort, usereverse()
on the returned list for descending sorts
- findOne
abstract Obj? findOne(Bool checked, |MongoQ queryFn)
An (optimised) method to return one document from the given
query
.Throws an
Err
if no documents are found andchecked
istrue
. Always throws anErr
if the query returns more than one document.- fromBsonDoc
abstract Obj? fromBsonDoc([Str:Obj?]? mongoDoc)
Converts the Mongo document to an entity instance.
The returned object is not guaranteed to be of any particular object, for this is just a convenience for calling
Converters.toFantom(...)
.- get
@
Operator
abstract Obj? get(Obj? id, Bool checked := true)Returns the document with the given Id. Convenience / shorthand notation for
findOne(["_id": id], checked)
- insert
abstract Obj insert(Obj entity)
Inserts the given entity. Returns the entity.
- isEmpty
abstract Bool isEmpty()
Returns
true
if the collection has no documents.Convenience for
datastore.exists && datastore.size == 0
.- make
static new make(Type entityType, MongoConnMgr connMgr, BsonConvs? bsonConvs := null, Str? dbName := null)
Create a new Datastore instance.
- name
abstract Str name()
The name of the associated Mongo Collection.
- query
abstract MongoQ query()
Returns a
MongoQ
that accepts fields as keys, and converts all values to BSON.Use the result of query with
find()
.- size
abstract Int size()
Returns the number of documents in the collection.
- toBsonDoc
abstract [Str:Obj?]? toBsonDoc(Obj? entity)
Converts the entity instance to a Mongo document.
Convenience for calling
Converters.toMongo(...)
.- type
abstract Type type()
The Fantom entity type this Datastore associates with.
- update
abstract Obj update(Obj entity, Bool checked := true)
Updates (and returns) the given entity. Throws an
Err
ifchecked
and nothing was updated.Will always throw
OptimisticLockErr
if the entity contains a_version
field which does not match what's in the database. On a successful save, this will increment the_version
field on the entity.