{"_id":"@bloxite/koa-dals","_rev":"8-47f89d9df26864a7e0a6621ad75ad409","name":"@bloxite/koa-dals","description":"Simple DALs management for Koa","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.1":{"name":"@bloxite/koa-dals","version":"0.0.1","description":"Simple DALs management for Koa","main":"src/index.js","scripts":{"test":"NODE_ENV=testing mocha","test:watch":"NODE_ENV=testing mocha --watch"},"repository":{"type":"git","url":"git+ssh://git@github.com/bloxite/koa-dals.git"},"authors":"Pavel Kilin <pavel@asdf404.net>, Bloxite Team (bloxite.net)","license":"MIT","homepage":"https://github.com/bloxite/koa-dals#readme","keywords":["koa","dals"],"devDependencies":{"babel-eslint":"^7.1.1","eslint":"^3.15.0","eslint-config-standard":"^6.1.0","eslint-plugin-mocha":"^4.8.0","eslint-plugin-promise":"^3.4.0","eslint-plugin-standard":"^2.0.1","mocha":"^2.5.3","sinon":"^1.17.7"},"dependencies":{"lodash.zipobject":"^4.1.3"},"gitHead":"316f39974aa87508a2d0828cc6082701677c2e41","bugs":{"url":"https://github.com/bloxite/koa-dals/issues"},"_id":"@bloxite/koa-dals@0.0.1","_shasum":"b2a0348f0d8b4b432d4aaa39428894ebbdebb66c","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.5.0","_npmUser":{"name":"bloxite","email":"admin@bloxite.net"},"dist":{"shasum":"b2a0348f0d8b4b432d4aaa39428894ebbdebb66c","tarball":"https://registry.npmjs.org/@bloxite/koa-dals/-/koa-dals-0.0.1.tgz","integrity":"sha512-yqtxF2QWjmX6oqHJdhsuHRU/3p/VUZsEGJiI4KgvfJL7H3KmM9/KAHaCGeTgBmo8tA9Pxz205j1L/NDWPTriLw==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIHg41gKEPGHbE+fkUaz/KNzLnQQ3GIjzxBZPBuvukRjDAiA2hFgVX5RIxvy+aOyor/erNOAuwcdZgHzMGeMjLhmzTQ=="}]},"maintainers":[{"name":"bloxite","email":"admin@bloxite.net"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/koa-dals-0.0.1.tgz_1487594673520_0.318002606742084"}},"0.0.2":{"name":"@bloxite/koa-dals","version":"0.0.2","description":"Simple DALs management for Koa","main":"src/index.js","scripts":{"test":"NODE_ENV=testing mocha","test:watch":"NODE_ENV=testing mocha --watch"},"repository":{"type":"git","url":"git+ssh://git@github.com/bloxite/koa-dals.git"},"authors":"Pavel Kilin <pavel@asdf404.net>, Bloxite Team (bloxite.net)","license":"MIT","homepage":"https://github.com/bloxite/koa-dals#readme","keywords":["koa","dals"],"devDependencies":{"babel-eslint":"^7.1.1","eslint":"^3.15.0","eslint-config-standard":"^6.1.0","eslint-plugin-mocha":"^4.8.0","eslint-plugin-promise":"^3.4.0","eslint-plugin-standard":"^2.0.1","mocha":"^2.5.3","sinon":"^1.17.7"},"dependencies":{"lodash.zipobject":"^4.1.3"},"gitHead":"ac4a2a603a857deff0c462499a3d7e680fb961ef","bugs":{"url":"https://github.com/bloxite/koa-dals/issues"},"_id":"@bloxite/koa-dals@0.0.2","_shasum":"02b13fcffbb2d9dc82e8a2fdbcce914c47f826cc","_from":".","_npmVersion":"4.1.2","_nodeVersion":"7.5.0","_npmUser":{"name":"bloxite","email":"admin@bloxite.net"},"dist":{"shasum":"02b13fcffbb2d9dc82e8a2fdbcce914c47f826cc","tarball":"https://registry.npmjs.org/@bloxite/koa-dals/-/koa-dals-0.0.2.tgz","integrity":"sha512-FLF0gij7Msjq+rfGXCin2+/PfsJMlRo33oUr0iifRi85vUYoB4x6MhPPTqBL9/dZ6xNn/cJsG5aBd+S1SYHoPQ==","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQClcx6sLI+FC+gFRkKZrr74ZCInpOsg9jjtjtzXNztTTQIhAIlFfvKsr1aswn0ThN4d3l74gvLMYPdWm/sthNkv8On2"}]},"maintainers":[{"name":"asdf404","email":"pavel@asdf404.net"},{"name":"bloxite","email":"admin@bloxite.net"}],"_npmOperationalInternal":{"host":"packages-18-east.internal.npmjs.com","tmp":"tmp/koa-dals-0.0.2.tgz_1487762248475_0.27320146397687495"}}},"readme":"# Koa DALs\n\n[![Build Status](https://travis-ci.org/Bloxite/koa-dals.svg?branch=master)](https://travis-ci.org/Bloxite/koa-dals)\n\nData access layers management for Koa@2.\n\n[![Standard - JavaScript Style Guide](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard)\n\n## Installation\n\n```\nnpm install @bloxite/koa-dals --save\n```\n\n## Usage\n\nImport guard object\n\n```js\nconst dals = require('@bloxite/koa-dals')\n// or in ES6-way\nimport dals from '@bloxite/koa-dals'\n```\n\nThis module made for work with [koa-router](https://www.npmjs.com/package/koa-router).\n\nThere is only three methods for manage your DALs: `connect`, `register` and `get`.\n\n### Connect to database\n\nFirst you need to create a connections factory, that will create a connection for every request. Of course you can use connections pool or share single connection, factory — it's just an abstraction. For create new connection use `connect` method:\n\n```js\n// let's connecto to MySQL server using pool from promise-mysql library\nconst mysql = require('promise-mysql').createPool(MYSQL_URL)\n// dals is a singleton object, so we can use it anywhere in program\nconst dals = require('@bloxite/koa-dals')\n\n// register connection\ndals.connect('mysql', {\n  // connect's functions \"take\" and \"release\" can return Promise or connec in syncronous way\n  take: () => mysql.getConnection()\n  release: (con) => mysql.releaseConnection(con)\n})\n```\n\nHere two parameters: first is name of connection, second is factory object that contain two properties: `take` and `release`. `take` is a connection factory, that provides new connection. `release` is a connection destructor. DALs manager share connections between every DAL, so you can use transactions inside your route. For example:\n\n```js\n// we assume `transactions`, `posts` and `user` DALs inside uses `mysql` connection\n// so `mysql` connection will be shared between them\nrouter.post(\n  // method `get` will be described below\n  '/posts', dals.get([ 'transactions', 'posts', 'user' ]),\n  async ({ dals: { transactions, posts, user } }) => {\n    // begin transaction\n    await transactions.begin()\n\n    posts.create(/* ... */)\n    user.increasePostsCount(/* ... */)\n\n    // commit transaction\n    await transactions.commit()\n  }\n)\n```\n\n### Create DAL\n\nTo register new DAL you should pass two arguments to method `register`: first — name of DAL, second — DAL description object.\n\nDAL description is an object with two properties. First property is `required` — is a list of required connections for current DAL, e.g. `[ 'mysql', 'redis' ]`. Second property is factory-function named `create`, that create a DAL object. This function receives object with connections and can return DAL.\n\nHere is example:\n\n```js\n// register a functions for manage blog posts\ndals.register('posts', {\n  required: [ 'mysql' ], // list of required connections\n  create: function posts ({ mysql }) { // method that creates DAL\n    // create new post\n    async function create ({ title, content }) {\n      await mysql.query('INSERT INTO ...')\n      return 'POST_CREATED'\n    }\n\n    return { create, ... }\n  }\n})\n```\n\n### Using DALs\n\nOkay, this is easiest part of this doc. To use your DAL you should use a `get` method. This method accepts list of DALs you want to use (e.g. `[ 'posts', 'user' ]`) and return a middleware for `koa-router`. Middleware patches request context and creates new property called `dals` that contains all required DALs. Example:\n\n```js\nrouter.get(\n  '/',\n  dals.get([ 'posts' ]) // load DAL for posts\n  async (ctx) => {\n    const { posts } = ctx.dals // here be you DALs\n\n    // use DAL\n    const status = await posts.create({\n      title: 'Hello World!',\n      content: '<h1>Hello World!</h1>\\nThis is my first post.'\n    })\n\n    ctx.response.body = status // \"POST_CREATED\"\n  }\n)\n```\n\n## License\n\nMIT License\n\nCopyright (c) 2017 Bloxite\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\nSOFTWARE.\n","maintainers":[],"time":{"modified":"2022-06-12T15:22:33.730Z","created":"2017-02-20T12:44:34.123Z","0.0.1":"2017-02-20T12:44:34.123Z","0.0.2":"2017-02-22T11:17:29.069Z"},"homepage":"https://github.com/bloxite/koa-dals#readme","keywords":["koa","dals"],"repository":{"type":"git","url":"git+ssh://git@github.com/bloxite/koa-dals.git"},"bugs":{"url":"https://github.com/bloxite/koa-dals/issues"},"license":"MIT","readmeFilename":"README.md"}