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

Source

abstract MongoColl collection()

The underlying MongoDB collection this Datastore wraps.

connMgr

Source

abstract MongoConnMgr connMgr()

The backing connector manager instance.

count

Source

abstract Int count(|MongoQ? queryFn := null)

Returns the number of documents that would be returned by the given query.

db

Source

abstract MongoDb db()

Creates an MongoDb instance of the associated DB.

delete

Source

abstract Void delete(Obj entity, Bool checked := true)

Deletes the given entity from the MongoDB. Throws an Err if checked and nothing was deleted.

deleteAll

Source

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

Source

abstract Void deleteById(Obj id, Bool checked := true)

Deletes entity with the given Id. Throws an Err if checked and nothing was deleted.

drop

Source

abstract This drop(Bool force := false)

Drops the underlying MongoDB collection.

Note that deleting all documents is MUCH quicker than dropping the Collection.

exists

Source

abstract Bool exists()

Returns true if the underlying MongoDB collection exists.

find

Source

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

Source

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, like limit or skip.

sort may one of:

  • Str - the name an index to be used as a hint
  • Str:Obj? - a ordered sort document of field names with the standard Mongo 1 and -1 values for ascending / descending
  • Field - the field to use for an (ascending) sort, use reverse() on the returned list for descending sorts
findOne

Source

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 and checked is true. Always throws an Err if the query returns more than one document.

fromBsonDoc

Source

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

Source

@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

Source

abstract Obj insert(Obj entity)

Inserts the given entity. Returns the entity.

isEmpty

Source

abstract Bool isEmpty()

Returns true if the collection has no documents.

Convenience for datastore.exists && datastore.size == 0.

make

Source

static new make(Type entityType, MongoConnMgr connMgr, BsonConvs? bsonConvs := null, Str? dbName := null)

Create a new Datastore instance.

name

Source

abstract Str name()

The name of the associated Mongo Collection.

query

Source

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

Source

abstract Int size()

Returns the number of documents in the collection.

toBsonDoc

Source

abstract [Str:Obj?]? toBsonDoc(Obj? entity)

Converts the entity instance to a Mongo document.

Convenience for calling Converters.toMongo(...).

type

Source

abstract Type type()

The Fantom entity type this Datastore associates with.

update

Source

abstract Obj update(Obj entity, Bool checked := true)

Updates (and returns) the given entity. Throws an Err if checked 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.