Discortics.db
Support: discord.gg/buHBCtE | NPM: npmjs.com/discortics.db
Discortics.db is a mongodb wrapper of Quick.db which is used to provide database support to Discortics, a Discord Bot, now public. This package is meant to provide an easy way for beginners and people of all levels to access & store data in mongodb environment. All data is stored persistently, you can either use mongodb cloud database or the local mongodb database.
- Persistent Storage - Data doesn't disappear on restarts.
- Compactible with Both Local/Cloud Database - You can pass the mongodb localhost database link/cloud database link.
- Easy to use - This package is Beginner Friendly, provides easier approach to mongodb in storing the data.
- More Features - Regularly maintained & includes options to even delete/format your table.
Installation
The recommended way to get started using the Node.js is by using the npm
(Node Package Manager) to install the dependency in your project.
npm install discortics.db --save
This will download the Discortics.db module & add a dependency entry in your package.json
file.
Example
const db = require('discortics.db');
async function run() {
// Initialise && Connect to your database.
await db.connect("YOUR_MONGDB_URL_WITH_DATABASE_NAME"); // You only need to initialise once. Add this code line in your main file (onReady Function if you're using in a discord bot)
//Selecting your Table. Creates if it doesn't exist yet.
var table = await new db.table('rpg'); //Required
//The rest is as same as in Quick.db, but returns a promise.
// Setting an object in the database:
await table.set('userInfo', { difficulty: 'Easy' })
// -> { difficulty: 'Easy' }
// Pushing an element to an array (that doesn't exist yet) in an object:
await table.push('userInfo.items', 'Sword')
// -> { difficulty: 'Easy', items: ['Sword'] }
// Adding to a number (that doesn't exist yet) in an object:
await table.add('userInfo.balance', 500)
// -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }
// Repeating previous examples:
await table.push('userInfo.items', 'Watch')
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }
await table.add('userInfo.balance', 500)
// -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }
// Fetching individual properties
await table.get('userInfo.balance') // -> 1000
await table.get('userInfo.items') // ['Sword', 'Watch']
}
run()
Documentation
All functions are asynchronous. Therefore it returns a promise. Learn about async/await HERE
await .connect(url)
This is a required step to initialise your database. You only need to run this once, so add it in your main file.
var db = require('discortics.db');
await db.connect(url) //You can use either the cloud database URL or the localhost URL
console.log("Connected to Database");
await new .table(name)
This fetches the name with the passed table name. If not found, the table is created. The name is case-sensitive. This is required before running any of the below functions
var economy = await new db.table('economy')
await economy.set('myBalance', 500) // -> true
var bal = await economy.get('myBalance')
console.log(bal) // -> 500
bal = await db.get('myBalance')
console.log(bal) // -> null
await .add(key, number)
This function adds a number to a key in the database. (If no existing number, it will add to 0)
await economy.add('myBalance',250)
bal = await economy.get('myBalance')
console.log(bal) // -> 250
Also allows for accessing properties using dot notation
bal = await economy.get('myUser')
console.log(bal) // -> { guild: null, balance: 500 }
await economy.add('myUser.balance', 250)
bal = await economy.get('myUser')
console.log(bal) // -> { guild: null, balance: 750 }
await .subtract(key, number)
This function subtracts a number to a key in the database. (If no existing number, it will add to 0)
await economy.subtract('myBalance',250)
bal = await economy.get('myBalance')
console.log(bal) // -> 250
Also allows for accessing properties using dot notation
bal = await economy.get('myUser')
console.log(bal) // -> { guild: null, balance: 750 }
await economy.subtract('myUser.balance', 250)
bal = await economy.get('myUser')
console.log(bal) // -> { guild: null, balance: 500 }
await .get(key)
This function results the data which relates to the passed key from the database.
await economy.set('myData', 'Hello World!')
var fetch = await economy.get('myData')
console.log(fetch)// -> 'Hello World!'
Also allows for accessing properties using dot notation
await economy.set('myUser', { guild: 'Discortics HQ', balance: 500 })
var fetch = await economy.get('myUser')
console.log(fetch) // -> { guild: 'Discortics HQ', balance: 500 }
await economy.get('myUser.guild') // -> "Discortics HQ"
await economy.get('myUser.balance') // -> 500
await economy.get('myUser.notAProp') // -> undefined
await .set(key,value)
This function sets new data based on a key in the database. (When using dot notation, if the object doesn't exist it'll create one)
await economy.set('myData.text', 'Hello World!') // -> 'Hello World!'
await economy.set('myData', { foo: 'bar' }) // -> { foo: 'bar' }
await .has(key)
This function returns a boolean based on whether an element or property exists.
await economy.set('myData', 'Hello World!')
// -> 'Hello World!'
await economy.has('myData') // -> true
await economy.has('myData.items') // -> false
await .delete(key)
This function deletes the specified key. Returns true/null if it was a success or not.
var fetch = await economy.get('myData')
console.log(fetch) // -> "Hello World!"
await economy.delete('myData')
// true
await .all()
This function returns the entire active table as an object
var fetch = await economy.all()
console.log(fetch) // -> {myData: "Helow World!", myUser: [Object]}
await .push(key, element)
This function will push into an array in the database based on the key. (If no existing array, it will create one)
await economy.set('myItems.weapons', ['Sword', 'Lock'])
// -> ['Sword', 'Lock']
await economy.push('myItems.weapons', 'Dagger')
// -> ['Sword', 'Lock', 'Dagger']
await .drop()
This function deletes the table.
await economy.drop()
// -> true
Environment Migration
This part of the guide helps you to migrate from different database to Discortics.db
Quick.db
This example code helps you transfer data from your quick.db database to discorticd.db mongodb database. But if you dont know/like to work with promises (an extra await
will be required in everycase here), its better to use quick.db. But if you want to have an easier online database management, go for transferring data.
const db = require('discortics.db')
var qdb = require('quick.db')
var lisy = [] // <- Enter your quick.db table names in it separated by comma. Example: ['table1', 'table2', 'table3']
async function run() {
await db.connect("YOUR_MONGODB_URL_WITH_DATABASE_NAME");
lisy.forEach(async (i) => {
var table1 = await new db.table(i);
var table2 = new qdb.table(i);
var alldata = table2.all();
alldata.forEach(async (j) => {
await table1.set(j.ID,j.data)
})
console.log("Finished table: "+i)
})
}
run()