jmrate
v1.0.1
Published
An easy way to create a new database with collections in MongoDB
Downloads
6
Maintainers
Readme
jmrate migrator for MongoDB
An easy way to create a new database with collections in MongoDB
Installation
Installation is done using the npm install:
$ npm i jmrate --save-dev
Simple usage:
const MongoMigrate = require('jmrate').MongoMigrate; // import jmrate into your file
/*
// Write the information, which you want to import
// Create the object with parameters:
1. name: string;
// Add validation if you need
2. additionalProperties: boolean; (optional)
3. required: string array; (optional)
4. properties: object of properties; (optional)
// Your data
5. items: mongo items array; (optional)
*/
const articles = {
name: 'articles',
// you can add validation
required: ['title', 'description'], // or string, if it's only one required property
properties: {
title: {
bsonType: 'string',
description: 'must be a string and is required'
},
description: {
bsonType: 'string',
description: 'must be a string and is not required'
},
comments: {
bsonType: 'array',
description: 'must be an array'
},
},
items: [
{
title: 'Article 1',
description: 'Short description of the Article 1',
body: 'Short information of the Article 1',
comments: [],
},
{
title: 'Article 2',
description: 'Short description of the Article 2',
body: 'Short information of the Article 2',
},
],
};
/* create the object with parameters:
1. mongoUrl: string;
2. collections (it can be an object or array - for many. For further convenience create
new files with your collections);
3. noDrop: boolean (optional parameter: true - if you want to add your data without
dropping a database);
4. notifications: boolean (optional parameter: true - if you want to enable the
notificator);
*/
const migrate = {
mongoUrl: 'mongodb://localhost:27017/newsDb',
collections: articles,
noDrop: true,
notifications: true,
};
new MongoMigrate(migrate);
For TypeScript or ES7+.
index file
import { OurFirstCollection } from './_first';
import { OurSecondCollection } from './_second';
import { MongoMigrate } from 'jmrate';
import { IMongoMigrate } from 'jmrate';
const COLLECTIONS = [
OurFirstCollection,
OurSecondCollection,
];
const migrate: IMongoMigrate = {
mongoUrl: 'mongodb://localhost:27017/newsDb',
collections: COLLECTIONS,
noDrop: true,
notifications: true,
};
new MongoMigrate(migrate);
collection file
import { MongoContent } from 'jmrate';
@MongoContent({
name: 'first',
additionalProperties: false,
required: ['_id', 'title', 'description'],
properties: {
_id: {
bsonType: 'objectId',
description: 'must be a string and is required',
},
title: {
bsonType: 'string',
description: 'must be a string and is required',
},
description: {
bsonType: 'string',
description: 'must be a string and is not required',
},
comments: {
bsonType: 'array',
description: 'must be an array',
},
},
items: [
{
title: 'Article 1',
description: 'Short description of the Article 1',
comments: [],
},
{
title: 'Article 2',
description: 'Short description of the Article 2',
comments: [],
},
{
title: 'Article 3',
description: 'Short description of the Article 3',
comments: [],
},
],
})
export class OurFirstCollection { }