Skip to content

Direct Access Object using Mongoose and Express.js

Notifications You must be signed in to change notification settings

mykiimike/daome

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Direct Access Object for Mongoose with Express.js

NPM

  • Easy dataTable
  • Auto API
  • Autonomous & standalone system
  • Auto modal (but require jquery)
  • Server and client
  • Fine permission control possibility
    • By API type (create, update...)
    • By Fields
  • Templates are based on EJS

Install

From server side

npm install daome

From client side

<script src="/yourPath/dao/assets/app.js"></script>

Introduction

Mongoose provides the method to manage the model. DAOME does not include this feature.

That why you have to define your own model using Mongoose before anything.

As DAOME provides the controller and views is it important to point that input validation is made by the model (Mongoose Model). But DAOME accepts access and priviledges control.

Server API is separed in 3 features:

  • The controller which will manage requests with express.js
  • The view manager (server and client side)
  • The express.js initializer

Real life example

### Server side

Initialise the DAOME module:

var daome = require("daome");

/* create a specific context where app is the express.js app handler. */
var daomeCtx = new daome(app, "/admin/dao");

/* All Web services (for the current context) are accessible from /admin/dao */

Define your Mongoose model as following (like we create a user backend management) :

this.userSchema = new Schema({
	name: {type: String, required: [true, "Please fill a valid name"] },
	mail: {
		type: String, 
		lowercase: true, 
		required: [true, "Please fill a valid mail address"],
		unique: [true, "Can not create account"],
		match: [/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/, 'Please fill a valid email address'] 
	},
	password: {
		type: String, 
		set: passwordSetter, // can be a HMAC conversion
		required: [true, "Please fill a valid password"] 
	},
	session: {type: String, unique: true, default: function() {
		return(jen.password(25, 60))
	} },
	admin: {type: Boolean, default: false },
	verified: {type: Boolean, default: false },
	createDate: { type: Date, default: Date.now }
});
this.userModel = mongoose.model('user', this.userSchema);

/* create a daome controller */
this.userController = daomeCtx.controller("user", this.userModel);

Once the model is create we define the server side DAOME up layer controller :

/* create a daome controller */
this.userController = daomeCtx.controller("user", this.userModel);

/* permission controller important to define */
this.userController.permission = function(req, sw, field) {
	if(isAdmin != true)
		return(false);
	return(true);
}

this.userController.views({
		name: {
			title: "User name",
			help: "Enter the account owner first and last name",
			editable: true,
			search: true,
			table: true,
			type: "text",
			/* do not set name */
			attr: {
				placeholder: "Give a name"
			},
		},
		mail: {
			title: "Login mail",
			help: "The mail address is used as user login",
			editable: true,
			search: true,
			table: true,
			type: "text",
			/* do not set name */
			attr: {
				placeholder: "Give a login address"
			},
		},
		password: {
			title: "Password",
			help: "Login password (make sure the password is safe)",
			editable: true,
			get: function(text) {
				return('');
			},
			type: "password",
		},
		
		verified: {
			title: "User verification",
			help: "Is the user confirms its subscribtion",
			editable: true,
			type: "checkbox",
			attr: {},
		},
		
		admin: {
			title: "Administration level",
			help: "Is the user can administrate the platform ?",
			editable: true,
			type: "checkbox",
			attr: {},
		},
		
		createDate: {
			attr: {}
		},
});

/* by doing this you expose the database to the Web */
daomeCtx.publish(this.userController);

Well done for the server side

### Client side

DAOME can be used of 2 ways. The first is a easy way where the dataset, modals and everything are generated by DAOME, but it requires you install bootstrap framework on your website. The other way calls as standalone way does not required any additionnal framework, it works alone.

In our example we use modals then we need Bootstrap.

<!DOCTYPE html>
<!--[if IE 9 ]><html class="no-js ie9"><![endif]-->
<!--[if (gt IE 9)|!(IE)]><!--><html class="no-js"> <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
<script src="/admin/dao/assets/app.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css" integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>
<body>
<div class="site-wrapper">
<div id="userModal"></div>

<div class="container">
	<header class="page-header">
		<h1>Users settings </h1>
	</header>
	
	<div class="container">

				<button id="userAdd" type="button" class="btn btn-primary">
				 Add user
				</button>

						
				<div id="userSearch">
				</div>
				<div id="userContent">
				</div>
				<div id="userPages">
				</div>
		</div>
	</div>
</div>



<script>
(function() {
	var userDao = new daome("user", "/admin/dao");
	var userDaoModalId = null;
	userDao.connect(function() {

		var addViews = {
			adminLabel: {
				title: "Is admin",
				table: true,
				get: function(id, doc, th) {
					var label = document.createElement("label");
					
					if(doc.admin == true) {
						label.setAttribute("class", "label label-danger");
						label.innerHTML = "YES";
					}
					else {
						label.setAttribute("class", "label label-success");
						label.innerHTML = "NO";
					}
	
					return(label);
				},
				attr: {},
			},
			activateLabel: {
				title: "Verified",
				table: true,
				get: function(id, doc, th) {
					var label = document.createElement("label");
					
					if(doc.verified == true) {
						label.setAttribute("class", "label label-success");
						label.innerHTML = "YES";
					}
					else {
						label.setAttribute("class", "label label-warning");
						label.innerHTML = "NO";
					}
	
					return(label);
				},
				attr: {},
			},
			
			buttonsDelete: {
				table: true,

				get: function(id, doc, th) {
					/* Edit button */
					var button = document.createElement("button");
					button.setAttribute("class", "btn btn-xs btn-secondary");
					button.setAttribute("data-mid", doc._id);
					button.innerHTML = "Delete";
					
					button.onclick = function() {
						var id = button.getAttribute("data-mid");
						userDao.modalRemove("Are you sure to want remove server ?", id);
					};
	
					return(button);
				},
				attr: {},
			},
			
			buttonsEdit: {
				table: true,

				get: function(id, doc, th) {
					/* Edit button */
					var button = document.createElement("button");
					button.setAttribute("class", "btn btn-xs btn-secondary");
					button.setAttribute("data-id", id);
					button.setAttribute("data-mid", doc._id);
					button.innerHTML = "Edit";
					
					button.onclick = function() {
						var id = button.getAttribute("data-mid");
						userDao.modalUpdate("Update satellite server informations", id);
					};
	
					return(button);
				},
				attr: {},
			},
			buttonsView: {
				table: true,

				get: function(id, doc, th) {
					/* View button */
					var button = document.createElement("button");
					button.setAttribute("class", "btn btn-xs btn-primary");
					button.setAttribute("data-id", id);
					button.setAttribute("data-mid", doc._id);
					button.innerHTML = "Detail";
					
					button.onclick = function() {
						var id = button.getAttribute("data-mid");
						userDao.modalView("Futher about sattelite server", id);
					};
					
					return(button);
				},
				attr: {},
			},
		}	
		

		userDao.views(addViews);
		
		userDaoModal = userDao.generateModal("userModal");
		userDaoSearch = userDao.generateSearch("userSearch");
		userDaoTable = userDao.generateTable("userContent");
		userDaoTable = userDao.generatePagination("userPages");

		$('#userAdd').click(function() {
			userDao.modalCreate("Create new server entry");
		});
    
	});
})();
</script>

</div>

<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

</body>
</html>

About

Direct Access Object using Mongoose and Express.js

Resources

Stars

Watchers

Forks

Packages

No packages published