Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Fawn provides the ability to carry out edits on a mongoDB database as a series of steps. If an error occurs on any of the steps, the database is returned to it's initial state (it's state before the transaction started).
View on GitHub If you're having trouble reading this on npm
Start mongoDB in a terminal: mongod
Then:
npm install fawn
var Fawn = require("fawn");
Say you have two bank accounts, one belongs to John Smith and the other belongs to Broke Ass. You would like to transfer $20 from John Smith to Broke Ass. Assuming all first name and last name pairs are unique, this might look like:
var task = Fawn.Task()
//assuming "Accounts" is the Accounts collection
task.update("Accounts", {firstName: "John", lastName: "Smith"}, {$inc: {balance: -20}})
.update("Accounts", {firstName: "Broke", lastName: "Ass"}, {$inc: {balance: 20}})
.run()
.then(function(){
//update is complete
})
.catch(function(err){
// Everything has been rolled back.
//log the error which caused the failure
console.log(err);
});
if you prefer not to chain function calls, you don't have to:
task.update("Accounts", {firstName: "Broke", lastName: "Ass"}, {$inc: {balance: -20}})
task.update("Accounts", {firstName: "Coke", lastName: "Dealer"}, {$inc: {balance: 20}})
task.run()
.then(function(){
//update is complete
})
.catch(function(err){
// Everything has been rolled back.
//log the error which caused the failure
console.log(err);
});
The server could crash before a task is complete, You can use the Roller to rollback all incomplete transactions before starting your server.
// assuming Fawn has been initialized. See Fawn.init below
var roller = Fawn.Roller();
roller.roll()
.then(function(){
// start server
});
db (required): mongoose instance or connection string
_collection (optional): Name of collection used internally by Fawn to store transactions
options (optional. lol): Connection options. Same as mongoose connection options
Note: if you're running multiple apps connected to the same db, provide a string value for _collection that's unique to each app. Do this to avoid a situation where one app rolls back the unfinished transaction(s) of another app.
If you're using mongoose in your project initialize Fawn with mongoose:
var mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017/testDB");
// remember, _collection is optional
Fawn.init(mongoose, "Fawn_collection_name_if_you_want_to_specify");
Without mongoose, Initialze Fawn like so:
// options object (http://mongoosejs.com/docs/connections.html#options)
var options = {
user: "teh_huose_kat",
pass: "teh_Kitti_passwrod"
}
var collection = "Fawn_collection_name_if_you_want_to_specify";
// remember, _collection and options are optional
Fawn.init("mongodb://127.0.0.1:27017/testDB", collection || null, options || null);
returns: A new task
After intitializing Fawn, create a task like so:
var task = Fawn.Task();
modelName (required): Name of the collection associated with this model
schema (required): Same as object passed to mongoose Schema. Also see validation
If you're using mongoose, define your models with mongoose wherever possible. If the model has been defined by mongoose before this function is called, mongoose will throw an OverwriteModelError and if it was defined by Fawn, Fawn will throw an Error. Models can be defined only once.
var schema = {
name: {type: String, required: true}
, specials: [{title: String, year: Number}]
}
task.initModel("comedians", schema);
Save operations to the "comedians" model will validate against the schema;
model (required): Name of the collection we're saving to or a mongoose model or a mongoose document
doc (optional): Object to save or a mongoose document
these are all valid:
var Cars = mongoose.model("cars", new Schema({make: String, year: Number}));
var toyota = new Cars({make: "Toyota", year: 2015});
task.save("cars", {make: "Toyota", year: 2015});
task.save(Cars, {make: "Toyota", year: 2015});
task.save("cars", toyota);
task.save(Cars, toyota);
task.save(toyota);
Note: No changes will be made to to your database until you call task.run()
model (required): Name of the collection we're updating or a mongoose model or a mongoose document
condition (required): Same as in [mongoose update][] and [mongodb][]
data (optional): Data to update with same as in [mongoose update][] and mongodb
These are all valid
var Cars = mongoose.model("cars", new Schema({make: String, year: Number}));
task.update("cars", {make: "Toyota"}, {year: 2016});
task.update(Cars, {make: "Toyota"}, {year: 2016});
Cars.findOne({make: "Toyota"}, function(toyota){
task.update(toyota, {year: 2016});
});
Note: No changes will be made to to your database until you call task.run()
options (required): Update options - same as in [mongoose update][]
Attach to update call as shown
task.update("cars", {make: "Toyota"}, {year: 2016})
.options({multi: true});
// Also valid
task.update("cars", {make: "Ford"}, {year: 2016});
task.options({multi: true});
Note: No changes will be made to to your database until you call task.run()
model (required): Name of the collection we're deleting from or a mongoose model or a mongoose document
condition (optional): Same as in mongoose
These are all valid
var Cars = mongoose.model("cars", new Schema({make: String, year: Number}));
// removes all cars with year === 2015
task.remove("cars", {year: 2015});
task.remove(Cars, {year: 2015});
Cars.findOne({year: 2015}, function(car){
// remove just this car
task.remove(car);
})
Note: No changes will be made to to your database until you call task.run()
returns: Promise
For the database changes to occur, you must call task.run(). This function returns a promise. If an error occurs, the promise is rejected with the error that caused the failure.
task.update("Accounts", {firstName: "John", lastName: "Smith"}, {$inc: {balance: -20}})
.update("Accounts", {firstName: "Broke", lastName: "Ass"}, {$inc: {balance: 20}})
.run()
.then(function(){
//update is complete
})
.catch(function(err){
// Everything has been rolled back.
//log the error which caused the failure
console.log(err);
});
returns: The Roller object
After initializing Fawn, get the Roller like so:
var Roller = Fawn.Roller();
Returns all the documents affected by incomplete transactions to their original state. Should only be used when no tasks are in progress, usually on server startup.
var roller = Fawn.Roller();
roller.roll()
.then(function(){
// start server
});
FAQs
Promise based library for transactions in MongoDB
The npm package fawn receives a total of 0 weekly downloads. As such, fawn popularity was classified as not popular.
We found that fawn demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.