#Help with db tables (JSON Driver)

92 messages · Page 1 of 1 (latest)

red yacht
#

I would like to know how it works to create a new table and how I can use it, switching between tables depending on the command that is executed in my bot. I would also like to know that it is possible to change the path of the quickdb.json file because I would like it to stay within the main structure of my bot. src/database

red cypress
# red yacht I would like to know how it works to create a new table and how I can use it, sw...

You can pass an object to the quick.db's constructor with the filePath option as documented here https://docs.plexidev.org/interfaces/IQuickDBOptions.html#filePath

Which is passed into this (the constructor) https://docs.plexidev.org/classes/QuickDB.html#constructor

for creating a table. you can use tableAsync.
This is the fixed version of table it will be removed in the next v10 release because it's only there as a quick fix.
https://docs.plexidev.org/classes/QuickDB.html#tableAsync
you just pass in the table name and it returns an other instance of quick.db with that table configured which you can than use to get/set and everything else

red yacht
#

ok, i will try

red cypress
#

if you need more help or examples. you can mention me if needed

red yacht
#

@red cypress it's correct?

const db = new QuickDB({
    driver: new JSONDriver(),
    filePath: "$$ROOT/src/database/quickdb.json",
});
red cypress
#

you need to make sure to path passeed in a valid path

red yacht
#

I use json because I find it easier

red cypress
#

I edited the code

#

it's an argument from JSONDriver not QuickDB my bad again

red yacht
#

i have a question

red cypress
#

yes?

red yacht
#

if I don't use the database in index.js I don't need to import it there, correct?

#

quickdb don't need to connect

red cypress
#

correct but if you use the json driver in multiple places (files) you will need to use createSingleton because the json driver is using memory as well. so creating multiple instance of the driver will cause desynchronization.

all you have to do is this

const db = QuickDB.createSingleton({
    driver: new JSONDriver({
     path: "$$ROOT/src/database/quickdb.json",
    }),
});

make sure every where you use quick.db it's like this.
the createSinglteon will make only one instance of the driver and will pass it on everytime

red yacht
red cypress
# red yacht would using sqlite fix this problem?

yes, sqlite doesn't have this problem
but the createSingleton as I just showed also fixes that problem. it's just that instead of new QuickDB({...
it's QuickDB.createSingleton({..
and it needs to be like for every quick.db import

red cypress
#

👍 I would definitely recommend it over json. I saw you said json was easier. if you mean editing it/viewing it. I recommend https://sqlitebrowser.org/ this tool for sqlites files.

#

it let's you see and edit sqlites files with a simple gui

red yacht
#

I'm still a bit confused, in the past I used mongoose but several problems were occurring to save money on model users so I decided to migrate to quick.db for ease.
But I don't know how I can define a table with information about servers and another with information about users. Maybe I'll create a command to register user in the database and use a guildCreate event to register the servers

red yacht
#

I'll try to get this working tonight when I get better-sqlite3 installed

#

better-sqlite done

red cypress
# red yacht I'm still a bit confused, in the past I used mongoose but several problems were ...

there is two main way of separating stuff in quick.db.

The no table method and the table method.

  1. the no table method
    mostly worst than the table method but it's often simpler to code in
await db.set(`users_${userid}`, "hi");
await db.get(`users_${userid}`);
``` by using a `_` serpator so you can have users\_ and servers\_ so the key will be different depending on each.

2) table method
mostly better but a bit harder to do
```js
const userTable = await db.tableAsync("users");

userTable.set(userid, "hi");

const serverTable = await db.tableAsync("servers");
...```
red yacht
red cypress
#

you're welcome

#

also quick thing.
the sqlite driver is the default so you can just do

const db = new QuickDB();

if you want to change the path

const db = new QuickDB({ filePath: "path/here/json.sqlite" });
red cypress
#

not exactly the same ^ I answered before you asked with the message just above

red yacht
#

to make it easier for me to find the server in the database, is it possible to define an object with the key being the server id?

red cypress
# red yacht the key not needs `""`?

it needs "" yes, '' also works.
what I used higher is called a template string. it's also a string like "" and '' but it's with back ticks

``

this lets you use variable in a string better example:

console.log("hi");
console.log('hi');
console.log(`hi`);
// ^ all the same

const world = "world";
console.log("hello" + world);
console.log('hello' + world);
console.log(`hello${world}`);
// ^ all the same
red yacht
#

example:

343572980351107077: {
lang: 'en-US',
},
1099032724729241692: {
lang: 'pt-BR',
}
red yacht
red cypress
#

yeah I was referencing to a potential variable

#

like world in the example I showed

red yacht
#

ok

red cypress
# red yacht its possible?

I did that because people often use ids as keys or in keys. so yes it's possible.

for example people often do things like this

await db.set(`xp_${userid}`, 10);

here the variable userid would be the userid of the user who ran the command.
so the key will be xp_145723964767862785 for example.

it's easy after to get the the xp back for this user when the id of the user is know. because you just need to do xp_ + userid.

you could also just put the userid directly as a key. that also works.

#

here is a quick example

// add 10 xp to the user
await db.add(`xp_${userid}`, 10);
const userXp = await db.get(`xp_${userid}`);

// check if user has 100 xp
if (userXp <= 100) {
  // level up the user
  await db.add(`level_${userid}`, 1);
  // set the user's xp back to 0
  await db.set(`xp_${userid}`, 0);
}
#

this will add 10 to the user.
when it reaches 100 or more. it will add 1 to user's level and set back the xp 0.

it's an example of a simple leveling system in quick.db

red yacht
#

hmm

#

for now this answers all my questions

#

thanks for all your help and patience

red cypress
#

you're welcome!

red yacht
#

@red cypress sorry for the mention, but does this work like db.tableAsync?

red cypress
#

It's the same as you have when you do new QuickDB

#

It will be the same

red yacht
#

so I can pass the parameter table in the QuickDB class declaration that will work, right?so I can pass the parameter table in the QuickDB class declaration that will work, right?

#
const db = new QuickDB({ 
  filePath: '../database/json.sqlite',
  table: 'servers'
});
red cypress
red yacht
red cypress
#

Most likely because it's a relative path

red yacht
red cypress
# red yacht can you show me a solution?

A simple solution is to make it a specific path instead.
The path module can be used (no need to install it, it comes with node)
This way you can get the current file location directory with __dirname and join it with your relative path.

For example

const path = require("path");
const specificPath = path.join(__dirname, "../database/json.sqlite");

So in this case if the folder tree looks like this
src
|-- commands
|------|---- file.js
|-- database

If the example code I showed is in file js it will place it correctly in the database folder because. __dirname will be equal to src/commands and the relative path joined to it goes down a level ../ so it is now src/ and it then goes in database so the final path will be src/database
The __dirname will take the path from the racine of your drive so in reality the path will be like
C:/something/something/src/database which means it's not relative anymore so it shouldn't change unless you move the file

red yacht
#

no more use 😦

red cypress
#

Oh that can happen if the file is correctly being used

#

I am not sure what operation you tried to do on it but make sure nothing is using it

red yacht
red cypress
#

If your code was still running. It's probably why it said that

red yacht
#

I'll leave it like that, I wanted to change it just for the aesthetics of the project

red cypress
#

Or it means you have an other application that has the file open like vscode or something else

red cypress
#

Also no need to apologize for mentioning me. You can mention me any time, any day. I am here for this so don't hesitate to mention and ask stuff

red yacht
red cypress
#

You're welcome!

red yacht
#

i prefer the ES Module syntax

#
const dbUsers = new QuickDB({
    filePath: "../../database/quickdb.sqlite",
    table: "users",
});
#

this sucks

red cypress
red yacht
#
src
  |-- commands
  |------|---- roleplay
  |------|--------|---- account.js
  |-- database
  |------|---- quickdb.sqlite

This is my folder structure

red cypress
#

When you run the code. where do you run it from?
outside of src ? like one folder outside ? that can also impact relative paths which is why I am asking

odd hemlock
#

Could be relative to src

red yacht
red cypress
red cypress
# red yacht i use `node .` inside the home folder (outside the src folder), but when I used ...

because relative paths can be handled differently which is why there is that confusion.
the quick.db path works with the current workdir of the command line.

so if you run node . outside src and want the database in database the relative path should be
./src/database/quickdb.sqlite

because it will start at the directory you ran the code.
which is somewhat bad if you run the code from an other place since it will not be the same path
which is why I recommended a specific path but if you run always the code from the home folder there should be no problem

#

mongodb was probably handling the path according to the current file

red yacht
#

hmm

red cypress
#

which seems like the sqlite driver doesn't do

red yacht
#

i understand

#

thanks again

red cypress
#

You're welcome

echo marsh
#

hello

#

how i can get fetch quickdb info from anotherfolder? using js

red cypress