1 | # moncrud-leo
|
2 | ## using MongoDB with Mongoose and Express
|
3 |
|
4 | Create a MongoDB Model and give it CRUD (Create, Read, Update, Delete) controls
|
5 | on an API route.
|
6 |
|
7 |
|
8 | ### Configure moncrud
|
9 |
|
10 | ```js
|
11 | var mc = require('moncrud-leo');
|
12 | // this depends on express, give the initializer the [app] element
|
13 | mc.init(app, {
|
14 | //settings
|
15 | // more details will be available later
|
16 | apidomain: 'api',
|
17 | connection: 'mongodb://localhost/collection'
|
18 | }, function(data, next){
|
19 | var hasAccess = true;
|
20 | //authenticate
|
21 | /*
|
22 | * data gives you details about the request,
|
23 | */
|
24 | next(hasAccess); // true/false return
|
25 | // this will return a response to the query based on a boolean.
|
26 | // if user is qualified after this test, the query is executed.
|
27 | // otherwise client get's an error response.
|
28 | });
|
29 | ```
|
30 |
|
31 | ### Using moncrud
|
32 |
|
33 | ```js
|
34 | // require moncrud
|
35 | var mc = require('moncrud-leo');
|
36 | // create a model
|
37 | var users = mc.model('users', {
|
38 | firstname: String,
|
39 | lastname: String,
|
40 | email: String,
|
41 | active: Boolean,
|
42 | username: String,
|
43 | password: String,
|
44 | roles: ['basic'],
|
45 | friends: [{
|
46 | type: mc.ObjectId,
|
47 | ref: 'users'
|
48 | }]
|
49 | // etc. Use the standard model parameters you would use for Mongoose
|
50 | });
|
51 | // After the model is created, you can initate CRUD
|
52 | // basic CRUD initializer:
|
53 | users.crud();
|
54 | // users.crud() function initiates HTTP\post calls: '/api/users/' [find, find-one, add, update, remove]
|
55 | // you can also customoze the controller
|
56 | users.crud({
|
57 | populate: {
|
58 | 'get': ['friends'],
|
59 | 'get-all': []
|
60 | },
|
61 | custom: {
|
62 | 'add-friend':function(req,res,model){
|
63 | // execute you standard calls here for a custom controller
|
64 | }
|
65 | }
|
66 |
|
67 | });
|
68 |
|
69 | ```
|
70 |
|
71 | ### This is a basic layout of the documention for this handy utility, more will come later. |
\ | No newline at end of file |