const mixinafMorphia::Datastore

afMorphia::Datastore

(Service) - Wraps a MongoDB Collection, converting Fantom entities to / from Mongo documents.

When injecting as a service, use the @Inject.type attribute to state which Entity type it is for:

@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 Collection collection()

The underlying MongoDB collection this Datastore wraps.

delete

Source

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

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

@see afMongo::Collection.delete

deleteById

Source

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

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

@see afMongo::Collection.delete

drop

Source

abstract This drop(Bool checked := true)

Drops the underlying MongoDB collection.

@see afMongo::Collection.drop

exists

Source

abstract Bool exists()

Returns true if the underlying MongoDB collection exists.

@see afMongo::Collection.exists

findAll

Source

abstract Obj[] findAll([Str:Obj?]? query := null, Obj? sort := null, Int skip := 0, Int? limit := null)

Returns a list of entities that match the given query.

Note: This method requires you to be familiar with Mongo query notation. If not, use the Query builder instead.

If sort is a Str it should the name of an index to use as a hint.

If sort is a [Str:Obj?] map, it should be a sort document with field names as keys. Values may either be the standard Mongo 1 and -1 for ascending / descending or the strings ASC / DESC.

The sort map, should it contain more than 1 entry, must be ordered.

@see afMongo::Collection.findAll

findCount

Source

abstract Int findCount([Str:Obj?]? query := null)

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

Note: This method requires you to be familiar with Mongo query notation. If not, use the Query builder instead.

@see afMongo::Collection.findCount

findOne

Source

abstract Obj? findOne([Str:Obj?]? query := null, Bool checked := true)

An (optimised) method to return one document from the given query.

Note: This method requires you to be familiar with Mongo query notation. If not, use the Query builder instead.

Throws MongoErr if no documents are found and checked is true, returns null otherwise. Always throws MongoErr if the query returns more than one document.

@see afMongo::Collection.findOne

fromMongoDoc

Source

abstract Obj fromMongoDoc(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.

@see afMongo::Collection.insert

isEmpty

Source

abstract Bool isEmpty()

Returns true if the collection has no documents.

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

name

Source

abstract Str name()

The simple name of the MongoDB collection.

qname

Source

abstract Str qname()

The qualified name of the MongoDB collection. It takes the form of:

<database>.<collection>
query

Source

abstract QueryExecutor query(Query? query := null)

Returns a Query object used to build Mongo queries.

size

Source

abstract Int size()

Returns the number of documents in the collection.

@see afMongo::Collection.size

toMongoDoc

Source

abstract Str:Obj? toMongoDoc(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 Void update(Obj entity, Bool? upsert := (Bool?)false, Bool checked := true)

Updates the given entity. Throws MorphiaErr if checked and nothing was updated.

@see afMongo::Collection.update