@onehat/data 1.4.3 → 1.4.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -414,29 +414,61 @@ describe('Repository Base', function() {
414
414
  });
415
415
 
416
416
  it('_calculatePaginationVars', function() {
417
- let results = this.Repository._calculatePaginationVars(15, 1, 10), // Capital "R"
418
- expected = {
419
- page: 1,
420
- pageSize: 10,
421
- total: 15,
422
- totalPages: 2,
423
- pageStart: 1,
424
- pageEnd: 10,
425
- pageTotal: 10,
426
- };
427
- expect(_.isEqual(results, expected)).to.be.true;
428
-
429
- results = this.Repository._calculatePaginationVars(15, 2, 10); // Capital "R"
430
- expected = {
431
- page: 2,
432
- pageSize: 10,
433
- total: 15,
434
- totalPages: 2,
435
- pageStart: 11,
436
- pageEnd: 15,
437
- pageTotal: 5,
438
- };
439
- expect(_.isEqual(results, expected)).to.be.true;
417
+ let results;
418
+
419
+ results = this.Repository._calculatePaginationVars(0, 1, 10);
420
+ expect(results.page).to.be.eq(1);
421
+ expect(results.pageSize).to.be.eq(10);
422
+ expect(results.total).to.be.eq(0);
423
+ expect(results.totalPages).to.be.eq(1);
424
+ expect(results.pageStart).to.be.eq(0);
425
+ expect(results.pageEnd).to.be.eq(0);
426
+ expect(results.pageTotal).to.be.eq(0);
427
+
428
+ results = this.Repository._calculatePaginationVars(15, 1, 10);
429
+ expect(results.page).to.be.eq(1);
430
+ expect(results.pageSize).to.be.eq(10);
431
+ expect(results.total).to.be.eq(15);
432
+ expect(results.totalPages).to.be.eq(2);
433
+ expect(results.pageStart).to.be.eq(1);
434
+ expect(results.pageEnd).to.be.eq(10);
435
+ expect(results.pageTotal).to.be.eq(10);
436
+
437
+ results = this.Repository._calculatePaginationVars(15, 2, 10);
438
+ expect(results.page).to.be.eq(2);
439
+ expect(results.pageSize).to.be.eq(10);
440
+ expect(results.total).to.be.eq(15);
441
+ expect(results.totalPages).to.be.eq(2);
442
+ expect(results.pageStart).to.be.eq(11);
443
+ expect(results.pageEnd).to.be.eq(15);
444
+ expect(results.pageTotal).to.be.eq(5);
445
+
446
+ results = this.Repository._calculatePaginationVars(15, 2, 5);
447
+ expect(results.page).to.be.eq(2);
448
+ expect(results.pageSize).to.be.eq(5);
449
+ expect(results.total).to.be.eq(15);
450
+ expect(results.totalPages).to.be.eq(3);
451
+ expect(results.pageStart).to.be.eq(6);
452
+ expect(results.pageEnd).to.be.eq(10);
453
+ expect(results.pageTotal).to.be.eq(5);
454
+
455
+ results = this.Repository._calculatePaginationVars(7, 2, 5);
456
+ expect(results.page).to.be.eq(2);
457
+ expect(results.pageSize).to.be.eq(5);
458
+ expect(results.total).to.be.eq(7);
459
+ expect(results.totalPages).to.be.eq(2);
460
+ expect(results.pageStart).to.be.eq(6);
461
+ expect(results.pageEnd).to.be.eq(7);
462
+ expect(results.pageTotal).to.be.eq(2);
463
+
464
+ results = this.Repository._calculatePaginationVars(15, 15, 1);
465
+ expect(results.page).to.be.eq(15);
466
+ expect(results.pageSize).to.be.eq(1);
467
+ expect(results.total).to.be.eq(15);
468
+ expect(results.totalPages).to.be.eq(15);
469
+ expect(results.pageStart).to.be.eq(15);
470
+ expect(results.pageEnd).to.be.eq(15);
471
+ expect(results.pageTotal).to.be.eq(1);
440
472
  });
441
473
 
442
474
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.4.3",
3
+ "version": "1.4.7",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/OneHatData.js CHANGED
@@ -282,12 +282,15 @@ export class OneHatData extends EventEmitter {
282
282
  if (this.isDestroyed) {
283
283
  throw new Error('this.createRepositories is no longer valid. OneHatData has been destroyed.');
284
284
  }
285
- let promises = [];
286
- _.each(schemas, (schema) => {
287
- promises.push( this.createRepository({ schema, }, bound) );
288
- });
289
-
290
- await Promise.all(promises);
285
+ const schemasArray = _.map(schemas, (schema) => schema);
286
+ let i, schema, repository;
287
+ for (i = 0; i < schemasArray.length; i++) {
288
+ schema = schemasArray[i];
289
+ repository = await this.createRepository({ schema, }, bound);
290
+ if (!repository) {
291
+ throw new Error('Repository could not be created');
292
+ }
293
+ }
291
294
  return this;
292
295
  }
293
296
 
@@ -292,8 +292,8 @@ class AjaxRepository extends Repository {
292
292
  */
293
293
  _onChangeSorters = () => {
294
294
  const sorter = this.sorters[0];
295
- this.setParam(this.paramSort, sorter.name);
296
- this.setParam(this.paramDirection, sorter.direction);
295
+ this.setParam(this.paramSort, sorter.name, true); // true to set baseParam
296
+ this.setParam(this.paramDirection, sorter.direction, true);
297
297
 
298
298
  if (this.isLoaded) {
299
299
  return this.reload();
@@ -319,8 +319,8 @@ class AjaxRepository extends Repository {
319
319
  * Refreshes entities.
320
320
  */
321
321
  _onChangePagination = () => {
322
- this.setParam(this.paramPageNum, this.page);
323
- this.setParam(this.paramPageSize, this.pageSize);
322
+ this.setParam(this.paramPageNum, this.page, true); // true to set baseParam
323
+ this.setParam(this.paramPageSize, this.pageSize, true);
324
324
 
325
325
  if (this.isLoaded) {
326
326
  return this.reload();
@@ -4,6 +4,21 @@ import AjaxRepository from './Ajax';
4
4
  import qs from 'qs';
5
5
  import _ from 'lodash';
6
6
 
7
+ const nonConditionFilters = [
8
+ 'q',
9
+ 'hydrate',
10
+ 'fields',
11
+ 'distinct',
12
+ 'leftJoinWith',
13
+ 'join',
14
+ 'where',
15
+ 'matching',
16
+ 'contain',
17
+ 'order',
18
+ 'limit',
19
+ 'page',
20
+ ];
21
+
7
22
  /**
8
23
  * This class contains overrides of specific functions in
9
24
  * AjaxRepository that are unique to OneBuild.
@@ -129,12 +144,16 @@ class OneBuildRepository extends AjaxRepository {
129
144
  // Clear existing "conditions" params
130
145
  if (!_.isEmpty(this._params)) {
131
146
  this._params = _.omitBy(this._params, (value, key) => {
132
- return key.match(/^conditions/);
147
+ return key.match(/^conditions/) || _.includes(nonConditionFilters, key);
133
148
  });
134
149
  }
135
150
 
136
151
  _.each(this.filters, (filter, ix) => {
137
- this.setParam('conditions[' + filter.name + ']', filter.value);
152
+ if (_.includes(nonConditionFilters, filter.name)) {
153
+ this.setParam(filter.name, filter.value);
154
+ } else {
155
+ this.setParam('conditions[' + filter.name + ']', filter.value);
156
+ }
138
157
  });
139
158
 
140
159
  if (this.isLoaded) {
@@ -779,12 +779,38 @@ export default class Repository extends EventEmitter {
779
779
  * @static
780
780
  */
781
781
  static _calculatePaginationVars = (total, page, pageSize) => {
782
+
783
+ // Special case: empty pages
784
+ if (total < 1) {
785
+ return {
786
+ page,
787
+ pageSize,
788
+ total,
789
+ totalPages: 1,
790
+ pageStart: 0,
791
+ pageEnd: 0,
792
+ pageTotal: 0,
793
+ };
794
+ }
795
+
782
796
  const totalPages = Math.ceil(total / pageSize),
783
- pageStart = !total ? 0 : ((page -1) * pageSize) + 1,
784
- remainder = total > pageSize ? total % pageSize : 0,
785
- pageTotal = (page === 1 && totalPages === 1) ? total :
786
- (page < totalPages ? pageSize : remainder),
787
- pageEnd = !total ? 0 : pageStart -1 + pageTotal;
797
+ pageStart = ((page -1) * pageSize) + 1;
798
+
799
+ let remainder,
800
+ pageEnd,
801
+ pageTotal;
802
+
803
+ if (page === 1 && totalPages === 1) {
804
+ pageTotal = total;
805
+ } else if (page < totalPages) {
806
+ pageTotal = pageSize;
807
+ } else {
808
+ // last page
809
+ remainder = total % pageSize;
810
+ pageTotal = remainder || pageSize;
811
+ }
812
+
813
+ pageEnd = pageStart + pageTotal -1;
788
814
 
789
815
  return {
790
816
  page,
@@ -1302,6 +1328,9 @@ export default class Repository extends EventEmitter {
1302
1328
  if (!_.isArray(entities)) {
1303
1329
  entities = [entities];
1304
1330
  }
1331
+ if (!entities.length) {
1332
+ return;
1333
+ }
1305
1334
  _.each(entities, (entity) => {
1306
1335
  entity.markDeleted(); // Entity is still there, it's just marked for deletion
1307
1336
  });