#Help with db tables (JSON Driver)
92 messages · Page 1 of 1 (latest)
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
ok, i will try
if you need more help or examples. you can mention me if needed
Ok thanks!
@red cypress it's correct?
const db = new QuickDB({
driver: new JSONDriver(),
filePath: "$$ROOT/src/database/quickdb.json",
});
oh sorry, I didn't see "(JSON Driver)"
for the json driver it's actuallly named just path.
const db = new QuickDB({
driver: new JSONDriver({
path: "$$ROOT/src/database/quickdb.json",
}),
});```
you need to make sure to path passeed in a valid path
I use json because I find it easier
i have a question
yes?
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
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
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
I'm going to use sqlite myself
👍 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
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
i use vscode extension, but thanks for this
I'll try to get this working tonight when I get better-sqlite3 installed
better-sqlite done
there is two main way of separating stuff in quick.db.
The no table method and the table method.
- 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");
...```
Oh, thanksthanks thanks thanks thanks
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" });
it's function with sqlite?
not exactly the same ^ I answered before you asked with the message just above
the key not needs ""?
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?
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
example:
343572980351107077: {
lang: 'en-US',
},
1099032724729241692: {
lang: 'pt-BR',
}
you used userid instead of 'userid'
ok
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
you're welcome!
@red cypress sorry for the mention, but does this work like db.tableAsync?
TableAsync gives you a quick.db instance when you resolve the promise
It's the same as you have when you do new QuickDB
It will be the same
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'
});
Yes, that works. If you use it right after it would be better to use as well right under
await db.init();
This will just make sure the table was created correctly.
PS sorry for the delay, something important came up
ok, np
But how do I create a sqlite database in the location specified here?
import { QuickDB } from "quick.db";
const db = new QuickDB({
filePath: '../database/json.sqlite',
table: "servers",
});
The filePath will create the database at the specified place. If it doesn't. It probably means the path you entered is not where you think it is leading to
Most likely because it's a relative path
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
no more use 😦
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
I tried to delete the file that is in the project's home folder to be able to put it in the src/database folder
If your code was still running. It's probably why it said that
I'll leave it like that, I wanted to change it just for the aesthetics of the project
Or it means you have an other application that has the file open like vscode or something else
That was it
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
Thank you very much! You are helping me a lot.
You're welcome!
Zelak, I will not go back to using normal syntax
i prefer the ES Module syntax
const dbUsers = new QuickDB({
filePath: "../../database/quickdb.sqlite",
table: "users",
});
this sucks
Than you will have to make sure your relative path is right. I cannot do much for you at this point
It's impossible the path is wrong
src
|-- commands
|------|---- roleplay
|------|--------|---- account.js
|-- database
|------|---- quickdb.sqlite
This is my folder structure
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
Could be relative to src
i use node . inside the home folder (outside the src folder), but when I used mongodb I used this path and it worked correctly
it doesn't, it from the current workdir of the command line
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
hmm
which seems like the sqlite driver doesn't do
You're welcome
Hi, you will have to be more specific. I didn't understand what you want to do