Skip to content

Commit

Permalink
Merge pull request #696 from Belphemur/develop
Browse files Browse the repository at this point in the history
release
  • Loading branch information
kodiakhq[bot] authored Oct 16, 2023
2 parents 5efd5ee + 5b4d05b commit 2019608
Show file tree
Hide file tree
Showing 9 changed files with 2,990 additions and 4,083 deletions.
30 changes: 29 additions & 1 deletion .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@
"code",
"doc"
]
},
{
"login": "CHR-onicles",
"name": "Divine Anum",
"avatar_url": "https://avatars.githubusercontent.com/u/44934037?v=4",
"profile": "http://divineanum.me",
"contributions": [
"doc"
]
},
{
"login": "michalfedyna",
"name": "Michał Fedyna",
"avatar_url": "https://avatars.githubusercontent.com/u/84079005?v=4",
"profile": "https://github.com/michalfedyna",
"contributions": [
"doc"
]
},
{
"login": "alexsad",
"name": "alex.query",
"avatar_url": "https://avatars.githubusercontent.com/u/2659613?v=4",
"profile": "https://github.com/alexsad",
"contributions": [
"code"
]
}
],
"contributorsPerLine": 7,
Expand All @@ -50,5 +77,6 @@
"repoType": "github",
"repoHost": "https://github.com",
"skipCi": false,
"commitConvention": "angular"
"commitConvention": "angular",
"commitType": "docs"
}
2 changes: 1 addition & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:

steps:
- name: Checkout repository
uses: actions/checkout@v3
uses: actions/checkout@v4

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
Expand Down
22 changes: 11 additions & 11 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: NodeJs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{secrets.NPM_TOKEN }}
NODEJS: 18
NODEJS: "lts/*"
YARN_CACHE: "yarn-cache"

on:
Expand All @@ -16,9 +16,9 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
node: [ '14', '16', '18' ]
node: ['lts/-1', 'lts/*', 'current' ]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set yarn cache
run: yarn config set cache-folder ${{env.YARN_CACHE}}
- name: Cache node modules
Expand All @@ -29,7 +29,7 @@ jobs:
path: ${{env.YARN_CACHE}}
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Setup Node.js ${{ matrix.node }}
uses: actions/setup-node@v3.6.0
uses: actions/setup-node@v3.8.1
with:
node-version: ${{ matrix.node }}
- name: Install dependencies
Expand All @@ -43,7 +43,7 @@ jobs:
ci-cd-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set yarn cache
run: yarn config set cache-folder ${{env.YARN_CACHE}}
- name: Cache node modules
Expand All @@ -54,7 +54,7 @@ jobs:
path: ${{env.YARN_CACHE}}
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
- name: Setup Node.js ${{env.NODEJS}}
uses: actions/setup-node@v3.6.0
uses: actions/setup-node@v3.8.1
with:
node-version: ${{env.NODEJS}}
- name: Install dependencies
Expand All @@ -66,11 +66,11 @@ jobs:
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set yarn cache
run: yarn config set cache-folder ${{env.YARN_CACHE}}
- name: Setup Node.js
uses: actions/setup-node@v3.6.0
uses: actions/setup-node@v3.8.1
with:
node-version: ${{env.NODEJS}}
- name: Cache node modules
Expand All @@ -85,7 +85,7 @@ jobs:
- name: Build package
run: yarn build:doc
- name: Deploy 🚀
uses: JamesIves/[email protected].1
uses: JamesIves/[email protected].3
with:
branch: gh-pages # The branch the action should deploy to.
folder: docs # The folder the action should deploy.
Expand All @@ -95,11 +95,11 @@ jobs:
if: github.ref == 'refs/heads/master'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set yarn cache
run: yarn config set cache-folder ${{env.YARN_CACHE}}
- name: Setup Node.js
uses: actions/setup-node@v3.6.0
uses: actions/setup-node@v3.8.1
with:
node-version: ${{env.NODEJS}}
- name: Cache node modules
Expand Down
101 changes: 56 additions & 45 deletions README.md

Large diffs are not rendered by default.

22 changes: 7 additions & 15 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"build": "tsc",
"build:doc": "typedoc --entryPointStrategy expand ./src",
"semantic-release": "semantic-release",
"travis-deploy-once": "travis-deploy-once",
"prepare": "husky install"
},
"repository": {
Expand All @@ -28,11 +27,6 @@
"url": "https://github.com/Belphemur/node-json-db/issues"
},
"homepage": "https://github.com/Belphemur/node-json-db",
"config": {
"commitizen": {
"path": "./node_modules/cz-conventional-changelog"
}
},
"files": [
"dist/",
"README.md",
Expand Down Expand Up @@ -61,20 +55,18 @@
"devDependencies": {
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"@types/jest": "^28.1.6",
"@types/node": "^18.0.0",
"@types/jest": "^29.5.0",
"@types/node": "^20.1.0",
"@types/rwlock": "^5.0.3",
"@types/safe-regex": "^1.1.2",
"cz-conventional-changelog": "^3.3.0",
"husky": "^8.0.0",
"jest": "^28.1.3",
"jest": "^29.5.0",
"last-release-git": "^0.0.3",
"safe-regex": "~2.1.1",
"semantic-release": "^21.0.0",
"travis-deploy-once": "^5.0.11",
"ts-jest": "^28.0.7",
"typedoc": "^0.23.1",
"typescript": "^5.0.2",
"semantic-release": "^22.0.0",
"ts-jest": "^29.1.0",
"typedoc": "^0.25.0",
"typescript": "^5.0.4",
"validate-commit-msg": "^2.14.0"
},
"husky": {
Expand Down
58 changes: 58 additions & 0 deletions src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export class JsonDB {
/**
* Get the wanted data
* @param dataPath path of the data to retrieve
* @returns {Promise<any>}
*/
public getData(dataPath: string): Promise<any> {
return readLockAsync(async () => {
Expand All @@ -152,6 +153,7 @@ export class JsonDB {
/**
* Same as getData only here it's directly typed to your object
* @param dataPath path of the data to retrieve
* @returns {Promise}
*/
public getObject<T>(dataPath: string): Promise<T> {
return this.getData(dataPath)
Expand All @@ -161,6 +163,8 @@ export class JsonDB {
* Same as getData but with your own object type and a possible default value when we can't find the data path
* @param dataPath path of the data to retrieve
* @param defaultValue value to use when the dataPath doesn't lead to data
* @returns {Promise}
* @throws {DataError}
*/
public async getObjectDefault<T>(dataPath: string, defaultValue?: T): Promise<T> {
try {
Expand All @@ -179,6 +183,7 @@ export class JsonDB {
/**
* Check for existing datapath
* @param dataPath
* @returns {Promise<boolean>}
*/
public async exists(dataPath: string): Promise<boolean> {
try {
Expand All @@ -195,6 +200,8 @@ export class JsonDB {
/**
* Returns the number of element which constitutes the array
* @param dataPath
* @returns {Promise<number>}
* @throws {DataError}
*/
public async count(dataPath: string): Promise<number> {
const result = await this.getData(dataPath)
Expand All @@ -211,6 +218,7 @@ export class JsonDB {
* @param dataPath base dataPath from where to start searching
* @param searchValue value to look for in the dataPath
* @param propertyName name of the property to look for searchValue
* @returns {Promise<number>}
*/
public async getIndex(
dataPath: string,
Expand All @@ -229,6 +237,7 @@ export class JsonDB {
* Return the index of the value inside the array. Returns -1, if no match is found.
* @param dataPath base dataPath from where to start searching
* @param searchValue value to look for in the dataPath
* @returns {Promise<number>}
*/
public async getIndexValue(dataPath: string, searchValue: string | number): Promise<number> {
return (await this.getArrayData(dataPath)).indexOf(searchValue)
Expand All @@ -247,6 +256,8 @@ export class JsonDB {
* Find all specific entry in an array/object
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
* @returns {Promise}
* @throws {DataError}
*/
public async filter<T>(rootPath: string, callback: FindCallback): Promise<T[] | undefined> {
const result = await this.getData(rootPath)
Expand Down Expand Up @@ -279,6 +290,8 @@ export class JsonDB {
* Find a specific entry in an array/object
* @param rootPath base dataPath from where to start searching
* @param callback method to filter the result and find the wanted entry. Receive the entry and it's index.
* @returns {Promise}
* @throws {DataError}
*/
public async find<T>(rootPath: string, callback: FindCallback): Promise<T | undefined> {
const result = await this.getData(rootPath)
Expand Down Expand Up @@ -308,6 +321,8 @@ export class JsonDB {
* @param dataPath path leading to the data
* @param data data to push
* @param override overriding or not the data, if not, it will merge them
* @returns {Promise<void>}
* @throws {DataError}
*/
public async push(dataPath: string, data: any, override: boolean = true): Promise<void> {
return writeLockAsync(async () => {
Expand Down Expand Up @@ -384,6 +399,8 @@ export class JsonDB {
/**
* Manually load the database
* It is automatically called when the first getData is done
* @return {Promise<void>}
* @throws {DatabaseError}
*/
public async load(): Promise<void> {
if (this.loaded) {
Expand All @@ -401,6 +418,8 @@ export class JsonDB {
* Manually save the database
* By default you can't save the database if it's not loaded
* @param force force the save of the database
* @return {Promise<void>}
* @throws {DatabaseError}
*/
public async save(force?: boolean): Promise<void> {
force = force || false
Expand All @@ -413,4 +432,43 @@ export class JsonDB {
throw new DatabaseError("Can't save the database", 2, err)
}
}


/**
* Convert a router style path to a normal path
* By default propertyName to search is "id"
* @param path router based path to a correct base path
* @param propertyName name of the property to look for searchValue
*/
public async fromPath(path: string, propertyName:string = 'id' ): Promise<string> {

const [,...pathToQuery] = path.split("/")

const pathObject = pathToQuery.reduce((prev, curr, indexPath) => {
const isKey = indexPath % 2 === 0
if (isKey) {
prev[`${curr}`] = ''
} else {
const keys = Object.keys(prev)
prev[`${keys[keys.length - 1]}`] = `${curr}`
}
return prev
}, {} as {[key: string]:string})

let normalPath: string[] = []

for await (const pathKey of Object.keys(pathObject)) {
normalPath.push(`/${pathKey}`)

const pathValue = pathObject[pathKey]
try {
const pathIndex = await this.getIndex(normalPath.join(""), pathValue, propertyName)
normalPath.push(`[${pathIndex}]`)
} catch (error) {
throw new DataError(`DataPath: ${normalPath.join("")}/${pathValue} not found.`, 13, error)
}
}

return normalPath.join("")
}
}
Loading

0 comments on commit 2019608

Please sign in to comment.