Skip to content

Instantly share code, notes, and snippets.

@dypsilon
Created August 9, 2016 19:21
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dypsilon/7c638c0dc6289b21af413d6abe3aee9d to your computer and use it in GitHub Desktop.
Save dypsilon/7c638c0dc6289b21af413d6abe3aee9d to your computer and use it in GitHub Desktop.
Reader + Future Monads Usage
/**
* This short program will encrypt the user password
* and insert a new record into a mock database.
*/
const Reader = require('fantasy-readers');
const Future = require('fluture');
const R = require('ramda');
const crypto = require('crypto');
// our mock database
const database = [
{ email: 'user@example.org', password: 'e0538fd8f022bb3b139d72cf12766cb0e31690ff' },
{ email: 'admin@example.org', password: '42c4fbf6fec201c66b82c97833b08d936d2cd526' }
]
// creates a statefull database connection
const connectTo = (db) => {
return {
insert: (doc) => Future.of(db.push(doc)),
get: (i) => Future.of(db[i]),
delete: (i) => Future.of(db.splice(i, 1)),
list: () => Future.of(db)
}
}
// some utility functions
const encrypt = (i) => crypto.createHash('sha1').update(i).digest('hex');
const encPassword = R.evolve({password: encrypt})
// utility function which makes it easier
// to ask for some dependency
// dependencies should reside in an object { dep: obj }
const inject = R.curry((dep, f, reader) => {
return reader.chain((v) => {
return Reader.ask.map((env) => {
return f(env[dep], v);
});
});
});
// this is how you access the cb connection inside the reader
const save = (db, user) => {
db.insert(user);
return db.list();
}
// the body of the program
const handler = R.pipe(
R.map(encPassword), // Reader Object
inject('db', save), // Reader Future Error Object
R.map(R.map(R.map(R.dissoc('password')))) // Reader Future Error Object
);
// this is our db connection now
const dbCon = connectTo(database);
const request = { email: 'new@example.org', password: 'secret' };
const result = handler(Reader.of(request))
.run({ 'db': dbCon }) // this is how you pass the db connection in
.fork(console.error, console.log); // now fork the future
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment