@onehat/data 1.19.22 → 1.19.25

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.
@@ -1114,6 +1114,38 @@ describe('Repository Base', function() {
1114
1114
  const str = this.repository.toString();
1115
1115
  expect(str).to.be.eq('NullRepository {bar} - foo');
1116
1116
  });
1117
+
1118
+ it('_insertBefore', function() {
1119
+
1120
+ const
1121
+ repository = this.repository,
1122
+ one = { id: 'one' },
1123
+ two = { id: 'two' },
1124
+ three = { id: 'three' };
1125
+
1126
+ // insert into empty array
1127
+ repository.entities = [];
1128
+ repository._insertBefore(one);
1129
+ expect(repository.entities.length).to.be.eq(1);
1130
+ expect(repository.entities[0]).to.be.eq(one);
1131
+
1132
+ // insert into array with one
1133
+ repository.entities = [two];
1134
+ repository._insertBefore(one, two);
1135
+ expect(repository.entities.length).to.be.eq(2);
1136
+ expect(repository.entities[0]).to.be.eq(one);
1137
+ expect(repository.entities[1]).to.be.eq(two);
1138
+
1139
+ // insert into middle of array
1140
+ repository.entities = [one, three];
1141
+ repository._insertBefore(two, three);
1142
+ expect(repository.entities.length).to.be.eq(3);
1143
+ expect(repository.entities[0]).to.be.eq(one);
1144
+ expect(repository.entities[1]).to.be.eq(two);
1145
+ expect(repository.entities[2]).to.be.eq(three);
1146
+
1147
+ repository.entities = []; // because the entities aren't real entities and we don't want a destory error
1148
+ });
1117
1149
  });
1118
1150
 
1119
1151
  describe('tree', function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.19.22",
3
+ "version": "1.19.25",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -277,20 +277,24 @@ class LocalFromRemoteRepository extends EventEmitter {
277
277
  // NORMAL PROCESS, basically call super()
278
278
  // This adds to the local repository, so we can sync later,
279
279
  // if needed.
280
- const normalAdd = this._getActiveRepository().add(data);
280
+ const normalAdd = await this._getActiveRepository().add(data);
281
281
  if (this.mode !== MODE_COMMAND_QUEUE || !this.isOnline) {
282
282
  return normalAdd;
283
283
  }
284
284
 
285
285
  // MODE_COMMAND_QUEUE -- try to sync now!
286
- const entity = await normalAdd;
287
- return await this.sync(entity);
286
+ return await this.sync(normalAdd);
288
287
  }
289
288
 
290
289
  /**
291
290
  * Syncs local and remote repositories, based on operation mode.
292
291
  */
293
292
  sync = async (entity, callback = null) => {
293
+
294
+ if (this.debugMode) {
295
+ console.log('sync');
296
+ }
297
+
294
298
  try {
295
299
  if (!this.isOnline) {
296
300
  this._doAutoSync(true);
@@ -590,6 +590,11 @@ class MemoryRepository extends Repository {
590
590
  }
591
591
  }
592
592
 
593
+
594
+ _insertBefore = (newEntity, entity = null) => {
595
+ throw Error('Not yet implemented');
596
+ }
597
+
593
598
  /**
594
599
  * Gets the total number of Entities in the storage medium,
595
600
  * before any sorting or filtering is applied.
@@ -307,6 +307,45 @@ class OneBuildRepository extends AjaxRepository {
307
307
  });
308
308
  }
309
309
 
310
+ remoteDuplicate = async (entity) => {
311
+
312
+ this.markLoading();
313
+
314
+ const
315
+ Model = this.getSchema().name,
316
+ id = entity.id,
317
+ result = await this._send('POST', Model + '/duplicate', { id });
318
+
319
+ if (!result) {
320
+ this.markLoading(false);
321
+ this.throwError('error duplicating on server');
322
+ return;
323
+ }
324
+
325
+ const {
326
+ root,
327
+ success,
328
+ total,
329
+ message
330
+ } = this._processServerResponse(result);
331
+
332
+ if (!success) {
333
+ this.markLoading(false);
334
+ throw Error(message);
335
+ }
336
+
337
+ // Click duplicateId. The new row appears directly above the one that was duplicated
338
+ // The new duplicate is selected
339
+ // The display field should read "{old name} (duplicate)"
340
+
341
+
342
+ const duplicateEntity = await this.createStandaloneEntity(root, true, true);
343
+ this._insertBefore(duplicateEntity, entity);
344
+
345
+ this.markLoading(false);
346
+ return duplicateEntity;
347
+ }
348
+
310
349
  getSingleEntityFromServer = async (id) => {
311
350
  if (this.isDestroyed) {
312
351
  this.throwError('this.getSingleEntityFromServer is no longer valid. Repository has been destroyed.');
@@ -317,6 +356,12 @@ class OneBuildRepository extends AjaxRepository {
317
356
  return;
318
357
  }
319
358
 
359
+ if (!id) {
360
+ return null;
361
+ }
362
+
363
+ this.markLoading();
364
+
320
365
  const idPropertyName = this.getSchema().model.idProperty;
321
366
  const params = {};
322
367
  params['conditions[' + idPropertyName + ']'] = id;
@@ -324,7 +369,7 @@ class OneBuildRepository extends AjaxRepository {
324
369
  const data = _.merge(params, this._baseParams);
325
370
 
326
371
  if (this.debugMode) {
327
- console.log('getSingleEntityFromServer', options);
372
+ console.log('getSingleEntityFromServer', data);
328
373
  }
329
374
 
330
375
  return this._send(this.methods.get, this.api.get, data)
@@ -347,7 +392,7 @@ class OneBuildRepository extends AjaxRepository {
347
392
  } = this._processServerResponse(result);
348
393
 
349
394
  if (!root[0]) {
350
- throw Error('Record does not exist!')
395
+ return null;
351
396
  }
352
397
 
353
398
  return this.createStandaloneEntity(root[0]);
@@ -1160,6 +1160,34 @@ export default class Repository extends EventEmitter {
1160
1160
  this.entities = [];
1161
1161
  }
1162
1162
 
1163
+ /**
1164
+ * Inserts the newEntity directly before entity on the current page.
1165
+ */
1166
+ _insertBefore = (newEntity, entity = null) => {
1167
+
1168
+ const
1169
+ currentEntities = this.getEntities(),
1170
+ foundIx = _.findIndex(currentEntities, ent => ent === entity),
1171
+ existingEntityIx = foundIx === -1 ? 0 : foundIx;
1172
+
1173
+ let firstHalf = [],
1174
+ secondHalf = [];
1175
+
1176
+ if (!currentEntities.length || existingEntityIx === 0) {
1177
+ firstHalf.push(newEntity);
1178
+ secondHalf = currentEntities;
1179
+ } else {
1180
+ firstHalf = _.slice(currentEntities, 0, existingEntityIx);
1181
+ firstHalf.push(newEntity);
1182
+ secondHalf = _.slice(currentEntities, existingEntityIx);
1183
+ }
1184
+
1185
+ this.entities = [
1186
+ ...firstHalf,
1187
+ ...secondHalf,
1188
+ ];
1189
+ }
1190
+
1163
1191
  /**
1164
1192
  * Deletes all locally cached entities in repository,
1165
1193
  * usually, the current "page".
@@ -1169,6 +1197,13 @@ export default class Repository extends EventEmitter {
1169
1197
  this._destroyEntities();
1170
1198
  }
1171
1199
 
1200
+
1201
+ // _ __ __
1202
+ // | | / /___ _/ /_ _____ _____
1203
+ // | | / / __ `/ / / / / _ \/ ___/
1204
+ // | |/ / /_/ / / /_/ / __(__ )
1205
+ // |___/\__,_/_/\__,_/\___/____/
1206
+
1172
1207
  /**
1173
1208
  * Gets an array of "submit" values objects for the entities
1174
1209
  * @return {array} map -