1
+ var MongoClient = require ( 'mongodb' ) . MongoClient
2
+
3
+ // move connecting into a function to avoid the 'pyramid of doom'
4
+ function getConnection ( cb ) {
5
+ MongoClient . connect ( 'mongodb://localhost' , function ( err , db ) {
6
+ if ( err ) return cb ( err ) ;
7
+
8
+ var simpleUser = db . collection ( 'simple' ) ;
9
+
10
+ // to search by name, index is need to get speed
11
+ // ref: http://docs.mongodb.org/manual/core/indexes-introduction/
12
+ simpleUser . ensureIndex ( { name : true } , function ( err ) {
13
+ if ( err ) return cb ( err ) ;
14
+ cb ( null , simpleUser ) ;
15
+ } ) ;
16
+ } ) ;
17
+ }
18
+
19
+ // an upsert will create a new record OR update an existing record
20
+ // which makes things easier, in mongo, we can do this with a
21
+ // findAndModify and passing the upsert option to have the update
22
+ // document returned, we pass the new option as well
23
+ function upsertUser ( collection , name , role , cb ) {
24
+ collection . findAndModify ( { name : name } , { } , { $set : { role : role } } ,
25
+ { upsert : true , new : true } , cb ) ;
26
+ }
27
+
28
+ // instead of finding just one user, we can list all of the documents
29
+ // by passing an empty selector. This returns a 'cursor', which allows
30
+ // us to walk through the documents look at how we do this in process
31
+ function readAll ( collection , cb ) {
32
+ collection . find ( { } , cb ) ;
33
+ }
34
+
35
+ function readRole ( collection , name , cb ) {
36
+ collection . findOne ( { name : name } , cb ) ;
37
+ }
38
+
39
+ function printUser ( user ) {
40
+ // make sure that we found our user
41
+ if ( ! user ) {
42
+ console . log ( "Couldn't find the user you asked for!" )
43
+ }
44
+
45
+ console . log ( user . name + ' has the role of ' + user . role ) ;
46
+ }
47
+
48
+ // the each method allows us to walk through the result set, notice
49
+ // the callback, as every time the callback is called, there is
50
+ // another chance of an error
51
+ function printUsers ( users , cb ) {
52
+ users . each ( function ( err , user ) {
53
+ if ( err ) return cb ( err ) ;
54
+ printUser ( user ) ;
55
+ } ) ;
56
+ }
57
+
58
+ function simpleCli ( operation , name , role , cb ) {
59
+ getConnection ( function ( err , collection ) {
60
+ if ( err ) return cb ( err ) ;
61
+
62
+ // we need to make sure to close the database, otherwise
63
+ // the process won't stop
64
+ function processUser ( err , user ) {
65
+ if ( err ) return cb ( user ) ;
66
+ printUser ( user ) ;
67
+ collection . db . close ( ) ;
68
+ cb ( ) ;
69
+ }
70
+
71
+ // use this function when dealing with lots of users
72
+ function processUsers ( err , users ) {
73
+ if ( err ) return cb ( err ) ;
74
+ // the callback to each is called for every result
75
+ // once it returns a null, we know the result is done
76
+ users . each ( function ( err , user ) {
77
+ if ( err ) return cb ( err ) ;
78
+ if ( user ) {
79
+ printUser ( user ) ;
80
+ } else {
81
+ collection . db . close ( ) ;
82
+ cb ( ) ;
83
+ }
84
+ } ) ;
85
+ }
86
+
87
+ if ( operation === 'list' ) {
88
+ readAll ( collection , processUsers ) ;
89
+ } else if ( operation === 'update' ) {
90
+ upsertUser ( collection , name , role , processUser ) ;
91
+ } else if ( operation === 'read' ) {
92
+ readRole ( collection , name , processUser ) ;
93
+ } else {
94
+ return cb ( new Error ( 'unknown operation!' ) ) ;
95
+ }
96
+ } ) ;
97
+ }
98
+
99
+ var operation = process . argv [ 2 ] ;
100
+ var name = process . argv [ 3 ] ;
101
+ var role = process . argv [ 4 ] ;
102
+
103
+ simpleCli ( operation , name , role , function ( err ) {
104
+ if ( err ) {
105
+ console . log ( "Had an error!" , err ) ;
106
+ process . exit ( 1 ) ;
107
+ }
108
+ } ) ;
0 commit comments