Git Product home page Git Product logo

fp-repository-js's Introduction

The Repository pattern in functional style on JavaScript

An example implementation of the Repository pattern using Functional Programming.

Prerequisites

  • Node.js
  • NPM
  • Basic knowledge of Functional Programming and Ramda library

Installing

Cloning

$ git clone https://github.com/dibikhin/fp-repository-js.git

Setting up

$ cd fp-repository-js
$ npm install

Running the tests

$ npm test

Usage

Suppose you have a stateful DB client demanding to be initialized or connected to a data source. So, you have to store its state somewhere and mock the DB when needed. Or you just want to have a familiar interface for data access.

The Repository pattern will help you. It provides the familiar interface and Functional Programming helps with handling state. Applying OOP you create instances of Repositories with generics or concrete classes holding a stateful DB context in a private field. Using Functional Programming you deal with modules instead of classes and inject state to functions with partial application.

Here we have:

  • DB stub — an example of DB client
  • Generic Repository — a template Repository for creating concrete ones
  • Users Repository — a concrete Repository for Users collection/table
  • Bootstrapper — a module for wiring things up

The concrete UsersRepository module is initiated in app_infra/bootstrapper by dal/users_repository_factory using dal/generic_repository and dal/db_stub. Dependencies are injected with partial application (Ramda's partial) which also handles state (db) through reference to app_infra/global_state (explicit state storage).

See a simple example of usage in test.js and run tests with npm test.

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Authors

  • Roman Dibikhin

Acknowledgments

Thanks to:

  • Martin Fowler for the Patterns of Enterprise Application Architecture book
  • Buzz de Cafe and Scott Sauyet for the Ramda library

Links

License

MIT

fp-repository-js's People

Contributors

dibikhin avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.