Skip to content

Commit

Permalink
Merge branch 'example2'
Browse files Browse the repository at this point in the history
  • Loading branch information
fand committed Mar 29, 2017
2 parents e12742a + 7b185ca commit 4628789
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 3 deletions.
2 changes: 1 addition & 1 deletion bin/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ if (cli.flags.transform) {
process.exit(-1);
});
} else {
asyncNode(cli.input[0])
asyncNode(cli.input[0], cli.input.slice(1))
.then(data => {
process.stdout.write(data);
})
Expand Down
11 changes: 11 additions & 0 deletions examples/mongo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# mongo

Get / Set values to MongoDB

## Usage

```
$ npm i
$ async-node index.js -- -s '{"name":"foo"}' # Insert a document
$ async-node index.js -- -g '{"name":"foo"}' # Find a document by name 'foo'
```
37 changes: 37 additions & 0 deletions examples/mongo/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const MongoClient = require('mongodb').MongoClient;

// Parse args
const o = require('minimist')(process.argv.slice(2));
if (!(o.s || o.set || o.g || o.get)) {
console.log(`
Usage:
$ async-node index.js -s '{"name":"foo"}'
$ async-node index.js -g '{"name":"foo"}'
`);
process.exit(-1);
}

// Parse JSON
let doc;
try {
doc = JSON.parse(`${o.s || o.set || o.g || o.get}`);
} catch (e) {
console.error('Invalid JSON format');
process.exit(-1);
}

// Connect to MongoDB
const db = await MongoClient.connect('mongodb://localhost:27017/async-node-example-mongo');
const collection = db.collection('async-node-example-mongo');

// Execute
if (o.s || o.set) {
const res = await collection.insertOne(doc);
console.log(res.ops[0]);
}
if (o.g || o.get) {
const res = await collection.findOne(doc);
console.log(res);
}

db.close();
19 changes: 19 additions & 0 deletions examples/mongo/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "mongo",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"get": "$(npm bin)/async-node index.js -- -g",
"set": "$(npm bin)/async-node index.js -- -s",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "fand <fand@gmork.in>",
"license": "MIT",
"dependencies": {
"@fand/async-node": "^0.1.1",
"minimist": "^1.2.0",
"mongodb": "^2.2.25"
}
}

0 comments on commit 4628789

Please sign in to comment.