@@ -3,10 +3,13 @@ import { Document } from '../model/document';
3
3
import { NewDocument } from '../model/document' ;
4
4
import { Query } from '../model/query' ;
5
5
import { Name } from '../tools/named' ;
6
- import { CategoryConverter } from './category -converter' ;
6
+ import { DocumentConverter } from './document -converter' ;
7
7
import { DatastoreErrors } from './datastore-errors' ;
8
8
import { DocumentCache } from './document-cache' ;
9
9
import { PouchdbDatastore } from './pouchdb/pouchdb-datastore' ;
10
+ import { WarningsUpdater } from './warnings-updater' ;
11
+ import { ProjectConfiguration } from '../services/project-configuration' ;
12
+ import { CategoryForm } from '../model/configuration/category-form' ;
10
13
11
14
12
15
/**
@@ -38,7 +41,8 @@ export class Datastore {
38
41
constructor ( private datastore : PouchdbDatastore ,
39
42
private indexFacade : IndexFacade ,
40
43
private documentCache : DocumentCache ,
41
- private categoryConverter : CategoryConverter ,
44
+ private documentConverter : DocumentConverter ,
45
+ private projectConfiguration : ProjectConfiguration ,
42
46
private getUser : ( ) => Name ) {
43
47
}
44
48
@@ -68,9 +72,12 @@ export class Datastore {
68
72
69
73
public async bulkCreate ( documents : Array < NewDocument > ) : Promise < Array < Document > > {
70
74
71
- return ( await this . datastore . bulkCreate ( documents , this . getUser ( ) ) ) . map ( document => {
72
- return this . updateIndex ( document ) ;
73
- } ) ;
75
+ const resultDocuments : Array < Document > = [ ] ;
76
+ for ( let document of await this . datastore . bulkCreate ( documents , this . getUser ( ) ) ) {
77
+ resultDocuments . push ( await this . updateIndex ( document ) ) ;
78
+ }
79
+
80
+ return resultDocuments ;
74
81
}
75
82
76
83
@@ -90,26 +97,45 @@ export class Datastore {
90
97
*/
91
98
public update : Datastore . Update = async ( document : Document , squashRevisionsIds ?: string [ ] ) : Promise < Document > => {
92
99
100
+ delete document . warnings ;
101
+
93
102
return this . updateIndex ( await this . datastore . update ( document , this . getUser ( ) , squashRevisionsIds ) ) ;
94
103
}
95
104
96
105
97
106
public async bulkUpdate ( documents : Array < Document > ) : Promise < Array < Document > > {
98
107
99
- return ( await this . datastore . bulkUpdate ( documents , this . getUser ( ) ) ) . map ( document => {
100
- return this . updateIndex ( document ) ;
101
- } ) ;
108
+ documents . forEach ( document => delete document . warnings ) ;
109
+
110
+ const resultDocuments : Array < Document > = [ ] ;
111
+ for ( let document of await this . datastore . bulkUpdate ( documents , this . getUser ( ) ) ) {
112
+ resultDocuments . push ( await this . updateIndex ( document ) ) ;
113
+ }
114
+
115
+ return resultDocuments ;
102
116
}
103
117
104
118
105
- private updateIndex ( document : Document ) {
119
+ private async updateIndex ( document : Document ) : Promise < Document > {
106
120
107
- const convertedDocument = this . categoryConverter . convert ( document ) ;
121
+ const convertedDocument = this . documentConverter . convert ( document ) ;
108
122
this . indexFacade . put ( convertedDocument ) ;
109
123
110
- return ! this . documentCache . get ( document . resource . id as any )
124
+ const previousVersion : Document | undefined = this . documentCache . get ( convertedDocument . resource . id ) ;
125
+ const previousIdentifier : string | undefined = previousVersion ?. resource . identifier ;
126
+
127
+ document = ! previousVersion
111
128
? this . documentCache . set ( convertedDocument )
112
129
: this . documentCache . reassign ( convertedDocument ) ;
130
+ const category : CategoryForm = this . projectConfiguration . getCategory ( document . resource . category ) ;
131
+
132
+ await WarningsUpdater . updateNonUniqueIdentifierWarning (
133
+ document , this . indexFacade , this , previousIdentifier , true
134
+ ) ;
135
+ await WarningsUpdater . updateResourceLimitWarning ( document , category , this . indexFacade , this , true ) ;
136
+ await WarningsUpdater . updateRelationTargetWarning ( document , this . indexFacade , this . documentCache , this , true ) ;
137
+
138
+ return document ;
113
139
}
114
140
115
141
@@ -134,6 +160,12 @@ export class Datastore {
134
160
135
161
await this . datastore . remove ( document ) ;
136
162
this . documentCache . remove ( document . resource . id ) ;
163
+
164
+ await WarningsUpdater . updateResourceLimitWarnings (
165
+ this ,
166
+ this . indexFacade ,
167
+ this . projectConfiguration . getCategory ( document . resource . category )
168
+ ) ;
137
169
}
138
170
139
171
@@ -157,7 +189,7 @@ export class Datastore {
157
189
return cachedDocument ;
158
190
}
159
191
160
- let document = this . categoryConverter . convert ( await this . datastore . fetch ( id , options ?. conflicts ) ) ;
192
+ let document = this . documentConverter . convert ( await this . datastore . fetch ( id , options ?. conflicts ) ) ;
161
193
162
194
return cachedDocument
163
195
? this . documentCache . reassign ( document )
@@ -200,7 +232,7 @@ export class Datastore {
200
232
201
233
public convert : Datastore . Convert = ( document : Document ) => {
202
234
203
- this . categoryConverter . convert ( document ) ;
235
+ this . documentConverter . convert ( document ) ;
204
236
}
205
237
206
238
@@ -229,7 +261,7 @@ export class Datastore {
229
261
*/
230
262
public async getRevision ( docId : string , revisionId : string ) : Promise < Document > {
231
263
232
- return this . categoryConverter . convert (
264
+ return this . documentConverter . convert (
233
265
await this . datastore . fetchRevision ( docId , revisionId ) ) ;
234
266
}
235
267
@@ -312,11 +344,10 @@ export class Datastore {
312
344
private async getDocumentsFromDatastore ( ids : string [ ] ) : Promise < Array < Document > > {
313
345
314
346
const documents : Array < Document > = [ ] ;
315
-
316
347
( await this . datastore . bulkFetch ( ids ) ) . forEach ( document => {
317
348
318
349
try {
319
- const convertedDocument = this . categoryConverter . convert ( document ) ;
350
+ const convertedDocument = this . documentConverter . convert ( document ) ;
320
351
documents . push ( this . documentCache . set ( convertedDocument ) ) ;
321
352
} catch ( errWithParams ) {
322
353
if ( errWithParams [ 0 ] === DatastoreErrors . UNKNOWN_CATEGORY ) {
0 commit comments