@docknetwork/wallet-sdk-data-store 0.4.19 → 1.4.0

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 (152) hide show
  1. package/jest.config.ts +0 -1
  2. package/lib/configs.d.ts.map +1 -1
  3. package/lib/configs.js +4 -9
  4. package/lib/configs.js.map +1 -1
  5. package/lib/helpers.d.ts +5 -4
  6. package/lib/helpers.d.ts.map +1 -1
  7. package/lib/helpers.js +53 -44
  8. package/lib/helpers.js.map +1 -1
  9. package/lib/index.d.ts +12 -3
  10. package/lib/index.d.ts.map +1 -1
  11. package/lib/index.js +44 -48
  12. package/lib/index.js.map +1 -1
  13. package/lib/logger.d.ts +2 -0
  14. package/lib/logger.d.ts.map +1 -1
  15. package/lib/logger.js +6 -0
  16. package/lib/logger.js.map +1 -1
  17. package/lib/types.d.ts +41 -2
  18. package/lib/types.d.ts.map +1 -1
  19. package/lib/types.js +8 -0
  20. package/lib/types.js.map +1 -1
  21. package/package.json +3 -2
  22. package/src/configs.ts +4 -9
  23. package/src/helpers.ts +17 -42
  24. package/src/index.ts +40 -31
  25. package/src/logger.ts +6 -0
  26. package/src/types.ts +46 -3
  27. package/test/mock-local-storage.ts +1 -1
  28. package/test/wallet.json +1 -1
  29. package/tsconfig.build.tsbuildinfo +1 -1
  30. package/LICENSE +0 -39
  31. package/lib/entities/document/create-document.d.ts +0 -10
  32. package/lib/entities/document/create-document.d.ts.map +0 -1
  33. package/lib/entities/document/create-document.js +0 -111
  34. package/lib/entities/document/create-document.js.map +0 -1
  35. package/lib/entities/document/document.entity.d.ts +0 -11
  36. package/lib/entities/document/document.entity.d.ts.map +0 -1
  37. package/lib/entities/document/document.entity.js +0 -55
  38. package/lib/entities/document/document.entity.js.map +0 -1
  39. package/lib/entities/document/get-all-documents.d.ts +0 -11
  40. package/lib/entities/document/get-all-documents.d.ts.map +0 -1
  41. package/lib/entities/document/get-all-documents.js +0 -78
  42. package/lib/entities/document/get-all-documents.js.map +0 -1
  43. package/lib/entities/document/get-documens-by-type.d.ts +0 -10
  44. package/lib/entities/document/get-documens-by-type.d.ts.map +0 -1
  45. package/lib/entities/document/get-documens-by-type.js +0 -71
  46. package/lib/entities/document/get-documens-by-type.js.map +0 -1
  47. package/lib/entities/document/get-document-by-id.d.ts +0 -13
  48. package/lib/entities/document/get-document-by-id.d.ts.map +0 -1
  49. package/lib/entities/document/get-document-by-id.js +0 -92
  50. package/lib/entities/document/get-document-by-id.js.map +0 -1
  51. package/lib/entities/document/get-document-correlations.d.ts +0 -10
  52. package/lib/entities/document/get-document-correlations.d.ts.map +0 -1
  53. package/lib/entities/document/get-document-correlations.js +0 -78
  54. package/lib/entities/document/get-document-correlations.js.map +0 -1
  55. package/lib/entities/document/helpers.d.ts +0 -30
  56. package/lib/entities/document/helpers.d.ts.map +0 -1
  57. package/lib/entities/document/helpers.js +0 -196
  58. package/lib/entities/document/helpers.js.map +0 -1
  59. package/lib/entities/document/index.d.ts +0 -10
  60. package/lib/entities/document/index.d.ts.map +0 -1
  61. package/lib/entities/document/index.js +0 -26
  62. package/lib/entities/document/index.js.map +0 -1
  63. package/lib/entities/document/remove-document.d.ts +0 -15
  64. package/lib/entities/document/remove-document.d.ts.map +0 -1
  65. package/lib/entities/document/remove-document.js +0 -94
  66. package/lib/entities/document/remove-document.js.map +0 -1
  67. package/lib/entities/document/update-document.d.ts +0 -10
  68. package/lib/entities/document/update-document.d.ts.map +0 -1
  69. package/lib/entities/document/update-document.js +0 -72
  70. package/lib/entities/document/update-document.js.map +0 -1
  71. package/lib/entities/document-type.entity.d.ts +0 -6
  72. package/lib/entities/document-type.entity.d.ts.map +0 -1
  73. package/lib/entities/document-type.entity.js +0 -32
  74. package/lib/entities/document-type.entity.js.map +0 -1
  75. package/lib/entities/log.entity.d.ts +0 -15
  76. package/lib/entities/log.entity.d.ts.map +0 -1
  77. package/lib/entities/log.entity.js +0 -108
  78. package/lib/entities/log.entity.js.map +0 -1
  79. package/lib/entities/network.entity.d.ts +0 -6
  80. package/lib/entities/network.entity.d.ts.map +0 -1
  81. package/lib/entities/network.entity.js +0 -35
  82. package/lib/entities/network.entity.js.map +0 -1
  83. package/lib/entities/transaction.entity.d.ts +0 -24
  84. package/lib/entities/transaction.entity.d.ts.map +0 -1
  85. package/lib/entities/transaction.entity.js +0 -139
  86. package/lib/entities/transaction.entity.js.map +0 -1
  87. package/lib/entities/wallet.entity.d.ts +0 -10
  88. package/lib/entities/wallet.entity.d.ts.map +0 -1
  89. package/lib/entities/wallet.entity.js +0 -109
  90. package/lib/entities/wallet.entity.js.map +0 -1
  91. package/lib/migration/bootstrap-tables.d.ts +0 -3
  92. package/lib/migration/bootstrap-tables.d.ts.map +0 -1
  93. package/lib/migration/bootstrap-tables.js +0 -8
  94. package/lib/migration/bootstrap-tables.js.map +0 -1
  95. package/lib/migration/index.d.ts +0 -8
  96. package/lib/migration/index.d.ts.map +0 -1
  97. package/lib/migration/index.js +0 -132
  98. package/lib/migration/index.js.map +0 -1
  99. package/lib/migration/migration1/index.d.ts +0 -16
  100. package/lib/migration/migration1/index.d.ts.map +0 -1
  101. package/lib/migration/migration1/index.js +0 -110
  102. package/lib/migration/migration1/index.js.map +0 -1
  103. package/lib/migration/migration1/migrate-v1-data.d.ts +0 -8
  104. package/lib/migration/migration1/migrate-v1-data.d.ts.map +0 -1
  105. package/lib/migration/migration1/migrate-v1-data.js +0 -171
  106. package/lib/migration/migration1/migrate-v1-data.js.map +0 -1
  107. package/lib/migration/migration1/v1-data-store.d.ts +0 -15
  108. package/lib/migration/migration1/v1-data-store.d.ts.map +0 -1
  109. package/lib/migration/migration1/v1-data-store.js +0 -80
  110. package/lib/migration/migration1/v1-data-store.js.map +0 -1
  111. package/lib/migrations/1691498362273-bootstrap.d.ts +0 -7
  112. package/lib/migrations/1691498362273-bootstrap.d.ts.map +0 -1
  113. package/lib/migrations/1691498362273-bootstrap.js +0 -178
  114. package/lib/migrations/1691498362273-bootstrap.js.map +0 -1
  115. package/lib/migrations/index.d.ts +0 -4
  116. package/lib/migrations/index.d.ts.map +0 -1
  117. package/lib/migrations/index.js +0 -5
  118. package/lib/migrations/index.js.map +0 -1
  119. package/lib/migrations-data-source.d.ts +0 -3
  120. package/lib/migrations-data-source.d.ts.map +0 -1
  121. package/lib/migrations-data-source.js +0 -8
  122. package/lib/migrations-data-source.js.map +0 -1
  123. package/lib/typeorm.d.ts +0 -2
  124. package/lib/typeorm.d.ts.map +0 -1
  125. package/lib/typeorm.js +0 -18
  126. package/lib/typeorm.js.map +0 -1
  127. package/setup-tests.ts +0 -0
  128. package/src/entities/document/create-document.ts +0 -72
  129. package/src/entities/document/decument.entity.test.ts +0 -135
  130. package/src/entities/document/document.entity.ts +0 -40
  131. package/src/entities/document/get-all-documents.ts +0 -39
  132. package/src/entities/document/get-documens-by-type.ts +0 -30
  133. package/src/entities/document/get-document-by-id.ts +0 -45
  134. package/src/entities/document/get-document-correlations.ts +0 -34
  135. package/src/entities/document/helpers.ts +0 -115
  136. package/src/entities/document/index.ts +0 -9
  137. package/src/entities/document/remove-document.ts +0 -34
  138. package/src/entities/document/update-document.ts +0 -25
  139. package/src/entities/document-type.entity.ts +0 -11
  140. package/src/entities/log.entity.ts +0 -46
  141. package/src/entities/network.entity.ts +0 -14
  142. package/src/entities/transaction.entity.ts +0 -67
  143. package/src/entities/wallet.entity.ts +0 -37
  144. package/src/migration/bootstrap-tables.ts +0 -5
  145. package/src/migration/index.ts +0 -53
  146. package/src/migration/migration1/index.ts +0 -64
  147. package/src/migration/migration1/migrate-v1-data.ts +0 -57
  148. package/src/migration/migration1/v1-data-store.ts +0 -39
  149. package/src/migrations/1691498362273-bootstrap.ts +0 -130
  150. package/src/migrations/index.ts +0 -3
  151. package/src/migrations-data-source.ts +0 -6
  152. package/src/typeorm.ts +0 -1
@@ -1,8 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var helpers_1 = require("./helpers");
4
- exports.default = (0, helpers_1.getDataSource)({
5
- dbType: 'sqlite',
6
- databasePath: 'data-store.sqlite',
7
- });
8
- //# sourceMappingURL=migrations-data-source.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"migrations-data-source.js","sourceRoot":"","sources":["../src/migrations-data-source.ts"],"names":[],"mappings":";;AAAA,qCAAwC;AAExC,kBAAe,IAAA,uBAAa,EAAC;IAC3B,MAAM,EAAE,QAAQ;IAChB,YAAY,EAAE,mBAAmB;CAClC,CAAC,CAAC"}
package/lib/typeorm.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from 'typeorm';
2
- //# sourceMappingURL=typeorm.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"typeorm.d.ts","sourceRoot":"","sources":["../src/typeorm.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC"}
package/lib/typeorm.js DELETED
@@ -1,18 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("typeorm"), exports);
18
- //# sourceMappingURL=typeorm.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"typeorm.js","sourceRoot":"","sources":["../src/typeorm.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,0CAAwB"}
package/setup-tests.ts DELETED
File without changes
@@ -1,72 +0,0 @@
1
- import {ContextProps, WalletDocument} from '../../types';
2
- import {v4 as uuid} from 'uuid';
3
- import {DocumentEntity} from './document.entity';
4
- import {getOrCreateDocumentTypes, saveOptions} from './helpers';
5
- import {getDocumentById} from './get-document-by-id';
6
- import {logger} from '../../logger';
7
-
8
- /**
9
- * Create document
10
- * @param dataStore
11
- * @param json
12
- */
13
- export async function createDocument({
14
- dataStore,
15
- json,
16
- }: ContextProps & {
17
- json: any;
18
- }): Promise<WalletDocument> {
19
- logger.debug(`Creating document with id ${json.id}...`);
20
-
21
- if (json.id) {
22
- const existingDocument = await getDocumentById({
23
- dataStore,
24
- id: json.id,
25
- });
26
-
27
- if (existingDocument) {
28
- logger.debug(`Document with id ${json.id} already exists`);
29
- throw new Error(`Document with id ${json.id} already exists`);
30
- }
31
- }
32
-
33
- const _typeRel = await getOrCreateDocumentTypes({
34
- dataStore,
35
- types: json.type,
36
- });
37
-
38
- if (!json.id) {
39
- json.id = uuid();
40
- }
41
-
42
- let networkId;
43
-
44
- if (json._networkId) {
45
- networkId = json._networkId;
46
- delete json._networkId;
47
- } else {
48
- const resolution = await dataStore.resolveDocumentNetwork({
49
- document: json,
50
- dataStore,
51
- });
52
-
53
- networkId = resolution.networkId;
54
- }
55
-
56
- const entity: DocumentEntity = {
57
- networkId,
58
- id: json.id,
59
- type: json.type,
60
- _typeRel,
61
- correlation: json.correlation || [],
62
- data: JSON.stringify(json),
63
- };
64
-
65
- const repository = dataStore.db.getRepository(DocumentEntity);
66
-
67
- const result = await repository.save(entity, saveOptions);
68
-
69
- logger.debug(`Document added to the wallet`);
70
-
71
- return result;
72
- }
@@ -1,135 +0,0 @@
1
- import {DocumentEntity} from './document.entity';
2
- import {createTestDataStore} from '../../../test/test-utils';
3
- import {DataStore} from '../../types';
4
- import {createDocument} from './create-document';
5
- import {getDocumentsByType} from './get-documens-by-type';
6
- import {getDocumentById} from './get-document-by-id';
7
- import {getDocumentCorrelations} from './get-document-correlations';
8
-
9
- const mockDocuments = [
10
- {
11
- '@context': [
12
- 'https://www.w3.org/2018/credentials/v1',
13
- 'https://www.w3.org/2018/credentials/examples/v1',
14
- ],
15
- id: 'http://example.gov/credentials/test',
16
- type: ['VerifiableCredential', 'UniversityDegreeCredential'],
17
- issuer: {
18
- id: 'did:example:123456789abcdefghi',
19
- },
20
- issuanceDate: '2020-03-10T04:24:12.164Z',
21
- credentialSubject: {
22
- id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
23
- degree: {
24
- type: 'BachelorDegree',
25
- name: 'Bachelor of Science and Arts',
26
- },
27
- },
28
- },
29
- {
30
- '@context': [
31
- 'https://www.w3.org/2018/credentials/v1',
32
- 'https://www.w3.org/2018/credentials/examples/v1',
33
- ],
34
- id: 'http://example.gov/credentials/test2',
35
- type: ['VerifiableCredential', 'BasicCredential'],
36
- issuer: {
37
- id: 'did:example:123456789abcdefghi',
38
- },
39
- issuanceDate: '2020-03-10T04:24:12.164Z',
40
- credentialSubject: {
41
- id: 'did:example:ebfeb1f712ebc6f1c276e12ec21',
42
- name: 'Testing',
43
- },
44
- },
45
- {
46
- '@context': ['https://w3id.org/wallet/v1'],
47
- id: 'urn:uuid:c410e44a-9525-11ea-bb37-0242ac130002',
48
- name: 'My Ropsten Mnemonic 1',
49
- image: 'https://via.placeholder.com/150',
50
- description: 'For testing only, totally compromised.',
51
- tags: ['professional', 'organization', 'compromised'],
52
- correlation: [],
53
- type: 'Mnemonic',
54
- value:
55
- 'humble piece toy mimic miss hurdle smile awkward patch drama hurry mixture',
56
- },
57
- {
58
- '@context': ['https://w3id.org/wallet/v1'],
59
- id: 'c47f3ed0-b4b5-4983-8962-759196fd3ece',
60
- type: 'Currency',
61
- value: 0,
62
- symbol: 'DOCK',
63
- },
64
- {
65
- '@context': ['https://w3id.org/wallet/v1'],
66
- id: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
67
- type: 'Address',
68
- value: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
69
- address: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
70
- name: 'Test',
71
- correlation: [
72
- 'urn:uuid:c410e44a-9525-11ea-bb37-0242ac130002',
73
- 'c47f3ed0-b4b5-4983-8962-759196fd3ece',
74
- ],
75
- },
76
- ];
77
-
78
- describe('DocumentEntity', () => {
79
- let dataStore: DataStore;
80
-
81
- beforeAll(async () => {
82
- dataStore = await createTestDataStore();
83
-
84
- for (const document of mockDocuments) {
85
- await createDocument({
86
- dataStore,
87
- json: document,
88
- });
89
- }
90
- });
91
-
92
- it('should be able to find a document by id', async () => {
93
- const mockData = mockDocuments[0];
94
- const document = await getDocumentById({
95
- dataStore,
96
- id: mockData.id,
97
- });
98
- expect(document).toBeDefined();
99
- expect(document.type).toEqual(mockData.type);
100
- });
101
-
102
- describe('getDocumentsByType', () => {
103
- it('should be able to query all VerifiableCredential', async () => {
104
- const documents = await getDocumentsByType({
105
- dataStore,
106
- type: 'VerifiableCredential',
107
- });
108
-
109
- expect(documents).toBeDefined();
110
- expect(documents.length).toEqual(2);
111
- });
112
-
113
- it('should be able query only BasicCredential', async () => {
114
- const documents = await getDocumentsByType({
115
- dataStore,
116
- type: 'BasicCredential',
117
- });
118
-
119
- expect(documents).toBeDefined();
120
- expect(documents.length).toEqual(1);
121
- });
122
- });
123
-
124
- describe('getDocumentCorrelations', () => {
125
- it('expect to query all correlations', async () => {
126
- const documents = await getDocumentCorrelations({
127
- dataStore,
128
- documentId: '37NKEP14n9omsAgxJ3sn14XtHo2vs5a34UcpCyybXQJoUQ92',
129
- });
130
-
131
- expect(documents).toBeDefined();
132
- expect(documents.length).toEqual(2);
133
- });
134
- });
135
- });
@@ -1,40 +0,0 @@
1
- import {
2
- Column,
3
- Entity,
4
- JoinTable,
5
- ManyToMany,
6
- PrimaryColumn,
7
- Relation,
8
- } from '../../typeorm';
9
- import {DocumentTypeEntity} from '../document-type.entity';
10
-
11
- @Entity()
12
- export class DocumentEntity {
13
- @PrimaryColumn('text')
14
- id: string;
15
-
16
- @Column('text', {
17
- nullable: true,
18
- })
19
- networkId: string;
20
-
21
- @Column('simple-array')
22
- type: string[];
23
-
24
- @ManyToMany(() => DocumentTypeEntity, type => type?.documents, {
25
- cascade: ['insert'],
26
- eager: true,
27
- nullable: true,
28
- })
29
- @JoinTable()
30
- _typeRel: Relation<DocumentTypeEntity[]>;
31
-
32
- // Add the ManyToMany self-referencing relationship to track document correlations
33
- // @ManyToMany(() => DocumentEntity, {cascade: true, nullable: true})
34
- // @JoinTable()
35
- @Column('simple-array')
36
- correlation: string[];
37
-
38
- @Column('blob')
39
- data: string;
40
- }
@@ -1,39 +0,0 @@
1
- import {WalletDocument, DataStore} from '../../types';
2
- import {toWalletDocument} from './helpers';
3
- import {DocumentEntity} from './document.entity';
4
-
5
- function toWalletDocumentExpanded(entity: DocumentEntity): WalletDocument {
6
- const result = toWalletDocument(entity);
7
-
8
- if (result) {
9
- result['_networkId'] = entity.networkId;
10
- }
11
-
12
- return result;
13
- }
14
-
15
- /**
16
- * Get all documents
17
- *
18
- * @param dataStore
19
- */
20
- export async function getAllDocuments({
21
- dataStore,
22
- allNetworks,
23
- }: {
24
- dataStore: DataStore;
25
- allNetworks?: boolean;
26
- }): Promise<WalletDocument[]> {
27
- const repository = dataStore.db.getRepository(DocumentEntity);
28
- const entities = await repository.find({
29
- where: allNetworks
30
- ? {}
31
- : {
32
- networkId: dataStore.networkId,
33
- },
34
- });
35
-
36
- const mapDocument = allNetworks ? toWalletDocumentExpanded : toWalletDocument;
37
-
38
- return Promise.all(entities.map(mapDocument));
39
- }
@@ -1,30 +0,0 @@
1
- import {ContextProps, WalletDocument} from '../../types';
2
- import {DocumentEntity} from './document.entity';
3
- import {createQueryBuilder} from '../../typeorm';
4
- import {toWalletDocument} from './helpers';
5
-
6
- /**
7
- * Get documents by type
8
- * @param dataStore
9
- * @param type
10
- */
11
- export async function getDocumentsByType({
12
- dataStore,
13
- type,
14
- }: ContextProps & {
15
- type: string;
16
- }): Promise<WalletDocument[]> {
17
- const repository = dataStore.db.getRepository(DocumentEntity);
18
-
19
- const documents = repository
20
- .createQueryBuilder('document')
21
- .leftJoinAndSelect('document._typeRel', 'documentType')
22
- .where('documentType.id = :type', {type})
23
- .andWhere('document.networkId = :networkId', {
24
- networkId: dataStore.networkId,
25
- });
26
-
27
- const result = await documents.getMany();
28
-
29
- return result.map(toWalletDocument);
30
- }
@@ -1,45 +0,0 @@
1
- import {ContextProps, WalletDocument} from '../../types';
2
- import {toWalletDocument} from './helpers';
3
- import {DocumentEntity} from './document.entity';
4
- import {In} from 'typeorm';
5
-
6
- /**
7
- * Get document by id
8
- * @param dataStore
9
- * @param id
10
- */
11
- export async function getDocumentById({
12
- dataStore,
13
- id,
14
- }: ContextProps & {
15
- id: string;
16
- }): Promise<WalletDocument> {
17
- const repository = dataStore.db.getRepository(DocumentEntity);
18
-
19
- const entity = await repository.findOne({
20
- where: {
21
- id: id,
22
- networkId: dataStore.networkId,
23
- },
24
- });
25
-
26
- return toWalletDocument(entity);
27
- }
28
-
29
- export async function getDocumentsById({
30
- dataStore,
31
- idList,
32
- }: ContextProps & {
33
- idList: string[];
34
- }): Promise<WalletDocument> {
35
- const repository = dataStore.db.getRepository(DocumentEntity);
36
-
37
- const entities = await repository.find({
38
- where: {
39
- id: In(idList),
40
- networkId: dataStore.networkId,
41
- },
42
- });
43
-
44
- return entities.map(toWalletDocument);
45
- }
@@ -1,34 +0,0 @@
1
- import {ContextProps, WalletDocument} from '../../types';
2
- import {DocumentEntity} from './document.entity';
3
- import {In} from '../../typeorm';
4
- import {toWalletDocument} from './helpers';
5
-
6
- /**
7
- * Get related documents
8
- * @param dataStore
9
- * @param type
10
- */
11
- export async function getDocumentCorrelations({
12
- dataStore,
13
- documentId,
14
- }: ContextProps & {
15
- documentId: string;
16
- }): Promise<WalletDocument[]> {
17
- const repository = dataStore.db.getRepository(DocumentEntity);
18
-
19
- const entity = await repository.findOne({
20
- where: {
21
- id: documentId,
22
- networkId: dataStore.networkId,
23
- },
24
- });
25
-
26
- const data = await repository.find({
27
- where: {
28
- id: In(entity.correlation),
29
- networkId: dataStore.networkId,
30
- },
31
- });
32
-
33
- return data.map(toWalletDocument);
34
- }
@@ -1,115 +0,0 @@
1
- import {ContextProps, WalletDocument} from '../../types';
2
- import {DocumentTypeEntity} from '../document-type.entity';
3
- import assert from 'assert';
4
- import {logger} from '../../logger';
5
- import {DocumentEntity} from './document.entity';
6
- import {In} from '../../typeorm';
7
-
8
- /**
9
- * Get document by id
10
- * @param dataStore
11
- * @param types
12
- */
13
- export async function getOrCreateDocumentTypes({
14
- dataStore,
15
- types,
16
- }: ContextProps & {
17
- types: string | string[];
18
- }): Promise<DocumentTypeEntity[]> {
19
- assert(!!types, 'Document type must be provided');
20
-
21
- logger.debug(`getOrCreateDocumentTypes: ${JSON.stringify(types)}`);
22
-
23
- if (!Array.isArray(types)) {
24
- types = [types];
25
- }
26
-
27
- const typeRepository = dataStore.db.getRepository(DocumentTypeEntity);
28
- const typeEntityList = [];
29
-
30
- for (const type of types) {
31
- let typeEntity = await typeRepository.findOne({
32
- where: {
33
- id: type,
34
- },
35
- });
36
-
37
- if (!typeEntity) {
38
- typeEntity = typeRepository.create({
39
- id: type,
40
- });
41
-
42
- await typeRepository.save(typeEntity);
43
- }
44
-
45
- typeEntityList.push(typeEntity);
46
- }
47
-
48
- return typeEntityList;
49
- }
50
-
51
- export async function findDocumentEntitiesById({
52
- dataStore,
53
- entityIds,
54
- }: ContextProps & {
55
- entityIds: string[];
56
- }): Promise<DocumentEntity[]> {
57
- assert(!!entityIds, 'Document ids must be provided');
58
- const repository = dataStore.db.getRepository(DocumentEntity);
59
-
60
- const entities = await repository.findBy({
61
- id: In(entityIds),
62
- });
63
-
64
- return entities;
65
- }
66
-
67
- /**
68
- * Convert document entity to wallet document
69
- * @param entity
70
- */
71
- export function toWalletDocument(entity: DocumentEntity): WalletDocument {
72
- if (!entity) {
73
- return entity;
74
- }
75
-
76
- const result = JSON.parse(entity.data);
77
-
78
- if (!result.id) {
79
- result.id = entity.id;
80
- }
81
-
82
- return result;
83
- }
84
-
85
- /**
86
- * Convert wallet document to document entity
87
- * @param walletDocument
88
- */
89
- export async function toDocumentEntity({
90
- dataStore,
91
- document,
92
- }: ContextProps & {
93
- document: any;
94
- }): Promise<DocumentEntity> {
95
- const type = document.type || [];
96
- const _typeRel = await getOrCreateDocumentTypes({
97
- dataStore,
98
- types: document.type,
99
- });
100
-
101
- return {
102
- id: document.id,
103
- type: type,
104
- data: JSON.stringify(document),
105
- _typeRel,
106
- correlation: document.correlation || [],
107
- networkId: dataStore.networkId,
108
- } as DocumentEntity;
109
- }
110
-
111
- export const saveOptions = {
112
- // Android is having issues when running multiple document saves in a short period of time
113
- // We will disable transactions for now until we find a better solution
114
- transaction: false,
115
- };
@@ -1,9 +0,0 @@
1
- export * from './document.entity';
2
- export * from './create-document';
3
- export * from './helpers';
4
- export * from './get-documens-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';
@@ -1,34 +0,0 @@
1
- import assert from 'assert';
2
- import {ContextProps} from '../../types';
3
- import {DocumentEntity} from './document.entity';
4
- import { logger } from '../../logger';
5
-
6
- /**
7
- * Remove document
8
- * @param dataStore
9
- * @param id
10
- */
11
- export async function removeDocument({
12
- dataStore,
13
- id,
14
- }: ContextProps & {id: string}): Promise<void> {
15
- assert(!!id, 'Document id is required');
16
-
17
- logger.debug(`Removing document with id ${id}`);
18
- const repository = dataStore.db.getRepository(DocumentEntity);
19
- await repository.delete({
20
- id,
21
- networkId: dataStore.networkId,
22
- });
23
- }
24
-
25
- /**
26
- * Remove all documents
27
- * @param dataStore
28
- */
29
- export async function removeAllDocuments({
30
- dataStore,
31
- }: ContextProps): Promise<void> {
32
- const repository = dataStore.db.getRepository(DocumentEntity);
33
- await repository.delete({});
34
- }
@@ -1,25 +0,0 @@
1
- import { ContextProps, WalletDocument } from '../../types';
2
- import { saveOptions, toDocumentEntity } from './helpers';
3
- import { DocumentEntity } from './document.entity';
4
- import { logger } from '../../logger';
5
-
6
- /**
7
- * Update document
8
- * @param dataStore
9
- * @param document
10
- */
11
- export async function updateDocument({
12
- dataStore,
13
- document,
14
- }: ContextProps & {document: WalletDocument}): Promise<WalletDocument> {
15
- logger.debug(`Updating document with id ${document.id}`);
16
-
17
- const repository = dataStore.db.getRepository(DocumentEntity);
18
- const entity = await toDocumentEntity({
19
- dataStore,
20
- document,
21
- });
22
- await repository.save(entity, saveOptions);
23
-
24
- return document;
25
- }
@@ -1,11 +0,0 @@
1
- import {Entity, Column, PrimaryColumn, ManyToMany} from '../typeorm';
2
- import {DocumentEntity} from './document/document.entity';
3
-
4
- @Entity()
5
- export class DocumentTypeEntity {
6
- @PrimaryColumn('text')
7
- id: string;
8
-
9
- @ManyToMany(() => DocumentEntity, document => document._typeRel)
10
- documents: DocumentEntity[];
11
- }
@@ -1,46 +0,0 @@
1
- import {Entity, Column, PrimaryColumn} from '../typeorm';
2
- import {ContextProps, DataStore} from '../types';
3
-
4
- @Entity()
5
- export class LogEntity {
6
- @PrimaryColumn()
7
- id: string;
8
-
9
- @Column()
10
- level: string;
11
-
12
- @Column()
13
- value: string;
14
-
15
- @Column()
16
- createdAt: Date;
17
- }
18
-
19
- export async function getLogs({
20
- dataStore,
21
- }: {
22
- dataStore: DataStore;
23
- }): Promise<LogEntity[]> {
24
- const repository = dataStore.db.getRepository(LogEntity);
25
- const entities = await repository.find({
26
- order: {
27
- createdAt: 'DESC',
28
- },
29
- });
30
-
31
- return entities;
32
- }
33
-
34
- export async function createLog({
35
- dataStore,
36
- log,
37
- }: {
38
- dataStore: DataStore;
39
- log: LogEntity,
40
- }): Promise<LogEntity> {
41
- const repository = dataStore.db.getRepository(LogEntity);
42
-
43
- log.createdAt = new Date();
44
-
45
- return repository.save(log);
46
- }