Skip to content

Commit a8e14b9

Browse files
committed
Splits *Association.extend() into .extend() + .autoFetch() (#15)
This allows associations to extend instance methods immediately and then autoFetch if needed.
1 parent a97197f commit a8e14b9

File tree

4 files changed

+67
-28
lines changed

4 files changed

+67
-28
lines changed

lib/Associations/Extend.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,19 @@ exports.prepare = function (db, Model, associations, association_properties, mod
3232
};
3333
};
3434

35-
exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
35+
exports.extend = function (Model, Instance, Driver, associations, opts) {
36+
for (var i = 0; i < associations.length; i++) {
37+
extendInstance(Model, Instance, Driver, associations[i], opts);
38+
}
39+
};
40+
41+
exports.autoFetch = function (Instance, associations, opts, cb) {
3642
if (associations.length === 0) {
3743
return cb();
3844
}
3945

4046
var pending = associations.length;
41-
var extendDone = function extendDone() {
47+
var autoFetchDone = function autoFetchDone() {
4248
pending -= 1;
4349

4450
if (pending === 0) {
@@ -47,11 +53,11 @@ exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
4753
};
4854

4955
for (var i = 0; i < associations.length; i++) {
50-
extendInstance(Model, Instance, Driver, associations[i], opts, extendDone);
56+
autoFetchInstance(Instance, associations[i], opts, autoFetchDone);
5157
}
5258
};
5359

54-
function extendInstance(Model, Instance, Driver, association, opts, cb) {
60+
function extendInstance(Model, Instance, Driver, association, opts) {
5561
Object.defineProperty(Instance, association.hasAccessor, {
5662
value : function (cb) {
5763
if (!Instance[Model.id]) {
@@ -129,8 +135,28 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
129135
},
130136
enumerable : false
131137
});
138+
}
139+
140+
function autoFetchInstance(Instance, association, opts, cb) {
141+
if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
142+
opts.autoFetchLimit = association.autoFetchLimit;
143+
}
144+
145+
if (opts.autoFetchLimit === 0 || (!opts.autoFetch && !association.autoFetch)) {
146+
return cb();
147+
}
148+
149+
if (Instance.isPersisted()) {
150+
Instance[association.getAccessor]({ autoFetchLimit: opts.autoFetchLimit - 1 }, function (err, Assoc) {
151+
if (!err) {
152+
Instance[association.name] = Assoc;
153+
}
132154

133-
return cb();
155+
return cb();
156+
});
157+
} else {
158+
return cb();
159+
}
134160
}
135161

136162
function ucfirst(text) {

lib/Associations/Many.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,19 @@ exports.prepare = function (Model, associations) {
7878
};
7979
};
8080

81-
exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
81+
exports.extend = function (Model, Instance, Driver, associations, opts) {
82+
for (var i = 0; i < associations.length; i++) {
83+
extendInstance(Model, Instance, Driver, associations[i], opts);
84+
}
85+
};
86+
87+
exports.autoFetch = function (Instance, associations, opts, cb) {
8288
if (associations.length === 0) {
8389
return cb();
8490
}
8591

8692
var pending = associations.length;
87-
var extendDone = function extendDone() {
93+
var autoFetchDone = function autoFetchDone() {
8894
pending -= 1;
8995

9096
if (pending === 0) {
@@ -93,11 +99,11 @@ exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
9399
};
94100

95101
for (var i = 0; i < associations.length; i++) {
96-
extendInstance(Model, Instance, Driver, associations[i], opts, extendDone);
102+
autoFetchInstance(Instance, associations[i], opts, autoFetchDone);
97103
}
98104
};
99105

100-
function extendInstance(Model, Instance, Driver, association, opts, cb) {
106+
function extendInstance(Model, Instance, Driver, association, opts) {
101107
if (Model.settings.get("instance.cascadeRemove")) {
102108
Instance.on("beforeRemove", function () {
103109
Instance[association.delAccessor]();
@@ -363,7 +369,9 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
363369
},
364370
enumerable: false
365371
});
372+
}
366373

374+
function autoFetchInstance(Instance, association, opts, cb) {
367375
if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
368376
opts.autoFetchLimit = association.autoFetchLimit;
369377
}

lib/Associations/One.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,19 @@ exports.prepare = function (Model, associations, association_properties, model_f
9797
};
9898
};
9999

100-
exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
100+
exports.extend = function (Model, Instance, Driver, associations, opts) {
101+
for (var i = 0; i < associations.length; i++) {
102+
extendInstance(Model, Instance, Driver, associations[i], opts);
103+
}
104+
};
105+
106+
exports.autoFetch = function (Instance, associations, opts, cb) {
101107
if (associations.length === 0) {
102108
return cb();
103109
}
104110

105111
var pending = associations.length;
106-
var extendDone = function extendDone() {
112+
var autoFetchDone = function autoFetchDone() {
107113
pending -= 1;
108114

109115
if (pending === 0) {
@@ -112,11 +118,11 @@ exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
112118
};
113119

114120
for (var i = 0; i < associations.length; i++) {
115-
extendInstance(Model, Instance, Driver, associations[i], opts, extendDone);
121+
autoFetchInstance(Instance, associations[i], opts, autoFetchDone);
116122
}
117123
};
118124

119-
function extendInstance(Model, Instance, Driver, association, opts, cb) {
125+
function extendInstance(Model, Instance, Driver, association, opts) {
120126
Object.defineProperty(Instance, association.hasAccessor, {
121127
value: function (opts, cb) {
122128
if (typeof opts == "function") {
@@ -209,7 +215,9 @@ function extendInstance(Model, Instance, Driver, association, opts, cb) {
209215
enumerable: false
210216
});
211217
}
218+
}
212219

220+
function autoFetchInstance(Instance, association, opts, cb) {
213221
if (!opts.hasOwnProperty("autoFetchLimit") || typeof opts.autoFetchLimit == "undefined") {
214222
opts.autoFetchLimit = association.autoFetchLimit;
215223
}

lib/Model.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ function Model(opts) {
8484
}
8585
}
8686

87+
var assoc_opts = {
88+
autoFetch : inst_opts.autoFetch || false,
89+
autoFetchLimit : inst_opts.autoFetchLimit,
90+
cascadeRemove : inst_opts.cascadeRemove
91+
};
8792
var pending = 2;
8893
var instance = new Instance(model, {
8994
uid : inst_opts.uid, // singleton unique id
@@ -114,21 +119,13 @@ function Model(opts) {
114119
if (model_fields !== null) {
115120
LazyLoad.extend(instance, model, opts.properties);
116121
}
117-
OneAssociation.extend(model, instance, opts.driver, one_associations, {
118-
autoFetch : inst_opts.autoFetch || false,
119-
autoFetchLimit : inst_opts.autoFetchLimit,
120-
cascadeRemove : inst_opts.cascadeRemove
121-
}, function () {
122-
ManyAssociation.extend(model, instance, opts.driver, many_associations, {
123-
autoFetch : inst_opts.autoFetch || false,
124-
autoFetchLimit : inst_opts.autoFetchLimit,
125-
cascadeRemove : inst_opts.cascadeRemove
126-
}, function () {
127-
ExtendAssociation.extend(model, instance, opts.driver, extend_associations, {
128-
autoFetch : inst_opts.autoFetch || false,
129-
autoFetchLimit : inst_opts.autoFetchLimit,
130-
cascadeRemove : inst_opts.cascadeRemove
131-
}, function () {
122+
OneAssociation.extend(model, instance, opts.driver, one_associations, assoc_opts);
123+
ManyAssociation.extend(model, instance, opts.driver, many_associations, assoc_opts);
124+
ExtendAssociation.extend(model, instance, opts.driver, extend_associations, assoc_opts);
125+
126+
OneAssociation.autoFetch(instance, one_associations, assoc_opts, function () {
127+
ManyAssociation.autoFetch(instance, many_associations, assoc_opts, function () {
128+
ExtendAssociation.autoFetch(instance, extend_associations, assoc_opts, function () {
132129
Hook.wait(instance, opts.hooks.afterAutoFetch, function (err) {
133130
if (--pending > 0) return;
134131
if (typeof cb == "function") {

0 commit comments

Comments
 (0)