{"id":19972586,"url":"https://github.com/jpb06/mongodb-generic-dal","last_synced_at":"2026-05-05T01:37:14.110Z","repository":{"id":57301782,"uuid":"323335608","full_name":"jpb06/mongodb-generic-dal","owner":"jpb06","description":"A simple data layer for mongodb","archived":false,"fork":false,"pushed_at":"2020-12-21T17:02:21.000Z","size":85,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-12T21:39:02.957Z","etag":null,"topics":["data-access-layer","mongodb","nodejs"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/jpb06.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":"2020-12-21T12:52:41.000Z","updated_at":"2023-02-08T18:00:44.000Z","dependencies_parsed_at":"2022-08-24T17:12:12.241Z","dependency_job_id":null,"html_url":"https://github.com/jpb06/mongodb-generic-dal","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/jpb06%2Fmongodb-generic-dal","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fmongodb-generic-dal/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fmongodb-generic-dal/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jpb06%2Fmongodb-generic-dal/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jpb06","download_url":"https://codeload.github.com/jpb06/mongodb-generic-dal/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241405427,"owners_count":19957816,"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":["data-access-layer","mongodb","nodejs"],"created_at":"2024-11-13T03:08:37.738Z","updated_at":"2026-05-05T01:37:09.069Z","avatar_url":"https://github.com/jpb06.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongodb-generic-dal\n\n![Statements](./badges/badge-statements.svg) ![Branches](./badges/badge-branches.svg) ![Functions](./badges/badge-functions.svg) ![Lines](./badges/badge-lines.svg)\n\n## purpose\n\nThis is a little library built on top mongodb native nodejs driver.\n\nThe package exposes its own declaration files; you won't need to install any @types/\\* if you use typescript.\n\n## Installation\n\nTo install, use either yarn or npm:\n\n```bash\nyarn add mongodb-generic-dal\n```\n\n```bash\nnpm i mongodb-generic-dal\n```\n\n## Prerequisites\n\nThe module relies on env variables to connect to a mongodb instance:\n\n- MONGODB_URL : any valid mongodb url - https://docs.mongodb.com/manual/reference/connection-string/\n- MONGODB_DB : the database to connect to\n- MONGODB_DB_USR : username (if authentication is required)\n- MONGODB_DB_PWD : password (if authentication is required)\n\n## Let's look at a code example\n\n```Typescript\nimport * as GenericDal from \"mongodb-generic-dal\";\n\ninterface MyData {\n   _id?: ObjectId;\n   name: string;\n   value: number;\n}\n\nconst letsCRUD = async () =\u003e {\n   const item = {\n      name: \"Cool\",\n      value: 1024\n   };\n   const id = await GenericDal.create\u003cMyData\u003e(\"mycollection\", item);\n   const persistedItem = await GenericDal.getBy\u003cMyData\u003e(\"mycollection\", { _id: id }, {});\n   const updatedItem = await GenericDal.createOrUpdate\u003cMyData\u003e(\"mycollection\", { _id: id }, {\n      name: \"Yolo\",\n      value: 0\n   });\n   const isDeleted = await GenericDal.remove(\"mycollection\", {_id: id});\n};\n```\n\n## API\n\n### create\n\nInserts a document in the specified collection. Returns the id of the document inserted.\n\n```Typescript\nconst create = async \u003cT\u003e(\n   collectionName: string,\n   value: OptionalId\u003cT\u003e\n): Promise\u003cObjectId | undefined\u003e\n```\n\n### createOrUpdate\n\nEither creates a new document or updates an existing one, depending on the presence of **term** in the collection.\n\n```Typescript\nconst createOrUpdate = async \u003cT\u003e(\n  collectionName: string,\n  term: object,\n  value: OptionalId\u003cT\u003e\n): Promise\u003cT | undefined\u003e\n```\n\n### getAll\n\nFetches all the documents in a collection.\n\n```Typescript\nconst getAll = async \u003cT\u003e(\n   collectionName: string\n): Promise\u003cArray\u003cT\u003e\u003e\n```\n\n### getBy\n\nFetches documents in collection matching **term**. Items can be sorted using **sort**; a limited number of documents can be returned by specifying **count**.\n\n```Typescript\nconst getBy = async \u003cT\u003e(\n  collectionName: string,\n  term: object,\n  sort: object,\n  count?: number\n): Promise\u003cArray\u003cT\u003e\u003e\n```\n\n### clearAndCreateMany\n\nRemoves all documents matching **term** in collection, then inserts **values** in the collection.\n\n```Typescript\nconst clearAndCreateMany = async \u003cT\u003e(\n  collectionName: string,\n  term: object,\n  values: Array\u003cOptionalId\u003cT\u003e\u003e\n): Promise\u003cboolean\u003e\n```\n\n### clearAllAndCreateMany\n\nRemoves all documents in collection, then inserts **values** in the collection.\n\n```Typescript\nconst clearAllAndCreateMany = async \u003cT\u003e(\n  collectionName: string,\n  values: Array\u003cOptionalId\u003cT\u003e\u003e\n): Promise\u003cboolean\u003e\n```\n\n### remove\n\nRemoves the document matching **term** from the collection.\n\n```Typescript\nconst remove = async \u003cT\u003e(\n  collectionName: string,\n  term: object\n): Promise\u003cboolean\u003e\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpb06%2Fmongodb-generic-dal","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjpb06%2Fmongodb-generic-dal","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjpb06%2Fmongodb-generic-dal/lists"}