1.0.0 • Published 5 years ago

mission.mongodb v1.0.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

mission.mongodb

Getting Started

import { BaseMongo, Factory, MongoCollection, MongoDbRepo, SchemaRepo, Validator } from 'mission.mongodb';

SchemaRepo.add('test-schema', {
    $schema: 'http://json-schema.org/draft-07/schema#',
    title: 'Product set',
    type: 'object',
    properties: {
        id: {
            description: 'The unique identifier for a product',
            type: 'number',
        },
        name: {
            type: 'string',
        },
        age: {
            type: 'number',
            exclusiveMinimum: 0,
        },
        place: {
            type: 'string',
        },
    },
    required: ['id', 'name', 'age'],
});

export class StartUp {
    public async start(): Promise<void> {
        await MongoDbRepo.connect('mongodb://localhost:27017', { useNewUrlParser: true });
        const data = { id: 1, name: 'Natarajan Ganapathi', age: 35, place: 'Namakkal' };
        const valid = await Validator.validate('test-schema', data);
        if (valid.errors) {
            // tslint:disable-next-line:no-console
            console.error(valid.errors);
            process.exit(1);
        }
        const res = await Factory.getBo<Table>(Table).insert(data);
        await MongoDbRepo.close();
        // tslint:disable-next-line:no-console
        console.log(res);
    }
}

@MongoCollection('mio', 'info')
export class Table extends BaseMongo<any> {
    public async insert(data: any) {
        const info = await this.collection();
        return await info.insertOne(data);
    }
}

new StartUp().start().catch((err) => {
    // tslint:disable-next-line:no-console
    console.error(err);
});

Prerequisites

Installing

Credits

License