@onehat/data 1.8.31 → 1.8.33

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.
@@ -39,8 +39,12 @@ describe('Entity', function() {
39
39
  });
40
40
 
41
41
  afterEach(function() {
42
- this.entity.destroy();
43
- this.schema.destroy();
42
+ if (!this.entity.isDestroyed) {
43
+ this.entity.destroy();
44
+ }
45
+ if (!this.schema.isDestroyed) {
46
+ this.schema.destroy();
47
+ }
44
48
  });
45
49
 
46
50
  describe('constructor', function() {
@@ -8,6 +8,12 @@ describe('MemoryRepository', function() {
8
8
  model: {
9
9
  idProperty: 'key',
10
10
  displayProperty: 'value',
11
+ sorters: [
12
+ {
13
+ name: 'key',
14
+ direction: 'ASC',
15
+ },
16
+ ],
11
17
  properties: [
12
18
  { name: 'key', type: 'int' },
13
19
  { name: 'value' },
@@ -19,6 +25,7 @@ describe('MemoryRepository', function() {
19
25
  id: 'foo',
20
26
  schema: this.schema,
21
27
  isPaginated: true,
28
+ isSorted: true,
22
29
  data: [
23
30
  { key: 1, value: 'one', },
24
31
  { key: 2, value: 'two', },
@@ -148,11 +155,19 @@ describe('MemoryRepository', function() {
148
155
 
149
156
  // setPageSize
150
157
  this.repository.setPageSize(2);
158
+ this.repository.setPage(2);
151
159
  expect(this.repository.pageTotal).to.be.eq(2);
160
+ let entitiesOnPage = this.repository.getEntitiesOnPage();
161
+ expect(_.size(entitiesOnPage)).to.be.eq(2);
162
+
163
+ expect(entitiesOnPage[0].id).to.be.eq(3);
164
+ expect(entitiesOnPage[1].id).to.be.eq(4);
152
165
 
153
166
  // setPage
154
167
  this.repository.setPage(3);
155
168
  expect(this.repository.pageTotal).to.be.eq(1);
169
+ entitiesOnPage = this.repository.getEntitiesOnPage();
170
+ expect(_.size(entitiesOnPage)).to.be.eq(1);
156
171
 
157
172
  // prevPage
158
173
  this.repository.prevPage();
@@ -206,7 +221,7 @@ describe('MemoryRepository', function() {
206
221
  const result = this.repository.getFirstBy((entity) => {
207
222
  return entity.id > 2;
208
223
  });
209
- expect(result.id).to.be.eq(5);
224
+ expect(result.id).to.be.eq(3);
210
225
  });
211
226
 
212
227
  it('Repository.* retrieve methods, after delete/add', async function() {
@@ -465,9 +480,13 @@ describe('MemoryRepository', function() {
465
480
 
466
481
  });
467
482
 
468
- it('toString', function() {
469
- const str = this.repository.toString();
470
- expect(str).to.be.eq('MemoryRepository {bar} - foo');
483
+ describe('toString', function() {
484
+
485
+ it('toString', function() {
486
+ const str = this.repository.toString();
487
+ expect(str).to.be.eq('MemoryRepository {bar} - foo');
488
+ });
489
+
471
490
  });
472
491
 
473
492
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.8.31",
3
+ "version": "1.8.33",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -417,13 +417,13 @@ class MemoryRepository extends Repository {
417
417
  }
418
418
 
419
419
  /**
420
- * Get an array of all active Entities,
421
- * with sorting and filtering applied.
420
+ * Override Repository.getEntities, so we can get an array of
421
+ * all *active* Entities, with sorting and filtering applied.
422
422
  * @return {array} Entities that passed through filter
423
423
  */
424
424
  getEntities = () => {
425
425
  if (this.isDestroyed) {
426
- throw Error('this.getAll is no longer valid. Repository has been destroyed.');
426
+ throw Error('this.getEntities is no longer valid. Repository has been destroyed.');
427
427
  }
428
428
  return this._getActiveEntities();
429
429
  }
@@ -564,9 +564,6 @@ class MemoryRepository extends Repository {
564
564
 
565
565
  destroy() {
566
566
  // objects associated with this RepositoryType
567
- _.each(this._keyedEntities, (entity) => {
568
- entity.destroy();
569
- })
570
567
  this._keyedEntities = null;
571
568
  this._unsortedEntities = null;
572
569
  this._filteredEntities = null;
@@ -1224,6 +1224,41 @@ export default class Repository extends EventEmitter {
1224
1224
  return _.filter(this.entities, entity => !entity.isPersisted);
1225
1225
  }
1226
1226
 
1227
+ /**
1228
+ * Get an array of all Entities.
1229
+ * Can be overridden by subclasses.
1230
+ * @return {array} Entities that passed through filter
1231
+ */
1232
+ getEntities = () => {
1233
+ if (this.isDestroyed) {
1234
+ throw Error('this.getEntities is no longer valid. Repository has been destroyed.');
1235
+ }
1236
+ return this.entities;
1237
+ }
1238
+ /* */
1239
+
1240
+ /**
1241
+ * Get an array of all Entities on current page,
1242
+ * which for the base Repository, means all entities.
1243
+ * Subclasses may change this behavior.
1244
+ * @return {array} Entities
1245
+ */
1246
+ getEntitiesOnPage = () => {
1247
+ if (this.isDestroyed) {
1248
+ throw Error('this.getPagedEntities is no longer valid. Repository has been destroyed.');
1249
+ }
1250
+ const entities = this.getEntities();
1251
+ if (!this.isPaginated) {
1252
+ return entities;
1253
+ }
1254
+ const
1255
+ pageIx = this.page -1, // zero-indexed page#
1256
+ start = pageIx * this.pageSize,
1257
+ end = start + this.pageSize;
1258
+ return entities.slice(start, end);
1259
+ }
1260
+ /* */
1261
+
1227
1262
  /**
1228
1263
  * Get all dirty (having unsaved changes) Entities
1229
1264
  * @param {array} entities - Array of entities to filter. Optional. Defaults to this.entities