@onehat/data 1.4.8 → 1.4.9

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.4.8",
3
+ "version": "1.4.9",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -1,7 +1,12 @@
1
+ /**
2
+ * This file is categorized as "Proprietary Framework Code"
3
+ * and is subject to the terms and conditions defined in
4
+ * file 'LICENSE.txt', which is part of this source code package.
5
+ */
1
6
  /** @module Repository */
2
7
 
3
8
  import OfflineRepository from '@onehat/data/src/Repository/Offline.js';
4
- import AsyncStorage from '@react-native-community/async-storage';
9
+ import AsyncStorage from '@react-native-async-storage/async-storage';
5
10
  import _ from 'lodash';
6
11
 
7
12
  /**
@@ -37,10 +42,12 @@ class AsyncStorageRepository extends OfflineRepository {
37
42
  value = result; // Invalid JSON, just return raw result
38
43
  }
39
44
  }
45
+
40
46
  return value;
41
47
  } catch (error) {
42
48
  if (this.debugMode) {
43
49
  const msg = error && error.message;
50
+ console.log('_storageGetValue error', msg);
44
51
  debugger;
45
52
  }
46
53
  }
@@ -60,7 +67,7 @@ class AsyncStorageRepository extends OfflineRepository {
60
67
  const results = await AsyncStorage.multiGet(this._namespace(keys));
61
68
 
62
69
  if (this.debugMode) {
63
- console.log(this.name, 'AsyncStorage.multiGet results', name, results);
70
+ console.log(this.name, 'AsyncStorage.multiGet results', results);
64
71
  }
65
72
 
66
73
  let values = [];
@@ -72,7 +79,17 @@ class AsyncStorageRepository extends OfflineRepository {
72
79
  } catch (e) {
73
80
  parsed = value; // Invalid JSON, just return raw result
74
81
  }
75
- values.push(parsed);
82
+ if (parsed === null) {
83
+ // Values should be stored as JSON, so it should be either {} or []. If it's null, that means the AsyncStorage can't find the record
84
+ // Delete the index to this record
85
+ const re = new RegExp('^' + this.name + '\-' + '(.*)'),
86
+ matches = key.match(re);
87
+ debugger;
88
+ const id = parseInt(matches, 10);
89
+ this._deleteFromIndex(id);
90
+ } else {
91
+ values.push(parsed);
92
+ }
76
93
  })
77
94
  }
78
95
 
@@ -84,6 +101,7 @@ class AsyncStorageRepository extends OfflineRepository {
84
101
  } catch (error) {
85
102
  if (this.debugMode) {
86
103
  const msg = error && error.message;
104
+ console.log('_storageGetMultiple error', msg);
87
105
  debugger;
88
106
  }
89
107
  }
@@ -103,6 +121,7 @@ class AsyncStorageRepository extends OfflineRepository {
103
121
  } catch (error) {
104
122
  if (this.debugMode) {
105
123
  const msg = error && error.message;
124
+ console.log('_storageSetValue error', msg);
106
125
  debugger;
107
126
  }
108
127
  }
@@ -126,11 +145,16 @@ class AsyncStorageRepository extends OfflineRepository {
126
145
  console.log(this.name, 'AsyncStorage.multiSet', keys);
127
146
  }
128
147
 
148
+ if (_.isEmpty(converted)) {
149
+ return;
150
+ }
151
+
129
152
  return await AsyncStorage.multiSet(converted);
130
153
 
131
154
  } catch (error) {
132
155
  if (this.debugMode) {
133
156
  const msg = error && error.message;
157
+ console.log('_storageSetMultiple error', msg);
134
158
  debugger;
135
159
  }
136
160
  }
@@ -151,6 +175,7 @@ class AsyncStorageRepository extends OfflineRepository {
151
175
  } catch (error) {
152
176
  if (this.debugMode) {
153
177
  const msg = error && error.message;
178
+ console.log('_storageDeleteValue error', msg);
154
179
  debugger;
155
180
  }
156
181
  }
@@ -171,6 +196,7 @@ class AsyncStorageRepository extends OfflineRepository {
171
196
  } catch (error) {
172
197
  if (this.debugMode) {
173
198
  const msg = error && error.message;
199
+ console.log('_storageDeleteMultiple error', msg);
174
200
  debugger;
175
201
  }
176
202
  }
@@ -1,94 +1,99 @@
1
- import OfflineRepository from '@onehat/data/src/Repository/Offline';
2
- import * as SecureStore from 'expo-secure-store'; // see: https://docs.expo.io/versions/latest/sdk/securestore/
3
- import _ from 'lodash';
4
-
5
1
  /**
6
- * Repository representing Expo's SecureStore
7
- * Uses expo-secure-store package
2
+ * This file is categorized as "Proprietary Framework Code"
3
+ * and is subject to the terms and conditions defined in
4
+ * file 'LICENSE.txt', which is part of this source code package.
8
5
  */
9
- class SecureStoreRepository extends OfflineRepository {
10
-
11
- constructor(config = {}) {
12
- super(...arguments);
13
- _.merge(this, config);
14
- }
15
-
16
- _storageGetValue = async (name) => {
17
- try {
18
-
19
- if (this.debugMode) {
20
- console.log(this.name, 'SecureStore.get', name);
21
- }
22
-
23
- const result = await SecureStore.getItemAsync(this._namespace(name));
24
-
25
- if (this.debugMode) {
26
- console.log(this.name, 'SecureStore.get results', name, result);
27
- }
28
-
29
- let value;
30
- if (!_.isNil(result)) {
31
- try {
32
- value = JSON.parse(result);
33
- } catch (e) {
34
- value = result; // Invalid JSON, just return raw result
35
- }
36
- }
37
- return value;
38
- } catch (error) {
39
- if (this.debugMode) {
40
- const msg = error && error.message;
41
- debugger;
42
- }
43
- }
44
- }
45
-
46
- _storageSetValue = async (name, value) => {
47
- try {
48
- if (!_.isString(value)) {
49
- value = JSON.stringify(value);
50
- }
51
- if (this.debugMode) {
52
- console.log(this.name, 'SecureStore.set', name, value);
53
- }
54
-
55
- const result = await SecureStore.setItemAsync(this._namespace(name), value);
56
-
57
- // if (this.debugMode) {
58
- // console.log(this.name, 'SecureStore.set results', name, result);
59
- // }
60
- return result;
61
- } catch (error) {
62
- if (this.debugMode) {
63
- const msg = error && error.message;
64
- debugger;
65
- }
66
- }
67
- }
68
-
69
- _storageDeleteValue = async (name) => {
70
- try {
71
- if (this.debugMode) {
72
- console.log(this.name, 'SecureStore.delete', name);
73
- }
74
-
75
- const result = await SecureStore.deleteItemAsync(this._namespace(name));
76
-
77
- // if (this.debugMode) {
78
- // console.log(this.name, 'SecureStore.delete results', name, result);
79
- // }
80
- return result;
81
- } catch (error) {
82
- if (this.debugMode) {
83
- const msg = error && error.message;
84
- debugger;
85
- }
86
- }
87
- }
88
-
89
- };
90
-
91
- SecureStoreRepository.className = 'SecureStore';
92
- SecureStoreRepository.type = 'secure';
93
-
94
- export default SecureStoreRepository;
6
+ import OfflineRepository from '@onehat/data/src/Repository/Offline.js';
7
+ import * as SecureStore from 'expo-secure-store'; // see: https://docs.expo.io/versions/latest/sdk/securestore/
8
+ import _ from 'lodash';
9
+
10
+ /**
11
+ * Repository representing Expo's SecureStore
12
+ * Uses expo-secure-store package
13
+ */
14
+ class SecureStoreRepository extends OfflineRepository {
15
+
16
+ constructor(config = {}) {
17
+ super(...arguments);
18
+ _.merge(this, config);
19
+ }
20
+
21
+ _storageGetValue = async (name) => {
22
+ try {
23
+
24
+ if (this.debugMode) {
25
+ console.log(this.name, 'SecureStore.get', name);
26
+ }
27
+
28
+ const result = await SecureStore.getItemAsync(this._namespace(name));
29
+
30
+ if (this.debugMode) {
31
+ console.log(this.name, 'SecureStore.get results', name, result);
32
+ }
33
+
34
+ let value;
35
+ if (!_.isNil(result)) {
36
+ try {
37
+ value = JSON.parse(result);
38
+ } catch (e) {
39
+ value = result; // Invalid JSON, just return raw result
40
+ }
41
+ }
42
+ return value;
43
+ } catch (error) {
44
+ if (this.debugMode) {
45
+ const msg = error && error.message;
46
+ debugger;
47
+ }
48
+ }
49
+ }
50
+
51
+ _storageSetValue = async (name, value) => {
52
+ try {
53
+ if (!_.isString(value)) {
54
+ value = JSON.stringify(value);
55
+ }
56
+ if (this.debugMode) {
57
+ console.log(this.name, 'SecureStore.set', name, value);
58
+ }
59
+
60
+ const result = await SecureStore.setItemAsync(this._namespace(name), value);
61
+
62
+ // if (this.debugMode) {
63
+ // console.log(this.name, 'SecureStore.set results', name, result);
64
+ // }
65
+ return result;
66
+ } catch (error) {
67
+ if (this.debugMode) {
68
+ const msg = error && error.message;
69
+ debugger;
70
+ }
71
+ }
72
+ }
73
+
74
+ _storageDeleteValue = async (name) => {
75
+ try {
76
+ if (this.debugMode) {
77
+ console.log(this.name, 'SecureStore.delete', name);
78
+ }
79
+
80
+ const result = await SecureStore.deleteItemAsync(this._namespace(name));
81
+
82
+ // if (this.debugMode) {
83
+ // console.log(this.name, 'SecureStore.delete results', name, result);
84
+ // }
85
+ return result;
86
+ } catch (error) {
87
+ if (this.debugMode) {
88
+ const msg = error && error.message;
89
+ debugger;
90
+ }
91
+ }
92
+ }
93
+
94
+ };
95
+
96
+ SecureStoreRepository.className = 'SecureStore';
97
+ SecureStoreRepository.type = 'secure';
98
+
99
+ export default SecureStoreRepository;
@@ -23,13 +23,8 @@ class OfflineRepository extends MemoryRepository {
23
23
 
24
24
  async initialize() {
25
25
  this.pauseEvents(); // Queue 'initialize' event from super
26
- await super.initialize();
27
26
 
28
- this._index = await this._getIndex();
29
- if (!this._index) {
30
- await this._setIndex([]);
31
- this._index = [];
32
- }
27
+ await super.initialize(); // Initializes index in _loadFromStorage()
33
28
 
34
29
  this.resumeEvents(true); // Now fire it!
35
30
  }
@@ -80,7 +75,14 @@ class OfflineRepository extends MemoryRepository {
80
75
  */
81
76
  _loadFromStorage = async () => {
82
77
  try {
83
- const ids = await this._getIndex(),
78
+
79
+ this._index = await this._getIndex();
80
+ if (!this._index) {
81
+ await this._setIndex([]);
82
+ this._index = [];
83
+ }
84
+
85
+ const ids = this._index,
84
86
  total = ids && ids.length ? ids.length : 0,
85
87
  results = [];
86
88