TSDB is a fast and reliable database port of the original sdb database written in PHP. It is my first TypeScript package, and I welcome any comments or feedback on my website or social media.
To install TSDB, you can use npm:
npm install ts-sdb
Here is an example of how to use TSDB in your TypeScript project:
import { TSDB } from 'tsdb';
// Create a new instance of TSDB
const db = new TSDB();
// Load the database
db.load('mydatabase');
// Perform operations on the database
db.create('key1', 'value1');
db.create('key2', 'value2');
db.update('key1', { value: 'newvalue' });
db.delete('key2');
// Save changes to the database
db.save();
The Debug class provides logging and error handling functionalities.
constructor(mode: boolean = false)
mode
(optional): A boolean value indicating the debug mode. Default isfalse
.
Logs a message to the console if debug mode is enabled.
log(message: string, flag: string = 'DEBUG'): boolean | void
message
: The message to be logged.flag
(optional): The log flag. Default is'DEBUG'
.
Logs an error message to the console.
error(message: string, code: string = 'unknown', flag: string = 'ERROR')
message
: The error message to be logged.code
(optional): The error code. Default is'unknown'
.flag
(optional): The error flag. Default is'ERROR'
.
The Data interface represents a database record.
interface Data {
id: any;
key: string;
value: any;
}
id
: The unique identifier of the record.key
: The key of the record.value
: The value associated with the key.
The TSDB class represents the TSDB database.
constructor()
Creates a new instance of the TSDB class.
Loads a database file.
load(file: string, directory: string = ''): boolean
file
: The name of the database file.directory
(optional): The directory where the database file is located. Default is an empty string.
Returns true
if the database is successfully loaded, false
otherwise.
Saves the changes made to the database.
save(): boolean
Returns true
if the changes are successfully saved, false
otherwise.
Creates a new record in the database.
create(key: string, value: any): number
key
: The key of the record.value
: The value associated with the key.
Returns the ID of the created record.
Reads a record from the database by its ID.
readById(id: number): Data | undefined
id
: The ID of the record.
Returns the record if found, undefined
otherwise.
Reads a record from the database by its key.
read(key: string): Data | undefined
key
: The key of
the record.
Returns the record if found, undefined
otherwise.
Updates a record in the database by its ID.
updateById(id: number, data: Partial<Data>): void
id
: The ID of the record.data
: The partial data to update.
Updates a record in the database by its key.
update(key: string, data: Partial<Data>): void
key
: The key of the record.data
: The partial data to update.
Deletes a record from the database by its key.
delete(key: string): void
key
: The key of the record.
Checks if a record with the given key exists in the database.
has(key: string): boolean
key
: The key to check.
Returns true
if the record exists, false
otherwise.
Returns a JSON string representation of the database.
map(): string | number
Returns the JSON string representation of the database if it exists, -1
otherwise.
Returns the database as an array of Data objects.
mapObject(): Data[] | number
Returns the database as an array of Data objects if it exists, -1
otherwise.
- devsimsek - Developer and maintainer
This project is licensed under the MIT License. See the LICENSE file for details.