katdb
v1.2.0
Published
Database similar to quick.db but using json files and tables
Downloads
30
Readme
katdb
Database similar to quick.db but using json files and tables
Contact me
Update: 1.2.0 Added backups 7/25/2023
Install
npm i katdb
Setup (Using CommonJS):
const { KatDataBase } = require('katdb')
const db = new KatDataBase({ //Create the a new database instance
path: './Database',
tables: ['main'],
backupConfig: {
pulse: 60*60000, //Interval to backup
logProcess: true, //Logs when a table was backuped
exitOnError?: true //If is "true" the error kills the process of node
}
})
db.on('start', () => {
console.log('Database is ready!')
})
db.start() // Starts the class
Methods and Events
| Method | Has Event? | Event Usage | |:------:|:----------:|:-----------:| | Start | ✓ | db.on('start', () => {...}) | | Close | ✓ | db.on('close', () => {...}) | | Set | ✓ | db.on('set', (key: string, value: any, table: string) => {...}) | | Get | ✓ | db.on('get', (key: string, table: string, output: any) => {...}) | | Delete | ✓ | db.on('delete', (key: string, table: string, output: any) => {...}) | | Push | ✓ | db.on('push', (key: string, value: any | any[], table: string, output: any[]) => {...}) | | Remove | ✓ | db.on('remove', (key: string, value: any | any[], table: string, output: any[]) => {...}) | | Shift | ✓ | db.on('shift', (key: string, table: string, output: any) => {...}) | | unShift | ✓ | db.on('unshift', (key: string, value: any | any[], table: string, output: any[]) => {...}) | | Pop | ✓ | db.on('pop', (key: string, table: string, output: any) => {...}) | | Add | ✓ | db.on('add', (key: string, value: number, table: string, output: number) => {...}) | | Sub | ✓ | db.on('sub', (key: string, value: number, table: string, output: number) => {...}) | | Multi | ✓ | db.on('multi', (key: string, value: number, table: string, output: number) => {...}) | | Divide | ✓ | db.on('divide', (key: string, value: number, table: string, output: number) => {...}) | | Backup | ✓ | db.on('backup', (path: string) => {...}) | | Has | ✗ | null | | Ping | ✗ | null | | existsTable | ✗ | null | | clearTable | ✗ | null | | Backup | ✓ | db.on('backup', (path: string) => {...}) | | restoreFromBackup | ✓ | db.on('restore', (restored: Array) => {...}) | | existsBackup | ✗ | null | | getBackups | ✗ | null | | getBackup | ✗ | null |
Start
Starts the database
- Usage:
start ()
- Example:
db.start() // Returns: void
Backup
Starts the database
- Usage:
backup ()
- Example:
db.backup() // Returns: boolean
/**
* Returns a boolean depending on the process, if it was successful it returns true, if an error ocurred it returns false and logs the error in the console
*/
Set
Sets a value from the provided key on the table (Default table: main
)
- Usage:
set (key: string, value: any, table: string)
- Example:
db.set('kingsbecats',{owner:true},'users') // Returns: {owner:true} (Promise)
db.set('kingsbecats.dev',true,'users') // Returns: true (Promise)
//Table: { "kingsbecats": { "owner": true, "dev": true } }
Get
Gets a value from the provided key on the table (Default table: main
)
- Usage:
get (key: string, table: string)
- Example:
db.get('kingsbecats','users') // Returns: { "owner": true, "dev": true } (Promise)
db.get('kingsbecats.owner','users') // Returns: true (Promise)
db.get('kingsbecats.dev','users') // Returns: true (Promise)
Delete
Deletes a value from the provided key on the table (Default table: main
)
- Usage:
delete (key: string, table: string)
- Example:
//Before: { "kingsbecats": { "owner": true, "dev": true } }
db.delete('kingsbecats.dev','users')
//After: { "kingsbecats": { "owner": true } }
Push
Pushs a value from the provided key on the table (Default table: main
)
- Usage:
push (key: string, value: any, table: string)
- Example:
db.push('kingsbecats.packages','katdb','users') // Returns: [ 'katdb' ] (Promise)
db.push('kingsbecats.packages','hybridcommands','users') // Returns: [ 'katdb', 'hybridcommands' ] (Promise)
Remove
Removes a value from the provided key on the table (Default table: main
)
- Usage:
remove (key: string, value: any, table: string)
- Example:
db.remove('kingsbecats.packages','katdb','users') // Returns: [ 'hybridcommands' ] (Promise)
db.remove('kingsbecats.packages','hybridcommands','users') // Returns: [ ] (Promise)
Shift
Removes and returns the first value from the provided key on the table (Default table: main
)
- Usage:
shift (key: string, table: string)
- Example:
db.get('kingsbecats.packages','users') // Returns: [ 'katdb', 'hybridcommands' ] (Promise)
db.shift('kingsbecats.packages','users') // Returns: 'katdb' (Promise)
db.get('kingsbecats.packages','users') // Returns: [ 'hybridcommands' ] (Promise)
Pop
Removes and returns the last value from the provided key on the table (Default table: main
)
- Usage:
pop (key: string, table: string)
- Example:
db.get('kingsbecats.packages','users') // Returns: [ 'katdb', 'hybridcommands' ] (Promise)
db.pop('kingsbecats.packages','users') // Returns: 'hybridcommands' (Promise)
db.get('kingsbecats.packages','users') // Returns: [ 'katdb' ] (Promise)
unShift
Adds at the begging the provided values from the provided key on the table (Default table: main
)
- Usage:
unshift (key: string, value: any, table: string)
- Example:
db.get('kingsbecats.packages','users') // Returns: [ 'hybridcommands' ] (Promise)
db.unshift('kingsbecats.packages','katdb','users') // Returns: [ 'katdb', 'hybridcommands' ] (Promise)
Add
Adds a value from the provided key and table (Default table: main
)
- Usage:
add (key: string, value: number, table: string)
- Example:
db.add('kingsbecats.money',5,'users') // Returns: 5 (Promise)
Sub
Substracts a value from the provided key and table (Default table: main
)
- Usage:
sub (key: string, value: number, table: string)
- Example:
db.sub('kingsbecats.money',1,'users') // Returns: 4 (Promise)
Multi
Multiply a value from the provided key and table (Default table: main
)
- Usage:
multi (key: string, value: number, table: string)
- Example:
db.multi('kingsbecats.money',2,'users') // Returns: 8 (Promise)
Divide
Divide a value from the provided key and table (Default table: main
)
- Usage:
divide (key: string, value: number, table: string)
- Example:
db.divide('kingsbecats.money',2,'users') // Returns: 4 (Promise)
Has
Verify if the key exists in provided table (Default table: main
)
- Usage:
has (key: string, table: string)
- Example:
db.has('kingsbecats','users') // Returns: true (Promise)
db.has('kingsbecats.owner','users') // Returns: false (Promise)
db.has('kingsbecats.dev','users') // Returns: true (Promise)
Ping
Gets the latency of the database
- Usage:
has ()
- Example:
db.ping() // Returns: 3 (Promise)
GetTable
Gets all data in the provided table
- Usage:
getTable (name: string)
- Example:
db.getTable('users') // Returns: { 'kingsbecats' : { owner : true, money : 4, packages : [ ] } } (Promise)
ExistsTable
Check if has a file linked with the table
- Usage:
existsTable (name: string)
- Example:
db.existsTable('users') // Returns: true (Promise)
db.existsTable('channels') // Returns: true (Promise)
ClearTable
Clear all data on the provided table
- Usage:
clearTable (name: string)
- Example:
db.clearTable('users') // Returns: true (Promise)
db.clearTable('channels') // SyntaxError
/**
* db.clearTable('users'): Returns true because, all data was
* db.clearTable('channels'): Returns a syntaxerror because, doesn't exists a table called "channels"
*/
Backup
Creates a backup of the tables
- Usage:
backup ()
- Example:
db.backup()
db.backups.pop() //Returns the last backup
existsBackup
Checks if the backup exists
- Usage:
existsBackup (name: string)
- Example:
db.existsBackup('hfdjsghfk') //Returns false, because no have a backup with name "hfdjsghfk"
db.existsBackup('1690311476859') //Returns true, because exists a backup with name "1690311476859"
getBackup
Gets the files and data on a backup
- Usage:
getBackup (name: string)
- Example:
db.getBackup('hfdjsghfk') //Returns SyntaxError, becuase you can't get data from a inexistent backup
db.getBackup('1690311476859') //Returns object, Example: { users: { "kingsbecats": { "developer": true } } }
restoreFromBackup
Restore the database from the provided backup id
- Usage:
restoreFromBackup (name: string)
- Example:
db.restoreFromBackup('hfdjsghfk') //Returns false and SyntaxError, becuase you can't restore data from a inexistent backup
db.restoreFromBackup('1690311476859') //Returns true, check your database, has been restored
getBackups
Get a array of strings, this are all avaliables backups
- Usage:
getBackups ()
- Example:
db.getBackups() //Returns: [ '1690311476859' ]