File tree 2 files changed +13
-14
lines changed
2 files changed +13
-14
lines changed Original file line number Diff line number Diff line change @@ -196,7 +196,6 @@ if (process.env['NODE_ENV'] !== 'production') {
196
196
observer . onReadStart = ( ) => {
197
197
prevEndpoints = JSON . stringify ( Object . keys ( db . data ) . sort ( ) )
198
198
}
199
-
200
199
observer . onReadEnd = ( data ) => {
201
200
if ( data === null ) {
202
201
return
Original file line number Diff line number Diff line change @@ -113,31 +113,31 @@ function randomId(): string {
113
113
return randomBytes ( 2 ) . toString ( 'hex' )
114
114
}
115
115
116
- function ensureItemsHaveIds ( items : Item [ ] ) : Item [ ] {
117
- return items . map ( ( item ) => {
116
+ function fixItemsIds ( items : Item [ ] ) {
117
+ items . forEach ( ( item ) => {
118
+ if ( typeof item [ 'id' ] === 'number' ) {
119
+ item [ 'id' ] = item [ 'id' ] . toString ( )
120
+ }
118
121
if ( item [ 'id' ] === undefined ) {
119
- return { ... item , id : randomId ( ) }
122
+ item [ 'id' ] = randomId ( )
120
123
}
121
- return item
122
124
} )
123
125
}
124
126
125
127
// Ensure all items have an id
126
- function ensureAllItemsHaveIds ( data : Data ) : Data {
127
- return Object . entries ( data ) . reduce (
128
- ( acc , [ key , value ] ) => ( {
129
- ...acc ,
130
- [ key ] : Array . isArray ( value ) ? ensureItemsHaveIds ( value ) : value ,
131
- } ) ,
132
- { } ,
133
- )
128
+ function fixAllItemsIds ( data : Data ) {
129
+ Object . values ( data ) . forEach ( ( value ) => {
130
+ if ( Array . isArray ( value ) ) {
131
+ fixItemsIds ( value )
132
+ }
133
+ } )
134
134
}
135
135
136
136
export class Service {
137
137
#db: Low < Data >
138
138
139
139
constructor ( db : Low < Data > ) {
140
- db . data = ensureAllItemsHaveIds ( db . data )
140
+ fixAllItemsIds ( db . data )
141
141
this . #db = db
142
142
}
143
143
You can’t perform that action at this time.
0 commit comments