Mongodb ORM connector based on mongoose and typegoose
npm install @biorate/mongodbMongodb ORM connector based on mongoose and typegoose
``ts
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
}
// Bind dependencies
container.bind
container.bind
container.bind
// Configure
container.get
MongoDB: [
{
name: 'connection',
host: 'mongodb://localhost:27017/',
options: {
useNewUrlParser: true,
useUnifiedTopology: true,
dbName: 'test',
},
},
],
});
(async () => {
const root = container.get
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,
// }
})();
``
- Documentation can be found here - docs.
See the CHANGELOG
Copyright (c) 2021-present Leonid Levkin (llevkin)