Skip to content

Commit fd23f7a

Browse files
committed
Initial version
1 parent a74ed23 commit fd23f7a

File tree

7 files changed

+192
-0
lines changed

7 files changed

+192
-0
lines changed

index.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**/
2+
const model = require('./lib/model');
3+
const modelate = require('./lib/modelate');
4+
5+
const models = model.models;
6+
7+
8+
function Modelate(name) {
9+
const self = this;
10+
11+
self.modelName = name;
12+
self.model = model.add;
13+
self.modelate = modelate;
14+
15+
models[name] = {};
16+
17+
return this;
18+
}
19+
20+
21+
module.exports = Modelate;
22+
/*{
23+
add: function () { },
24+
remove: null,
25+
list: null,
26+
modelate: null
27+
};*/
28+

lib/model.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const models = {};
2+
3+
function model(data) {
4+
// ToDo: Validate model
5+
// ToDo: Check if model already exists to merge instead of set
6+
models[this.name] = data;
7+
8+
return this;
9+
}
10+
11+
module.exports = {
12+
models: models,
13+
add: model
14+
};

lib/modelate.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const util = require('./util');
2+
3+
const models = require('./model').models;
4+
const validate = require('./validate');
5+
6+
function modelate(data) {
7+
console.log('modeling', data, 'for object with type', this.modelName);
8+
9+
const model = util.clone(models[this.name]);
10+
11+
for(let prop in model) {
12+
if(validate(data[prop], model[prop])) {
13+
model[prop] = data[prop];
14+
} else {
15+
delete model[prop];
16+
}
17+
}
18+
19+
return model;
20+
}
21+
22+
module.exports = modelate;

lib/util.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/** Deep Copy
2+
* Return a copy of the object passed as param, doing (recursivelly) copys of
3+
* all the objects the initial has.
4+
*
5+
* Caution!!!
6+
* This function is recursive. Copying a very deep object might concern heavy
7+
* performance issues. Use your brain before the function.
8+
*/
9+
function deepCopy(oldObj) {
10+
var newObj = oldObj;
11+
if (oldObj && typeof oldObj === 'object') {
12+
newObj = Object.prototype.toString.call(oldObj) === '[object Array]' ? [] : {};
13+
for (var i in oldObj) {
14+
newObj[i] = deepCopy(oldObj[i]);
15+
}
16+
}
17+
return newObj;
18+
}
19+
20+
/** Merge
21+
* Perform a complete merge of two objects.
22+
*
23+
* Caution!!!
24+
* A third parameter, avoidDeepCopy is also included to avoid creating a deep
25+
* copy of the objects. With this parameter to true, original objects may get
26+
* updated, linked or similar unexpected behaviour.
27+
*/
28+
function merge(old, obj, avoidDeepCopy) {
29+
30+
let dest, orig;
31+
32+
if(avoidDeepCopy) {
33+
dest = old;
34+
orig = obj;
35+
} else {
36+
dest = deepCopy(old);
37+
orig = deepCopy(obj);
38+
}
39+
40+
for(let prop in orig) {
41+
dest[prop] = orig[prop];
42+
}
43+
44+
return dest;
45+
}
46+
47+
48+
module.exports = {
49+
merge: merge,
50+
clone: deepCopy
51+
};

lib/validate.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
3+
function validate(data, model) {
4+
let isValid = true;
5+
6+
if(model.type && typeof(data) === model.type) {
7+
isValid = true;
8+
9+
// maxLength
10+
if(model.maxLength && data.length > model.maxLength) {
11+
isValid = false;
12+
}
13+
// minLenght
14+
if(model.minLength && data.length < model.minLength) {
15+
isValid = false;
16+
}
17+
} else {
18+
isValid = false;
19+
}
20+
21+
return isValid;
22+
}
23+
24+
module.exports = validate;

package.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "modelate",
3+
"version": "0.1.0",
4+
"description": "A simple data modeling tool for NodeJS",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"repository": {
10+
"type": "git",
11+
"url": "git+https://github.com/CodingCarlos/modelate.git"
12+
},
13+
"keywords": [
14+
"modelate",
15+
"data",
16+
"modeling",
17+
"modelator",
18+
"model",
19+
"models",
20+
"validate",
21+
"validator",
22+
"validating",
23+
"orm"
24+
],
25+
"author": "CodingCarlos <[email protected]>",
26+
"license": "MIT",
27+
"bugs": {
28+
"url": "https://github.com/CodingCarlos/modelate/issues"
29+
},
30+
"homepage": "https://github.com/CodingCarlos/modelate#readme"
31+
}

test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/* Just a first basic test */
2+
var Modelate = require('./index');
3+
4+
var model = {
5+
name: {
6+
type: 'string',
7+
length: { // For now, length do nothng.
8+
max: 10,
9+
min: 1
10+
}
11+
}
12+
};
13+
var user = Modelate('User').model(model);
14+
15+
var data = {
16+
name: 'Paco',
17+
surname: 'santos' // Surname shall be removed
18+
};
19+
var result = user.modelate(data);
20+
21+
22+
console.log(result); // Shall be { name: 'Paco' }

0 commit comments

Comments
 (0)