Skip to content

Commit

Permalink
Merge pull request #20 from zishone/dev
Browse files Browse the repository at this point in the history
mongover@1.3.2
  • Loading branch information
zishone committed Apr 17, 2020
2 parents fc8af22 + e3268a7 commit d1ad583
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 27 deletions.
71 changes: 55 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,27 @@ const { apply } = require('mongover');
const options = {
specPath: './mongover',
uri: 'mongodb://127.0.0.1:27017/',
seedOnly: false
dbs: [],
alias: [],
collections: [],
seedOnly: false,
migrateForce: false,
info: '_info',
socketTimeoutMS: 3600000,
};
```
| MongoverOptions | Type | Description | Default |
|--------------- |--------- |-------------------------------------------------------------------------------- |---------------------------- |
| uri | string | MongoDB Server connection string. | mongodb://127.0.0.1:27017/ |
| specPath | string | Path to Mongover Specification. | ./mongover |
| seedOnly | boolean | Specifies if mongover should only seed instead of migrating existing database. | false |
| MongoverOptions | Type | Description | Default |
|--------------- |--------- |------------------------------------------------------------------------------------------------------------------- |----------------------------------- |
| specPath | string | Path to Mongover Specification. | `./mongover` |
| uri | string | MongoDB Server connection string. | `mongodb://127.0.0.1:27017/` |
| dbs | string[] | Specifies which databases to apply. | `[] // all dbs in specPath` |
| alias | string[] | Specifies the aliases of the specified databases to apply, a database will use the alias corresponding to its index. | `[] // no aliases` |
| collections | string[] | Specifies which collections to apply. | `[] // all collections in specPath ` |
| seedOnly | boolean | Specifies if mongover should only seed instead of migrating existing database. | `false` |
| migrateForce | boolean | Specifies if mongover should migrate the database even if the specified version is lower or the same. | `false` |
| info | string | Specifies the collection name of the database information. | `_info` |
| socketTimeoutMS | number | Specifies how long a send or receive on a socket can take before timing out in milliseconds. | `3600000` |


#### 3. Apply Mongover Specification
```javascript
Expand Down Expand Up @@ -64,7 +77,7 @@ $ mongover <command> [<args>] [<options>]
**SYNOPSIS**

```shell
$ mongover extract [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-c <collectionName>[,...]]] [-f dir|-f json] [-e jsonl|-e json|-e no [-q "<query>"]]
$ mongover extract [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-c <collectionName>[,...]]] [-f dir|-f json] [-e jsonl|-e json|-e no [-q "<query>"]] [-i <infoCollection>] [--socketTimeoutMS <milliseconds>]
```
**ARGUMENTS**
Expand All @@ -85,14 +98,16 @@ $ mongover <command> [<args>] [<options>]
-q or --query specifies a filter which data to be exported from the MongoDB Server.
-i or --info specifies the collection name of the database information such as it's version. Defaults to '_info'.
-i or --info specifies the collection name of the database information. Defaults to '_info'.
--socketTimeoutMS specifies how long a send or receive on a socket can take before timing out in milliseconds. Defaults to '3600000'.
* **apply**: applies the current Mongover Specification to the MongoDB Server.
**SYNOPSIS**
```shell
$ mongover apply [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-a <alias>[,...]]] [-c <collectionName>[,...]] [-s]
$ mongover apply [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-a <alias>[,...]]] [-c <collectionName>[,...]] [-s] [-m] [-i <infoCollection>] [--socketTimeoutMS <milliseconds>]
```
**ARGUMENTS**
Expand All @@ -111,9 +126,11 @@ $ mongover <command> [<args>] [<options>]
-s or --seedOnly specifies if mongover should only seed the database instead of migrating it when it already exists.
-m or --migrateForce specifies if mongover should migrate the database even if the specified version is the same.
-m or --migrateForce specifies if mongover should migrate the database even if the specified version is lower or the same.
-i or --info specifies the collection name of the database information. Defaults to '_info'.
-i or --info specifies the collection name of the database information such as it's version. Defaults to '_info'.
--socketTimeoutMS specifies how long a send or receive on a socket can take before timing out in milliseconds. Defaults to '3600000'.
* **help**: outputs Mongover usage.
Expand All @@ -130,7 +147,7 @@ $ mongover <command> [<args>] [<options>]
.
├── dbName/ # Database Directory
│ ├── data/
│ │ ├── collectionName.jsonl # Export file to be upserted to dbName.collectionName (alternatively `json|js`)
│ │ ├── collectionName.jsonl # Export/Import file to be upserted to dbName.collectionName (alternatively `json|js`)
│ │ └── ...
│ ├── collections/
│ │ ├── collectionName.spec.json # Collection Migration/Seeding Specification file
Expand All @@ -143,7 +160,7 @@ $ mongover <command> [<args>] [<options>]
{
"seedOnly": false, // Specifies if existing database should be migrated or only seeded.
"migrateForce": false, // Specifies if mongover should migrate the database even if the specified version is the same.
"info": "_info", // Specifies the collection name of the database information such as it's version. Defaults to '_info'.
"info": "_info", // Specifies the collection name of the database information. Defaults to '_info'.
"dropFirst": false, // Specifies if existing database should be dropped before specification is applied.
"alias": "dbName", // Alias/Name the database specification will be deployed as.
"version": "1.0.0", // Specifies the version of the database, this will determine if the database needs to be migrated or not.
Expand Down Expand Up @@ -184,7 +201,7 @@ $ mongover <command> [<args>] [<options>]
.
├── dbName/ # Database Directory
│ ├── data/
│ │ ├── collectionName.jsonl # Export file to be upserted to dbName.collectionName (alternatively `json|js`)
│ │ ├── collectionName.jsonl # Export/Import file to be upserted to dbName.collectionName (alternatively `json|js`)
│ │ └── ...
│ └── db.spec.json # Database Migration/Seeding Specification file
└── ...
Expand All @@ -194,7 +211,7 @@ $ mongover <command> [<args>] [<options>]
{
"seedOnly": false, // Specifies if existing database should be migrated or only seeded.
"migrateForce": false, // Specifies if mongover should migrate the database even if the specified version is the same.
"info": "_info", // Specifies the collection name of the database information such as it's version. Defaults to '_info'.
"info": "_info", // Specifies the collection name of the database information. Defaults to '_info'.
"dropFirst": false, // Specifies if existing database should be dropped before specification is applied.
"alias": "dbName", // Alias/Name the database specification will be deployed as.
"version": "1.0.0", // Specifies the version of the database, this will determine if the database needs to be migrated or not.
Expand Down Expand Up @@ -226,7 +243,7 @@ $ mongover <command> [<args>] [<options>]
}
```
## Data Export File
## Data Export/Import File
* **jsonl**
```json
{"_id":{"$oid":"aaaaaaaaaaaaaaaaaaaaaaaa"},"fieldName": 1}
Expand Down Expand Up @@ -256,6 +273,28 @@ $ mongover <command> [<args>] [<options>]
];
```
### Multiple Export/Import Files Per Collection
To have multiple `json/js/jsonl` files in `dbName/data/` for one collection, follow this naming format:
```
collectionName.jsonl
collectionName.1.jsonl
collectionName.2.jsonl
collectionName.foo.jsonl
collectionName.bar.jsonl
collectionName.json
collectionName.1.json
collectionName.2.json
collectionName.foo.json
collectionName.bar.json
collectionName.js
collectionName.1.js
collectionName.2.js
collectionName.foo.js
collectionName.bar.js
```
## Authors
* **Zishran Julbert Garces**
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongover",
"version": "1.3.1",
"version": "1.3.2",
"description": "A MongoDB Server Database Migration Tool",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -63,6 +63,7 @@
"keywords": [
"mongover",
"mongodb",
"migrate",
"migration",
"versioning",
"database",
Expand Down
2 changes: 1 addition & 1 deletion src/cli/commands/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function apply(options: MongoverOptions): Promise<void> {
try {
logger.cli('Applying Mongover Specification: %s', options.specPath);
await coreApply(options);
logger.cli('Done applying Mongover Specification: %s', options.specPath);
logger.cli('Done applying Mongover Specification: %s', options.specPath!.replace(process.cwd(), '.'));
} catch (error) {
logger.cli('Error applying Mongover Specification: %O', error);
throw error;
Expand Down
6 changes: 5 additions & 1 deletion src/cli/commands/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ export async function extract(options: MongoverOptions): Promise<void> {
try {
logger.cli('Extracting Mongover Specification: %s', options.specPath);
options.specPath! = isAbsolute(options.specPath!) ? options.specPath! : join(process.cwd(), options.specPath!);
const client = await connectServer(options.uri!, { useNewUrlParser: true });
const client = await connectServer(options.uri!, {
useNewUrlParser: true,
useUnifiedTopology: true,
socketTimeoutMS: options.socketTimeoutMS,
});
for (const dbName of options.dbs!) {
logger.cli('--- Extracting Database: %s', dbName);
const db = client.db(dbName);
Expand Down
1 change: 1 addition & 0 deletions src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async function mongover(args: string[]) {
'dbs',
'collections',
'info',
'socketTimeoutMS',
],
boolean: [
'seedOnly',
Expand Down
2 changes: 2 additions & 0 deletions src/core/apply.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function apply(options: MongoverOptions = parseOptions({})): Promis
const client = await connectServer(options.uri!, {
useNewUrlParser: true,
useUnifiedTopology: true,
socketTimeoutMS: options.socketTimeoutMS,
});
for (const database of databases) {
if (options.dbs!.length === 0 || options.dbs!.includes(database.name)) {
Expand Down Expand Up @@ -94,6 +95,7 @@ export async function apply(options: MongoverOptions = parseOptions({})): Promis
}
}
}
logger.info('Done applying Mongover Specification: %s', options.specPath!.replace(process.cwd(), '.'));
await client.close();
} catch (error) {
logger.error('Error applying Mongover Specification to the Server: %O', error);
Expand Down
2 changes: 1 addition & 1 deletion src/core/import-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export async function importData(collection: Collection, dataSpec: DataSpec, dat
try {
const filenameArr = dataPath.split('.');
const fileType = filenameArr.pop();
if (filenameArr.pop() !== 'd') {
if (filenameArr.pop() !== 'd' || fileType !== 'ts') {
logger.info('Importing Data: %s', dataPath.replace(process.cwd(), '.'));
logger.cli('------- Importing Data: %s', dataPath.replace(process.cwd(), '.'));
switch (fileType) {
Expand Down
1 change: 1 addition & 0 deletions src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface MongoverOptions {
seedOnly?: boolean;
migrateForce?: boolean;
info?: string;
socketTimeoutMS?: number;
}

export interface DatabaseSpec {
Expand Down
14 changes: 8 additions & 6 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { join } from 'path';
import { DatabaseSpec, MongoverOptions } from '../types/types';

export const usage = `
Expand All @@ -18,7 +17,7 @@ Usage:
extract [<specPath>] [<options>]
- Extracts the Mongover Specification of an existing MongoDB Server and initializes a new Mongover Specification with it.
SYNOPSIS
$ mongover extract [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-c <collectionName>[,...]]] [-f dir|-f json] [-e jsonl|-e json|-e no [-q "<query>"]]
$ mongover extract [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-c <collectionName>[,...]]] [-f dir|-f json] [-e jsonl|-e json|-e no [-q "<query>"]] [-i <infoCollection>] [--socketTimeoutMS <milliseconds>]
ARGUMENTS
<specPath> path to mongover specification. Defaults to current working directory.
OPTIONS
Expand All @@ -28,12 +27,13 @@ Usage:
-f or --format specifies Mongover Specification format, choose between 'dir' and 'json'. Defaults to 'dir'.
-e or --export specifies if data from the MongoDB Server should also be exported, choose between 'jsonl', 'json' and 'no'. Defaults to 'no'.
-q or --query specifies a filter which data to be exported from the MongoDB Server.
-i or --info specifies the collection name of the database information such as it's version. Defaults to '_info'.
-i or --info specifies the collection name of the database information. Defaults to '_info'.
--socketTimeoutMS specifies how long a send or receive on a socket can take before timing out in milliseconds. Defaults to '3600000'.
apply [<specPath>] [<options>]
- applies the current Mongover Specification to the MongoDB Server.
SYNOPSIS
$ mongover apply [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-a <alias>[,...]]] [-c <collectionName>[,...]] [-s]
$ mongover apply [<specPath>] [-u "<uri>"] [-d <dbName>[,...] [-a <alias>[,...]]] [-c <collectionName>[,...]] [-s] [-m] [-i <infoCollection>] [--socketTimeoutMS <milliseconds>]
ARGUMENTS
<specPath> path to mongover specification. Defaults to current working directory.
OPTIONS
Expand All @@ -42,8 +42,9 @@ Usage:
-a or --alias specifies the aliases of the specified databases to apply, a database will use the alias corresponding to its index separated by commas.
-c or --collections specifies which collections to apply. Defaults to all collections in specified databases.
-s or --seedOnly specifies if mongover should only seed the database instead of migrating it when it already exists.
-m or --migrateForce specifies if mongover should migrate the database even if the specified version is the same.
-i or --info specifies the collection name of the database information such as it's version. Defaults to '_info'.
-m or --migrateForce specifies if mongover should migrate the database even if the specified version is lower or the same.
-i or --info specifies the collection name of the database information. Defaults to '_info'.
--socketTimeoutMS specifies how long a send or receive on a socket can take before timing out in milliseconds.
`.trim();

export const mongoverOptionsDefaults: MongoverOptions = {
Expand All @@ -58,6 +59,7 @@ export const mongoverOptionsDefaults: MongoverOptions = {
seedOnly: false,
migrateForce: false,
info: '_info',
socketTimeoutMS: 3600000,
};

export const databaseSpecTemplate: DatabaseSpec = {
Expand Down
5 changes: 5 additions & 0 deletions src/utils/parse-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export function parseOptions(args: any): MongoverOptions {
} else {
mongoverOptions.info = undefined;
}
if (typeof args.socketTimeoutMS === 'string') {
mongoverOptions.socketTimeoutMS = parseFloat(args.socketTimeoutMS);
} else if (typeof args.socketTimeoutMS === 'number') {
mongoverOptions.socketTimeoutMS = args.socketTimeoutMS;
}

if (mongoverOptions.alias!.length !== mongoverOptions.dbs!.length) {
throw new Error('-d | --dbs and -a | --alias should have the same length.');
Expand Down

0 comments on commit d1ad583

Please sign in to comment.