1.1.3 • Published 3 years ago

db-repository-mongo v1.1.3

Weekly downloads
4
License
MIT
Repository
github
Last release
3 years ago

Db-Repository-Mongo

Db-Repository-Mongo is a mongoDb implementation for 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 = <DbTest>{
      test: 'text1'
  }

  // create repository
  const testRepo: db.IRepository<DbTest> = db.repo<DbTest>({ 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();
1.1.3

3 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago