Skip to content

Commit

Permalink
Small fixes before publish.
Browse files Browse the repository at this point in the history
  • Loading branch information
MegalithOfficial committed Jun 17, 2024
1 parent 4a3fd2b commit 746d097
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 17 deletions.
18 changes: 12 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ const jsonDriver = new JSONDriver<format>('users.json');
// Create a new Megdb instance with the JSONDriver instance
const megDB = new Megdb<format>(jsonDriver);

// Use the MegDB instance
async function main() {
// Set a value
await megDB.set('john', { name: 'John Doe', age: 30, hobbies: ['reading', 'coding'] });

// Get a value
const john = await megDB.get('john.name');
console.log(john.name); // Outputs: John Doe
const john = await megDB.get('john');
console.log(john!.name); // Outputs: John Doe

// Update a value
await megDB.add('john.age', 1);
Expand All @@ -68,7 +67,7 @@ async function main() {
console.log(updatedHobbies); // Outputs: ['reading', 'coding', 'gaming']

// Pull a value from an array
await megDB.filter('john', hobby => hobby !== 'coding');
await megDB.filter('john.hobbies', hobby => hobby !== 'coding');

// Get the updated array
const finalHobbies = await megDB.get('john.hobbies');
Expand All @@ -81,10 +80,18 @@ async function main() {
const deletedValue = await megDB.get('john.age');
console.log(deletedValue); // Outputs: undefined

// Get the type of the 'john.hobbies' property
const typeofhobbies = await megDB.typeof('john.hobbies');
console.log(typeofhobbies); // Outputs: object

// Get the type of the 'john.name' property
const typeofname = await megDB.typeof('john.name');
console.log(typeofname); // Outputs: string

// Get all data
const allData = await megDB.all();
console.log(allData); // Outputs: { john: { name: 'John Doe', hobbies: ['reading', 'gaming'] } }
}
};

main();
```
Expand Down Expand Up @@ -138,7 +145,6 @@ const jsonDriver = new JSONDriver<format>('users.json');
// Create a new Megdb instance with the JSONDriver instance
const megDB = new Megdb<format>(jsonDriver);

// Utilize the MegDB instance
async function main() {
// Operations with MegDB...
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "meg.db-light",
"version": "1.1.0",
"version": "1.1.1",
"description": "MegDB-light, A lightweight TypeScript database module providing a type-safe API for efficient data storage and retrieval. Easily extendable with custom drivers, including a built-in JSONDriver.",
"main": "lib/main.js",
"scripts": {
Expand Down
15 changes: 8 additions & 7 deletions src/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Megdb<T> {
await this.driver.saveData({} as any);
};

public async typeof<K extends keyof T>(key: K): Promise<string> {
public async typeof<K extends Path<T>>(key: K): Promise<string> {
const userObject = await this.get(key);

if (userObject !== null && userObject !== undefined) {
Expand Down Expand Up @@ -76,11 +76,12 @@ export class Megdb<T> {
public async pull<K extends Path<T>, U>(path: K, value: PathValue<T, K> extends Array<infer I> ? I : never): Promise<void> {
const data = await this.driver.loadData();
const currentArray = _.get(data, path, []);
if (Array.isArray(currentArray)) {
_.pull(currentArray, value);
_.set(data as object, path, currentArray);
await this.driver.saveData(data);
};
if (!Array.isArray(currentArray)) {
throw new Error(`Cannot perform 'pull' operation on non-array property '${path as string}'.`);
}
_.pull(currentArray, value);
_.set(data as object, path, currentArray);
await this.driver.saveData(data);
};

public async filter<K extends Path<T>>(path: K, predicate: (value: PathValue<T, K> extends Array<infer I> ? I : never) => boolean): Promise<void> {
Expand All @@ -96,4 +97,4 @@ export class Megdb<T> {
public async all(): Promise<T> {
return await this.driver.loadData();
};
}
};
2 changes: 1 addition & 1 deletion src/drivers/JSONDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ export class JSONDriver<T> extends BaseDriver<T> {
});
});
}
}
}
11 changes: 9 additions & 2 deletions tests/generalTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ async function main() {
const deletedValue = await megDB.get('john.age');
console.log(deletedValue); // Outputs: undefined

// Get the type of the 'john.hobbies' property
const typeofhobbies = await megDB.typeof('john.hobbies');
console.log(typeofhobbies); // Outputs: object

// Get the type of the 'john.name' property
const typeofname = await megDB.typeof('john.name');
console.log(typeofname); // Outputs: string

// Get all data
const allData = await megDB.all();
console.log(allData); // Outputs: { john: { name: 'John Doe', hobbies: ['reading', 'gaming'] } }
}


main();
main();
1 change: 1 addition & 0 deletions users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"john":{"name":"John Doe","hobbies":["reading","gaming"]}}

0 comments on commit 746d097

Please sign in to comment.