@onehat/data 1.8.33 → 1.8.35

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.
@@ -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;
@@ -229,6 +229,16 @@ describe('Repository Base', function() {
229
229
  expect(didFireChangeSorters).to.be.true;
230
230
  });
231
231
 
232
+ it('getSortField', function() {
233
+ const sortField = this.repository.getSortField();
234
+ expect(sortField).to.be.eq('value');
235
+ });
236
+
237
+ it('getSortDirection', function() {
238
+ const sortDirection = this.repository.getSortDirection();
239
+ expect(sortDirection).to.be.eq('ASC');
240
+ });
241
+
232
242
  });
233
243
 
234
244
  describe('filtering', function() {
@@ -652,6 +662,14 @@ describe('Repository Base', function() {
652
662
  expect(result.value).to.be.eq('three');
653
663
  });
654
664
 
665
+ it('getIxById', function() {
666
+ let ix = this.repository.getIxById(1);
667
+ expect(ix).to.be.eq(0);
668
+
669
+ ix = this.repository.getIxById(3);
670
+ expect(ix).to.be.eq(2);
671
+ });
672
+
655
673
  it('getBy', function() {
656
674
  const result = this.repository.getBy(entity => entity.id === 2 || entity.id === 3);
657
675
  expect(result.length).to.be.eq(2);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.8.33",
3
+ "version": "1.8.35",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -416,6 +416,24 @@ class MemoryRepository extends Repository {
416
416
  return this._keyedEntities[id] ? this._keyedEntities[id] : null;
417
417
  }
418
418
 
419
+ /**
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
424
+ */
425
+ getIxById = (id) => {
426
+ if (this.isDestroyed) {
427
+ throw Error('this.getIxById is no longer valid. Repository has been destroyed.');
428
+ }
429
+
430
+ const ix = this.getEntitiesOnPage().findIndex((entity) => entity.id === id);
431
+ if (ix >= 0) {
432
+ return ix;
433
+ }
434
+ return undefined;
435
+ }
436
+
419
437
  /**
420
438
  * Override Repository.getEntities, so we can get an array of
421
439
  * all *active* Entities, with sorting and filtering applied.
@@ -305,7 +305,7 @@ export default class Repository extends EventEmitter {
305
305
  */
306
306
  _createStatics = () => {
307
307
  if (this.isDestroyed) {
308
- throw Error('this._createStatics is no longer valid. Entity has been destroyed.');
308
+ throw Error('this._createStatics is no longer valid. Repository has been destroyed.');
309
309
  }
310
310
  const staticsDefinitions = this.schema.repository.statics || this.originalConfig.statics; // The latter is mainly for lfr repositories
311
311
  if (!_.isEmpty(staticsDefinitions)) {
@@ -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
@@ -1310,11 +1327,39 @@ export default class Repository extends EventEmitter {
1310
1327
  */
1311
1328
  getSchema = () => {
1312
1329
  if (this.isDestroyed) {
1313
- throw Error('this.getSchema is no longer valid. Entity has been destroyed.');
1330
+ throw Error('this.getSchema is no longer valid. Repository has been destroyed.');
1314
1331
  }
1315
1332
  return this.schema;
1316
1333
  }
1317
1334
 
1335
+ /**
1336
+ * Gets the sort field, if only one sorter is applied.
1337
+ * @return {Schema} schema
1338
+ */
1339
+ getSortField = () => {
1340
+ if (this.isDestroyed) {
1341
+ throw Error('this.getSortField is no longer valid. Repository has been destroyed.');
1342
+ }
1343
+ if (!this.allowsMultiSort || this.sorters.length < 1) {
1344
+ return null;
1345
+ }
1346
+ return this.sorters[0].name;
1347
+ }
1348
+
1349
+ /**
1350
+ * Gets the sort direction, if only one sorter is applied.
1351
+ * @return {Schema} schema
1352
+ */
1353
+ getSortDirection = () => {
1354
+ if (this.isDestroyed) {
1355
+ throw Error('this.getSortDirection is no longer valid. Repository has been destroyed.');
1356
+ }
1357
+ if (!this.allowsMultiSort || this.sorters.length < 1) {
1358
+ return null;
1359
+ }
1360
+ return this.sorters[0].direction;
1361
+ }
1362
+
1318
1363
  /**
1319
1364
  * Gets the associated Repository
1320
1365
  * @param {string} repositoryName - Name of the Repository to retrieve
@@ -1322,7 +1367,7 @@ export default class Repository extends EventEmitter {
1322
1367
  */
1323
1368
  getAssociatedRepository = (repositoryName) => {
1324
1369
  if (this.isDestroyed) {
1325
- throw Error('this.getAssociatedRepository is no longer valid. Entity has been destroyed.');
1370
+ throw Error('this.getAssociatedRepository is no longer valid. Repository has been destroyed.');
1326
1371
  }
1327
1372
 
1328
1373
  const schema = this.getSchema();