11import _ from "lodash" ;
22import { Maybe } from "../../types/utils" ;
3+ import { Id } from "../common/entities/Schemas" ;
34import { MetadataImportParams } from "../metadata/entities/MetadataSynchronizationParams" ;
45import { ObjectSharing } from "../storage/repositories/StorageClient" ;
56
67export type DataStoreNamespace = string ;
78export type DataStoreKey = { id : string ; value : any } ;
89
9- export type DataStoreAttrs = { namespace : DataStoreNamespace ; keys : DataStoreKey [ ] ; sharing : Maybe < ObjectSharing > } ;
10+ export type DataStoreAttrs = {
11+ namespace : DataStoreNamespace ;
12+ keys : DataStoreKey [ ] ;
13+ sharing : Maybe < ObjectSharing > ;
14+ action ?: MetadataImportParams [ "mergeMode" ] ;
15+ } ;
16+ export type DataStoreOptions = { action : MetadataImportParams [ "mergeMode" ] } ;
17+ type NamespaceWithAction = { namespace : DataStoreNamespace ; options : DataStoreOptions } ;
1018
1119export class DataStoreMetadata {
1220 public readonly namespace : DataStoreNamespace ;
1321 public readonly keys : DataStoreKey [ ] ;
1422 public readonly sharing : Maybe < ObjectSharing > ;
23+ public readonly action ?: MetadataImportParams [ "mergeMode" ] ;
1524
1625 static NS_SEPARATOR = "[NS]" ;
1726
1827 constructor ( data : DataStoreAttrs ) {
1928 this . keys = data . keys ;
2029 this . namespace = data . namespace ;
2130 this . sharing = data . sharing ;
31+ this . action = data . action ;
2232 }
2333
2434 static buildFromKeys ( keysWithNamespaces : string [ ] ) : DataStoreMetadata [ ] {
@@ -42,10 +52,7 @@ export class DataStoreMetadata {
4252 return new DataStoreMetadata ( {
4353 namespace,
4454 keys : _ ( keys )
45- . map ( key => {
46- if ( ! key . key ) return undefined ;
47- return { id : key . key , value : "" } ;
48- } )
55+ . map ( key => ( key . key ? { id : key . key , value : "" } : undefined ) )
4956 . compact ( )
5057 . value ( ) ,
5158 sharing : undefined ,
@@ -57,20 +64,25 @@ export class DataStoreMetadata {
5764 }
5865
5966 static combine (
67+ ids : Id [ ] ,
6068 origin : DataStoreMetadata [ ] ,
6169 destination : DataStoreMetadata [ ] ,
62- action : MetadataImportParams [ "mergeMode" ] = "MERGE"
70+ options : DataStoreOptions
6371 ) : DataStoreMetadata [ ] {
6472 const destinationDataStore = _ . keyBy ( destination , "namespace" ) ;
73+ const namespacesWithAction = this . mergeOrReplaceNameSpace ( ids , options ) ;
6574
6675 return _ ( origin )
6776 . map ( originItem => {
77+ const namespaceAction = namespacesWithAction . find (
78+ ( { namespace } ) => namespace === originItem . namespace
79+ ) ;
6880 const destItem = destinationDataStore [ originItem . namespace ] ;
6981
7082 if ( ! destItem ) return originItem ;
7183
7284 const combinedKeys =
73- action === "MERGE"
85+ namespaceAction ?. options . action === "MERGE"
7486 ? _ ( originItem . keys )
7587 . unionBy ( destItem . keys , record => record . id )
7688 . value ( )
@@ -80,6 +92,7 @@ export class DataStoreMetadata {
8092 namespace : originItem . namespace ,
8193 keys : combinedKeys ,
8294 sharing : originItem . sharing ,
95+ action : namespaceAction ?. options . action ,
8396 } ) ;
8497 } )
8598 . value ( ) ;
@@ -98,4 +111,30 @@ export class DataStoreMetadata {
98111 static getDataStoreIds ( keys : string [ ] ) : string [ ] {
99112 return keys . filter ( id => id . includes ( DataStoreMetadata . NS_SEPARATOR ) ) ;
100113 }
114+
115+ static isDataStoreId ( id : string ) : boolean {
116+ return id . includes ( DataStoreMetadata . NS_SEPARATOR ) ;
117+ }
118+
119+ static isNamespaceOnlySelected ( id : DataStoreNamespace ) : boolean {
120+ return _ ( id ) . split ( DataStoreMetadata . NS_SEPARATOR ) . compact ( ) . value ( ) . length === 1 ;
121+ }
122+
123+ static mergeOrReplaceNameSpace ( metadataIds : Id [ ] , options : DataStoreOptions ) : NamespaceWithAction [ ] {
124+ return _ ( metadataIds )
125+ . map ( ( id ) : Maybe < NamespaceWithAction > => {
126+ if ( ! this . isDataStoreId ( id ) ) return undefined ;
127+ const [ namespace ] = id . split ( DataStoreMetadata . NS_SEPARATOR ) ;
128+ if ( ! namespace ) return undefined ;
129+
130+ const isNamespaceSelected = this . isNamespaceOnlySelected ( id ) ;
131+ return { namespace : namespace , options : { action : isNamespaceSelected ? options . action : "MERGE" } } ;
132+ } )
133+ . compact ( )
134+ . value ( ) ;
135+ }
136+
137+ static generateKeyId ( namespace : DataStoreNamespace , keyId : string ) {
138+ return [ namespace , DataStoreMetadata . NS_SEPARATOR , keyId ] . join ( "" ) ;
139+ }
101140}
0 commit comments