https://github.com/vpavel04/db-repository-mongo
mongoDB implementation for db-repository
https://github.com/vpavel04/db-repository-mongo
database mongodb repository-pattern typescript
Last synced: 3 months ago
JSON representation
mongoDB implementation for db-repository
- Host: GitHub
- URL: https://github.com/vpavel04/db-repository-mongo
- Owner: vpavel04
- License: mit
- Created: 2019-07-25T07:07:16.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2021-05-09T01:53:29.000Z (about 5 years ago)
- Last Synced: 2025-10-06T15:25:22.411Z (9 months ago)
- Topics: database, mongodb, repository-pattern, typescript
- Language: TypeScript
- Homepage:
- Size: 28.3 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Db-Repository-Mongo
Db-Repository-Mongo is a mongoDb implementation for [Db-Repository](https://github.com/vpavel04/db-repository) in TypeScript
# How to use it
```
npm install --save 'db-repository';
npm install --save 'db-repository-mongo';
```
Sample code
```
// store mongoDbUri in MONGODB_PATH env variable.
process.env.MONGODB_PATH = 'mongoDb uri';
import * as db from 'db-repository';
import * as mongoDbRepo from 'db-repository-mongo';
db.use(mongoDbRepo.init());
// define new type that works with IRepository
interface DbTest extends db.IDbObject {
test: string;
}
const testFn = async () => {
const dbObject: DbTest = {
test: 'text1'
}
// create repository
const testRepo: db.IRepository = db.repo({ table: 'test' });
// add object
await testRepo.add(dbObject);
// query all
const allObjects = await testRepo.list(db.query().all());
// query by id
const objectById = await testRepo.list(db.query().byId(dbObject._id));
// update object
dbObject.test = 'text2';
const nUpdated = await testRepo.update(dbObject);
// delete objects
const nDeleted = await testRepo.remove(db.query().all());
};
testFn();
```