@docknetwork/wallet-sdk-data-store-web 0.4.19

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.
Files changed (87) hide show
  1. package/jest.config.ts +20 -0
  2. package/lib/entities/document/create-document.d.ts +12 -0
  3. package/lib/entities/document/create-document.d.ts.map +1 -0
  4. package/lib/entities/document/create-document.js +150 -0
  5. package/lib/entities/document/create-document.js.map +1 -0
  6. package/lib/entities/document/document.entity.d.ts +8 -0
  7. package/lib/entities/document/document.entity.d.ts.map +1 -0
  8. package/lib/entities/document/document.entity.js +3 -0
  9. package/lib/entities/document/document.entity.js.map +1 -0
  10. package/lib/entities/document/get-all-documents.d.ts +11 -0
  11. package/lib/entities/document/get-all-documents.d.ts.map +1 -0
  12. package/lib/entities/document/get-all-documents.js +73 -0
  13. package/lib/entities/document/get-all-documents.js.map +1 -0
  14. package/lib/entities/document/get-document-by-id.d.ts +13 -0
  15. package/lib/entities/document/get-document-by-id.d.ts.map +1 -0
  16. package/lib/entities/document/get-document-by-id.js +76 -0
  17. package/lib/entities/document/get-document-by-id.js.map +1 -0
  18. package/lib/entities/document/get-document-correlations.d.ts +10 -0
  19. package/lib/entities/document/get-document-correlations.d.ts.map +1 -0
  20. package/lib/entities/document/get-document-correlations.js +62 -0
  21. package/lib/entities/document/get-document-correlations.js.map +1 -0
  22. package/lib/entities/document/get-documents-by-type.d.ts +10 -0
  23. package/lib/entities/document/get-documents-by-type.d.ts.map +1 -0
  24. package/lib/entities/document/get-documents-by-type.js +61 -0
  25. package/lib/entities/document/get-documents-by-type.js.map +1 -0
  26. package/lib/entities/document/helpers.d.ts +18 -0
  27. package/lib/entities/document/helpers.d.ts.map +1 -0
  28. package/lib/entities/document/helpers.js +81 -0
  29. package/lib/entities/document/helpers.js.map +1 -0
  30. package/lib/entities/document/index.d.ts +10 -0
  31. package/lib/entities/document/index.d.ts.map +1 -0
  32. package/lib/entities/document/index.js +26 -0
  33. package/lib/entities/document/index.js.map +1 -0
  34. package/lib/entities/document/remove-document.d.ts +16 -0
  35. package/lib/entities/document/remove-document.d.ts.map +1 -0
  36. package/lib/entities/document/remove-document.js +89 -0
  37. package/lib/entities/document/remove-document.js.map +1 -0
  38. package/lib/entities/document/update-document.d.ts +11 -0
  39. package/lib/entities/document/update-document.d.ts.map +1 -0
  40. package/lib/entities/document/update-document.js +78 -0
  41. package/lib/entities/document/update-document.js.map +1 -0
  42. package/lib/entities/log.entity.d.ts +13 -0
  43. package/lib/entities/log.entity.d.ts.map +1 -0
  44. package/lib/entities/log.entity.js +91 -0
  45. package/lib/entities/log.entity.js.map +1 -0
  46. package/lib/entities/network.entity.d.ts +6 -0
  47. package/lib/entities/network.entity.d.ts.map +1 -0
  48. package/lib/entities/network.entity.js +3 -0
  49. package/lib/entities/network.entity.js.map +1 -0
  50. package/lib/entities/wallet.entity.d.ts +10 -0
  51. package/lib/entities/wallet.entity.d.ts.map +1 -0
  52. package/lib/entities/wallet.entity.js +77 -0
  53. package/lib/entities/wallet.entity.js.map +1 -0
  54. package/lib/index.d.ts +3 -0
  55. package/lib/index.d.ts.map +1 -0
  56. package/lib/index.js +185 -0
  57. package/lib/index.js.map +1 -0
  58. package/lib/localStorageJSON.d.ts +7 -0
  59. package/lib/localStorageJSON.d.ts.map +1 -0
  60. package/lib/localStorageJSON.js +80 -0
  61. package/lib/localStorageJSON.js.map +1 -0
  62. package/package.json +32 -0
  63. package/scripts/publish.sh +3 -0
  64. package/src/__tests__/index.test.ts +56 -0
  65. package/src/__tests__/v1-data-store.test.ts +24 -0
  66. package/src/entities/document/create-document.ts +82 -0
  67. package/src/entities/document/decument.entity.test.ts +135 -0
  68. package/src/entities/document/document.entity.ts +7 -0
  69. package/src/entities/document/get-all-documents.ts +42 -0
  70. package/src/entities/document/get-document-by-id.ts +31 -0
  71. package/src/entities/document/get-document-correlations.ts +22 -0
  72. package/src/entities/document/get-documents-by-type.ts +21 -0
  73. package/src/entities/document/helpers.ts +50 -0
  74. package/src/entities/document/index.ts +9 -0
  75. package/src/entities/document/remove-document.ts +43 -0
  76. package/src/entities/document/update-document.ts +36 -0
  77. package/src/entities/log.entity.ts +26 -0
  78. package/src/entities/network.entity.ts +5 -0
  79. package/src/entities/wallet.entity.ts +29 -0
  80. package/src/index.ts +134 -0
  81. package/src/localStorageJSON.ts +24 -0
  82. package/test/mock-local-storage.ts +17 -0
  83. package/test/test-utils.ts +23 -0
  84. package/test/wallet.json +209 -0
  85. package/tsconfig.build.json +25 -0
  86. package/tsconfig.build.tsbuildinfo +1 -0
  87. package/tsconfig.json +32 -0
@@ -0,0 +1,42 @@
1
+ import {
2
+ WalletDocument,
3
+ DataStore,
4
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
5
+ import {toWalletDocument} from './helpers';
6
+ import {DocumentEntity} from './document.entity';
7
+ import {localStorageJSON} from '../../localStorageJSON';
8
+
9
+ function toWalletDocumentExpanded(entity: DocumentEntity): WalletDocument {
10
+ const result = toWalletDocument(entity);
11
+
12
+ if (result) {
13
+ result['_networkId'] = entity.networkId;
14
+ }
15
+
16
+ return result;
17
+ }
18
+
19
+ /**
20
+ * Get all documents
21
+ *
22
+ * @param dataStore
23
+ */
24
+ export async function getAllDocuments({
25
+ dataStore,
26
+ allNetworks,
27
+ }: {
28
+ dataStore: DataStore;
29
+ allNetworks?: boolean;
30
+ }): Promise<WalletDocument[]> {
31
+ let entities = (await localStorageJSON.getItem('documents')) || [];
32
+
33
+ if (!allNetworks) {
34
+ entities = entities.filter(
35
+ (entity: DocumentEntity) => entity.networkId === dataStore.networkId,
36
+ );
37
+ }
38
+
39
+ const mapDocument = allNetworks ? toWalletDocumentExpanded : toWalletDocument;
40
+
41
+ return Promise.all(entities.map(mapDocument));
42
+ }
@@ -0,0 +1,31 @@
1
+ import {
2
+ ContextProps,
3
+ WalletDocument,
4
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
5
+ import {getAllDocuments} from './get-all-documents';
6
+
7
+ /**
8
+ * Get document by id
9
+ * @param dataStore
10
+ * @param id
11
+ */
12
+ export async function getDocumentById({
13
+ dataStore,
14
+ id,
15
+ }: ContextProps & {
16
+ id: string;
17
+ }): Promise<WalletDocument> {
18
+ const allDocs = await getAllDocuments({dataStore, allNetworks: false});
19
+ return allDocs.find(doc => doc.id === id);
20
+ }
21
+
22
+ export async function getDocumentsById({
23
+ dataStore,
24
+ idList,
25
+ }: ContextProps & {
26
+ idList: string[];
27
+ }): Promise<WalletDocument> {
28
+ const allDocs = await getAllDocuments({dataStore, allNetworks: false});
29
+
30
+ return allDocs.filter(doc => idList.includes(doc.id));
31
+ }
@@ -0,0 +1,22 @@
1
+ import {
2
+ ContextProps,
3
+ WalletDocument,
4
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
5
+ import {getAllDocuments} from './get-all-documents';
6
+
7
+ /**
8
+ * Get related documents
9
+ * @param dataStore
10
+ * @param type
11
+ */
12
+ export async function getDocumentCorrelations({
13
+ dataStore,
14
+ documentId,
15
+ }: ContextProps & {
16
+ documentId: string;
17
+ }): Promise<WalletDocument[]> {
18
+ const allDocs = await getAllDocuments({dataStore, allNetworks: false});
19
+ const entity = allDocs.find(doc => doc.id === documentId);
20
+
21
+ return allDocs.filter(doc => entity.correlation.includes(doc.id));
22
+ }
@@ -0,0 +1,21 @@
1
+ import {
2
+ ContextProps,
3
+ WalletDocument,
4
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
5
+ import {getAllDocuments} from './get-all-documents';
6
+
7
+ /**
8
+ * Get documents by type
9
+ * @param dataStore
10
+ * @param type
11
+ */
12
+ export async function getDocumentsByType({
13
+ dataStore,
14
+ type,
15
+ }: ContextProps & {
16
+ type: string;
17
+ }): Promise<WalletDocument[]> {
18
+ const allDocs = await getAllDocuments({dataStore, allNetworks: false});
19
+
20
+ return allDocs.filter(doc => doc.type === type || doc.type.includes(type));
21
+ }
@@ -0,0 +1,50 @@
1
+ import {ContextProps, WalletDocument} from '@docknetwork/wallet-sdk-data-store/src/types';
2
+ import assert from 'assert';
3
+ import {DocumentEntity} from './document.entity';
4
+
5
+
6
+
7
+ /**
8
+ * Convert document entity to wallet document
9
+ * @param entity
10
+ */
11
+ export function toWalletDocument(entity: DocumentEntity): WalletDocument {
12
+ if (!entity?.data) {
13
+ return entity;
14
+ }
15
+
16
+ const result: any = entity.data;
17
+
18
+ if (!result.id) {
19
+ result.id = entity.id;
20
+ }
21
+
22
+ return result;
23
+ }
24
+
25
+ /**
26
+ * Convert wallet document to document entity
27
+ * @param walletDocument
28
+ */
29
+ export async function toDocumentEntity({
30
+ dataStore,
31
+ document,
32
+ }: ContextProps & {
33
+ document: any;
34
+ }): Promise<DocumentEntity> {
35
+ const type = document.type || [];
36
+
37
+ return {
38
+ id: document.id,
39
+ type: type,
40
+ data: document,
41
+ correlation: document.correlation || [],
42
+ networkId: dataStore.networkId,
43
+ } as DocumentEntity;
44
+ }
45
+
46
+ export const saveOptions = {
47
+ // Android is having issues when running multiple document saves in a short period of time
48
+ // We will disable transactions for now until we find a better solution
49
+ transaction: false,
50
+ };
@@ -0,0 +1,9 @@
1
+ export * from './document.entity';
2
+ export * from './create-document';
3
+ export * from './helpers';
4
+ export * from './get-documents-by-type';
5
+ export * from './get-document-by-id';
6
+ export * from './get-all-documents';
7
+ export * from './update-document';
8
+ export * from './remove-document';
9
+ export * from './get-document-correlations';
@@ -0,0 +1,43 @@
1
+ import assert from 'assert';
2
+ import {
3
+ ContextProps,
4
+ DataStoreEvents,
5
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
6
+ import {logger} from '@docknetwork/wallet-sdk-data-store/src/logger';
7
+ import {getAllDocuments} from './get-all-documents';
8
+ import {localStorageJSON} from '../../localStorageJSON';
9
+
10
+ /**
11
+ * Remove document
12
+ * @param dataStore
13
+ * @param id
14
+ */
15
+ export async function removeDocument({
16
+ dataStore,
17
+ id,
18
+ options,
19
+ }: ContextProps & {id: string; options?: any}): Promise<void> {
20
+ assert(!!id, 'Document id is required');
21
+
22
+ logger.debug(`Removing document with id ${id}`);
23
+ const allDocs = await getAllDocuments({dataStore, allNetworks: true});
24
+ const filteredDocs = allDocs.filter(
25
+ doc => doc.id !== id && doc.networkId !== dataStore.networkId,
26
+ );
27
+
28
+ localStorageJSON.setItem('documents', filteredDocs);
29
+
30
+ dataStore.events.emit(DataStoreEvents.DocumentDeleted, id);
31
+ }
32
+
33
+ /**
34
+ * Remove all documents
35
+ * @param dataStore
36
+ */
37
+ export async function removeAllDocuments({
38
+ dataStore,
39
+ }: ContextProps): Promise<void> {
40
+ localStorageJSON.setItem('documents', []);
41
+
42
+ dataStore.events.emit(DataStoreEvents.AllDocumentsDeleted);
43
+ }
@@ -0,0 +1,36 @@
1
+ import {
2
+ ContextProps,
3
+ WalletDocument,
4
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
5
+ import {toDocumentEntity} from './helpers';
6
+ import {logger} from '@docknetwork/wallet-sdk-data-store/src/logger';
7
+ import {localStorageJSON} from '../../localStorageJSON';
8
+
9
+ /**
10
+ * Update document
11
+ * @param dataStore
12
+ * @param document
13
+ */
14
+ export async function updateDocument({
15
+ dataStore,
16
+ document,
17
+ }: ContextProps & {
18
+ document: WalletDocument;
19
+ options?: any;
20
+ }): Promise<WalletDocument> {
21
+ logger.debug(`Updating document with id ${document.id}`);
22
+
23
+ const allDocs = (await localStorageJSON.getItem('documents')) || [];
24
+ const index = allDocs.findIndex(doc => doc.id === document.id);
25
+
26
+ allDocs[index] = await toDocumentEntity({
27
+ dataStore,
28
+ document,
29
+ });
30
+
31
+ await localStorageJSON.setItem('documents', allDocs);
32
+
33
+ dataStore.events.emit('DocumentUpdated', document);
34
+
35
+ return document;
36
+ }
@@ -0,0 +1,26 @@
1
+ import {DataStore} from '@docknetwork/wallet-sdk-data-store/src/types';
2
+ import {localStorageJSON} from '../localStorageJSON';
3
+
4
+ export interface LogEntity {
5
+ id: string;
6
+ level: string;
7
+ value: string;
8
+ createdAt: Date;
9
+ }
10
+
11
+ export async function getLogs(): Promise<LogEntity[]> {
12
+ return localStorageJSON.getItem('logs') || [];
13
+ }
14
+
15
+ export async function createLog({
16
+ log,
17
+ }: {
18
+ dataStore: DataStore;
19
+ log: LogEntity;
20
+ }): Promise<LogEntity> {
21
+ log.createdAt = new Date();
22
+
23
+ const logs = await getLogs();
24
+
25
+ return localStorageJSON.setItem('logs', [...logs, log]);
26
+ }
@@ -0,0 +1,5 @@
1
+ export interface NetworkEntity {
2
+ id: string;
3
+ name: string;
4
+ configs: string;
5
+ }
@@ -0,0 +1,29 @@
1
+ import {localStorageJSON} from '../localStorageJSON';
2
+ import {ContextProps} from '@docknetwork/wallet-sdk-data-store/src/types';
3
+
4
+ export interface WalletEntity {
5
+ id: string;
6
+ version?: string;
7
+ networkId: string;
8
+ }
9
+
10
+ export async function getWallet({
11
+ dataStore,
12
+ }: ContextProps): Promise<WalletEntity> {
13
+ const result = await localStorageJSON.getItem('wallet');
14
+ return result;
15
+ }
16
+
17
+ export function createWallet({dataStore}: ContextProps): Promise<WalletEntity> {
18
+ return localStorageJSON.setItem('wallet', {
19
+ id: 'configs',
20
+ ...dataStore,
21
+ });
22
+ }
23
+
24
+ export function updateWallet({dataStore}: ContextProps): Promise<WalletEntity> {
25
+ return localStorageJSON.setItem('wallet', {
26
+ id: 'configs',
27
+ ...dataStore,
28
+ });
29
+ }
package/src/index.ts ADDED
@@ -0,0 +1,134 @@
1
+ import assert from 'assert';
2
+ import {createWallet, getWallet, updateWallet} from './entities/wallet.entity';
3
+ import {
4
+ createDocument,
5
+ getAllDocuments,
6
+ removeDocument,
7
+ getDocumentsById,
8
+ getDocumentsByType,
9
+ updateDocument,
10
+ getDocumentById,
11
+ removeAllDocuments,
12
+ getDocumentCorrelations,
13
+ } from './entities/document';
14
+ import {
15
+ createDataStore as createGenericDataStore,
16
+ parseConfigs,
17
+ setLocalStorage,
18
+ } from '@docknetwork/wallet-sdk-data-store/src/index';
19
+
20
+ import {
21
+ DataSource,
22
+ DataStore,
23
+ DataStoreConfigs,
24
+ } from '@docknetwork/wallet-sdk-data-store/src/types';
25
+ import { logger } from '@docknetwork/wallet-sdk-data-store/src/logger';
26
+
27
+ // setLocalStorage(global.localStorage);
28
+
29
+ export async function createDataStore(
30
+ configs: DataStoreConfigs,
31
+ ): Promise<DataStore> {
32
+ const dataSource: DataSource = {
33
+ async destroy() {
34
+ return;
35
+ },
36
+ async initialize() {
37
+ return;
38
+ }
39
+ }
40
+
41
+ const dataStore = await createGenericDataStore({
42
+ configs: configs,
43
+ dataSource: dataSource as any,
44
+ documentStore: {
45
+ addDocument: async (json, options) => {
46
+ return createDocument({
47
+ dataStore,
48
+ json,
49
+ options,
50
+ });
51
+ },
52
+ removeDocument: async (id, options) => {
53
+ return removeDocument({
54
+ dataStore,
55
+ id,
56
+ options,
57
+ });
58
+ },
59
+ updateDocument: async (document, options) => {
60
+ return updateDocument({
61
+ dataStore,
62
+ document,
63
+ options,
64
+ });
65
+ },
66
+ getDocumentById: async id => {
67
+ return getDocumentById({
68
+ dataStore,
69
+ id,
70
+ });
71
+ },
72
+ getDocumentsByType: async type => {
73
+ return getDocumentsByType({
74
+ dataStore,
75
+ type,
76
+ });
77
+ },
78
+ getDocumentsById: async idList => {
79
+ return getDocumentsById({
80
+ dataStore,
81
+ idList,
82
+ });
83
+ },
84
+ getAllDocuments: async (allNetworks?: boolean) => {
85
+ return getAllDocuments({
86
+ allNetworks,
87
+ dataStore,
88
+ });
89
+ },
90
+ removeAllDocuments: async () => {
91
+ return removeAllDocuments({
92
+ dataStore,
93
+ });
94
+ },
95
+ getDocumentCorrelations: async documentId => {
96
+ return getDocumentCorrelations({
97
+ dataStore,
98
+ documentId,
99
+ });
100
+ },
101
+ },
102
+ localStorageImpl: global.localStorage,
103
+ walletStore: {
104
+ getWallet: async () => {
105
+ return getWallet({dataStore});
106
+ },
107
+ updateWallet: async () => {
108
+ return updateWallet({dataStore});
109
+ },
110
+ },
111
+ });
112
+
113
+ // await migrate({dataStore});
114
+
115
+ let wallet = await dataStore.wallet.getWallet();
116
+
117
+ if (!wallet) {
118
+ wallet = await createWallet({
119
+ dataStore,
120
+ });
121
+ }
122
+
123
+ dataStore.networkId = wallet.networkId;
124
+ dataStore.network = dataStore.networks.find(
125
+ item => item.id === wallet.networkId,
126
+ );
127
+
128
+ dataStore.documents.getAllDocuments().then(documents => {
129
+ logger.debug(`Wallet loaded with ${documents.length} documents`);
130
+ });
131
+
132
+
133
+ return dataStore;
134
+ }
@@ -0,0 +1,24 @@
1
+ let _localStorage;
2
+
3
+ export const setLocalStorageImpl = (impl: any) => {
4
+ _localStorage = impl;
5
+ };
6
+
7
+ export const localStorageJSON = {
8
+ getItem: async (key: string) => {
9
+ const value = await _localStorage.getItem(key);
10
+ try {
11
+ return JSON.parse(value);
12
+ } catch (e) {
13
+ return value;
14
+ }
15
+ },
16
+ setItem: async (key: string, value: any) => {
17
+ const serializedValue = typeof value === "string" ? value : JSON.stringify(value);
18
+ await _localStorage.setItem(key, serializedValue);
19
+ return value;
20
+ },
21
+ removeItem: (key: string) => {
22
+ return _localStorage.removeItem(key);
23
+ },
24
+ };
@@ -0,0 +1,17 @@
1
+ import {LocalStorage} from '../src/migration/migration1/v1-data-store';
2
+
3
+ export const createMockLocalStorage = (): LocalStorage => {
4
+ let data: any = {};
5
+
6
+ return {
7
+ getItem: jest.fn(async key => {
8
+ return data[key];
9
+ }),
10
+ setItem: jest.fn(async (key, value) => {
11
+ data[key] = value;
12
+ }),
13
+ removeItem: jest.fn(async key => {
14
+ delete data[key];
15
+ }),
16
+ };
17
+ };
@@ -0,0 +1,23 @@
1
+ import {createMockLocalStorage} from './mock-local-storage';
2
+ import walletJSON from './wallet.json';
3
+ import {setV1LocalStorage} from '../src/migration/migration1/v1-data-store';
4
+ import {createDataStore} from '../src';
5
+ import {DataStore} from '../src/types';
6
+
7
+ export async function setupV1MockDataStore(): Promise<void> {
8
+ const mockLocalStorage = createMockLocalStorage();
9
+ setV1LocalStorage(mockLocalStorage);
10
+ await mockLocalStorage.setItem('wallet', JSON.stringify(walletJSON));
11
+ }
12
+
13
+ export async function createV1EmptyDataStore() {
14
+ const mockLocalStorage = createMockLocalStorage();
15
+ setV1LocalStorage(mockLocalStorage);
16
+ }
17
+
18
+ export function createTestDataStore(): Promise<DataStore> {
19
+ return createDataStore({
20
+ dropSchema: true,
21
+ databasePath: ':memory:',
22
+ });
23
+ }