Skip to content

Commit f04085b

Browse files
committed
adding relationship / cache methods to Collection
1 parent 7f8e1cd commit f04085b

File tree

4 files changed

+175
-14
lines changed

4 files changed

+175
-14
lines changed

dist/amd/dl.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,6 @@ DL.Collection.prototype.where = function(objects, _operation, _value) {
847847
return this;
848848
};
849849

850-
851850
/**
852851
* Find first item by _id
853852
* @method find
@@ -878,6 +877,39 @@ DL.Collection.prototype.find = function(_id) {
878877
return promise;
879878
};
880879

880+
/**
881+
* Set the relationships that should be eager loaded.
882+
* @method with
883+
* @param {String} ...
884+
* @return {DL.Collection}
885+
*
886+
* @example Simple relationship
887+
*
888+
* client.collection('books').with('author').each(function(book) {
889+
* console.log("Author: ", book.author.name);
890+
* });
891+
*
892+
* @example Multiple relationships
893+
*
894+
* client.collection('books').with('author', 'publisher').each(function(book) {
895+
* console.log("Author: ", book.author.name);
896+
* console.log("Publisher: ", book.publisher.name);
897+
* });
898+
*
899+
* @example Nested relationships
900+
*
901+
* client.collection('books').with('author.contacts').each(function(book) {
902+
* console.log("Author: ", book.author.name);
903+
* console.log("Contacts: ", book.author.contacts);
904+
* });
905+
*
906+
*/
907+
DL.Collection.prototype.with = function() {
908+
this.options.with = arguments;
909+
return this;
910+
};
911+
912+
881913
/**
882914
* Group results by field
883915
* @method group
@@ -1065,6 +1097,7 @@ DL.Collection.prototype.reset = function() {
10651097
this._group = [];
10661098
this._limit = null;
10671099
this._offset = null;
1100+
this._remember = null;
10681101
return this;
10691102
};
10701103

@@ -1131,6 +1164,25 @@ DL.Collection.prototype.offset = function(int) {
11311164
return this;
11321165
};
11331166

1167+
/**
1168+
* Indicate that the query results should be cached.
1169+
*
1170+
* @method remember
1171+
* @param {Number} minutes
1172+
* @return {DL.Collection} this
1173+
*
1174+
* @example Caching a query
1175+
*
1176+
* client.collection('posts').sort('updated_at', -1).limit(5).remember(10).then(function(data) {
1177+
* // ...
1178+
* });
1179+
*
1180+
*/
1181+
DL.Collection.prototype.remember = function(minutes) {
1182+
this._remember = minutes;
1183+
return this;
1184+
};
1185+
11341186
/**
11351187
* Get channel for this collection.
11361188
* @method channel
@@ -1312,9 +1364,10 @@ DL.Collection.prototype._validateName = function(name) {
13121364
DL.Collection.prototype.buildQuery = function() {
13131365
var query = {};
13141366

1315-
// apply limit / offset
1367+
// apply limit / offset and remember
13161368
if (this._limit !== null) { query.limit = this._limit; }
13171369
if (this._offset !== null) { query.offset = this._offset; }
1370+
if (this._remember !== null) { query.remember = this._remember; }
13181371

13191372
// apply wheres
13201373
if (this.wheres.length > 0) {
@@ -1336,7 +1389,8 @@ DL.Collection.prototype.buildQuery = function() {
13361389
first: 'f',
13371390
aggregation: 'aggr',
13381391
operation: 'op',
1339-
data: 'data'
1392+
data: 'data',
1393+
with: 'with'
13401394
};
13411395

13421396
for (f in shortnames) {
@@ -1351,7 +1405,6 @@ DL.Collection.prototype.buildQuery = function() {
13511405
return query;
13521406
};
13531407

1354-
13551408
/**
13561409
* module DL
13571410
* class DL.CollectionItem
@@ -1548,6 +1601,7 @@ DL.Pagination.prototype.isFetching = function() {
15481601
DL.Pagination.prototype.then = function() {
15491602
};
15501603

1604+
15511605
/**
15521606
* @module DL
15531607
* @class DL.System

dist/dl.js

Lines changed: 58 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10392,7 +10392,6 @@ DL.Collection.prototype.where = function(objects, _operation, _value) {
1039210392
return this;
1039310393
};
1039410394

10395-
1039610395
/**
1039710396
* Find first item by _id
1039810397
* @method find
@@ -10423,6 +10422,39 @@ DL.Collection.prototype.find = function(_id) {
1042310422
return promise;
1042410423
};
1042510424

10425+
/**
10426+
* Set the relationships that should be eager loaded.
10427+
* @method with
10428+
* @param {String} ...
10429+
* @return {DL.Collection}
10430+
*
10431+
* @example Simple relationship
10432+
*
10433+
* client.collection('books').with('author').each(function(book) {
10434+
* console.log("Author: ", book.author.name);
10435+
* });
10436+
*
10437+
* @example Multiple relationships
10438+
*
10439+
* client.collection('books').with('author', 'publisher').each(function(book) {
10440+
* console.log("Author: ", book.author.name);
10441+
* console.log("Publisher: ", book.publisher.name);
10442+
* });
10443+
*
10444+
* @example Nested relationships
10445+
*
10446+
* client.collection('books').with('author.contacts').each(function(book) {
10447+
* console.log("Author: ", book.author.name);
10448+
* console.log("Contacts: ", book.author.contacts);
10449+
* });
10450+
*
10451+
*/
10452+
DL.Collection.prototype.with = function() {
10453+
this.options.with = arguments;
10454+
return this;
10455+
};
10456+
10457+
1042610458
/**
1042710459
* Group results by field
1042810460
* @method group
@@ -10610,6 +10642,7 @@ DL.Collection.prototype.reset = function() {
1061010642
this._group = [];
1061110643
this._limit = null;
1061210644
this._offset = null;
10645+
this._remember = null;
1061310646
return this;
1061410647
};
1061510648

@@ -10676,6 +10709,25 @@ DL.Collection.prototype.offset = function(int) {
1067610709
return this;
1067710710
};
1067810711

10712+
/**
10713+
* Indicate that the query results should be cached.
10714+
*
10715+
* @method remember
10716+
* @param {Number} minutes
10717+
* @return {DL.Collection} this
10718+
*
10719+
* @example Caching a query
10720+
*
10721+
* client.collection('posts').sort('updated_at', -1).limit(5).remember(10).then(function(data) {
10722+
* // ...
10723+
* });
10724+
*
10725+
*/
10726+
DL.Collection.prototype.remember = function(minutes) {
10727+
this._remember = minutes;
10728+
return this;
10729+
};
10730+
1067910731
/**
1068010732
* Get channel for this collection.
1068110733
* @method channel
@@ -10857,9 +10909,10 @@ DL.Collection.prototype._validateName = function(name) {
1085710909
DL.Collection.prototype.buildQuery = function() {
1085810910
var query = {};
1085910911

10860-
// apply limit / offset
10912+
// apply limit / offset and remember
1086110913
if (this._limit !== null) { query.limit = this._limit; }
1086210914
if (this._offset !== null) { query.offset = this._offset; }
10915+
if (this._remember !== null) { query.remember = this._remember; }
1086310916

1086410917
// apply wheres
1086510918
if (this.wheres.length > 0) {
@@ -10881,7 +10934,8 @@ DL.Collection.prototype.buildQuery = function() {
1088110934
first: 'f',
1088210935
aggregation: 'aggr',
1088310936
operation: 'op',
10884-
data: 'data'
10937+
data: 'data',
10938+
with: 'with'
1088510939
};
1088610940

1088710941
for (f in shortnames) {
@@ -10896,7 +10950,6 @@ DL.Collection.prototype.buildQuery = function() {
1089610950
return query;
1089710951
};
1089810952

10899-
1090010953
/**
1090110954
* module DL
1090210955
* class DL.CollectionItem
@@ -11093,6 +11146,7 @@ DL.Pagination.prototype.isFetching = function() {
1109311146
DL.Pagination.prototype.then = function() {
1109411147
};
1109511148

11149+
1109611150
/**
1109711151
* @module DL
1109811152
* @class DL.System

dist/dl.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/collection.js

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@ DL.Collection.prototype.where = function(objects, _operation, _value) {
125125
return this;
126126
};
127127

128-
129128
/**
130129
* Find first item by _id
131130
* @method find
@@ -156,6 +155,39 @@ DL.Collection.prototype.find = function(_id) {
156155
return promise;
157156
};
158157

158+
/**
159+
* Set the relationships that should be eager loaded.
160+
* @method with
161+
* @param {String} ...
162+
* @return {DL.Collection}
163+
*
164+
* @example Simple relationship
165+
*
166+
* client.collection('books').with('author').each(function(book) {
167+
* console.log("Author: ", book.author.name);
168+
* });
169+
*
170+
* @example Multiple relationships
171+
*
172+
* client.collection('books').with('author', 'publisher').each(function(book) {
173+
* console.log("Author: ", book.author.name);
174+
* console.log("Publisher: ", book.publisher.name);
175+
* });
176+
*
177+
* @example Nested relationships
178+
*
179+
* client.collection('books').with('author.contacts').each(function(book) {
180+
* console.log("Author: ", book.author.name);
181+
* console.log("Contacts: ", book.author.contacts);
182+
* });
183+
*
184+
*/
185+
DL.Collection.prototype.with = function() {
186+
this.options.with = arguments;
187+
return this;
188+
};
189+
190+
159191
/**
160192
* Group results by field
161193
* @method group
@@ -343,6 +375,7 @@ DL.Collection.prototype.reset = function() {
343375
this._group = [];
344376
this._limit = null;
345377
this._offset = null;
378+
this._remember = null;
346379
return this;
347380
};
348381

@@ -409,6 +442,25 @@ DL.Collection.prototype.offset = function(int) {
409442
return this;
410443
};
411444

445+
/**
446+
* Indicate that the query results should be cached.
447+
*
448+
* @method remember
449+
* @param {Number} minutes
450+
* @return {DL.Collection} this
451+
*
452+
* @example Caching a query
453+
*
454+
* client.collection('posts').sort('updated_at', -1).limit(5).remember(10).then(function(data) {
455+
* // ...
456+
* });
457+
*
458+
*/
459+
DL.Collection.prototype.remember = function(minutes) {
460+
this._remember = minutes;
461+
return this;
462+
};
463+
412464
/**
413465
* Get channel for this collection.
414466
* @method channel
@@ -590,9 +642,10 @@ DL.Collection.prototype._validateName = function(name) {
590642
DL.Collection.prototype.buildQuery = function() {
591643
var query = {};
592644

593-
// apply limit / offset
645+
// apply limit / offset and remember
594646
if (this._limit !== null) { query.limit = this._limit; }
595647
if (this._offset !== null) { query.offset = this._offset; }
648+
if (this._remember !== null) { query.remember = this._remember; }
596649

597650
// apply wheres
598651
if (this.wheres.length > 0) {
@@ -614,7 +667,8 @@ DL.Collection.prototype.buildQuery = function() {
614667
first: 'f',
615668
aggregation: 'aggr',
616669
operation: 'op',
617-
data: 'data'
670+
data: 'data',
671+
with: 'with'
618672
};
619673

620674
for (f in shortnames) {
@@ -628,4 +682,3 @@ DL.Collection.prototype.buildQuery = function() {
628682

629683
return query;
630684
};
631-

0 commit comments

Comments
 (0)