@onehat/data 1.4.2 → 1.4.6

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.2",
3
+ "version": "1.4.6",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -47,8 +47,7 @@
47
47
  "numeral": "^2.0.6",
48
48
  "qs": "^6.10.1",
49
49
  "relative-time-parser": "^1.0.13",
50
- "uuid-random": "^1.3.2",
51
- "uuidv4": "^3.0.1"
50
+ "uuid": "^8.3.2"
52
51
  },
53
52
  "devDependencies": {
54
53
  "@babel/core": "^7.14.6",
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
 
@@ -4,7 +4,9 @@ import EventEmitter from '@onehat/events';
4
4
  import Repository from '../Repository';
5
5
  import Command from './Command';
6
6
  import moment from 'relative-time-parser'; // Notice this version of moment is imported from 'relative-time-parser', and may be out of sync with our general 'moment' package
7
- import uuid from 'uuid-random'; // TEMP, until Expo supports cypto.getRandomValues() - https://github.com/uuidjs/uuid/issues/375
7
+ import {
8
+ v4 as uuid,
9
+ } from 'uuid';
8
10
  import _ from 'lodash';
9
11
 
10
12
  export const MODE_LOCAL_MIRROR = 'MODE_LOCAL_MIRROR';
@@ -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) {
@@ -2,7 +2,9 @@
2
2
 
3
3
  import EventEmitter from '@onehat/events';
4
4
  import Entity from '../Entity';
5
- import uuid from 'uuid-random'; // TEMP, until Expo supports cypto.getRandomValues() - https://github.com/uuidjs/uuid/issues/375
5
+ import {
6
+ v4 as uuid,
7
+ } from 'uuid';
6
8
  import _ from 'lodash';
7
9
 
8
10
  /**
@@ -777,12 +779,38 @@ export default class Repository extends EventEmitter {
777
779
  * @static
778
780
  */
779
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
+
780
796
  const totalPages = Math.ceil(total / pageSize),
781
- pageStart = !total ? 0 : ((page -1) * pageSize) + 1,
782
- remainder = total > pageSize ? total % pageSize : 0,
783
- pageTotal = (page === 1 && totalPages === 1) ? total :
784
- (page < totalPages ? pageSize : remainder),
785
- 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;
786
814
 
787
815
  return {
788
816
  page,