-
Notifications
You must be signed in to change notification settings - Fork 4
/
Base.js
146 lines (131 loc) · 4.67 KB
/
Base.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* The base constructor that is extended by each domain constructor
* Contains the basic methods save/remove...
*/
"use strict";
module.exports = function(db, name, config) {
var domain = require('couch-ar');
var helpers = require('./helpers');
var that = {};
configureHasMany();
configureHasOne();
that.serialize = function() {
var obj = Object.getOwnPropertyNames(config.properties).reduce(function(obj, prop) {
obj[prop] = that[prop];
return obj;
}, {});
obj.type = name;
obj._id = obj.id;
obj._rev = obj.rev;
return obj;
}
that.save = function(callback) {
callback = callback || function() {
}
that.beforeSave && that.beforeSave();
var out = that.serialize();
that.dateCreated = that.dateCreated || new Date();
that.lastUpdated = new Date();
db.save(that.id, that.serialize(), function(err, res) {
if (res && res.ok) {
that.id = res.id;
that.rev = res.rev
}
if(err) {
callback(err, res);
} else {
that.afterSave ? that.afterSave(res, callback) : callback(err, res);
}
});
}
that.remove = function(callback) {
if (that.id) {
db.remove(that.id, that.rev, function(err, res) {
that.id = err ? that.id : undefined;
callback(err, res);
});
} else {
callback();
}
}
return that;
function configureHasOne() {
Object.keys(config.hasOne || {}).forEach(function(propName) {
var model = domain[config.hasOne[propName]];
var upperPropName = helpers.toUpper(propName);
var idProp = propName + 'Id';
config.properties[idProp] = {};
addSetter();
addGetter();
function addSetter() {
that['set' + upperPropName] = function(it) {
if(it && (it.id === undefined)) {
throw 'Can not set non-persisted entity to hasOne';
}
that[idProp] = it && it.id;
}
}
function addGetter() {
that['get' + upperPropName] = function(cb) {
if(that[idProp] !== undefined) {
model.findById(that[idProp], cb);
} else {
cb(undefined);
}
}
}
});
}
function configureHasMany() {
Object.keys(config.hasMany || {}).forEach(function(propName){
var model;
var singularPropName;
var propDef = config.hasMany[propName];
if(typeof propDef === 'object') {
model = propDef.type && domain[propDef.type];
singularPropName = propDef.singular;
} else {
model = domain[propDef];
}
singularPropName = singularPropName || propName.replace(/(.*)s$/,'$1');
var upperPropName = helpers.toUpper(propName);
var singularUpperPropName = helpers.toUpper(singularPropName);
var idsArray = that[singularPropName + 'Ids'] = [];
addGetter();
addAdder();
addRemover();
config.properties[singularPropName + 'Ids'] = {};
function addGetter() {
that['get' + upperPropName] = function(cb) {
var count = 0;
var things = [];
var ids = idsArray.slice(0);
ids.length === 0 && cb([]);
ids.forEach(function(id) {
model.findById(id, function(thing) {
things.push(thing);
count++;
count === ids.length && cb(things);
});
});
}
}
function addAdder() {
that['add' + singularUpperPropName] = function(it) {
if(it.id === undefined) {
throw 'Can not add non-persisted entity to hasMany';
}
idsArray.indexOf(it.id) === -1 && idsArray.push(it.id);
}
}
function addRemover() {
that['remove' + singularUpperPropName] = function(it) {
var idx = idsArray.indexOf(it.id);
if(idx !== -1) {
idsArray.splice(idx,1);
}
}
}
});
}
}