Skip to content

Commit 2019608

Browse files
Merge pull request #696 from Belphemur/develop
release
2 parents 5efd5ee + 5b4d05b commit 2019608

File tree

9 files changed

+2990
-4083
lines changed

9 files changed

+2990
-4083
lines changed

.all-contributorsrc

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,33 @@
4242
"code",
4343
"doc"
4444
]
45+
},
46+
{
47+
"login": "CHR-onicles",
48+
"name": "Divine Anum",
49+
"avatar_url": "https://avatars.githubusercontent.com/u/44934037?v=4",
50+
"profile": "http://divineanum.me",
51+
"contributions": [
52+
"doc"
53+
]
54+
},
55+
{
56+
"login": "michalfedyna",
57+
"name": "Michał Fedyna",
58+
"avatar_url": "https://avatars.githubusercontent.com/u/84079005?v=4",
59+
"profile": "https://github.com/michalfedyna",
60+
"contributions": [
61+
"doc"
62+
]
63+
},
64+
{
65+
"login": "alexsad",
66+
"name": "alex.query",
67+
"avatar_url": "https://avatars.githubusercontent.com/u/2659613?v=4",
68+
"profile": "https://github.com/alexsad",
69+
"contributions": [
70+
"code"
71+
]
4572
}
4673
],
4774
"contributorsPerLine": 7,
@@ -50,5 +77,6 @@
5077
"repoType": "github",
5178
"repoHost": "https://github.com",
5279
"skipCi": false,
53-
"commitConvention": "angular"
80+
"commitConvention": "angular",
81+
"commitType": "docs"
5482
}

.github/workflows/codeql-analysis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535

3636
steps:
3737
- name: Checkout repository
38-
uses: actions/checkout@v3
38+
uses: actions/checkout@v4
3939

4040
# Initializes the CodeQL tools for scanning.
4141
- name: Initialize CodeQL

.github/workflows/nodejs.yml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name: NodeJs
22
env:
33
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
44
NPM_TOKEN: ${{secrets.NPM_TOKEN }}
5-
NODEJS: 18
5+
NODEJS: "lts/*"
66
YARN_CACHE: "yarn-cache"
77

88
on:
@@ -16,9 +16,9 @@ jobs:
1616
runs-on: ubuntu-latest
1717
strategy:
1818
matrix:
19-
node: [ '14', '16', '18' ]
19+
node: ['lts/-1', 'lts/*', 'current' ]
2020
steps:
21-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
2222
- name: Set yarn cache
2323
run: yarn config set cache-folder ${{env.YARN_CACHE}}
2424
- name: Cache node modules
@@ -29,7 +29,7 @@ jobs:
2929
path: ${{env.YARN_CACHE}}
3030
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
3131
- name: Setup Node.js ${{ matrix.node }}
32-
uses: actions/setup-node@v3.6.0
32+
uses: actions/setup-node@v3.8.1
3333
with:
3434
node-version: ${{ matrix.node }}
3535
- name: Install dependencies
@@ -43,7 +43,7 @@ jobs:
4343
ci-cd-check:
4444
runs-on: ubuntu-latest
4545
steps:
46-
- uses: actions/checkout@v3
46+
- uses: actions/checkout@v4
4747
- name: Set yarn cache
4848
run: yarn config set cache-folder ${{env.YARN_CACHE}}
4949
- name: Cache node modules
@@ -54,7 +54,7 @@ jobs:
5454
path: ${{env.YARN_CACHE}}
5555
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
5656
- name: Setup Node.js ${{env.NODEJS}}
57-
uses: actions/setup-node@v3.6.0
57+
uses: actions/setup-node@v3.8.1
5858
with:
5959
node-version: ${{env.NODEJS}}
6060
- name: Install dependencies
@@ -66,11 +66,11 @@ jobs:
6666
if: github.ref == 'refs/heads/master'
6767
runs-on: ubuntu-latest
6868
steps:
69-
- uses: actions/checkout@v3
69+
- uses: actions/checkout@v4
7070
- name: Set yarn cache
7171
run: yarn config set cache-folder ${{env.YARN_CACHE}}
7272
- name: Setup Node.js
73-
uses: actions/setup-node@v3.6.0
73+
uses: actions/setup-node@v3.8.1
7474
with:
7575
node-version: ${{env.NODEJS}}
7676
- name: Cache node modules
@@ -85,7 +85,7 @@ jobs:
8585
- name: Build package
8686
run: yarn build:doc
8787
- name: Deploy 🚀
88-
uses: JamesIves/github-pages-deploy-action@v4.4.1
88+
uses: JamesIves/github-pages-deploy-action@v4.4.3
8989
with:
9090
branch: gh-pages # The branch the action should deploy to.
9191
folder: docs # The folder the action should deploy.
@@ -95,11 +95,11 @@ jobs:
9595
if: github.ref == 'refs/heads/master'
9696
runs-on: ubuntu-latest
9797
steps:
98-
- uses: actions/checkout@v3
98+
- uses: actions/checkout@v4
9999
- name: Set yarn cache
100100
run: yarn config set cache-folder ${{env.YARN_CACHE}}
101101
- name: Setup Node.js
102-
uses: actions/setup-node@v3.6.0
102+
uses: actions/setup-node@v3.8.1
103103
with:
104104
node-version: ${{env.NODEJS}}
105105
- name: Cache node modules

README.md

Lines changed: 56 additions & 45 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
"build": "tsc",
1010
"build:doc": "typedoc --entryPointStrategy expand ./src",
1111
"semantic-release": "semantic-release",
12-
"travis-deploy-once": "travis-deploy-once",
1312
"prepare": "husky install"
1413
},
1514
"repository": {
@@ -28,11 +27,6 @@
2827
"url": "https://github.com/Belphemur/node-json-db/issues"
2928
},
3029
"homepage": "https://github.com/Belphemur/node-json-db",
31-
"config": {
32-
"commitizen": {
33-
"path": "./node_modules/cz-conventional-changelog"
34-
}
35-
},
3630
"files": [
3731
"dist/",
3832
"README.md",
@@ -61,20 +55,18 @@
6155
"devDependencies": {
6256
"@semantic-release/changelog": "^6.0.0",
6357
"@semantic-release/git": "^10.0.0",
64-
"@types/jest": "^28.1.6",
65-
"@types/node": "^18.0.0",
58+
"@types/jest": "^29.5.0",
59+
"@types/node": "^20.1.0",
6660
"@types/rwlock": "^5.0.3",
6761
"@types/safe-regex": "^1.1.2",
68-
"cz-conventional-changelog": "^3.3.0",
6962
"husky": "^8.0.0",
70-
"jest": "^28.1.3",
63+
"jest": "^29.5.0",
7164
"last-release-git": "^0.0.3",
7265
"safe-regex": "~2.1.1",
73-
"semantic-release": "^21.0.0",
74-
"travis-deploy-once": "^5.0.11",
75-
"ts-jest": "^28.0.7",
76-
"typedoc": "^0.23.1",
77-
"typescript": "^5.0.2",
66+
"semantic-release": "^22.0.0",
67+
"ts-jest": "^29.1.0",
68+
"typedoc": "^0.25.0",
69+
"typescript": "^5.0.4",
7870
"validate-commit-msg": "^2.14.0"
7971
},
8072
"husky": {

src/JsonDB.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class JsonDB {
141141
/**
142142
* Get the wanted data
143143
* @param dataPath path of the data to retrieve
144+
* @returns {Promise<any>}
144145
*/
145146
public getData(dataPath: string): Promise<any> {
146147
return readLockAsync(async () => {
@@ -152,6 +153,7 @@ export class JsonDB {
152153
/**
153154
* Same as getData only here it's directly typed to your object
154155
* @param dataPath path of the data to retrieve
156+
* @returns {Promise}
155157
*/
156158
public getObject<T>(dataPath: string): Promise<T> {
157159
return this.getData(dataPath)
@@ -161,6 +163,8 @@ export class JsonDB {
161163
* Same as getData but with your own object type and a possible default value when we can't find the data path
162164
* @param dataPath path of the data to retrieve
163165
* @param defaultValue value to use when the dataPath doesn't lead to data
166+
* @returns {Promise}
167+
* @throws {DataError}
164168
*/
165169
public async getObjectDefault<T>(dataPath: string, defaultValue?: T): Promise<T> {
166170
try {
@@ -179,6 +183,7 @@ export class JsonDB {
179183
/**
180184
* Check for existing datapath
181185
* @param dataPath
186+
* @returns {Promise<boolean>}
182187
*/
183188
public async exists(dataPath: string): Promise<boolean> {
184189
try {
@@ -195,6 +200,8 @@ export class JsonDB {
195200
/**
196201
* Returns the number of element which constitutes the array
197202
* @param dataPath
203+
* @returns {Promise<number>}
204+
* @throws {DataError}
198205
*/
199206
public async count(dataPath: string): Promise<number> {
200207
const result = await this.getData(dataPath)
@@ -211,6 +218,7 @@ export class JsonDB {
211218
* @param dataPath base dataPath from where to start searching
212219
* @param searchValue value to look for in the dataPath
213220
* @param propertyName name of the property to look for searchValue
221+
* @returns {Promise<number>}
214222
*/
215223
public async getIndex(
216224
dataPath: string,
@@ -229,6 +237,7 @@ export class JsonDB {
229237
* Return the index of the value inside the array. Returns -1, if no match is found.
230238
* @param dataPath base dataPath from where to start searching
231239
* @param searchValue value to look for in the dataPath
240+
* @returns {Promise<number>}
232241
*/
233242
public async getIndexValue(dataPath: string, searchValue: string | number): Promise<number> {
234243
return (await this.getArrayData(dataPath)).indexOf(searchValue)
@@ -247,6 +256,8 @@ export class JsonDB {
247256
* Find all specific entry in an array/object
248257
* @param rootPath base dataPath from where to start searching
249258
* @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}
250261
*/
251262
public async filter<T>(rootPath: string, callback: FindCallback): Promise<T[] | undefined> {
252263
const result = await this.getData(rootPath)
@@ -279,6 +290,8 @@ export class JsonDB {
279290
* Find a specific entry in an array/object
280291
* @param rootPath base dataPath from where to start searching
281292
* @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}
282295
*/
283296
public async find<T>(rootPath: string, callback: FindCallback): Promise<T | undefined> {
284297
const result = await this.getData(rootPath)
@@ -308,6 +321,8 @@ export class JsonDB {
308321
* @param dataPath path leading to the data
309322
* @param data data to push
310323
* @param override overriding or not the data, if not, it will merge them
324+
* @returns {Promise<void>}
325+
* @throws {DataError}
311326
*/
312327
public async push(dataPath: string, data: any, override: boolean = true): Promise<void> {
313328
return writeLockAsync(async () => {
@@ -384,6 +399,8 @@ export class JsonDB {
384399
/**
385400
* Manually load the database
386401
* It is automatically called when the first getData is done
402+
* @return {Promise<void>}
403+
* @throws {DatabaseError}
387404
*/
388405
public async load(): Promise<void> {
389406
if (this.loaded) {
@@ -401,6 +418,8 @@ export class JsonDB {
401418
* Manually save the database
402419
* By default you can't save the database if it's not loaded
403420
* @param force force the save of the database
421+
* @return {Promise<void>}
422+
* @throws {DatabaseError}
404423
*/
405424
public async save(force?: boolean): Promise<void> {
406425
force = force || false
@@ -413,4 +432,43 @@ export class JsonDB {
413432
throw new DatabaseError("Can't save the database", 2, err)
414433
}
415434
}
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+
}
416474
}

0 commit comments

Comments
 (0)