File tree Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Expand file tree Collapse file tree 2 files changed +27
-2
lines changed Original file line number Diff line number Diff line change @@ -91,8 +91,17 @@ const Nested =
9191 return nested ;
9292 } ;
9393
94- const putNested = async ( object : NestedValue ) : Promise < string [ ] > => {
95- const flattenedEntries = flatten ( object ) ;
94+ type PutNestedFunction = {
95+ ( object : NestedValue ) : Promise < string [ ] >
96+ ( key : string , object : NestedValue ) : Promise < string [ ] >
97+ }
98+ const putNested : PutNestedFunction = async ( keyOrObject , object ?: NestedValue | undefined ) : Promise < string [ ] > => {
99+ let flattenedEntries : { key : string ; value : DagCborEncodable } [ ] ;
100+ if ( typeof keyOrObject === "string" ) {
101+ flattenedEntries = flatten ( object ) . map ( entry => ( { key : `${ keyOrObject } /${ entry . key } ` , value : entry . value } ) ) ;
102+ } else {
103+ flattenedEntries = flatten ( keyOrObject ) ;
104+ }
96105 return await Promise . all (
97106 flattenedEntries . map ( ( e ) => put ( e . key , e . value ) ) ,
98107 ) ;
Original file line number Diff line number Diff line change @@ -224,6 +224,22 @@ describe("Nested Database", () => {
224224 ] ) ;
225225 } ) ;
226226
227+ it ( "put key nested value" , async ( ) => {
228+ await db . put ( "a" , { b : 2 , c : 3 } )
229+ await db . put ( "a" , { b : 1 } ) ;
230+
231+ const actual = toNested ( await db . all ( ) ) ;
232+ expect ( actual ) . to . deep . equal ( { a : { b : 1 } } ) ;
233+ } ) ;
234+
235+ it ( "put nested value merges with previous values" , async ( ) => {
236+ await db . put ( "a" , { b : 2 , c : 3 } )
237+ await db . putNested ( "a" , { b : 1 } ) ;
238+
239+ const actual = toNested ( await db . all ( ) ) ;
240+ expect ( actual ) . to . deep . equal ( { a : { b : 1 , c : 3 } } ) ;
241+ } ) ;
242+
227243 it ( "returns all values" , async ( ) => {
228244 const keyvalue : {
229245 key : string ;
You can’t perform that action at this time.
0 commit comments