@onehat/data 1.19.3 → 1.19.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.
@@ -30,7 +30,35 @@ describe('Schema', function() {
30
30
  expect(_.isEqual(propertyDefinition.name, 'groups_users__id')).to.be.true;
31
31
  });
32
32
 
33
- it.only('getTitles, getVirtualdPropertyNames, getIsFilteringDisabledPropertyNames, getIsEditingDisabledPropertyNames, getFieldGroupNames, getFilterTypes', function() {
33
+ it('getDefaultValues', function() {
34
+ const
35
+ defaultValues = this.schema.getDefaultValues(),
36
+ expectedDefaultValues = {
37
+ groups_users__id: null,
38
+ groups_users__group_id: null,
39
+ groups_users__user_id: null,
40
+ groups__id: null,
41
+ groups__name: 'default_group_name',
42
+ groups__description: null,
43
+ users__id: null,
44
+ users__username: 'default_username',
45
+ users__password: null,
46
+ users__full_name: null,
47
+ users__first_name: null,
48
+ users__last_name: null,
49
+ users__job_title: null,
50
+ users__email: null,
51
+ users__login_count: null,
52
+ users__last_login: null,
53
+ };
54
+
55
+ console.log('defaultValues', defaultValues);
56
+ console.log('expectedDefaultValues', expectedDefaultValues);
57
+
58
+ expect(defaultValues).to.be.eql(expectedDefaultValues);
59
+ });
60
+
61
+ it('getTitles, getVirtualdPropertyNames, getIsFilteringDisabledPropertyNames, getIsEditingDisabledPropertyNames, getFieldGroupNames, getFilterTypes', function() {
34
62
  const
35
63
  schema = new Schema({
36
64
  id: 'foo',
@@ -11,10 +11,10 @@ const GroupsUsers = {
11
11
  { name: 'groups_users__group_id', mapping: 'group_id', type: 'int' },
12
12
  { name: 'groups_users__user_id', mapping: 'user_id', type: 'int' },
13
13
  { name: 'groups__id', mapping: 'group.id', type: 'int' },
14
- { name: 'groups__name', mapping: 'group.name', type: 'string' },
14
+ { name: 'groups__name', mapping: 'group.name', type: 'string', defaultValue: 'default_group_name', },
15
15
  { name: 'groups__description', mapping: 'group.description', type: 'string' },
16
16
  { name: 'users__id', mapping: 'user.id', type: 'int' },
17
- { name: 'users__username', mapping: 'user.username', type: 'string' },
17
+ { name: 'users__username', mapping: 'user.username', type: 'string', defaultValue: 'default_username', },
18
18
  { name: 'users__password', mapping: 'user.password', type: 'string' },
19
19
  { name: 'users__full_name', mapping: 'user.full_name', type: 'string' },
20
20
  { name: 'users__first_name', mapping: 'user.first_name', type: 'string' },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.19.3",
3
+ "version": "1.19.4",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -459,7 +459,7 @@ class MemoryRepository extends Repository {
459
459
 
460
460
  /**
461
461
  * Get a single Entity's index by its id.
462
- * Overrides Repository method, to limite results to only the current page.
462
+ * Overrides Repository method, to limit results to only the current page.
463
463
  * @param {integer} id - id of record to retrieve
464
464
  * @return {integer} The numerical index, or undefined
465
465
  */
@@ -236,6 +236,10 @@ class OneBuildRepository extends AjaxRepository {
236
236
  };
237
237
  }
238
238
 
239
+ if (!result) {
240
+ debugger;
241
+ }
242
+
239
243
  const
240
244
  response = _.isPlainObject(result.data) ? result.data : this.reader.read(result.data),
241
245
  root = response[this.rootProperty],
@@ -1,6 +1,7 @@
1
1
  /** @module Schema */
2
2
 
3
3
  import EventEmitter from '@onehat/events';
4
+ import PropertyTypes from '../Property/index.js';
4
5
  import _ from 'lodash';
5
6
 
6
7
  /**
@@ -255,6 +256,34 @@ export default class Schema extends EventEmitter {
255
256
  return found;
256
257
  }
257
258
 
259
+ getDefaultValues = () => {
260
+ if (this.isDestroyed) {
261
+ this.throwError('this.getDefaultValues is no longer valid. Schema has been destroyed.');
262
+ return;
263
+ }
264
+
265
+ const found = {};
266
+ _.each(this.model.properties, (property) => {
267
+ let defaultValue = null;
268
+ if (!_.isNil(property.defaultValue)) {
269
+ defaultValue = property.defaultValue;
270
+ } else {
271
+ // Look in the property types for a default value
272
+ let propertyType = PropertyTypes[property.type];
273
+ if (!_.isNil(propertyType)) {
274
+ defaultValue = propertyType.defaultValue;
275
+ } else {
276
+ propertyType = PropertyTypes['string'];
277
+ if (!_.isNil(propertyType.defaultValue)) {
278
+ defaultValue = propertyType.defaultValue;
279
+ }
280
+ }
281
+ }
282
+ found[property.name] = defaultValue;
283
+ });
284
+ return found;
285
+ }
286
+
258
287
  getVirtualPropertyNames = () => {
259
288
  if (this.isDestroyed) {
260
289
  this.throwError('this.getVirtualPropertyNames is no longer valid. Schema has been destroyed.');