Skip to content

Commit 1bbad4a

Browse files
authored
Merge pull request #195 from docknetwork/chore/update-transaction-management
update transaction management
2 parents 4fc4d5c + f553eb7 commit 1bbad4a

File tree

2 files changed

+53
-57
lines changed

2 files changed

+53
-57
lines changed
Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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';
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';
77

88
/**
99
* Create document
@@ -18,57 +18,55 @@ export async function createDocument({
1818
}): Promise<WalletDocument> {
1919
logger.debug(`Creating document with id ${json.id}...`);
2020

21-
return dataStore.db.transaction(async transactionalEntityManager => {
22-
if (json.id) {
23-
const existingDocument = await getDocumentById({
24-
dataStore,
25-
id: json.id,
26-
});
27-
28-
if (existingDocument) {
29-
logger.debug(`Document with id ${json.id} already exists`);
30-
throw new Error(`Document with id ${json.id} already exists`);
31-
}
32-
}
33-
34-
const _typeRel = await getOrCreateDocumentTypes({
21+
if (json.id) {
22+
const existingDocument = await getDocumentById({
3523
dataStore,
36-
types: json.type,
24+
id: json.id,
3725
});
3826

39-
if (!json.id) {
40-
json.id = uuid();
27+
if (existingDocument) {
28+
logger.debug(`Document with id ${json.id} already exists`);
29+
throw new Error(`Document with id ${json.id} already exists`);
4130
}
31+
}
4232

43-
let networkId;
33+
const _typeRel = await getOrCreateDocumentTypes({
34+
dataStore,
35+
types: json.type,
36+
});
4437

45-
if (json._networkId) {
46-
networkId = json._networkId;
47-
delete json._networkId;
48-
} else {
49-
const resolution = await dataStore.resolveDocumentNetwork({
50-
document: json,
51-
dataStore,
52-
});
38+
if (!json.id) {
39+
json.id = uuid();
40+
}
5341

54-
networkId = resolution.networkId;
55-
}
42+
let networkId;
5643

57-
const entity: DocumentEntity = {
58-
networkId,
59-
id: json.id,
60-
type: json.type,
61-
_typeRel,
62-
correlation: json.correlation || [],
63-
data: JSON.stringify(json),
64-
};
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+
});
6552

66-
const repository = transactionalEntityManager.getRepository(DocumentEntity);
53+
networkId = resolution.networkId;
54+
}
6755

68-
const result = await repository.save(entity, saveOptions);
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+
};
6964

70-
logger.debug(`Document added to the wallet`);
65+
const repository = dataStore.db.getRepository(DocumentEntity);
7166

72-
return result;
73-
});
67+
const result = await repository.save(entity, saveOptions);
68+
69+
logger.debug(`Document added to the wallet`);
70+
71+
return result;
7472
}

packages/data-store/src/entities/document/update-document.ts

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,14 @@ export async function updateDocument({
1212
dataStore,
1313
document,
1414
}: ContextProps & {document: WalletDocument}): Promise<WalletDocument> {
15-
return dataStore.db.transaction(async transactionalEntityManager => {
16-
logger.debug(`Updating document with id ${document.id}`);
15+
logger.debug(`Updating document with id ${document.id}`);
1716

18-
const repository = transactionalEntityManager.getRepository(DocumentEntity);
19-
const entity = await toDocumentEntity({
20-
dataStore,
21-
document,
22-
});
23-
await repository.save(entity, saveOptions);
24-
25-
return document;
17+
const repository = dataStore.db.getRepository(DocumentEntity);
18+
const entity = await toDocumentEntity({
19+
dataStore,
20+
document,
2621
});
22+
await repository.save(entity, saveOptions);
23+
24+
return document;
2725
}

0 commit comments

Comments
 (0)