@@ -141,6 +141,7 @@ export class JsonDB {
141
141
/**
142
142
* Get the wanted data
143
143
* @param dataPath path of the data to retrieve
144
+ * @returns {Promise<any> }
144
145
*/
145
146
public getData ( dataPath : string ) : Promise < any > {
146
147
return readLockAsync ( async ( ) => {
@@ -152,6 +153,7 @@ export class JsonDB {
152
153
/**
153
154
* Same as getData only here it's directly typed to your object
154
155
* @param dataPath path of the data to retrieve
156
+ * @returns {Promise }
155
157
*/
156
158
public getObject < T > ( dataPath : string ) : Promise < T > {
157
159
return this . getData ( dataPath )
@@ -161,6 +163,8 @@ export class JsonDB {
161
163
* Same as getData but with your own object type and a possible default value when we can't find the data path
162
164
* @param dataPath path of the data to retrieve
163
165
* @param defaultValue value to use when the dataPath doesn't lead to data
166
+ * @returns {Promise }
167
+ * @throws {DataError }
164
168
*/
165
169
public async getObjectDefault < T > ( dataPath : string , defaultValue ?: T ) : Promise < T > {
166
170
try {
@@ -179,6 +183,7 @@ export class JsonDB {
179
183
/**
180
184
* Check for existing datapath
181
185
* @param dataPath
186
+ * @returns {Promise<boolean> }
182
187
*/
183
188
public async exists ( dataPath : string ) : Promise < boolean > {
184
189
try {
@@ -195,6 +200,8 @@ export class JsonDB {
195
200
/**
196
201
* Returns the number of element which constitutes the array
197
202
* @param dataPath
203
+ * @returns {Promise<number> }
204
+ * @throws {DataError }
198
205
*/
199
206
public async count ( dataPath : string ) : Promise < number > {
200
207
const result = await this . getData ( dataPath )
@@ -211,6 +218,7 @@ export class JsonDB {
211
218
* @param dataPath base dataPath from where to start searching
212
219
* @param searchValue value to look for in the dataPath
213
220
* @param propertyName name of the property to look for searchValue
221
+ * @returns {Promise<number> }
214
222
*/
215
223
public async getIndex (
216
224
dataPath : string ,
@@ -229,6 +237,7 @@ export class JsonDB {
229
237
* Return the index of the value inside the array. Returns -1, if no match is found.
230
238
* @param dataPath base dataPath from where to start searching
231
239
* @param searchValue value to look for in the dataPath
240
+ * @returns {Promise<number> }
232
241
*/
233
242
public async getIndexValue ( dataPath : string , searchValue : string | number ) : Promise < number > {
234
243
return ( await this . getArrayData ( dataPath ) ) . indexOf ( searchValue )
@@ -247,6 +256,8 @@ export class JsonDB {
247
256
* Find all specific entry in an array/object
248
257
* @param rootPath base dataPath from where to start searching
249
258
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
259
+ * @returns {Promise }
260
+ * @throws {DataError }
250
261
*/
251
262
public async filter < T > ( rootPath : string , callback : FindCallback ) : Promise < T [ ] | undefined > {
252
263
const result = await this . getData ( rootPath )
@@ -279,6 +290,8 @@ export class JsonDB {
279
290
* Find a specific entry in an array/object
280
291
* @param rootPath base dataPath from where to start searching
281
292
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
293
+ * @returns {Promise }
294
+ * @throws {DataError }
282
295
*/
283
296
public async find < T > ( rootPath : string , callback : FindCallback ) : Promise < T | undefined > {
284
297
const result = await this . getData ( rootPath )
@@ -308,6 +321,8 @@ export class JsonDB {
308
321
* @param dataPath path leading to the data
309
322
* @param data data to push
310
323
* @param override overriding or not the data, if not, it will merge them
324
+ * @returns {Promise<void> }
325
+ * @throws {DataError }
311
326
*/
312
327
public async push ( dataPath : string , data : any , override : boolean = true ) : Promise < void > {
313
328
return writeLockAsync ( async ( ) => {
@@ -384,6 +399,8 @@ export class JsonDB {
384
399
/**
385
400
* Manually load the database
386
401
* It is automatically called when the first getData is done
402
+ * @return {Promise<void> }
403
+ * @throws {DatabaseError }
387
404
*/
388
405
public async load ( ) : Promise < void > {
389
406
if ( this . loaded ) {
@@ -401,6 +418,8 @@ export class JsonDB {
401
418
* Manually save the database
402
419
* By default you can't save the database if it's not loaded
403
420
* @param force force the save of the database
421
+ * @return {Promise<void> }
422
+ * @throws {DatabaseError }
404
423
*/
405
424
public async save ( force ?: boolean ) : Promise < void > {
406
425
force = force || false
@@ -413,4 +432,43 @@ export class JsonDB {
413
432
throw new DatabaseError ( "Can't save the database" , 2 , err )
414
433
}
415
434
}
435
+
436
+
437
+ /**
438
+ * Convert a router style path to a normal path
439
+ * By default propertyName to search is "id"
440
+ * @param path router based path to a correct base path
441
+ * @param propertyName name of the property to look for searchValue
442
+ */
443
+ public async fromPath ( path : string , propertyName :string = 'id' ) : Promise < string > {
444
+
445
+ const [ , ...pathToQuery ] = path . split ( "/" )
446
+
447
+ const pathObject = pathToQuery . reduce ( ( prev , curr , indexPath ) => {
448
+ const isKey = indexPath % 2 === 0
449
+ if ( isKey ) {
450
+ prev [ `${ curr } ` ] = ''
451
+ } else {
452
+ const keys = Object . keys ( prev )
453
+ prev [ `${ keys [ keys . length - 1 ] } ` ] = `${ curr } `
454
+ }
455
+ return prev
456
+ } , { } as { [ key : string ] :string } )
457
+
458
+ let normalPath : string [ ] = [ ]
459
+
460
+ for await ( const pathKey of Object . keys ( pathObject ) ) {
461
+ normalPath . push ( `/${ pathKey } ` )
462
+
463
+ const pathValue = pathObject [ pathKey ]
464
+ try {
465
+ const pathIndex = await this . getIndex ( normalPath . join ( "" ) , pathValue , propertyName )
466
+ normalPath . push ( `[${ pathIndex } ]` )
467
+ } catch ( error ) {
468
+ throw new DataError ( `DataPath: ${ normalPath . join ( "" ) } /${ pathValue } not found.` , 13 , error )
469
+ }
470
+ }
471
+
472
+ return normalPath . join ( "" )
473
+ }
416
474
}
0 commit comments