@onehat/data 1.8.32 → 1.8.34
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.
|
|
43
|
-
|
|
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() {
|
|
@@ -210,6 +210,19 @@ describe('MemoryRepository', function() {
|
|
|
210
210
|
expect(result.value).to.be.eq('four');
|
|
211
211
|
});
|
|
212
212
|
|
|
213
|
+
it.only('getIxById', function() {
|
|
214
|
+
this.repository.setPage(1);
|
|
215
|
+
this.repository.setPageSize(2);
|
|
216
|
+
let ix = this.repository.getIxById(1);
|
|
217
|
+
expect(ix).to.be.eq(0);
|
|
218
|
+
|
|
219
|
+
ix = this.repository.getIxById(2);
|
|
220
|
+
expect(ix).to.be.eq(1);
|
|
221
|
+
|
|
222
|
+
ix = this.repository.getIxById(3);
|
|
223
|
+
expect(ix).to.be.undefined;
|
|
224
|
+
});
|
|
225
|
+
|
|
213
226
|
it('getBy', function() {
|
|
214
227
|
const results = this.repository.getBy((entity) => {
|
|
215
228
|
return entity.id > 2;
|
|
@@ -652,6 +652,14 @@ describe('Repository Base', function() {
|
|
|
652
652
|
expect(result.value).to.be.eq('three');
|
|
653
653
|
});
|
|
654
654
|
|
|
655
|
+
it('getIxById', function() {
|
|
656
|
+
let ix = this.repository.getIxById(1);
|
|
657
|
+
expect(ix).to.be.eq(0);
|
|
658
|
+
|
|
659
|
+
ix = this.repository.getIxById(3);
|
|
660
|
+
expect(ix).to.be.eq(2);
|
|
661
|
+
});
|
|
662
|
+
|
|
655
663
|
it('getBy', function() {
|
|
656
664
|
const result = this.repository.getBy(entity => entity.id === 2 || entity.id === 3);
|
|
657
665
|
expect(result.length).to.be.eq(2);
|
package/package.json
CHANGED
package/src/Repository/Memory.js
CHANGED
|
@@ -417,36 +417,33 @@ class MemoryRepository extends Repository {
|
|
|
417
417
|
}
|
|
418
418
|
|
|
419
419
|
/**
|
|
420
|
-
* Get
|
|
421
|
-
*
|
|
422
|
-
* @
|
|
420
|
+
* Get a single Entity's index by its id.
|
|
421
|
+
* Overrides Repository method, to limite results to only the current page.
|
|
422
|
+
* @param {integer} id - id of record to retrieve
|
|
423
|
+
* @return {integer} The numerical index, or undefined
|
|
423
424
|
*/
|
|
424
|
-
|
|
425
|
+
getIxById = (id) => {
|
|
425
426
|
if (this.isDestroyed) {
|
|
426
|
-
throw Error('this.
|
|
427
|
+
throw Error('this.getIxById is no longer valid. Repository has been destroyed.');
|
|
427
428
|
}
|
|
428
|
-
|
|
429
|
+
|
|
430
|
+
const ix = this.getEntitiesOnPage().findIndex((entity) => entity.id === id);
|
|
431
|
+
if (ix >= 0) {
|
|
432
|
+
return ix;
|
|
433
|
+
}
|
|
434
|
+
return undefined;
|
|
429
435
|
}
|
|
430
|
-
/* */
|
|
431
436
|
|
|
432
437
|
/**
|
|
433
|
-
*
|
|
434
|
-
* with sorting and filtering applied.
|
|
438
|
+
* Override Repository.getEntities, so we can get an array of
|
|
439
|
+
* all *active* Entities, with sorting and filtering applied.
|
|
435
440
|
* @return {array} Entities that passed through filter
|
|
436
441
|
*/
|
|
437
|
-
|
|
442
|
+
getEntities = () => {
|
|
438
443
|
if (this.isDestroyed) {
|
|
439
|
-
throw Error('this.
|
|
444
|
+
throw Error('this.getEntities is no longer valid. Repository has been destroyed.');
|
|
440
445
|
}
|
|
441
|
-
|
|
442
|
-
if (!this.isPaginated) {
|
|
443
|
-
return entities;
|
|
444
|
-
}
|
|
445
|
-
const
|
|
446
|
-
pageIx = this.page -1, // zero-indexed page#
|
|
447
|
-
start = pageIx * this.pageSize,
|
|
448
|
-
end = start + this.pageSize;
|
|
449
|
-
return entities.slice(start, end);
|
|
446
|
+
return this._getActiveEntities();
|
|
450
447
|
}
|
|
451
448
|
/* */
|
|
452
449
|
|
|
@@ -1166,6 +1166,23 @@ export default class Repository extends EventEmitter {
|
|
|
1166
1166
|
return this.getFirstBy(entity => entity.id === id);
|
|
1167
1167
|
}
|
|
1168
1168
|
|
|
1169
|
+
/**
|
|
1170
|
+
* Get a single Entity's index by its id.
|
|
1171
|
+
* @param {integer} id - id of record to retrieve
|
|
1172
|
+
* @return {integer} The numerical index, or undefined
|
|
1173
|
+
*/
|
|
1174
|
+
getIxById = (id) => {
|
|
1175
|
+
if (this.isDestroyed) {
|
|
1176
|
+
throw Error('this.getIxById is no longer valid. Repository has been destroyed.');
|
|
1177
|
+
}
|
|
1178
|
+
|
|
1179
|
+
const ix = this.entities.findIndex((entity) => entity.id === id);
|
|
1180
|
+
if (ix >= 0) {
|
|
1181
|
+
return ix;
|
|
1182
|
+
}
|
|
1183
|
+
return undefined;
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1169
1186
|
/**
|
|
1170
1187
|
* Get an array of Entities by supplied filter function
|
|
1171
1188
|
* @param {function} filter - Filter function to apply to all entities
|
|
@@ -1224,6 +1241,41 @@ export default class Repository extends EventEmitter {
|
|
|
1224
1241
|
return _.filter(this.entities, entity => !entity.isPersisted);
|
|
1225
1242
|
}
|
|
1226
1243
|
|
|
1244
|
+
/**
|
|
1245
|
+
* Get an array of all Entities.
|
|
1246
|
+
* Can be overridden by subclasses.
|
|
1247
|
+
* @return {array} Entities that passed through filter
|
|
1248
|
+
*/
|
|
1249
|
+
getEntities = () => {
|
|
1250
|
+
if (this.isDestroyed) {
|
|
1251
|
+
throw Error('this.getEntities is no longer valid. Repository has been destroyed.');
|
|
1252
|
+
}
|
|
1253
|
+
return this.entities;
|
|
1254
|
+
}
|
|
1255
|
+
/* */
|
|
1256
|
+
|
|
1257
|
+
/**
|
|
1258
|
+
* Get an array of all Entities on current page,
|
|
1259
|
+
* which for the base Repository, means all entities.
|
|
1260
|
+
* Subclasses may change this behavior.
|
|
1261
|
+
* @return {array} Entities
|
|
1262
|
+
*/
|
|
1263
|
+
getEntitiesOnPage = () => {
|
|
1264
|
+
if (this.isDestroyed) {
|
|
1265
|
+
throw Error('this.getPagedEntities is no longer valid. Repository has been destroyed.');
|
|
1266
|
+
}
|
|
1267
|
+
const entities = this.getEntities();
|
|
1268
|
+
if (!this.isPaginated) {
|
|
1269
|
+
return entities;
|
|
1270
|
+
}
|
|
1271
|
+
const
|
|
1272
|
+
pageIx = this.page -1, // zero-indexed page#
|
|
1273
|
+
start = pageIx * this.pageSize,
|
|
1274
|
+
end = start + this.pageSize;
|
|
1275
|
+
return entities.slice(start, end);
|
|
1276
|
+
}
|
|
1277
|
+
/* */
|
|
1278
|
+
|
|
1227
1279
|
/**
|
|
1228
1280
|
* Get all dirty (having unsaved changes) Entities
|
|
1229
1281
|
* @param {array} entities - Array of entities to filter. Optional. Defaults to this.entities
|