migrate-mongo
Advanced tools
Comparing version
@@ -6,3 +6,3 @@ const _ = require("lodash"); | ||
const status = require("./status"); | ||
const configFile = require("../env/configFile"); | ||
const config = require("../env/config"); | ||
const migrationsDir = require("../env/migrationsDir"); | ||
@@ -34,7 +34,6 @@ const hasCallback = require('../utils/has-callback'); | ||
} | ||
const config = await configFile.read(); | ||
const collectionName = config.changelogCollectionName; | ||
const collection = db.collection(collectionName); | ||
const { changelogCollectionName } = await config.read(); | ||
const changelogCollection = db.collection(changelogCollectionName); | ||
try { | ||
await collection.deleteOne({ fileName: lastAppliedItem.fileName }); | ||
await changelogCollection.deleteOne({ fileName: lastAppliedItem.fileName }); | ||
downgraded.push(lastAppliedItem.fileName); | ||
@@ -41,0 +40,0 @@ } catch (err) { |
@@ -5,3 +5,3 @@ const fs = require("fs-extra"); | ||
const migrationsDir = require("../env/migrationsDir"); | ||
const configFile = require("../env/configFile"); | ||
const config = require("../env/config"); | ||
@@ -12,3 +12,3 @@ function copySampleConfigFile() { | ||
process.cwd(), | ||
configFile.DEFAULT_CONFIG_FILE_NAME | ||
config.DEFAULT_CONFIG_FILE_NAME | ||
); | ||
@@ -24,5 +24,5 @@ return fs.copy(source, destination); | ||
await migrationsDir.shouldNotExist(); | ||
await configFile.shouldNotExist(); | ||
await config.shouldNotExist(); | ||
await copySampleConfigFile(); | ||
return createMigrationsDirectory(); | ||
}; |
const { find } = require("lodash"); | ||
const migrationsDir = require("../env/migrationsDir"); | ||
const configFile = require("../env/configFile"); | ||
const config = require("../env/config"); | ||
module.exports = async db => { | ||
await migrationsDir.shouldExist(); | ||
await configFile.shouldExist(); | ||
await config.shouldExist(); | ||
const fileNames = await migrationsDir.getFileNames(); | ||
const config = await configFile.read(); | ||
const collectionName = config.changelogCollectionName; | ||
const collection = db.collection(collectionName); | ||
const changelog = await collection.find({}).toArray(); | ||
const { changelogCollectionName } = await config.read(); | ||
const changelogCollection = db.collection(changelogCollectionName); | ||
const changelog = await changelogCollection.find({}).toArray(); | ||
@@ -15,0 +14,0 @@ const statusTable = fileNames.map(fileName => { |
@@ -7,3 +7,3 @@ const _ = require("lodash"); | ||
const status = require("./status"); | ||
const configFile = require("../env/configFile"); | ||
const config = require("../env/config"); | ||
const migrationsDir = require("../env/migrationsDir"); | ||
@@ -37,5 +37,4 @@ const hasCallback = require('../utils/has-callback'); | ||
const config = await configFile.read(); | ||
const collectionName = config.changelogCollectionName; | ||
const collection = db.collection(collectionName); | ||
const { changelogCollectionName } = await config.read(); | ||
const changelogCollection = db.collection(changelogCollectionName); | ||
@@ -46,3 +45,3 @@ const { fileName } = item; | ||
try { | ||
await collection.insertOne({ fileName, appliedAt }); | ||
await changelogCollection.insertOne({ fileName, appliedAt }); | ||
} catch (err) { | ||
@@ -49,0 +48,0 @@ throw new Error(`Could not update changelog: ${err.message}`); |
const { MongoClient } = require("mongodb"); | ||
const _ = require("lodash"); | ||
const configFile = require("./configFile"); | ||
const config = require("./config"); | ||
module.exports = { | ||
async connect() { | ||
const config = await configFile.read(); | ||
const url = _.get(config, "mongodb.url"); | ||
const databaseName = _.get(config, "mongodb.databaseName"); | ||
const options = _.get(config, "mongodb.options"); | ||
const configContent = await config.read(); | ||
const url = _.get(configContent, "mongodb.url"); | ||
const databaseName = _.get(configContent, "mongodb.databaseName"); | ||
const options = _.get(configContent, "mongodb.options"); | ||
@@ -16,9 +16,2 @@ if (!url) { | ||
if (!databaseName) { | ||
throw new Error( | ||
"No `databaseName` defined in config file! This is required since migrate-mongo v3. " + | ||
"See https://github.com/seppevs/migrate-mongo#initialize-a-new-project" | ||
); | ||
} | ||
const client = await MongoClient.connect( | ||
@@ -25,0 +18,0 @@ url, |
const fs = require("fs-extra"); | ||
const path = require("path"); | ||
const configFile = require("./configFile"); | ||
const config = require("./config"); | ||
@@ -11,4 +11,4 @@ const DEFAULT_MIGRATIONS_DIR_NAME = "migrations"; | ||
try { | ||
const config = await configFile.read(); | ||
migrationsDir = config.migrationsDir; // eslint-disable-line | ||
const configContent = await config.read(); | ||
migrationsDir = configContent.migrationsDir; // eslint-disable-line | ||
// if config file doesn't have migrationsDir key, assume default 'migrations' dir | ||
@@ -37,4 +37,4 @@ if (!migrationsDir) { | ||
try { | ||
const config = await configFile.read(); | ||
migrationFileExtension = config.migrationFileExtension || DEFAULT_MIGRATION_EXT; | ||
const configContent = await config.read(); | ||
migrationFileExtension = configContent.migrationFileExtension || DEFAULT_MIGRATION_EXT; | ||
} catch (err) { | ||
@@ -44,3 +44,3 @@ // config file could not be read, assume default extension | ||
} | ||
if (migrationFileExtension && !migrationFileExtension.startsWith('.')) { | ||
@@ -47,0 +47,0 @@ throw new Error('migrationFileExtension must start with dot'); |
@@ -7,3 +7,3 @@ const init = require("./actions/init"); | ||
const database = require("./env/database"); | ||
const config = require("./env/configFile"); | ||
const config = require("./env/config"); | ||
@@ -10,0 +10,0 @@ module.exports = { |
{ | ||
"name": "migrate-mongo", | ||
"version": "8.0.0", | ||
"version": "8.1.0", | ||
"description": "A database migration tool for MongoDB in Node", | ||
@@ -5,0 +5,0 @@ "main": "lib/migrate-mongo.js", |
@@ -1,9 +0,10 @@ | ||
# migrate-mongo | ||
A database migration tool for MongoDB in Node. | ||
<p align="center"> | ||
<img src="/migrate-mongo-logo.png" alt="migrate-mongo database migration tool for Node.js"/> | ||
✨ [](https://travis-ci.org/seppevs/migrate-mongo) [](https://coveralls.io/r/seppevs/migrate-mongo) [](https://www.npmjs.org/package/migrate-mongo) [](https://www.npmjs.org/package/migrate-mongo) [](https://david-dm.org/seppevs/migrate-mongo) [](https://snyk.io/test/github/seppevs/migrate-mongo) ✨ | ||
[](https://travis-ci.org/seppevs/migrate-mongo) [](https://coveralls.io/r/seppevs/migrate-mongo) [](https://www.npmjs.org/package/migrate-mongo) [](https://www.npmjs.org/package/migrate-mongo) [](https://david-dm.org/seppevs/migrate-mongo) [](https://snyk.io/test/github/seppevs/migrate-mongo) | ||
<a href='https://ko-fi.com/D1D5O6O6' target='_blank'><img height='24' style='border:0px;height:36px;' src='https://az743702.vo.msecnd.net/cdn/kofi2.png?v=0' border='0' alt='Buy Me a Coffee at ko-fi.com' /></a> | ||
migrate-mongo is a database migration tool for MongoDB running in Node.js | ||
</p> | ||
## Installation | ||
@@ -84,2 +85,7 @@ ````bash | ||
Alternatively, you can also encode your database name in the url (and leave out the `databaseName` property): | ||
```` | ||
url: "mongodb://localhost:27017/YOURDATABASE", | ||
```` | ||
### Creating a new migration script | ||
@@ -297,2 +303,9 @@ To create a new database migration script, just run the ````migrate-mongo create [description]```` command. | ||
### Version | ||
To know which version of migrate-mongo you're running, just pass the `version` option: | ||
````bash | ||
$ migrate-mongo version | ||
```` | ||
## API Usage | ||
@@ -351,2 +364,27 @@ | ||
### `config.set(yourConfigObject) → Promise<JSON>` | ||
Tell migrate-mongo NOT to use the `migrate-mongo-config.js` file, but instead use the config object passed as the first argument of this function. | ||
When using this feature, please do this at the very beginning of your program. | ||
Example: | ||
```javascript | ||
const { config, up } = require('../lib/migrate-mongo'); | ||
const myConfig = { | ||
mongodb: { | ||
url: "mongodb://localhost:27017/mydatabase", | ||
options: { useNewUrlParser: true } | ||
}, | ||
migrationsDir: "migrations", | ||
changelogCollectionName: "changelog", | ||
migrationFileExtension: ".js" | ||
}; | ||
config.set(myConfig); | ||
// then, use the API as you normally would, eg: | ||
await up(); | ||
``` | ||
### `up(MongoDb, MongoClient) → Promise<Array<fileName>>` | ||
@@ -353,0 +391,0 @@ |
@@ -10,3 +10,3 @@ const { expect } = require("chai"); | ||
let migrationsDir; | ||
let configFile; | ||
let config; | ||
let fs; | ||
@@ -22,3 +22,3 @@ | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -37,7 +37,7 @@ shouldExist: sinon.stub().returns(Promise.resolve()), | ||
migrationsDir = mockMigrationsDir(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
fs = mockFs(); | ||
create = proxyquire("../../lib/actions/create", { | ||
"../env/migrationsDir": migrationsDir, | ||
"../env/configFile": configFile, | ||
"../env/config": config, | ||
"fs-extra": fs | ||
@@ -73,5 +73,5 @@ }); | ||
it("should not be necessary to have an config file present", async () => { | ||
it("should not be necessary to have an config present", async () => { | ||
await create("my_description"); | ||
expect(configFile.shouldExist.called).to.equal(false); | ||
expect(config.shouldExist.called).to.equal(false); | ||
}); | ||
@@ -78,0 +78,0 @@ |
@@ -9,3 +9,3 @@ const { expect } = require("chai"); | ||
let status; | ||
let configFile; | ||
let config; | ||
let migrationsDir; | ||
@@ -32,3 +32,3 @@ let db; | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -74,3 +74,3 @@ shouldExist: sinon.stub().returns(Promise.resolve()), | ||
"./status": status, | ||
"../env/configFile": configFile, | ||
"../env/config": config, | ||
"../env/migrationsDir": migrationsDir | ||
@@ -85,3 +85,3 @@ }); | ||
status = mockStatus(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
migrationsDir = mockMigrationsDir(); | ||
@@ -88,0 +88,0 @@ db = mockDb(); |
@@ -9,3 +9,3 @@ const { expect } = require("chai"); | ||
let migrationsDir; | ||
let configFile; | ||
let config; | ||
let fs; | ||
@@ -19,3 +19,3 @@ | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -35,7 +35,7 @@ shouldNotExist: sinon.stub().returns(Promise.resolve()) | ||
migrationsDir = mockMigrationsDir(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
fs = mockFs(); | ||
init = proxyquire("../../lib/actions/init", { | ||
"../env/migrationsDir": migrationsDir, | ||
"../env/configFile": configFile, | ||
"../env/config": config, | ||
"fs-extra": fs | ||
@@ -65,7 +65,7 @@ }); | ||
await init(); | ||
expect(configFile.shouldNotExist.called).to.equal(true); | ||
expect(config.shouldNotExist.called).to.equal(true); | ||
}); | ||
it("should not continue and yield an error if the config file already exists", async () => { | ||
configFile.shouldNotExist.returns( | ||
config.shouldNotExist.returns( | ||
Promise.resolve(new Error("Config exists")) | ||
@@ -72,0 +72,0 @@ ); |
@@ -9,3 +9,3 @@ const { expect } = require("chai"); | ||
let migrationsDir; | ||
let configFile; | ||
let config; | ||
let fs; | ||
@@ -30,3 +30,3 @@ let db; | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -77,3 +77,3 @@ shouldExist: sinon.stub().returns(Promise.resolve()), | ||
migrationsDir = mockMigrationsDir(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
fs = mockFs(); | ||
@@ -83,3 +83,3 @@ db = mockDb(); | ||
"../env/migrationsDir": migrationsDir, | ||
"../env/configFile": configFile, | ||
"../env/config": config, | ||
"fs-extra": fs | ||
@@ -108,7 +108,7 @@ }); | ||
await status(db); | ||
expect(configFile.shouldExist.called).to.equal(true); | ||
expect(config.shouldExist.called).to.equal(true); | ||
}); | ||
it("should yield an error when config file does not exist", async () => { | ||
configFile.shouldExist.returns( | ||
config.shouldExist.returns( | ||
Promise.reject(new Error("config file does not exist")) | ||
@@ -115,0 +115,0 @@ ); |
@@ -9,3 +9,3 @@ const { expect } = require("chai"); | ||
let status; | ||
let configFile; | ||
let config; | ||
let migrationsDir; | ||
@@ -42,3 +42,3 @@ let db; | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -92,3 +92,3 @@ shouldExist: sinon.stub().returns(Promise.resolve()), | ||
"./status": status, | ||
"../env/configFile": configFile, | ||
"../env/config": config, | ||
"../env/migrationsDir": migrationsDir | ||
@@ -104,3 +104,3 @@ }); | ||
status = mockStatus(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
migrationsDir = mockMigrationsDir(); | ||
@@ -182,3 +182,2 @@ db = mockDb(); | ||
// TODO this test first also had a list of migrated files (on error), review ! | ||
it("should stop migrating when an error occurred and yield the error", async () => { | ||
@@ -185,0 +184,0 @@ secondPendingMigration.up.returns(Promise.reject(new Error("Nope"))); |
@@ -8,3 +8,3 @@ const { expect } = require("chai"); | ||
let database; | ||
let configFile; | ||
let config; | ||
let mongodb; | ||
@@ -33,3 +33,3 @@ let client; | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -51,7 +51,7 @@ read: sinon.stub().returns(configObj) | ||
client = mockClient(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
mongodb = mockMongodb(); | ||
database = proxyquire("../../lib/env/database", { | ||
"./configFile": configFile, | ||
"./config": config, | ||
mongodb | ||
@@ -92,15 +92,2 @@ }); | ||
it("should yield an error when no databaseName is defined in the config file", async () => { | ||
delete configObj.mongodb.databaseName; | ||
try { | ||
await database.connect(); | ||
expect.fail("Error was not thrown"); | ||
} catch (err) { | ||
expect(err.message).to.equal( | ||
"No `databaseName` defined in config file! This is required since migrate-mongo v3. " + | ||
"See https://github.com/seppevs/migrate-mongo#initialize-a-new-project" | ||
); | ||
} | ||
}); | ||
it("should yield an error when unable to connect", async () => { | ||
@@ -107,0 +94,0 @@ mongodb.MongoClient.connect.returns( |
@@ -10,3 +10,3 @@ const { expect } = require("chai"); | ||
let fs; | ||
let configFile; | ||
let config; | ||
@@ -20,3 +20,3 @@ function mockFs() { | ||
function mockConfigFile() { | ||
function mockConfig() { | ||
return { | ||
@@ -32,6 +32,6 @@ read: sinon.stub().returns({ | ||
fs = mockFs(); | ||
configFile = mockConfigFile(); | ||
config = mockConfig(); | ||
migrationsDir = proxyquire("../../lib/env/migrationsDir", { | ||
"fs-extra": fs, | ||
"./configFile": configFile | ||
"./config": config | ||
}); | ||
@@ -42,3 +42,3 @@ }); | ||
it("should use the configured relative migrations dir when a config file is available", async () => { | ||
configFile.read.returns({ | ||
config.read.returns({ | ||
migrationsDir: "custom-migrations-dir" | ||
@@ -52,3 +52,3 @@ }); | ||
it("should use the configured absolute migrations dir when a config file is available", async () => { | ||
configFile.read.returns({ | ||
config.read.returns({ | ||
migrationsDir: "/absolute/path/to/my/custom-migrations-dir" | ||
@@ -62,3 +62,3 @@ }); | ||
it("should use the default migrations directory when no migrationsDir is specified in the config file", async () => { | ||
configFile.read.returns({}); | ||
config.read.returns({}); | ||
expect(await migrationsDir.resolve()).to.equal( | ||
@@ -70,3 +70,3 @@ path.join(process.cwd(), "migrations") | ||
it("should use the default migrations directory when unable to read the config file", async () => { | ||
configFile.read.throws(new Error("Cannot read config file")); | ||
config.read.throws(new Error("Cannot read config file")); | ||
expect(await migrationsDir.resolve()).to.equal( | ||
@@ -128,3 +128,3 @@ path.join(process.cwd(), "migrations") | ||
it("should list only files with configured extension", async () => { | ||
configFile.read.returns({ | ||
config.read.returns({ | ||
migrationFileExtension: ".ts" | ||
@@ -166,3 +166,3 @@ }); | ||
it("should provide the value if specified", async () => { | ||
configFile.read.returns({ | ||
config.read.returns({ | ||
migrationFileExtension: ".ts" | ||
@@ -174,3 +174,3 @@ }); | ||
it("should error if the extension does not start with dot", async () => { | ||
configFile.read.returns({ | ||
config.read.returns({ | ||
migrationFileExtension: "js" | ||
@@ -186,3 +186,3 @@ }); | ||
it("should use the default if not specified", async() => { | ||
configFile.read.returns({ | ||
config.read.returns({ | ||
migrationFileExtension: undefined | ||
@@ -194,3 +194,3 @@ }); | ||
it("should use the default if config file not found", async() => { | ||
configFile.read.throws(); | ||
config.read.throws(); | ||
const ext = await migrationsDir.resolveMigrationFileExtension(); | ||
@@ -197,0 +197,0 @@ expect(ext).to.equal(".js"); |
URL strings
Supply chain riskPackage contains fragments of external URLs or IP addresses, which the package may be accessing at runtime.
Found 1 instance in 1 package
88953
15.33%34
3.03%1665
0.3%426
9.79%6
-14.29%