Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Belphemur committed Apr 9, 2023
2 parents 9569699 + db7b5cb commit e67764d
Show file tree
Hide file tree
Showing 7 changed files with 823 additions and 635 deletions.
4 changes: 4 additions & 0 deletions .github/FUNDING.YML
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# These are supported funding model platforms

github: Belphemur
custom: 'https://soundswitch.aaflalo.me/#donate'
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ try {
console.error(error);
};

//easier than try catch when the path doesn't lead to data
//this will return `myDefaultValue` if `/super/path` don't have data, else it return the data
var data = await db.getObjectDefault<string>("/super/path", "myDefaultValue");

// Deleting data
await db.delete("/test1");

Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,11 @@
"jest": "^28.1.3",
"last-release-git": "^0.0.3",
"safe-regex": "~2.1.1",
"semantic-release": "^20.0.4",
"semantic-release": "^21.0.0",
"travis-deploy-once": "^5.0.11",
"ts-jest": "^28.0.7",
"typedoc": "^0.23.1",
"typescript": "^4.4.3",
"typescript": "^5.0.2",
"validate-commit-msg": "^2.14.0"
},
"husky": {
Expand Down
21 changes: 20 additions & 1 deletion src/JsonDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,25 @@ export class JsonDB {
return this.getData(dataPath)
}

/**
* 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
*/
public async getObjectDefault<T>(dataPath: string, defaultValue?: T): Promise<T> {
try {
return await this.getData(dataPath);
} catch (e) {
if (!(e instanceof DataError)) {
throw e;
}
if (e.id != 5) {
throw e;
}
return defaultValue as T;
}
}

/**
* Check for existing datapath
* @param dataPath
Expand Down Expand Up @@ -291,7 +310,7 @@ export class JsonDB {
* @param override overriding or not the data, if not, it will merge them
*/
public async push(dataPath: string, data: any, override: boolean = true): Promise<void> {
return writeLockAsync( async () => {
return writeLockAsync(async () => {
const dbData = await this.getParentData(dataPath, true)
// if (!dbData) {
// throw new Error('Data not found')
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ArrayInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class ArrayInfo {

private getArrayDataAndIndexFromProperty(data: KeyValue): any {
let indexToPull = 0
let tempData = data[this.property] ?? data
let tempData = data instanceof Array ? data : data[this.property] ?? data
if (this.indicies.length > 0) {
indexToPull = +this.indicies[this.indicies.length - 1]
for (let i = 0; i < this.indicies.length - 1; i++) {
Expand Down
33 changes: 33 additions & 0 deletions test/02-jsondb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import {DatabaseError, DataError} from "../src/lib/Errors"
import {JsonDB} from "../src/JsonDB"
import * as fs from 'fs'
import { Config } from "../src/lib/JsonDBConfig"
import {writeLockAsync} from "../src/lock/Lock";
import {TimeoutError} from "../src/lock/Error";

const testFile1 = "test/test_file1"
const testFile2 = "test/dirCreation/test_file2"
Expand Down Expand Up @@ -95,6 +97,23 @@ describe('JsonDB', () => {
const result = await db.getObject<Test>("@/hello");
expect(result).toBe(object)
})

test('should store the data with typing and default', async () => {
const object = {Hello: "test", World: 0} as Test;
await db.push("@lol@test", object)
const result = await db.getObjectDefault<Test>("@lol@test");
expect(result).toBe(object)
})

test('should get default value when not finding path', async () => {
const result = await db.getObjectDefault<string>("@lol@test@nah", "defaultValue");
expect(result).toBe("defaultValue")
})

test('should get exception when not the right data type', async () => {
await expect(async () => await db.getObjectDefault<string>("@lol@test[0]", "defaultValue")).rejects.toThrowError(DataError)
})

test('should have data at root', async () => {
expect(await db.exists('@test@test')).toBeTruthy()
})
Expand Down Expand Up @@ -451,6 +470,20 @@ describe('JsonDB', () => {
}
)

//issue #571
test(
'should have no issue with numerical property for getting data in an array',
async () => {
await db.push('/issue571/1[]', "Hello", true)
await db.push('/issue571/1[]', "world", true)
let data = await db.getObject("/issue571/1[0]");
expect(data).toBe("Hello");
data = await db.getObject("/issue571/1[-1]");
expect(data).toBe("world");

}
)

test(
'should throw an error when trying to append to a non array',
async () => {
Expand Down
Loading

0 comments on commit e67764d

Please sign in to comment.