Object-oriented database connection for a Node.js backend application

In my current project, I am developing a backend application using node with typescript / javascript. The backend is linked to an sqlite database, and I have organized the code so that all database operations are contained in one file.

I'm facing a decision on whether to create a module that will be required by other modules, or to use a class that establishes a connection to the database in its constructor method.

Personally, I find it odd to encapsulate all database functionality in a non-object module. What would be considered the best practice in this scenario, and why? (I realize this may seem like a simple question, but as a hobbyist, I appreciate any guidance)

Thank you in advance

Answer №1

For my situation, I establish the database connection prior to starting the server. Here's a rough outline in pseudocode:

connectorDB.connect( path, config, () => {
    server.listen( port )
})

The timing of establishing the database connection may vary depending on the significance of this connection to the overall functionality of your application.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Determine the existence of a Firebase Facebook user without the need to first create a user from an anonymous account

When working with Firebase, I am faced with the challenge of checking if a Facebook user exists without automatically creating a new user account. The situation arises when an anonymous user attempts to log in using their Facebook credentials, and I want t ...

Unable to successfully send a POST request from the client-side frontend to the local node server; only GET requests are functioning properly

While attempting to submit a post request to my local server at localhost:8080/page, I encountered the message 'Cannot GET /page'. My browser is Google Chrome and the network tab in the developer tools shows: Below is the front end code featurin ...

AJAX does not support functioning links

I've been experimenting with AJAX on my website, and encountered an issue. When I dynamically add content to the page using container.insertAdjacentHTML('beforeend', thing); everything looks fine. However, when I try to click on the links wi ...

Importing/Requiring an External Module in Typescript Node using a Symbolic Link in the

I am in the process of migrating a Node + Express application to TypeScript and have encountered an issue with using external modules. Previously, I was utilizing the "symlink trick" to avoid dealing with relative paths. This is how it used to work withou ...

The Angular multi select tree feature seems to be malfunctioning

I recently incorporated a plugin called angular-multi-select-tree from GitHub into my project. Here is how I added it: First, I installed it via bower using the command bower install angular-multi-select-tree --save. This updated the bower.json file to i ...

End-to-end Testing with WebdriverJS, Selenium, and Jasmine

After trying multiple examples with slight variations in the code, I am still unable to get it to work as expected. The code below consists of commented sections that claim to work but do not in my case. The tools I am using include: - selenium-webdriver ...

Checkbox Hierarchy: Children marked as either Checked or Unchecked based on parent selection

Hello, I have a form that contains nested checkboxes on three levels. Using jQuery, I am trying to check/uncheck all the children when I check a parent level... and if any of the children are unchecked, uncheck the parent as well. I have attempted this m ...

Retrieving the body of a GET request using NodeJS and axios

Let me share my request with you: axios.get(BASE_URI + '/birds/random', {Stuff: "STUFF"}) .then(randBird=>{ const birdData = randBird.data const bird = { age: birdData.age, ...

Utilizing Nested ControlGroups in Angular2 rc1: A Comprehensive Guide

Seeking assistance with understanding the functionality of a control group. Attempting to implement something similar to this: app.component.ts: import { Component, OnInit } from "@angular/core"; import { FORM_DIRECTIVES, FormBuilder, ControlGroup } from ...

Develop instances from a class in Angular 2

Hello there! I have a question about creating a class that implements an interface and utilizes data from a .get service to instantiate a new object. Here is an example of what I'm trying to achieve: import { Component, OnInit } from '@angular/c ...

Retrieving data from PHP using jQuery and JSON

Struggling to retrieve various variable outcomes from PHP using jQuery JSON. Encountering issues with null values appearing in the console (like in the title), causing script failures. Currently, attempting to fill input fields with data received from PHP. ...

Adjust the knock date to be two days earlier than the delivery date using vue.js

I am brand new to the world of vue.js and I have encountered a question. My form includes two date input fields labeled as Delivery date and Knock date. My goal is to automatically set the knock date to be two days before the delivery date. For instance, ...

Can a Handlebar variable be accessed using an external .js file?

Hello there! Despite the ongoing pandemic, I hope you are doing well. Currently, I'm deep into a school project and seem to have hit a roadblock that I can't maneuver around. My main dilemma lies in finding a way to access the data communicated b ...

Creating a never-ending scroll with a combination of throttling and debouncing techniques

I have a list with a pager where I am attempting to implement an effect that when you request the next/previous page, the current list is automatically scrolled to the end/beginning. While backward scrolling works fine, forward scrolling often pages multip ...

Executing an HTTP POST request within an Angular 7 route guard: A step-by-step guide

In my Angular 7 application, I have implemented a route guard that is dependent on the response of a post call. The guard is structured like this: canActivate = (_router: ActivatedRouteSnapshot): boolean => { console.log('in link expiry guard& ...

Adjust the size of the leftIcon in a ListItem using Material UI

Currently, I am working on a React project where I am trying to adjust the height and width of the leftIcon in a ListItem component from Material UI. The issue I am facing is that I want the image within the leftIcon to match the height of the whole ListIt ...

Why isn't the variable changing when exported to another file in Node.js?

My issue revolves around handling two arrays of objects in server.js, which I export using module.exports for use in another file called scores.js. The problem arises when I attempt to reset these arrays for a new game, as some data does not update correct ...

What is the best way to incorporate dynamic fields in React js that are determined by my chosen attributes?

My task involves creating dynamic fields based on selected attributes using two array objects - addAttributes and fakeAttributes. The fakeAttributes hold details of the selected attributes, which are displayed when passed through a dropdown select componen ...

Is it possible for search engines like google to index Javascript content that is stored within divs and loaded onto the page?

My website utilizes JavaScript to dynamically add content to div elements, like so: <script> //This content is generated by PHP var contents = [ "Item 1", "Item 2" ]; //Display the first item document.getElementById( "item" ).textCo ...

Await the resolution of multiple individual promises

I am working with Angular promises in multiple controllers, and I need to trigger a function once all of them have completed. Is there a way to achieve this using events or promises? ...