You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Im trying to create a database module so every component of my app can import it and get functions like setId, getId, deleteId but im getting this error when trying to run it wherever the module itself or importing it.
Already tried to reinstall node, npm and removed node_modules
Node version: v20.13.1
Sqlite3 version: 5.1.7
OS: WSL running Ubuntu distro under Windows x64
// sqlite3 libraryconstsqlite3=require("sqlite3").verbose();// native node modulesconstappRoot=require('app-root-path');constrootPath=appRoot.path;constpath=require("node:path");// local modulesconst{ log }=require("../../modules/essentials");const{ presetsDbPath }=require('../../../config.json');/** * This function will take some environment variables and check validations before throwing the path of the database that will be used * for the *presets* DBM. * @returns {string} */functionloadDbPath(){try{// check if channelsDbPath is defined in config.jsonif(presetsDbPath){returnpath.join(rootPath,presetsDbPath);}else{thrownewError("The channelsDbPath value is not defined correctly in the config.json file.");}}catch(error){log.error(`Error loading database path: ${error.message}. Asuka is using the default database path until it's correctly assigned again.`);returnpath.join(rootPath,'src','db','channelsDb.db');}}/** * Database initialization, this function will only be called ONE TIME and then {@link dbInstance} will handle and return the database connections for better efficiency. * @returns {object} */asyncfunctiondbInit(){constDB_PATH=loadDbPath();log.debug("Current database path:",DB_PATH);constdbModule=require("../schemas/dbSchema.json");constdb=newsqlite3.Database(DB_PATH,async(err)=>{if(err){log.error("Error trying to open local database:",err.message);console.error(err);}else{db.run('PRAGMA foreign_keys = ON;');try{awaitPromise.all([createListsTable(db,dbModule.users),createListsTable(db,dbModule.servers),createListsTable(db,dbModule.roles),createListsTable(db,dbModule.rolesToGive),createListsTable(db,dbModule.rolePermissions),createListsTable(db,dbModule.channels)]);console.log("All tables are OK.");}catch(error){log.error("Error creating tables:",error);log.error(error);}}});/** * Creates a table with actually sql into the db given, just for automatization. *I know i can optimize this, but meh.* * @param {object} db - SQLITE3 Database connection Object. * @param {string} code - Arbitrary SQL query. * @returns {Promise<void>} */asyncfunctioncreateListsTable(db,code){returnnewPromise((resolve,reject)=>{db.run(code,(err)=>{if(err){log.error(`Error trying to create table:`,err);reject(err);}});});}returndb;}letdb;/** * `Handles database connections`, for efficiency this will return a database connection and if there is not one yet, **will open it and hold it so you will get the same connection** or if the database is starting this will check every 150 milliseconds to return the connection already made. * @returns {Promise<object>} */asyncfunctiondbInstance(){letdbi;asyncfunctionwaitFor(){returnnewPromise(resolve=>{setTimeout(()=>{if(db===1){waitFor().then(resolve());}else{resolve(db);}},100);});}if(db===1){dbi=awaitwaitFor();returndbi;}else{if(typeofdb!=='object'){db=1;db=awaitdbInit();dbi=db;log.debug('Database instance has been started.');returndbi;}else{returndb;}}}module.exports=dbInstance;
Steps to Reproduce
Running any query to the database or a test like this using nodejs
constdbInstance=require("./DatabaseModule");/** * **Manage the function of setting up identifiers and objects inside the database of Asuka instances and presets**. Also called `DBM`. _(Database Manager/Module.)_. * DBM requires the *main configuration file appended to be accordingly set and mounted with working paths* ~or~ the _default internal configuration will override the wrong preset given by the user from the config.json file_. * @param {number} type - **Range between 1-5.** _you should give specific properties_ inside the {@link options} parameter required for this function for each different number in this range. * ``` * 1: user<?userRole>, 2: role<roleName, ?roleColor>, 3: channel<channelName, channelType, ?isNsfw>, 4: server<isPrivate, ?serverName>, 5: roleToGive<userId> * ``` * @param {number} id - _This is the main ID to insert into the database_, **it will be checked before being inserted** and this number `depends` on the range given by {@link type} * ``` * 1: user ID expected. 2: role ID expected. 3: channel ID expected. 4: server ID expected. * ``` * _5: role Id expected and an user ID inside the options parameter with the key userId._ {@link options} * @param {number} serverId - Server ID where _the actions are being taken_. This can be `0` if it is not related to ~an action being taken inside a server~. _(ex. like inserting an id of a server)_. * Otherwise the function will throw an error. * @param {Object} options - **Optional extra parameters**. _The properties of this object will be interpreted as the {@link type}_ param says. This parameter WILL require one or more properties by each range given by the type param. * @param {string} [options.userRole] - `?Optional` This is just a common name for _searching a user inside the database_. * @param {string} [options.roleName] - `<Required>` This is the **name of the role** for setting it into the DB. Anyways, if the name is modified inside Discord, `the bot will adjust permissions and functions ignoring if the name is changed`. * **Maximum characters**: 2000 * @param {string} [options.roleColor] - `?Optional` This is the hexadecimal color of the role, is none is given, then just no color is being applied. * @param {string} [options.channelName] - `<Required>` The channel _name of the channel_ to build. * @param {number} [options.channelType] - `<Required>` The _type of the channel_ to build. **Range between 0-4**. * ``` * 0: category, 1: text, 2: voice, 3: stage * ``` * @param {number} [options.isNsfw] - `?Optional` Works like a boolean, will define if the channel to build is NSFW or SFW. **Range between 0-1**. * ``` * 0: false, 1: true * ``` * @param {number} [options.isPrivate] - `<Required>` This will _define is the server to build should be treated like a private or a public server_. This is required for **logic reasons**. * ``` * Works like a boolean. 0: false, 1: true * ``` * @param {string} [options.serverName] - `<Required>` The name of the server to build, **Min to max characters**: 2-32. * @param {number} [options.userId] - `<Required>` This will **define the user ID to give the role given by {@link id}**. _Just for synchronization and sequential actions_. * @returns {Promise<object>} */asyncfunctionsetId(type,id,serverId,options){constdb=awaitdbInstance();if((!options||typeofoptions!=="object")||(!type||typeoftype!=="number")||(!id||typeofid!=="number")||(!serverId||typeofserverId!=="number")){thrownewError("Invalid parameters. (src/modules/presetsDb.js:setId())");}lettable;/** * Type 1 validations. */if(type===1){if(options.userRole){if(typeofoptions.userRole!=="string"){thrownewError("Invalid userRole type inside the options parameter. (src/modules/presetsDb.js:setId())");}}}/** * Type 2 validations. */if(type===2){if(options.roleName){if(options.roleColor){if(typeofoptions.roleColor!=="number"){thrownewError("Invalid roleColor type inside the options parameter. (src/modules/presetsDb.js:setId())");}}}else{thrownewError("roleName property was not present inside the options parameter. (src/modules/presetsDb.js:setId())");}}/** * Type 3 validations. */if(type===3){if(options.channelName){if(typeofoptions.channelName!=="string"){thrownewError("Invalid channelName type inside the options parameter. (src/modules/presetsDb.js:setId())");}if(options.channelType){if(options.channelType!=="number"){thrownewError("Invalid channelType type inside the options parameter. (src/modules/presetsDb.js:setId())");}if(options.isNsfw&&typeofoptions.isNsfw!=="number"){thrownewError("Invalid isNsfw type inside the options parameter. (src/modules/presetsDb.js:setId())");}}else{thrownewError("channelType property was not present inside the options parameter. (src/modules/presetsDb.js:setId())");}}else{thrownewError("channelName property was not present inside the options parameter. (src/modules/presetsDb.js:setId())");}}/** * Type 4 validations. */if(type===4){if(options.isPrivate){if(typeofoptions.isPrivate!=="number"){thrownewError("Invalid isPrivate datatype inside the options parameter. Number expected. (src/modules/presetsDb.js:setId())")}}else{thrownewError("isPrivate property was not present inside the options parameter. (src/modules/presetsDb.js:setId())");}if(options.serverName&&typeofoptions.serverName!=="string"){thrownewError("Invalid serverName property inside the options parameter. Expected string. (src/modules/presetsDb.js:setId())");}}/** * Type 5 validations. */if(type===5){if(options.userId){if(typeofoptions.userId!=="number"){thrownewError("Invalid userId property inside of the options parameter. Expected number. [Type 5](src/modules/presetsDb.js:setId())")}}else{thrownewError("userId was not present inside the options parameter. [Type 5](src/modules/presetsDb.js:setId())")}}letsql;letcolumns;constdate=newDate();switch(type){case1:
table='users';if(options.userRole){sql=`${id}, '${options.userRole}', '${date}'`;columns='userId, role, createdAt';}else{sql=`${id}, '${date}'`;columns='userId, createdAt';}db.run(`INSERT INTO users (${columns}) VALUES(${sql})`,(err)=>{if(err){thrownewError(err.message);}});break;case2:
table='roles';if(options.roleColor){sql=`${id}, ${options.roleName}, ${options.roleColor}, ${date}`;columns='roleId, roleName, roleColor, createdAt';}else{sql=`${id}, ${options.roleName}, ${date}`;columns='roleId, roleName, createdAt';}db.run(`INSERT INTO roles (${columns}) VALUES (?)`,[sql],(err)=>{thrownewError(err);})break;case3:
table='channels';if(options.isNsfw){sql=`${id}, ${options.channelType}, ${options.channelName}, ${options.isNsfw}, ${date}`;columns='channelId, channelType, channelName, isNsfw, createdAt';}else{sql=`${id}, ${options.channelType}, ${options.channelName}, ${date}`;columns='channelId, channelType, channelName, createdAt';}db.run(`INSERT INTO channels (${columns}) VALUES (?)`,[sql],(err)=>{thrownewError(err);})break;case4:
table='servers';if(options.serverName){sql=`${id}, ${options.isPrivate}, ${options.serverName}, ${date}`;columns='serverId, isPrivate, serverName, createdAt';}else{sql=`${id}, ${options.isPrivate}${date}`;columns='serverId, isPrivate, createdAt';}db.run(`INSERT INTO servers (${columns}) VALUES (?)`,[sql],(err)=>{thrownewError(err);})break;case5:
table="rolesToGive";sql=`${id}, ${options.userId}, ${date}`;columns='roleId, userId, createdAt'db.run(`INSERT INTO rolesToGive (${columns}) VALUES (?)`,[sql],(err)=>{thrownewError(err);})break;default:
thrownewError('Invalid type. Only 0-5 types accepted. [Invalid type](src/modules/presetsDb.js:setId())');}returntable;}/** * `Retrieves an object of the information about an ID from the Database`. It can be null if not found the ID given into the specified table/type. * @param {number} type - `Range between 0-5.` * ``` * 1: user<userId, role, createdAt>, 2: role<roleId, roleName, roleColor, createdAt>, 3: channel<channelId, channelName, channelType, isNsfw, createdAt>, 4: server<serverId, isPrivate, serverName, createdAt>, 5: roleToGive<roleId, userId, createdAt> * ``` * @param {number} id - The ID to search. * @returns {Promise<null | object>} */asyncfunctiongetId(type,id){constdb=awaitdbInstance();if((!type||typeoftype!=="number")||(!id||typeofid!=="number")){thrownewTypeError("Invalid parameters.");}letquery;switch(type){case1:
db.get(`SELECT * FROM users WHERE userId = ?`,[id],(err,row)=>{if(err){thrownewError(err.message);}query=row;});break;case2:
db.get(`SELECT * FROM roles WHERE roleId = ?`,[id],(err,row)=>{if(err){thrownewError(err.message);}query=row;});break;case3:
db.get(`SELECT * FROM channels WHERE channelId = ?`,[id],(err,row)=>{if(err){thrownewError(err.message);}query=row;});break;case4:
db.get(`SELECT * FROM servers WHERE serverId = ?`,[id],(err,row)=>{if(err){thrownewError(err.message);}query=row;});break;case5:
db.all(`SELECT * FROM rolesToGive WHERE roleId = ?`,[id],(err,rows)=>{if(err){thrownewError(err);}query=rows;});break;default:
thrownewError('Invalid type. Only 0-5 types accepted. [Invalid type](src/modules/presetsDb.js:setId())');}returnquery;}/** * `Deletes an object with the specified ID from the Database`. * @param {number} type - `Range between 1-5.` * ``` * 1: user, 2: role, 3: channel, 4: server, 5: roleToGive * ``` * @param {number} id - The ID to delete. * @returns {Promise<object>} */asyncfunctiondeleteId(type,id){constdb=awaitdbInstance();if((!type||typeoftype!=="number")||(!id||typeofid!=="number")){thrownewTypeError("Invalid parameters.");}letquery;switch(type){case1:
query=awaitdb.run(`DELETE FROM users WHERE userId = ?`,[id]);break;case2:
query=awaitdb.run(`DELETE FROM roles WHERE roleId = ?`,[id]);break;case3:
query=awaitdb.run(`DELETE FROM channels WHERE channelId = ?`,[id]);break;case4:
query=awaitdb.run(`DELETE FROM servers WHERE serverId = ?`,[id]);break;case5:
query=awaitdb.run(`DELETE FROM rolesToGive WHERE roleId = ?`,[id]);break;default:
thrownewError('Invalid type. Only 1-5 types accepted.');}returnquery;}module.exports={
setId,
getId,
deleteId
}
Issue Summary
Error trying to make a database module for my app
Im trying to create a database module so every component of my app can import it and get functions like setId, getId, deleteId but im getting this error when trying to run it wherever the module itself or importing it.
Already tried to reinstall node, npm and removed node_modules
Node version: v20.13.1
Sqlite3 version: 5.1.7
OS: WSL running Ubuntu distro under Windows x64
I get this sometimes too:
The module code(Any suggestion is apprecciated)
Steps to Reproduce
Running any query to the database or a test like this using nodejs
Code:
Version
5.1.7
Node.js Version
v20.13.1
How did you install the library?
npm install sqlite3 with npm 10.5.2
arch: 'x64',
platform: 'linux',
release: {
name: 'node',
lts: 'Iron'
}
The text was updated successfully, but these errors were encountered: