Skip to content

Commit

Permalink
Merge d6e4b07 into bd11229
Browse files Browse the repository at this point in the history
  • Loading branch information
okv committed Dec 30, 2019
2 parents bd11229 + d6e4b07 commit b84510c
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 37 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ All executed migrations names will be stored at `_migrations` collection in the
current database. Object with following properties will be passed to `migrate`
and `rollback` functions:

*Please note* that mainstream mongodb adapter version (>= 1.x) requires
east >= 1.x, for using adapter with older east versions (prior to 1.x) please
use mongodb adapter version < 1.x.

* `db` - instance of [mongodb native db](http://mongodb.github.io/node-mongodb-native/api-generated/db.html)
* `dropIndexIfExists` function(collection, index, callback) - helper function
which can be used for dropping index in safe way (contrasting to
Expand Down
53 changes: 21 additions & 32 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,47 +37,36 @@ Adapter.prototype._setDbAndClient = function(dbOrClient) {
}
};

Adapter.prototype.connect = function(callback) {
var self = this;

MongoClient.connect(
self.params.url,
self.params.options || {},
function(err, dbOrClient) {
if (err) {
return callback(err);
}

self._setDbAndClient(dbOrClient);

self.collection = self.db.collection('_migrations');

callback(null, {
db: self.db,
dropIndexIfExists: self.helpers.dropIndexIfExists
});
}
);
Adapter.prototype.connect = function() {
return Promise.resolve()
.then(() => MongoClient.connect(this.params.url, this.params.options))
.then((dbOrClient) => {
this._setDbAndClient(dbOrClient);
this.collection = this.db.collection('_migrations');

return {
db: this.db,
dropIndexIfExists: this.helpers.dropIndexIfExists
};
});
};

Adapter.prototype.disconnect = function(callback) {
Adapter.prototype.disconnect = function() {
if (this.client) {
this.client.close(callback);
return this.client.close();
} else if (this.db) {
this.db.close(callback);
return this.db.close();
} else {
callback();
return Promise.resolve();
}
};

Adapter.prototype.getExecutedMigrationNames = function(callback) {
this.collection.find({}).toArray(function(err, docs) {
if (err) {
return callback(err);
}

callback(null, docs.map(function(doc) { return doc._id; }));
});
return Promise.resolve()
.then(() => this.collection.find({}).toArray())
.then((docs) => {
return docs.map((doc) => doc._id);
});
};

Adapter.prototype.markExecuted = function(name, callback) {
Expand Down
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@
},
"dependencies": {
"mongodb-uri": "0.9.7"
},
"engines": {
"node": ">=4.0.0"
}
}
5 changes: 0 additions & 5 deletions test/utils/createAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ module.exports = () => {

const adapter = new Adapter(defaultParams);

adapter.connect = promisify(adapter.connect.bind(adapter));
adapter.disconnect = promisify(adapter.disconnect.bind(adapter));
adapter.getExecutedMigrationNames = promisify(
adapter.getExecutedMigrationNames.bind(adapter)
);
adapter.helpers.dropIndexIfExists = promisify(
adapter.helpers.dropIndexIfExists
);
Expand Down

0 comments on commit b84510c

Please sign in to comment.