Skip to content

Commit 0e78593

Browse files
committed
Add inherited models and bugfix
1 parent e69ccaa commit 0e78593

File tree

5 files changed

+109
-14
lines changed

5 files changed

+109
-14
lines changed

examples/chat.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* Just a first basic test */
2+
var Modelate = require('../index');
3+
4+
// Models
5+
var userModel = {
6+
name: {
7+
type: 'string',
8+
length: { // For now, length do nothng.
9+
max: 10,
10+
min: 1
11+
},
12+
value: {
13+
eq: 'Paco'
14+
}
15+
},
16+
surname: {
17+
type: 'string',
18+
length: { // For now, length do nothng.
19+
max: 3,
20+
min: 1
21+
},
22+
value: {
23+
contains: 'os'
24+
}
25+
},
26+
age: {
27+
type: 'number',
28+
value: {
29+
max: 95,
30+
min: 18
31+
}
32+
},
33+
dni: {
34+
type: 'string',
35+
value: {
36+
max: 95,
37+
min: 18
38+
},
39+
func: function startOnFive(value) {
40+
if(value.charAt(0) === '5') {
41+
return true;
42+
}
43+
44+
return false;
45+
}
46+
}
47+
};
48+
var User = Modelate('User').set(userModel);
49+
50+
// Modelate a user
51+
var myUserData = {
52+
name: 'Paco',
53+
surname: 'santos',
54+
age: 19,
55+
dni: '51402430A'
56+
};
57+
var myUser = User.modelate(myUserData);
58+
console.log('........ myUser:');
59+
console.log(myUser);
60+
console.log('................');
61+
62+
var messageModel = {
63+
user: {
64+
model: 'User'
65+
},
66+
// date: {
67+
// type: 'date'
68+
// },
69+
message: {
70+
type: 'string',
71+
length: {
72+
min: 1,
73+
max: 255
74+
}
75+
}
76+
};
77+
var Message = Modelate('Message').set(messageModel);
78+
79+
// Modelate a message
80+
var myMessageData = {
81+
message: 'I am using models inside models. Crazy.',
82+
// date: new Date(),
83+
user: myUser
84+
};
85+
var myMessage = Message.modelate(myMessageData);
86+
console.log(myMessage);

index.js

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,21 @@
22
const model = require('./lib/model');
33
const modelate = require('./lib/modelate');
44

5-
const models = model.models;
5+
// const models = model.models;
66

77

88
function Modelate(name) {
99
const self = this;
1010

1111
self.modelName = name;
12-
self.model = model.add;
12+
self.model = model.get;
13+
self.set = model.add;
1314
self.modelate = modelate;
1415

15-
models[name] = {};
16+
// models[name] = {};
1617

1718
return this;
1819
}
1920

2021

2122
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,24 @@ const models = {};
33
function model(data) {
44
// ToDo: Validate model
55
// ToDo: Check if model already exists to merge instead of set
6-
models[this.name] = data;
6+
for(let prop in data) {
7+
if(data[prop].model) {
8+
data[prop] = models[data[prop].model];
9+
}
10+
}
11+
12+
models[this.modelName] = data;
713

814
return this;
915
}
1016

17+
function get() {
18+
return models[this.modelName];
19+
}
20+
21+
1122
module.exports = {
1223
models: models,
13-
add: model
24+
add: model,
25+
get: get
1426
};

lib/modelate.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ const util = require('./util');
33
const models = require('./model').models;
44
const validate = require('./validate');
55

6+
// var modelate = function modelate(data) {
67
function modelate(data) {
7-
const model = util.clone(models[this.name]);
8+
9+
console.log(this.modelName);
10+
const model = util.clone(models[this.modelName]);
811

912
for(let prop in model) {
1013
if(validate(data[prop], model[prop])) {

lib/validators/type.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
function isValid(data, model) {
1010
if(!model.type) {
11-
return false;
11+
return true;
1212
}
1313

1414
if((typeof(data)).toUpperCase() === model.type.toUpperCase()) {

0 commit comments

Comments
 (0)