{"id":17766604,"url":"https://github.com/floatdrop/connect-once","last_synced_at":"2025-03-15T13:30:51.183Z","repository":{"id":13375348,"uuid":"16063181","full_name":"floatdrop/connect-once","owner":"floatdrop","description":"Connect once and memorize connection for next usages","archived":false,"fork":false,"pushed_at":"2015-08-02T09:58:48.000Z","size":310,"stargazers_count":9,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-14T01:31:40.941Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://floatdrop.github.io/connect-once/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/floatdrop.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-01-20T06:48:14.000Z","updated_at":"2016-02-23T17:38:45.000Z","dependencies_parsed_at":"2022-09-13T22:43:06.215Z","dependency_job_id":null,"html_url":"https://github.com/floatdrop/connect-once","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fconnect-once","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fconnect-once/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fconnect-once/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/floatdrop%2Fconnect-once/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/floatdrop","download_url":"https://codeload.github.com/floatdrop/connect-once/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243508137,"owners_count":20301958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-26T20:33:02.214Z","updated_at":"2025-03-15T13:30:51.177Z","avatar_url":"https://github.com/floatdrop.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# connect-once [![Build Status][travis-image]][travis-url]\n\n:warning: __Deprecated. Use Promises instead.__\n\n\u003e Memorizing async function result\n\nThis module solves [problem](http://stackoverflow.com/questions/6275643/node-js-design-pattern-for-creating-db-connection-once/25209316#25209316) with connection creating in Node.JS.\n\nMost known example - MongoDB connections. Often you want to get one connection shared with all incoming requests. However there are two edges, that can hurt you, when your application starting under significant load. Connection should be:\n\n * Created once for all requests, that are coming at the same time - otherwise you can take down your database connection pool.\n * Ready before first request arrives - to reduce page loading time.\n\nAlso it is nice to have some retries, when database decides to take a rest. For this (and other) tasks connect-once was written. \n\n## Usage\n\nYou can see [express-mongo-db](https://github.com/floatdrop/express-mongo-db) and [express-mongoose-db](https://github.com/floatdrop/express-mongoose-db) as real-life examples of connect-once usage.\n\nInstall package with npm:\n\n`npm i connect-once --save`\n\nThen create a connection:\n\n```js\nvar connectOnce = require('connect-once');\n\nvar connection = new connectOnce({ \n    retries: 60, \n    reconnectWait: 1000\n}, MongoClient.connect, 'mongodb://localhost/test');\n\nconnection.when('available', function (err, db) {\n    // Do stuff\n});\n```\n\n## Heartbeat\n\nHeartbeat is useful, when you want check connection from time to time. For example, if you working with mongodb and cache connection to db - what happens when server, which connection binded to goes to heaven? Connection is lost and programmer should recreate it.\n\n__Note:__ for now there is no way to stop heartbeats.\n\n```js\nvar connectOnce = require('connect-once');\n\nvar connection = new connectOnce({ \n    heartbeat: function (db, beat) {\n        db.stats(function (err) {\n            if (!err) { beat(); }\n        });\n    }\n}, MongoClient.connect, 'mongodb://localhost/test');\n\nconnection.when('available', function (err, db) {\n    // Do stuff\n});\n```\n\n## Force retry (update cached value)\n\nThere are interesting use case of connect-once in a wild: it can be used as some sort of in-memory cache. Because it consumes general function with callback - there is no difference, what to memorize - connection or abstract object.\n\nSuppose you have a function `getCache(callback)`, that invokes `callback` with next arguments: `null, new Date()`. To update cached value from time-to time you can use next code:\n\n```js\nvar connectOnce = require('connect-once');\nvar connection = new connectOnce(getCache);\nsetInterval(function () {\n    connection.retry(new Error('Update cache'));\n}, 5000);\n```\n\nAlso `available` event will be fired every 5000 seconds.\n\n## API\n\nYou can read [source code](https://github.com/floatdrop/connect-once/blob/master/index.js) (65 sloc!) or generated [documentation of Connection class](http://floatdrop.github.io/connect-once/Connection.html) which is not very nice. \n\n# License\n\n(MIT License) (c) 2013 Vsevolod Strukchinsky (floatdrop@gmail.com)\n\n\n[npm-url]: https://npmjs.org/package/connect-once\n[npm-image]: https://badge.fury.io/js/connect-once.png\n\n[travis-url]: http://travis-ci.org/floatdrop/connect-once\n[travis-image]: https://travis-ci.org/floatdrop/connect-once.svg?branch=master\u0026style=flat\n\n[coveralls-url]: https://coveralls.io/r/floatdrop/connect-once\n[coveralls-image]: https://coveralls.io/repos/floatdrop/connect-once/badge.png\n\n[depstat-url]: https://david-dm.org/floatdrop/connect-once\n[depstat-image]: https://david-dm.org/floatdrop/connect-once.png?theme=shields.io\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatdrop%2Fconnect-once","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffloatdrop%2Fconnect-once","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffloatdrop%2Fconnect-once/lists"}