module Moongoon::Collection::Versioning::Static

Defined in:

Instance Method Summary

Instance Method Detail

def clear_history : Nil #

Clears the history collection.

NOTE Use with caution!

Will remove all the versions in the history collection.


def count_versions(id, query = BSON.new, **args) : Int32 | Int64 #

Counts the number of versions associated with a document that matches the id argument.

user_id = "123456"
User.count_versions user_id

def create_version_by_id(id) : String? #

Saves a copy of a document matching the id argument in the history collection and returns the id of the copy.

NOTE similar to create_version.


def create_version_by_id(id, &block : self -> self) : String? #

Saves a copy with changes of a document matching the id argument in the history collection and returns the id of the copy.

NOTE similar to create_version.


def find_all_versions(id, query = BSON.new, fields = nil, skip = 0, limit = 0, order_by = {_id: -1}, **args) : Array(self) #

Finds all versions for a document matching has the id argument and returns an array of Moongoon::Collection instances.

NOTE Versions are sorted by creation date.

user_id = "123456"
versions = User.find_all_versions user_id

def find_latest_version_by_id(id, fields = nil, **args) : self? #

Finds the latest version of a model by id and returns an instance of Moongoon::Collection.

Same syntax as Moongoon::Collection.find_by_id.

# "123456" is an _id in the original collection.
user_version = user.find_latest_version_by_id "123456"

def find_specific_version(id, query = BSON.new, fields = nil, skip = 0, **args) : self? #

Finds a specific version of a model by id and returns an instance of Moongoon::Collection.

Same syntax as Moongoon::Collection.find_by_id.

# "123456" is an _id in the history collection.
user_version = user.find_specific_version "123456"

def find_specific_version!(id, **args) : self #

NOTE Similar to self.find_specific_version but will raise if the version is not found.


def find_specific_versions(ids, query = BSON.new, fields = nil, skip = 0, limit = 0, order_by = {_id: -1}, **args) : Array(self) #

Finds one or more versions by their ids and returns an array of Moongoon::Collection instances.

NOTE Versions are sorted by creation date in descending order.

names = ["John", "Jane"]
ids = names.map { |name|
  user = User.new name: name
  user.insert
  user.create_version
}
# Contains one version for both models.
versions = User.find_specific_versions ids