@biorate/mongodb
TypeScript icon, indicating that this package has built-in type declarations

1.65.4 • Public • Published

Mongodb

Mongodb ORM connector based on mongoose and typegoose

Examples:

import { inject, container, Types, Core } from '@biorate/inversion';
import { IConfig, Config } from '@biorate/config';
import {
  Severity,
  modelOptions,
  Prop,
  MongoDBConnector,
  IMongoDBConnector,
  model,
  ReturnModelType,
} from '@biorate/mongodb';

// Define models
@modelOptions({
  options: {
    allowMixed: Severity.ALLOW,
  },
  schemaOptions: { collection: 'test', versionKey: false },
})
export class TestModel {
  @Prop()
  firstName: string;

  @Prop()
  lastName: string;

  @Prop()
  age: number;
}

// Define root
export class Root extends Core() {
  @inject(MongoDBConnector) public connector: IMongoDBConnector;
  @model(TestModel) public test: ReturnModelType<typeof TestModel>;
}

// Bind dependencies
container.bind<IConfig>(Types.Config).to(Config).inSingletonScope();
container.bind<IMongoDBConnector>(MongoDBConnector).toSelf().inSingletonScope();
container.bind<Root>(Root).toSelf().inSingletonScope();

// Configure
container.get<IConfig>(Types.Config).merge({
  MongoDB: [
    {
      name: 'connection',
      host: 'mongodb://localhost:27017/',
      options: {
        useNewUrlParser: true,
        useUnifiedTopology: true,
        dbName: 'test',
      },
    },
  ],
});

(async () => {
  const root = container.get<Root>(Root);
  await root.$run();
  await root.connector.connection().dropDatabase();

  const connection = root.connector.connection('connection'); // Get connection instance
  console.log(connection);

  await new root.test({
    firstName: 'Vasya',
    lastName: 'Pupkin',
    age: 36,
  }).save(); // insert data into test collection

  // Get data from database
  const data = await root.test.find({ firstName: 'Vasya' }, { _id: 0 });
  console.log(data); // {
                     //   firstName: 'Vasya',
                     //   lastName: 'Pupkin',
                     //   age: 36,
                     // }
})();

Learn

  • Documentation can be found here - docs.

Release History

See the CHANGELOG

License

MIT

Copyright (c) 2021-present Leonid Levkin (llevkin)

Readme

Keywords

Package Sidebar

Install

npm i @biorate/mongodb

Weekly Downloads

5

Version

1.65.4

License

MIT

Unpacked Size

208 kB

Total Files

35

Last publish

Collaborators

  • llevkin