@@ -27,7 +27,7 @@ export namespace ConnectedDocs {
27
27
otherVersions : Array < Document > ) {
28
28
29
29
const connectedDocs = await getExistingConnectedDocs (
30
- datastore . get , relationNames , [ document ] . concat ( otherVersions )
30
+ datastore , relationNames , [ document ] . concat ( otherVersions )
31
31
) ;
32
32
33
33
const docsToUpdate = updateRelations (
@@ -39,7 +39,7 @@ export namespace ConnectedDocs {
39
39
40
40
for ( const doc of connectedDocs ) datastore . convert ( doc ) ;
41
41
42
- await updateDocs ( datastore . update , docsToUpdate ) ;
42
+ await updateDocs ( datastore , docsToUpdate ) ;
43
43
}
44
44
45
45
@@ -48,7 +48,7 @@ export namespace ConnectedDocs {
48
48
inverseRelationsMap : Relation . InverseRelationsMap ,
49
49
document : Document ) {
50
50
51
- const connectedDocs = await getExistingConnectedDocsForRemove ( datastore . get , relationNames , document ) ;
51
+ const connectedDocs = await getExistingConnectedDocsForRemove ( datastore , relationNames , document ) ;
52
52
53
53
const docsToUpdate = updateRelations (
54
54
document ,
@@ -57,41 +57,39 @@ export namespace ConnectedDocs {
57
57
false
58
58
) ;
59
59
60
- await updateDocs ( datastore . update , docsToUpdate ) ;
60
+ await updateDocs ( datastore , docsToUpdate ) ;
61
61
}
62
62
63
63
64
- async function updateDocs ( update : Datastore . Update , docsToUpdate : Array < Document > ) {
64
+ async function updateDocs ( datastore : Datastore , docsToUpdate : Array < Document > ) {
65
65
66
66
// Note that this does not update a document for being target of isRecordedIn
67
67
for ( let docToUpdate of docsToUpdate ) {
68
- await update ( docToUpdate , undefined ) ;
68
+ await datastore . update ( docToUpdate , undefined ) ;
69
69
}
70
70
}
71
71
72
72
73
- async function getExistingConnectedDocs ( get : Datastore . Get ,
74
- relationNames : Array < Name > ,
73
+ async function getExistingConnectedDocs ( datastore : Datastore , relationNames : Array < Name > ,
75
74
documents : Array < Document > ) {
76
75
77
- const uniqueConnectedDocIds = getUniqueConnectedDocumentsIds (
78
- documents , relationNames ) ;
76
+ const uniqueConnectedDocIds = getUniqueConnectedDocumentsIds ( documents , relationNames , datastore ) ;
79
77
80
- return getDocumentsForIds ( get , uniqueConnectedDocIds , id => {
78
+ return getDocumentsForIds ( datastore , uniqueConnectedDocIds , id => {
81
79
console . warn ( 'connected document not found' , id ) ;
82
80
} )
83
81
}
84
82
85
83
86
- async function getExistingConnectedDocsForRemove ( get : Datastore . Get , relationNames : Array < Name > , document : Document ) {
84
+ async function getExistingConnectedDocsForRemove ( datastore : Datastore , relationNames : Array < Name > ,
85
+ document : Document ) {
87
86
88
- const uniqueConnectedDocIds = getUniqueConnectedDocumentsIds (
89
- [ document ] , relationNames ) ;
87
+ const uniqueConnectedDocIds = getUniqueConnectedDocumentsIds ( [ document ] , relationNames , datastore ) ;
90
88
91
89
const liesWithinTargets = Resource . getRelationTargets ( document . resource , [ 'liesWithin' ] ) ;
92
90
const recordedInTargets = Resource . getRelationTargets ( document . resource , [ 'isRecordedIn' ] ) ;
93
91
94
- return getDocumentsForIds ( get , uniqueConnectedDocIds , id => {
92
+ return getDocumentsForIds ( datastore , uniqueConnectedDocIds , id => {
95
93
if ( liesWithinTargets . includes ( id ) || recordedInTargets . includes ( id ) ) {
96
94
// this can happen due to deletion order during deletion with descendants
97
95
} else {
@@ -101,15 +99,13 @@ export namespace ConnectedDocs {
101
99
}
102
100
103
101
104
- async function getDocumentsForIds ( get : Datastore . Get ,
105
- ids : string [ ] ,
106
- handleError : ( id : string ) => void ) {
102
+ async function getDocumentsForIds ( datastore : Datastore , ids : string [ ] , handleError : ( id : string ) => void ) {
107
103
108
104
const connectedDocuments : Array < Document > = [ ] ;
109
105
for ( let id of ids ) {
110
106
111
107
try {
112
- connectedDocuments . push ( await get ( id ) ) ;
108
+ connectedDocuments . push ( await datastore . get ( id ) ) ;
113
109
} catch {
114
110
handleError ( id ) ;
115
111
}
@@ -118,15 +114,26 @@ export namespace ConnectedDocs {
118
114
}
119
115
120
116
121
- function getUniqueConnectedDocumentsIds ( documents : Array < Document > ,
122
- allowedRelations : string [ ] ) {
117
+ function getUniqueConnectedDocumentsIds ( documents : Array < Document > , allowedRelations : string [ ] ,
118
+ datastore : Datastore ) {
123
119
124
120
const getAllRelationTargetsForDoc = ( doc : Document ) : string [ ] =>
125
- Resource . getRelationTargets ( doc . resource , allowedRelations ) ;
121
+ Resource . getRelationTargets ( doc . resource , allowedRelations )
122
+ . concat ( getPresentInDocumentIds ( doc , datastore ) ) ;
126
123
127
124
return flow (
128
125
documents ,
129
126
flatMap ( getAllRelationTargetsForDoc ) ,
130
127
subtract ( documents . map ( toResourceId ) ) ) ;
131
128
}
129
+
130
+
131
+ function getPresentInDocumentIds ( document : Document , datastore : Datastore ) : string [ ] {
132
+
133
+ return datastore . findIds ( {
134
+ constraints : {
135
+ 'isPresentIn:contain' : document . resource . id
136
+ }
137
+ } ) . ids ;
138
+ }
132
139
}
0 commit comments