{"id":16597258,"url":"https://github.com/adrianmcli/node-shared-resource-example","last_synced_at":"2025-03-06T19:32:42.428Z","repository":{"id":73736850,"uuid":"90245513","full_name":"adrianmcli/node-shared-resource-example","owner":"adrianmcli","description":"How to share a database connection between files in Node.js","archived":false,"fork":false,"pushed_at":"2017-05-04T09:36:00.000Z","size":3,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-17T04:44:24.080Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/adrianmcli.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-05-04T09:27:59.000Z","updated_at":"2018-08-11T12:23:25.000Z","dependencies_parsed_at":"2023-04-01T10:34:12.755Z","dependency_job_id":null,"html_url":"https://github.com/adrianmcli/node-shared-resource-example","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrianmcli%2Fnode-shared-resource-example","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrianmcli%2Fnode-shared-resource-example/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrianmcli%2Fnode-shared-resource-example/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/adrianmcli%2Fnode-shared-resource-example/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/adrianmcli","download_url":"https://codeload.github.com/adrianmcli/node-shared-resource-example/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242274661,"owners_count":20101085,"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-11T23:55:21.528Z","updated_at":"2025-03-06T19:32:42.403Z","avatar_url":"https://github.com/adrianmcli.png","language":"JavaScript","readme":"# Node.js Shared (instantiated) Resource Example\n\nIf you only have one database connection, how do you share access to it between different files without clumsily passing the object around?\n\nMake sure node.js is installed and run `node index.js` from the folder.\n\nThe database connection is initialized in `db.js` and it's simply required by  `index.js` and `feature.js` whenever it needs it.\n\nBy running `index.js`, you should see the following output:\n\n```\n[1] index.js:\n[2] index.js: foo_from_index.js\n[1] feature.js: foo_from_index.js\n[2] feature.js: foo_from_index.js,bar_from_feature.js\n[3] index.js: foo_from_index.js,bar_from_feature.js\n```\n\nFor convenience, here is the entirety of the code:\n\n### `db.js`\n\n```js\nfunction db() {\n  const state = [];\n  return {\n    insert: function(x) {\n      return state.push(x);\n    },\n    get: function() {\n      return state;\n    },\n  };\n}\n\nconst myDb = new db();\n\nmodule.exports = myDb;\n```\n\n### `index.js`\n\n```js\n// Simply require the instantiated db object\nconst db = require('./db')\n\n// Check to see that it is fresh\nconsole.log('[1] index.js: ' + db.get())\n\n// Insert something inside and check to see that it was successful\ndb.insert('foo_from_index.js')\nconsole.log('[2] index.js: ' + db.get())\n\n// Require the code from feature.js\nconst feature = require('./feature')\n\n// Check the state again to see that it contains code inserted by both index.js and feature.js\nconsole.log('[3] index.js: ' + db.get())\n```\n\n### `feature.js`\n\n```js\n// Simply require the instantiated db object\nconst db = require(\"./db\");\n\n// Check to see what's already inside it\nconsole.log(\"[1] feature.js: \" + db.get());\n\n// Add another item and inspect its contents\ndb.insert(\"bar_from_feature.js\");\nconsole.log(\"[2] feature.js: \" + db.get());\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrianmcli%2Fnode-shared-resource-example","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fadrianmcli%2Fnode-shared-resource-example","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fadrianmcli%2Fnode-shared-resource-example/lists"}