Database similar to quick.db but using json files and tables
npm install katdbfix
npm i katdb
`
Setup (Using CommonJS):
`js
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 |
$3
Starts the database
- Usage: start ()
- Example:
`js
db.start() // Returns: void
`
$3
Starts the database
- Usage: backup ()
- Example:
`js
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
*/
`
$3
Sets a value from the provided key on the table (Default table: main)
- Usage: set (key: string, value: any, table: string)
- Example:
`js
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 } }
`
$3
Gets a value from the provided key on the table (Default table: main)
- Usage: get (key: string, table: string)
- Example:
`js
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)
`
$3
Deletes a value from the provided key on the table (Default table: main)
- Usage: delete (key: string, table: string)
- Example:
`js
//Before: { "kingsbecats": { "owner": true, "dev": true } }
db.delete('kingsbecats.dev','users')
//After: { "kingsbecats": { "owner": true } }
`
$3
Pushs a value from the provided key on the table (Default table: main)
- Usage: push (key: string, value: any, table: string)
- Example:
`js
db.push('kingsbecats.packages','katdb','users') // Returns: [ 'katdb' ] (Promise)
db.push('kingsbecats.packages','hybridcommands','users') // Returns: [ 'katdb', 'hybridcommands' ] (Promise)
`
$3
Removes a value from the provided key on the table (Default table: main)
- Usage: remove (key: string, value: any, table: string)
- Example:
`js
db.remove('kingsbecats.packages','katdb','users') // Returns: [ 'hybridcommands' ] (Promise)
db.remove('kingsbecats.packages','hybridcommands','users') // Returns: [ ] (Promise)
`
$3
Removes and returns the first value from the provided key on the table (Default table: main)
- Usage: shift (key: string, table: string)
- Example:
`js
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)
`
$3
Removes and returns the last value from the provided key on the table (Default table: main)
- Usage: pop (key: string, table: string)
- Example:
`js
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)
`
$3
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:
`js
db.get('kingsbecats.packages','users') // Returns: [ 'hybridcommands' ] (Promise)
db.unshift('kingsbecats.packages','katdb','users') // Returns: [ 'katdb', 'hybridcommands' ] (Promise)
`
$3
Adds a value from the provided key and table (Default table: main)
- Usage: add (key: string, value: number, table: string)
- Example:
`js
db.add('kingsbecats.money',5,'users') // Returns: 5 (Promise)
`
$3
Substracts a value from the provided key and table (Default table: main)
- Usage: sub (key: string, value: number, table: string)
- Example:
`js
db.sub('kingsbecats.money',1,'users') // Returns: 4 (Promise)
`
$3
Multiply a value from the provided key and table (Default table: main)
- Usage: multi (key: string, value: number, table: string)
- Example:
`js
db.multi('kingsbecats.money',2,'users') // Returns: 8 (Promise)
`
$3
Divide a value from the provided key and table (Default table: main)
- Usage: divide (key: string, value: number, table: string)
- Example:
`js
db.divide('kingsbecats.money',2,'users') // Returns: 4 (Promise)
`
$3
Verify if the key exists in provided table (Default table: main)
- Usage: has (key: string, table: string)
- Example:
`js
db.has('kingsbecats','users') // Returns: true (Promise)
db.has('kingsbecats.owner','users') // Returns: false (Promise)
db.has('kingsbecats.dev','users') // Returns: true (Promise)
`
$3
Gets the latency of the database
- Usage: has ()
- Example:
`js
db.ping() // Returns: 3 (Promise)
`
$3
Gets all data in the provided table
- Usage: getTable (name: string)
- Example:
`js
db.getTable('users') // Returns: { 'kingsbecats' : { owner : true, money : 4, packages : [ ] } } (Promise)
`
$3
Check if has a file linked with the table
- Usage: existsTable (name: string)
- Example:
`js
db.existsTable('users') // Returns: true (Promise)
db.existsTable('channels') // Returns: true (Promise)
`
$3
Clear all data on the provided table
- Usage: clearTable (name: string)
- Example:
`js
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"
*/
`
$3
Creates a backup of the tables
- Usage: backup ()
- Example:
`js
db.backup()
db.backups.pop() //Returns the last backup
`
$3
Checks if the backup exists
- Usage: existsBackup (name: string)
- Example:
`js
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"
`
$3
Gets the files and data on a backup
- Usage: getBackup (name: string)
- Example:
`js
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 } } }
`
$3
Restore the database from the provided backup id
- Usage: restoreFromBackup (name: string)
- Example:
`js
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
`
$3
Get a array of strings, this are all avaliables backups
- Usage: getBackups ()
- Example:
`js
db.getBackups() //Returns: [ '1690311476859' ]
``