-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
428 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Oops, something went wrong.