Skip to content

Commit

Permalink
Merge 516fee2 into 00f21b6
Browse files Browse the repository at this point in the history
  • Loading branch information
okv committed Jan 21, 2020
2 parents 00f21b6 + 516fee2 commit 4bc1b17
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 20 deletions.
2 changes: 1 addition & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
node_modules
lib/migrationTemplate.js
lib/migrationTemplates/
5 changes: 2 additions & 3 deletions .nycrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
"testUtils/**",
"tap-snapshots/**",
"coverage/**",
"migrations/**",
"lib/migrationTemplate.js"
"migrations/**"
]
}
}
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,32 @@ which can be used for dropping index in safe way (contrasting to
function returns promise and can be used that way instead of providing
callback.

east mongo package also provides following migration templates:

* lib/migrationTemplates/promises.js - default migration template, uses
built-in `Promise` in `migrate`, `rollback` functions.
* lib/migrationTemplates/async.js - migration template that uses async
functions to describe `migrate`, `rollback` functions.

Default migration template will be used if `template` is not set. To get path
of another template `require.resolve` could be used, e.g. at `.eastrc`:

```js
module.exports = {
template: require.resolve('east-mongo/lib/migrationTemplates/async.js')
}
```

[![Npm version](https://img.shields.io/npm/v/east-mongo.svg)](https://www.npmjs.org/package/east-mongo)
[![Build Status](https://travis-ci.org/okv/east-mongo.svg?branch=master)](https://travis-ci.org/okv/east-mongo)
[![Coverage Status](https://coveralls.io/repos/github/okv/east-mongo/badge.svg?branch=master)](https://coveralls.io/github/okv/east-mongo?branch=master)
[![Known Vulnerabilities](https://snyk.io/test/npm/east-mongo/badge.svg)](https://snyk.io/test/npm/east-mongo)


## Node.js compatibility

east mongo requires node.js >= 4 to work.

## Installation

mongodb adapter requires `mongodb` package as peer dependency (versions 2.x and
Expand Down
2 changes: 1 addition & 1 deletion lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function Adapter(params) {
}

Adapter.prototype.getTemplatePath = function getTemplatePath() {
return path.join(__dirname, 'migrationTemplate.js');
return path.join(__dirname, 'migrationTemplates', 'promises.js');
};

// mongodb driver 2.x returns `db`, 3.x returns `client` as a result of
Expand Down
10 changes: 0 additions & 10 deletions lib/migrationTemplate.js

This file was deleted.

6 changes: 6 additions & 0 deletions lib/migrationTemplates/async.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

exports.migrate = async ({db}) => {
};

exports.rollback = async ({db}) => {
};
12 changes: 12 additions & 0 deletions lib/migrationTemplates/promises.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

exports.migrate = function(params) {
const db = params.db;

return Promise.resolve();
};

exports.rollback = function(params) {
const db = params.db;

return Promise.resolve();
};
6 changes: 6 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"devDependencies": {
"blue-tape": "1.0.0",
"coveralls": "3.0.9",
"es6-promisify": "6.0.2",
"eslint": "4.19.1",
"eslint-config-airbnb-base": "12.1.0",
"eslint-plugin-import": "2.18.2",
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/getTemplatePath/withSuitableParams.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ test(

assert.equal(
templatePath,
'../../../lib/migrationTemplate.js',
'../../../lib/migrationTemplates/promises.js',
'should return path to migration template'
);

Expand Down
68 changes: 68 additions & 0 deletions test/lib/migrationTemplates.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
'use strict';

const testUtils = require('../utils');
const pathUtils = require('path');
const fs = require('fs');
const promisify = require('es6-promisify').promisify;

const test = testUtils.test;

test('migrationTemplates', (assert) => {
const migrationTemplatesPath = pathUtils.resolve(
__dirname,
'../../lib/migrationTemplates'
);
const tryMigrationTemplate = (filename) => {
return Promise.resolve()
// eslint-disable-next-line import/no-dynamic-require
.then(() => require(pathUtils.join(migrationTemplatesPath, filename)))
.then((migration) => {
return Promise.all([
migration.migrate({}),
migration.rollback({})
]);
});
};
const readdir = promisify(fs.readdir);
const nodeMajor = Number(process.versions.node.split('.')[0]);

return Promise.resolve()
.then(() => readdir(migrationTemplatesPath))
.then((filenames) => {
assert.equal(
filenames.indexOf('promises.js') !== -1,
true,
'should contain promises migration template'
);
assert.equal(
filenames.indexOf('async.js') !== -1,
true,
'should contain promises migration template'
);
assert.equal(
filenames.length,
2,
'should contain only 2 templates'
);

if (nodeMajor >= 8) {
return Promise.all([
tryMigrationTemplate('promises.js'),
tryMigrationTemplate('async.js')
]);
} else {
return Promise.all([
tryMigrationTemplate('promises.js')
]);
}
})
.then(() => {
if (nodeMajor >= 8) {
assert.pass('promises template should be requireble');
assert.pass('async template should be requireble');
} else {
assert.pass('promises template should be requireble');
assert.skip('do not require async template on nodejs < 8');
}
});
});
8 changes: 4 additions & 4 deletions test/lib/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ const packageJson = require('../../package.json');

const test = testUtils.test;

test('module', (t) => {
t.equal(libModule, Adapter, 'should export adapter class');
test('module', (assert) => {
assert.equal(libModule, Adapter, 'should export adapter class');

t.equal(
assert.equal(
packageJson.main,
'lib/index.js',
'should be set as main in package.json'
);

t.end();
assert.end();
});

0 comments on commit 4bc1b17

Please sign in to comment.