@nymphjs/nymph 1.0.0-beta.85 → 1.0.0-beta.87

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.
@@ -2,7 +2,6 @@ import fs from 'node:fs';
2
2
  import { difference } from 'lodash-es';
3
3
  import ReadLines from 'n-readlines';
4
4
  import strtotime from 'locutus/php/datetime/strtotime.js';
5
- import { guid } from '@nymphjs/guid';
6
5
 
7
6
  import type Nymph from '../Nymph.js';
8
7
  import type { Selector, Options, FormattedSelector } from '../Nymph.types.js';
@@ -609,7 +608,7 @@ export default abstract class NymphDriver {
609
608
  return true;
610
609
  }
611
610
  // Search through arrays and objects looking for the reference.
612
- if (Array.isArray(value) || typeof value === 'object') {
611
+ if (value != null && (Array.isArray(value) || typeof value === 'object')) {
613
612
  for (const key in value) {
614
613
  if (this.entityReferenceSearch(value[key], guids)) {
615
614
  return true;
@@ -985,7 +984,7 @@ export default abstract class NymphDriver {
985
984
  if (this.putDataCounter == 100) {
986
985
  throw new Error('Infinite loop detected in Entity loading.');
987
986
  }
988
- entity.$putData(data, sdata);
987
+ entity.$putData(data, sdata, 'server');
989
988
  this.putDataCounter--;
990
989
  if (this.nymph.config.cache) {
991
990
  this.pushCache(guid, cdate, mdate, tags, data, sdata);
@@ -1037,6 +1036,9 @@ export default abstract class NymphDriver {
1037
1036
  | ((success: boolean) => Promise<boolean>)
1038
1037
  | null = null,
1039
1038
  ) {
1039
+ const originalGuid = entity.guid;
1040
+ const originalCdate = entity.cdate;
1041
+ const originalMdate = entity.mdate;
1040
1042
  // Get a modified date.
1041
1043
  let mdate = Date.now();
1042
1044
  if (entity.mdate != null) {
@@ -1056,56 +1058,68 @@ export default abstract class NymphDriver {
1056
1058
  await startTransactionCallback();
1057
1059
  }
1058
1060
  let success = false;
1059
- if (entity.guid == null) {
1060
- const cdate = mdate;
1061
- const newId = guid();
1062
- success = await saveNewEntityCallback({
1063
- entity,
1064
- guid: newId,
1065
- tags,
1066
- data,
1067
- sdata,
1068
- uniques,
1069
- cdate,
1070
- etype,
1071
- });
1072
- if (success) {
1073
- entity.guid = newId;
1074
- entity.cdate = cdate;
1075
- entity.mdate = mdate;
1061
+ try {
1062
+ if (entity.guid == null) {
1063
+ const cdate = mdate;
1064
+ const newId = entity.$getGuaranteedGUID();
1065
+ success = await saveNewEntityCallback({
1066
+ entity,
1067
+ guid: newId,
1068
+ tags,
1069
+ data,
1070
+ sdata,
1071
+ uniques,
1072
+ cdate,
1073
+ etype,
1074
+ });
1075
+ if (success) {
1076
+ entity.guid = newId;
1077
+ entity.cdate = cdate;
1078
+ entity.mdate = mdate;
1079
+ }
1080
+ } else {
1081
+ // Removed any cached versions of this entity.
1082
+ if (this.nymph.config.cache) {
1083
+ this.cleanCache(entity.guid);
1084
+ }
1085
+ success = await saveExistingEntityCallback({
1086
+ entity,
1087
+ guid: entity.guid,
1088
+ tags,
1089
+ data,
1090
+ sdata,
1091
+ uniques,
1092
+ mdate,
1093
+ etype,
1094
+ });
1095
+ if (success) {
1096
+ entity.mdate = mdate;
1097
+ }
1076
1098
  }
1077
- } else {
1078
- // Removed any cached versions of this entity.
1079
- if (this.nymph.config.cache) {
1080
- this.cleanCache(entity.guid);
1099
+ if (commitTransactionCallback) {
1100
+ success = await commitTransactionCallback(success);
1081
1101
  }
1082
- success = await saveExistingEntityCallback({
1083
- entity,
1084
- guid: entity.guid,
1085
- tags,
1086
- data,
1087
- sdata,
1088
- uniques,
1089
- mdate,
1090
- etype,
1091
- });
1092
- if (success) {
1093
- entity.mdate = mdate;
1102
+ // Cache the entity.
1103
+ if (success && this.nymph.config.cache) {
1104
+ this.pushCache(
1105
+ entity.guid as string,
1106
+ entity.cdate as number,
1107
+ entity.mdate as number,
1108
+ entity.tags,
1109
+ data,
1110
+ sdata,
1111
+ );
1094
1112
  }
1113
+ } catch (e: any) {
1114
+ entity.guid = originalGuid;
1115
+ entity.cdate = originalCdate;
1116
+ entity.mdate = originalMdate;
1117
+ throw e;
1095
1118
  }
1096
- if (commitTransactionCallback) {
1097
- success = await commitTransactionCallback(success);
1098
- }
1099
- // Cache the entity.
1100
- if (success && this.nymph.config.cache) {
1101
- this.pushCache(
1102
- entity.guid as string,
1103
- entity.cdate as number,
1104
- entity.mdate as number,
1105
- entity.tags,
1106
- data,
1107
- sdata,
1108
- );
1119
+ if (!success) {
1120
+ entity.guid = originalGuid;
1121
+ entity.cdate = originalCdate;
1122
+ entity.mdate = originalMdate;
1109
1123
  }
1110
1124
  return success;
1111
1125
  }
@@ -1140,6 +1154,7 @@ export default abstract class NymphDriver {
1140
1154
  entity.$putData(
1141
1155
  this.entityCache[guid]['data'],
1142
1156
  this.entityCache[guid]['sdata'],
1157
+ 'server',
1143
1158
  );
1144
1159
  return entity;
1145
1160
  }
@@ -159,6 +159,35 @@ export function EntitiesTest(
159
159
  expect(testEntity.$is(resultEntity)).toEqual(false);
160
160
  });
161
161
 
162
+ it('guaranteed guid', async () => {
163
+ // Creating entity...
164
+ const testEntity = await TestModel.factory();
165
+
166
+ // Saving entity...
167
+ testEntity.name = 'Entity Test ' + new Date().toLocaleString();
168
+ testEntity.null = null;
169
+ testEntity.string = 'test';
170
+ testEntity.array = [];
171
+ testEntity.match = matchValue;
172
+ testEntity.number = 30;
173
+ testEntity.numberString = '30';
174
+ testEntity.timestamp = Date.now();
175
+
176
+ // Get the guaranteed GUID.
177
+ const testGuid = testEntity.$getGuaranteedGUID();
178
+
179
+ expect(await testEntity.$save()).toEqual(true);
180
+ expect(testEntity.guid).not.toBeNull();
181
+ expect(testEntity.guid).toEqual(testGuid);
182
+
183
+ // Retrieving entity by GUID...
184
+ let resultEntity = await nymph.getEntity({ class: TestModel }, testGuid);
185
+ expect(testEntity.$is(resultEntity)).toEqual(true);
186
+
187
+ // Delete the entity.
188
+ expect(await testEntity.$delete()).toEqual(true);
189
+ });
190
+
162
191
  it('transactions', async () => {
163
192
  await createTestEntities();
164
193
 
package/src/testMocks.ts CHANGED
@@ -54,7 +54,7 @@ export class MockNymphDriver {
54
54
  entity.cdate = entityJson.cdate;
55
55
  entity.mdate = entityJson.mdate;
56
56
  entity.tags = entityJson.tags;
57
- entity.$putData(entityJson.data, {});
57
+ entity.$putData(entityJson.data, {}, 'server');
58
58
 
59
59
  return entity as EntityInstanceType<T>;
60
60
  }
package/src/utils.ts CHANGED
@@ -88,7 +88,12 @@ export function classNamesToEntityConstructors(
88
88
  }
89
89
 
90
90
  export function entitiesToReferences(item: any): any {
91
- if (item instanceof Entity && typeof item.$toReference === 'function') {
91
+ if (item == null) {
92
+ return item;
93
+ } else if (
94
+ item instanceof Entity &&
95
+ typeof item.$toReference === 'function'
96
+ ) {
92
97
  // Convert entities to references.
93
98
  return item.$toReference();
94
99
  } else if (Array.isArray(item)) {
@@ -110,7 +115,9 @@ export function referencesToEntities(
110
115
  nymph: Nymph,
111
116
  useSkipAc = false,
112
117
  ): any {
113
- if (Array.isArray(item)) {
118
+ if (item == null) {
119
+ return item;
120
+ } else if (Array.isArray(item)) {
114
121
  // Check if it's a reference.
115
122
  if (item[0] === 'nymph_entity_reference') {
116
123
  try {