Skip to content

Commit

Permalink
Update examples
Browse files Browse the repository at this point in the history
  • Loading branch information
pkosiec committed Oct 2, 2018
1 parent 2d4532b commit 62063af
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 93 deletions.
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"build": "npm run cleanup; tsc",
"cleanup": "rm -rf ./dist",
"preversion": "npm run build",
"test": "jest",
"test": "jest -i",
"test:unit": "jest unit",
"test:integration": "jest integration",
"test:watch": "jest --watch --coverage"
Expand Down
2 changes: 1 addition & 1 deletion core/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ RUN npm run cleanup
ENV CI true

# Run tests on container start
CMD npm test -- --ci --coverage && \
CMD npm test -- -i --ci --coverage && \
cat ./coverage/lcov.info | ./node_modules/.bin/codacy-coverage --language typescript
22 changes: 6 additions & 16 deletions docker-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ RUN rm -rf /app/src/
# Stage 2: CLI
#

FROM node:10-alpine AS cliBuilder
FROM node:10-alpine
LABEL Maintainer="Pawel Kosiec <pawel@kosiec.net>"

ENV CLI_DIR=./cli
Expand All @@ -37,7 +37,7 @@ WORKDIR /app
COPY $CLI_DIR/package.json $CLI_DIR/package-lock.json /app/
RUN npm i --no-optional

# Copy core
# Copy built core
COPY --from=coreBuilder /app/ /app/node_modules/mongo-seeding/

# Copy app sources
Expand All @@ -48,20 +48,10 @@ COPY $CLI_DIR/bin/ /app/bin/
# Build app
RUN npm run build

# Remove sources
# Remove unnecessary sources
RUN rm -rf /app/src/

#
# Stage 2
#

FROM node:10-alpine
WORKDIR /app

# Copy built app
COPY --from=cliBuilder /app/ /app/

# Create a symlink for "seed" command
RUN npm link
# Create a symlink
RUN npm link mongo-seeding-cli

CMD seed
CMD seed
2 changes: 1 addition & 1 deletion docker-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ You can prepare a customized Docker image for data import. It allows you to prep
ENV DB_NAME mydbname
ENV DB_PORT 27017
ENV DROP_DATABASE true
ENV REPLACE_ID_TO_UNDERSCORE_ID true
ENV REPLACE_ID true
#
# Set default workspace to not specify it every time the image is ran
Expand Down
2 changes: 1 addition & 1 deletion docker-image/test/tester/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"scripts": {
"start": "npm test",
"test": "jest"
"test": "jest -i"
},
"dependencies": {
"mongodb": "^3.1.3",
Expand Down
2 changes: 1 addition & 1 deletion examples/custom-docker-image/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ RUN npm install
ENV DB_NAME dockerexample
ENV DB_PORT 30000
ENV DROP_DATABASE true
ENV REPLACE_ID_TO_UNDERSCORE_ID true
ENV REPLACE_ID true

# Set default workdir to simplify running the image
WORKDIR /data-import//data
22 changes: 11 additions & 11 deletions examples/custom-docker-image/sample-data/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
const { ObjectId } = require('mongodb');
const { createHash } = require('crypto');

const mapToEntities = names => {
return names.map(name => {
const id = getObjectId(name);

return {
id,
name,
};
});
};

const getObjectId = name => {
const hash = createHash('sha1')
.update(name, 'utf8')
Expand All @@ -24,6 +13,17 @@ const getObjectIds = names => {
return names.map(name => getObjectId(name));
};

const mapToEntities = names => {
return names.map(name => {
const id = getObjectId(name);

return {
id,
name,
};
});
};

module.exports = {
mapToEntities,
getObjectId,
Expand Down
48 changes: 27 additions & 21 deletions examples/import-data-ts/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Data import files are prepared to show multiple possibilities of TypeScript data
## Prerequisites

In order to run this example, it's recommended to install the following tools:

- [Docker](https://docker.com) (optional) - to run MongoDB and Mongo Seeding Docker Image
- [NodeJS with NPM](https://nodejs.org) (optional) - to run JavaScript example

Expand All @@ -30,48 +31,53 @@ In order to run this example, it's recommended to install the following tools:
In order to import the sample data, use one of Mongo Seeding tools. The following instructions will result in sample data import to `testing` database. The database will be dropped before import.

### JavaScript library

To import data from TypeScript files, the application, that utilizes Mongo Seeding library has to have TypeScript execution support configured. This tutorial shows how to setup it properly.

1. Initialize a new Node.js project in this directory (folder, which contains this Readme file) with the command:
1. Initialize a new Node.js project in this directory (folder, which contains this Readme file) with the command:

```
npm init -y
```

1. Create a new `index.js` file in the same directory with the following content:

```javascript
require('ts-node').register(); // enable TypeScript execution
1. Create a new `index.js` file in the same directory with the following content:

```javascript
const path = require('path');
const { seedDatabase } = require('mongo-seeding');
const { Seeder } = require('mongo-seeding');
const config = {
database: {
database: {
name: 'testing',
},
replaceIdWithUnderscoreId: true,
dropDatabase: true,
inputPath: path.resolve('./example/data'),
supportedExtensions: ['js', 'json', 'ts'] // evaluating TypeScript files has to be turned on
},
dropDatabase: true,
};
seedDatabase(config)
.then(() => {
const seeder = new Seeder(config);
const collections = seeder.readCollectionsFromPath(
path.resolve('./example/data'),
{
extensions: ['js', 'json', 'ts'],
transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
},
);
seeder
.import(collections)
.then(() => {
console.log('Success');
})
.catch(err => {
})
.catch(err => {
console.log('Error', err);
});
});
```
1. To install all required dependencies used in your `index.js` application, run the command:
1. To install all required dependencies used in your `index.js` application, run the command:
```bash
npm install mongo-seeding typescript ts-node --save
```
1. Turn on the debug output from `mongo-seeding` library and run the newly created app to import data:
1. Turn on the debug output from `mongo-seeding` library and run the newly created app to import data:
```bash
DEBUG=mongo-seeding node index.js
Expand Down
22 changes: 11 additions & 11 deletions examples/import-data-ts/example/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { ObjectId } from 'mongodb';
import { createHash } from 'crypto';

export const mapToEntities = (names: string[]) => {
return names.map(name => {
const id = getObjectId(name);

return {
id,
name,
};
});
};

export const getObjectId = (name: string): ObjectId => {
const hash = createHash('sha1')
.update(name, 'utf8')
Expand All @@ -23,3 +12,14 @@ export const getObjectId = (name: string): ObjectId => {
export const getObjectIds = (names: string[]): ObjectId[] => {
return names.map(name => getObjectId(name));
};

export const mapToEntities = (names: string[]) => {
return names.map(name => {
const id = getObjectId(name);

return {
id,
name,
};
});
};
43 changes: 25 additions & 18 deletions examples/import-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Data import files are prepared to show multiple possibilities of data definition
## Prerequisites

In order to run this example, it's recommended to install the following tools:

- [Docker](https://docker.com) (optional) - to run MongoDB and Mongo Seeding Docker Image
- [NodeJS with NPM](https://nodejs.org) (optional) - to run JavaScript example

Expand All @@ -31,43 +32,49 @@ In order to import the sample data, use one of Mongo Seeding tools. The followin

### JavaScript library

1. Initialize a new Node.js project in this directory (folder, which contains this Readme file) with the command:
1. Initialize a new Node.js project in this directory (folder, which contains this Readme file) with the command:

```
npm init -y
```

1. Create a new `index.js` file in the same directory with the following content:
1. Create a new `index.js` file in the same directory with the following content:

```javascript
const path = require('path');
const { seedDatabase } = require('mongo-seeding');
const { Seeder } = require('mongo-seeding');
const config = {
database: {
database: {
name: 'testing',
},
replaceIdWithUnderscoreId: true,
dropDatabase: true,
inputPath: path.resolve('./example/data'),
},
dropDatabase: true,
};
seedDatabase(config)
.then(() => {
const seeder = new Seeder(config);
const collections = seeder.readCollectionsFromPath(
path.resolve('./example/data'),
{
transformers: [Seeder.Transformers.replaceDocumentIdWithUnderscoreId],
},
);
seeder
.import(collections)
.then(() => {
console.log('Success');
})
.catch(err => {
})
.catch(err => {
console.log('Error', err);
});
});
```
1. To install all dependencies used in your `index.js` application, run the command:
1. To install all dependencies used in your `index.js` application, run the command:
```bash
npm install mongo-seeding --save
```
1. Turn on the debug output from `mongo-seeding` library and run the newly created app to import data:
1. Turn on the debug output from `mongo-seeding` library and run the newly created app to import data:
```bash
DEBUG=mongo-seeding node index.js
Expand Down
22 changes: 11 additions & 11 deletions examples/import-data/example/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
const { ObjectId } = require('mongodb');
const { createHash } = require('crypto');

const mapToEntities = names => {
return names.map(name => {
const id = getObjectId(name);

return {
id,
name,
};
});
};

const getObjectId = name => {
const hash = createHash('sha1')
.update(name, 'utf8')
Expand All @@ -24,6 +13,17 @@ const getObjectIds = names => {
return names.map(name => getObjectId(name));
};

const mapToEntities = names => {
return names.map(name => {
const id = getObjectId(name);

return {
id,
name,
};
});
};

module.exports = {
mapToEntities,
getObjectId,
Expand Down

0 comments on commit 62063af

Please sign in to comment.