Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
good.db
Advanced tools
Readme
Sure, let's include examples for all methods in the package, organized alphabetically:
GoodDB is a lightweight and flexible TypeScript database wrapper that simplifies interactions with your database. It offers a simple API, supports various drivers, and is designed with modern TypeScript development in mind.
You can install GoodDB via npm:
npm install gooddb
You can customize the database configuration by passing options to the driver:
const db = new GoodDB(new JSONDriver({ path: './database.json' }), {
table: 'data',
nested: '.',
nestedIsEnabled: true,
});
table
(string): The name of the table/collection in the database.
nested
(string): The character used to separate nested keys.
nestedIsEnabled
(boolean): Whether to enable nested key handling.
GoodDB supports the following drivers:
The CacheDriver
stores data in memory. It is suitable for small applications and is easy to set up.
import { GoodDB, CacheDriver } from 'gooddb';
const db = new GoodDB(new CacheDriver());
The SQLiteDriver
stores data in an SQLite database. It is suitable for small to medium-sized applications and is easy to set up.
import { GoodDB, SQLiteDriver } from 'gooddb';
const db = new GoodDB(new SQLiteDriver({ path: './database.sqlite' }));
The YMLDriver
stores data in a YML file. It is suitable for small to medium-sized applications and is easy to set up.
import { GoodDB, YMLDriver } from 'gooddb';
const db = new GoodDB(new YMLDriver({ path: './database.yml' }));
The JSONDriver
stores data in a JSON file. It is suitable for small to medium-sized applications and is easy to set up.
import { GoodDB, JSONDriver } from 'gooddb';
const db = new GoodDB(new JSONDriver({ path: './database.json' }));
The MongoDBDriver
stores data in a MongoDB database. It is suitable for medium to large-sized applications and offers advanced features.
import { GoodDB, MongoDBDriver } from 'gooddb';
const db = new GoodDB(new MongoDBDriver({ uri: 'mongodb://localhost:27017/mydb' }));
await db.connect();
set(key: string, value: any, options?: methodOptions)
Set a value in the database:
// Sync
db.set('user', { name: 'Alice', age: 25 }); // true
get(key: string, options?: methodOptions)
Get a value from the database:
db.get('user'); // { name: 'Alice', age: 25 }
has(key: string, options?: methodOptions)
Check if a key exists in the database:
db.has('user'); // true
delete(key: string, options?: methodOptions)
Delete a key from the database:
db.delete('user'); // true
push(key: string, value: any, options?: methodOptions)
Push a value to an array in the database:
db.push('users', { name: 'Alice', age: 25 }); // true
db.push('users', { name: 'Bob', age: 30 }); // true
db.push('users', { name: 'Charlie', age: 35 }); // true
db.get('users'); // [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]
shift(key: string, options?: methodOptions)
Remove the first element from an array in the database:
db.shift('users'); // { name: 'Alice', age: 25 }
db.get('users'); // [{ name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]
unshift(key: string, value: any, options?: methodOptions)
Add an element to the beginning of an array in the database:
db.unshift('users', { name: 'Alice', age: 25 }); // true
db.get('users'); // [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]
pop(key: string, options?: methodOptions)
Remove the last element from an array in the database:
db.pop('users'); // { name: 'Charlie', age: 35 }
db.get('users'); // [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }]
pull(key: string, value: any, options?: methodOptions)
Remove a value from an array in the database:
db.pull('users', { name: 'Bob', age: 30 }); // true
db.get('users'); // [{ name: 'Alice', age: 25 }]
db.pull('users', (element) => element.age > 20); // true
db.get('users'); // []
add(key: string, operand: number, options?: methodOptions)
Add a number to a value in the database:
db.set('score', 10);
db.add('score', 5); // 15
subtract(key: string, operand: number, options?: methodOptions)
Subtract a number from a value in the database:
db.set('score', 10);
db.subtract('score', 5); // 5
multiply(key: string, operand: number, options?: methodOptions)
Multiply a value in the database by a number:
db.set('score', 10);
db.multiply('score', 5); // 50
divide(key: string, operand: number, options?: methodOptions)
Divide a value in the database by a number:
db.set('score', 10);
db.divide('score', 5); // 2
math(key: string, operator: string, operand: number, options?: methodOptions)
Perform a mathematical operation on a value in the database:
db.set('score', 10);
db.math('score', '+', 5); // 15
db.math('score', '-', 5); // 10
db.math('score', '*', 5); // 50
db.math('score', '/', 5); // 10
type(key: string, options?: methodOptions)
Get the type of a value in the database:
db.set('user', { name: 'Alice', age: 25 });
db.type('user'); // 'object'
size(key: string, options?: methodOptions)
Get the size of a value in the database:
db.set('users', [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]);
db.size('users'); // 3
startsWith(key: string, options?: methodsOptions)
Get all the keys that start with a given string:
db.set('user1', { name: 'Alice', age: 25 });
db.set('user2', { name: 'Bob', age: 30 });
db.set('user3', { name: 'Charlie', age: 35 });
db.startsWith('user'); // { user1: { name: 'Alice', age: 25 }, user2: { name: 'Bob', age: 30 }, user3: { name: 'Charlie', age: 35 } }
endsWith(key: string, options?: methodsOptions)
Get all the keys that end with a given string:
db.set('user1', { name: 'Alice', age: 25 });
db.set('user2', { name: 'Bob', age: 30 });
db.set('user3', { name: 'Charlie', age: 35 });
db.endsWith('1'); // { user1: { name: 'Alice', age: 25 } }
includes(key: string, options?: methodsOptions)
Get all the keys that include a given string:
db.set('user1', { name: 'Alice', age: 25 });
db.set('user2', { name: 'Bob', age: 30 });
db.set('user3', { name: 'Charlie', age: 35 });
db.includes('user'); // { user1: { name: 'Alice', age: 25 }, user2: { name: 'Bob', age: 30 }, user3: { name: 'Charlie', age: 35 } }
keys()
Get all the keys in the database:
db.set('user1', { name: 'Alice', age: 25 });
db.set('user2', { name: 'Bob', age: 30 });
db.set('user3', { name: 'Charlie', age: 35 });
db.keys(); // ['user1', 'user2', 'user3']
values()
Get all the values in the database:
db.set('user1', { name: 'Alice', age: 25 });
db.set('user2', { name: 'Bob', age: 30 });
db.set('user3', { name: 'Charlie', age: 35 });
db.values(); // [{ name: 'Alice', age: 25 }, { name: 'Bob', age: 30 }, { name: 'Charlie', age: 35 }]
all()
Get all the entries in the database:
db.set('user1', { name: 'Alice', age: 25 });
db.set('user2', { name: 'Bob', age: 30 });
db.set('user3', { name: 'Charlie', age: 35 });
db.all(); // { user1: { name: 'Alice', age: 25 }, user2: { name: 'Bob', age: 30 }, user3: { name: 'Charlie', age: 35 } }
clear()
Clear the database:
db.clear(); // true
table(name: string)
Make table operations on the database:
db.table('users').set('user1', { name: 'Alice', age: 25 }); // true
db.table('users').get('user1'); // { name: 'Alice', age: 25 }
connect()
Connect to the database (for MongoDBDriver
):
await db.connect();
disconnect()
Disconnect from the database (for MongoDBDriver
):
await db.disconnect();
Contributions are welcome! For major changes, please open an issue first to discuss what you would like to change.
If you have any questions or need assistance, please feel free to open an issue or contact us at
GoodDB is licensed under the MIT License. See the LICENSE file for details.
FAQs
A simple, fast, and powerful database package for Node.js
The npm package good.db receives a total of 124 weekly downloads. As such, good.db popularity was classified as not popular.
We found that good.db demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.