-
Notifications
You must be signed in to change notification settings - Fork 0
persistence
Tower's standard persistence methods come in the form of common methods you would find in other mapping frameworks.
Model.create
Model.delete
Model.destroy
Model#save
Model#updateAttributes
Model#updateAttribute
Model#destroy
Inserts a new document into the database given the provided attributes. This will run validations and will return the document whether it was persisted or not. You can check Model#persisted? to see if it was successful.
# Insert a new German poet to the db.
Person.create(firstName: "Heinrich", lastName: "Heine")
# This can also take a block.
Person.create firstName: "Heinrich", (record) ->
doc.lastName = "Heine"
store.create { "firstName" : "Heinrich", "lastName" : "Heine" }
Saves the document to the database. If the document is new then the entire document will be inserted. If the document is already saved then only changes to the document will the persisted. This runs validations by default, however they can be switched off by providing an option to the method. Returns true if validation passed and false if not.
# Insert a new German poet to the db.
person = Person.new(firstName: "Heinrich", lastName: "Heine")
person.save()
# Save without running validations.
person.save(validate: false)
# Save an existing document's changed fields.
person.firstName = "Christian Johan"
person.save()
# Insert command for the new document.
collections["people"].insert({
"_id" : ..., "firstName" : "Heinrich", "lastName" : "Heine"
})
# Update command for the changed document.
collections["people"].update({
{ "_id" : ... },
{ "$set" : { "firstName" : "Christian Johan" } }
})
Modifies the provided attributes to new values and persists them in a single call. This runs validations and will return true if they passed, false if not.
# Update the provided attributes.
person.updateAttributes(firstName: "Jean", lastName: "Zorg")
# Update command for the changed document.
collections["people"].update({
{ "_id" : ... },
{ "$set" : { "firstName" : "Jean", "lastName" : "Zorg" } }
})
Updates a single attribute in the database without going through the normal validation procedure, but does fire callbacks. Returns true if save was successful, false if not.
# Update the provided attribute.
person.updateAttribute(:firstName, "Jean")
# Update command for the changed document.
collections["people"].update({
{ "_id" : ... },
{ "$set" : { "firstName" : "Jean" } }
})
Deletes the document from the database without running callbacks.
person.delete()
collections["people"].remove("_id" : ... )
Deletes the document from the database while running destroy callbacks.
person.destroy()
collections["people"].remove("_id" : ... )
Deletes all matching documents in the database given the supplied conditions. See the criteria section on deletion for preferred ways to perform these actions. This does not run any callbacks on the matching documents.
# Delete all the documents from the collection.
Person.delete()
# Delete all matching documents.
Person.where(firstName: "Heinrich").delete()
# Delete all command.
collections["people"].remove
# Delete all matching command.
collections["people"].remove("firstName" : "Heinrich")
Deletes all matching documents in the database given the supplied conditions. See the criteria section on deletion for preferred ways to perform these actions. This runs destroy callbacks on all matching documents.
# Destroy all the documents from the collection.
Person.destroy()
# Destroy all matching documents.
Person.where(firstName: "Heinrich").destroy()
# Destroy all command.
collections["people"].remove
# Destroy all matching command.
collections["people"].remove("firstName" : "Heinrich")
User.update 1, 2, 3, name: "John", (error, records)
User.update 1, 2, 3, name: "John", (error)
User.update [1, 2, 3], name: "John", (error, records)
User.update [1, 2, 3], name: "John", (error)
User.update name: "John", (error, records)
User.update name: "John", (error)
User.update 1, 2, 3, {name: "John"}, {instantiate: false, validate: false}, (error)
User.update {name: "John"}, {instantiate: false, validate: false}, (error)
# Model.create
User.create(firstName: "Lance")
User.where(firstName: "Lance").create()
User.where(firstName: "Lance").create([{lastName: "Pollard"}, {lastName: "Smith"}])
User.where(firstName: "Lance").create(new User(lastName: "Pollard"))
# Model.update
User.where(firstName: "Lance").update(1, 2, 3)
User.update(User.first(), User.last(), firstName: "Lance")
User.update([User.first(), User.last()], firstName: "Lance")
User.update([1, 2], firstName: "Lance")