Skip to content

Instantly share code, notes, and snippets.

@gabejohnson
Created November 3, 2017 15:58
Show Gist options
  • Save gabejohnson/105c3b7d722006a9aea3d26439c2109f to your computer and use it in GitHub Desktop.
Save gabejohnson/105c3b7d722006a9aea3d26439c2109f to your computer and use it in GitHub Desktop.
import { Right, Future, pipe, curry2, ifElse, isNotNil, memoize, chain, map } from "../fp"
// mssql.js
import sql from "mssql"
const createPoolAsync = memoize(connectionString =>
new Future( ( reject, resolve ) => {
const pool = new sql.ConnectionPool(
connectionString,
ifElse( isNotNil, reject, () => resolve( pool ) )
);
} );
export const connectAsync = curry2((connectionString, dbName ) =>
createPoolAsync(connectionString.replace( "%dbName%", dbName ));
export const query = curry2(( connection, text ) =>
pipe( [
chain( pool => Future.tryP( () => pool.request().query( text ) ) ),
map( result => result.recordset )
], connection )
);
// serviceManagement.js
import { query, connectAsync } from "../data/mssql"
export const getActiveServicesAsync = connection =>
query( connection, "<Some SELECT query text here>" )
// somewhere.js
import { connectAsync } from "../data/mssql";
import { getActiveServicesAsync } from "./serviceManagement";
const connection = connectAsync("connection string", "db name");
getActiveServicesAsync(connection).fork(err => {
// Stuff
}, services => {
// Stuff
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment