@onehat/data 1.10.2 → 1.10.4

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.
@@ -8,9 +8,9 @@ import KeyValues from '../../src/Schema/KeyValues.js';
8
8
 
9
9
  // NOTE: Cypress can't handle async functions for beforeEach,
10
10
  // so we have to manually apply it to every test. Ugh!
11
- async function beforeEach() {
12
- this.oneHatData = new OneHatData();
13
- this.schema = this.oneHatData.createSchema({
11
+ async function beforeEach(that) {
12
+ that.oneHatData = new OneHatData();
13
+ that.schema = that.oneHatData.createSchema({
14
14
  name: 'bar',
15
15
  model: {
16
16
  idProperty: 'key',
@@ -22,14 +22,14 @@ async function beforeEach() {
22
22
  },
23
23
  repository: 'memory',
24
24
  });
25
- await this.oneHatData.createRepository({
25
+ await that.oneHatData.createRepository({
26
26
  id: 'foo',
27
- schema: this.schema,
27
+ schema: that.schema,
28
28
  }, true);
29
- this.repository = this.oneHatData.getRepositoryById('foo');
29
+ that.repository = that.oneHatData.getRepositoryById('foo');
30
30
  }
31
- function afterEach() {
32
- window.oneHatData.destroy();
31
+ function afterEach(that) {
32
+ that.oneHatData.destroy();
33
33
  }
34
34
 
35
35
  describe('OneHatData', function() {
@@ -250,15 +250,30 @@ describe('OneHatData', function() {
250
250
 
251
251
  it('getRepository', function() {
252
252
  (async function() {
253
+
253
254
  await beforeEach();
254
255
 
255
- const result = this.oneHatData.getRepository('bar');
256
- expect(result).to.be.eq(this.repository);
256
+ const result = that.oneHatData.getRepository('bar');
257
+ expect(result).to.be.eq(that.repository);
257
258
 
258
259
  afterEach();
259
260
  })();
260
261
  });
261
262
 
263
+ it.only('getUniqueRepository', function() {
264
+ (async () => {
265
+ const that = {};
266
+ await beforeEach(that);
267
+
268
+ const
269
+ repo1 = that.oneHatData.getRepository('bar'),
270
+ repo2 = that.oneHatData.getRepository('bar', true);
271
+ expect(repo1 !== repo2).to.be.true;
272
+
273
+ afterEach(that);
274
+ })();
275
+ });
276
+
262
277
  it('getRepositoriesBy', function() {
263
278
  (async function() {
264
279
  await beforeEach();
@@ -345,7 +360,6 @@ describe('OneHatData', function() {
345
360
 
346
361
  it('isEntity', async function() {
347
362
  (async function() {
348
- debugger;
349
363
  await beforeEach();
350
364
  const oneHatData = this.oneHatData;
351
365
  const repository = oneHatData.getRepository('bar');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.10.2",
3
+ "version": "1.10.4",
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/Entity.js CHANGED
@@ -23,7 +23,7 @@ import _ from 'lodash';
23
23
  * - entity.getPropertySubmitValue('users__last_name');
24
24
  *
25
25
  * @extends EventEmitter
26
- * @fires ['change', 'reset', 'save', 'delete', 'destroy']
26
+ * @fires ['change', 'changeValidity', 'reset', 'reload', 'save', 'delete', 'undelete', 'destroy']
27
27
  */
28
28
  class Entity extends EventEmitter {
29
29
 
package/src/OneHatData.js CHANGED
@@ -12,6 +12,9 @@ import {
12
12
  default as Schema,
13
13
  CoreSchemas,
14
14
  } from './Schema/index.js';
15
+ import {
16
+ v4 as uuid,
17
+ } from 'uuid';
15
18
  import _ from 'lodash';
16
19
 
17
20
  /**
@@ -443,7 +446,7 @@ export class OneHatData extends EventEmitter {
443
446
  * @param {string} name - Name of Schema
444
447
  * @return {Repository} repository
445
448
  */
446
- getRepository = (name) => {
449
+ getRepository = (name, unique = false) => {
447
450
  if (this.isDestroyed) {
448
451
  throw new Error('this.getRepository is no longer valid. OneHatData has been destroyed.');
449
452
  }
@@ -451,6 +454,16 @@ export class OneHatData extends EventEmitter {
451
454
  if (!schema) {
452
455
  return null;
453
456
  }
457
+ if (unique) {
458
+ const
459
+ repoToClone = schema.getBoundRepository(),
460
+ clone = _.cloneDeep(repoToClone);
461
+
462
+ const id = uuid();
463
+ clone.name = clone.name + '-' + id;
464
+ clone.id = id;
465
+ return clone;
466
+ }
454
467
  return schema.getBoundRepository();
455
468
  }
456
469
 
@@ -8,7 +8,7 @@ import _ from 'lodash';
8
8
  * This class should not be instantiated directly.
9
9
  * Rather, instantiate a subclass, like StringProperty
10
10
  * @extends EventEmitter
11
- * @fires ['change', 'destroy']
11
+ * @fires ['change', 'changeValidity', 'destroy']
12
12
  */
13
13
  export default class Property extends EventEmitter {
14
14
 
@@ -253,8 +253,11 @@ class MemoryRepository extends Repository {
253
253
  static _getNatSort = (sorter) => {
254
254
  const
255
255
  name = sorter.name,
256
- direction = sorter.direction.toUpperCase(),
257
- sortFn = natsort.default({ desc: direction !== 'ASC', });
256
+ direction = sorter.direction.toUpperCase();
257
+
258
+ const ns = natsort.default || natsort; // When imported into cypress, natsort has default, when in React Native, it doesn't.
259
+
260
+ const sortFn = ns({ desc: direction !== 'ASC', });
258
261
  return (entity1, entity2) => {
259
262
  const
260
263
  a = entity1[name],