Created
August 9, 2016 19:21
-
-
Save dypsilon/7c638c0dc6289b21af413d6abe3aee9d to your computer and use it in GitHub Desktop.
Revisions
-
Tim Navrotskyy created this gist
Aug 9, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,62 @@ /** * 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: '[email protected]', password: 'e0538fd8f022bb3b139d72cf12766cb0e31690ff' }, { email: '[email protected]', 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: '[email protected]', 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