In my efforts to reset the TypeORM MySQL database upon server shutdown in NestJS, I am exploring different approaches

I am looking for a way to clear all entries in my database when the server shuts down. Can anyone help with this?

export class RoomsService {
  async onApplicationShutdown() {
    await this.roomService.deleteAll()
  }
async deleteAll(): Promise<DeleteResult> {
    const res = await this.roomRepository.delete('*');
    return res;
  } 

During shutdown, I encounter this error message (even without using async/await)

Error: Pool is closed.
at C:\Users\Admin\Desktop\sites\UltimatePartyRoom\upr-server\node_modules\mysql2\lib\pool.js:39:40
at processTicksAndRejections (node:internal/process/task_queues:77:11)

Answer №1

The issue arises when attempting to close the application (onApplicationShutdown) as the database connection is already severed. To resolve this, it is necessary to clear the database before reaching this point.

Instead of using onApplicationShutdown, consider utilizing OnModuleDestroy as shown below:

export class RoomsService implements OnModuleDestroy {
  async onModuleDestroy() {
    await this.roomService.deleteAll()
  }
}

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

To overcome the 401 (Unauthorized) error on specific requests, how can one bypass it when trying to access http://localhost:3000/user/auth

I am encountering a 401 (Unauthorized) error when trying to access certain requests that do not require user authentication. For instance, in an eCommerce store, all visitors should be able to view products without logging in, but sellers need to authentic ...

Updating Angular model remotely without relying solely on the controller

I am struggling to call the addRectangleMethod method from my Javascript code in order to retrieve server-side information into the Angular datamodel. However, I keep encountering an error stating that the method I'm trying to call is undefined. It&ap ...

Ways to display information that lacks significance

Two tables are in my possession, pemesanan detail_pemesanan Exploring this query has been my approach: select pemesanan.tgl_pesan, SUM(detail_pemesanan.jumlah) as jumlah FROM pemesanan JOIN detail_pemesanan ON pemesanan.id_pemesanan = detail_peme ...

What are the most effective methods for handling extensive "task queues" or "input queues"?

Our system operates with numerous input queues that contain IDs of items required for jobs. Each queue can hold tens of thousands to millions of IDs, and our jobs typically retrieve batches of around 20,000 IDs from a single queue. Meanwhile, our producers ...

Encountering Angular Material UI issues following the transition from version 11 to 12

I am currently in the process of updating my Angular application from version 11 to 12, integrating Angular Material, and encountering some error messages: Error No.1 - Error NG8002: Cannot bind to 'ngModel' as it is not a recognized property of ...

Error: Visual Studio unable to locate Firebase node module

After running the command npm install firebase --save in the root of my project folder, a firebase folder was successfully added to my node_modules directory and the packages.json file was updated accordingly. In addition to using typescript, I have an ap ...

What is the best way to utilize the next-env.d.ts file within Next.js?

In my Next.js TypeScript project, I came across a file named next-env.d.ts. This got me thinking about how I can declare enums that would be accessible across all my Next.js files. Can you guide me on how to achieve this and use the enums throughout my p ...

An asynchronous function operates incessantly

I have implemented the following code in React using hooks to fetch data from two different sources. const [ permissionTree, setPermissionTree ] = useState([]); const [ availablePermissionsInRole, setAvailablePermissionsInRole ] = useState<Permission[] ...

Struggling to make type definitions work in react-hook-form 7

Upon defining the types of my form fields, I encountered an issue where each field seems to take on all three different types. This is puzzling as I have specified 3 distinct types for my 3 different fields. What could be causing this confusion? https://i ...

Typescript's dynamic React component and its conditional types

I am currently working on a dynamic React component and I am facing an issue with properly passing the correct propType based on the selected component. The error arises when using <SelectComponent {...props.props} /> because the props do not match t ...

Utilizing Next.js with a unique custom Node.js backend implementation

I have successfully developed the frontend using Next.js in order to utilize SSR and enhance SEO. Additionally, I have a personalized NodeJS+express server for managing users and other database tasks (tested thoroughly with Postman). My next step is to in ...

Experiment with redirect testing in Jest with Express

I am currently utilizing Jest to conduct tests on my code. My main objective is to verify the redirection from HTTP to HTTPS, specifically if it exists within process.env.IS_PRODUCTION. However, I find myself at a loss on how to effectively test this scen ...

What is the best way to enable the delete function exclusively for users who are logged in?

I am currently working on implementing a delete function in JavaScript that will remove a MySQL row if and only if it was created by the user who is currently logged in. This means that users cannot delete rows they did not create. Below is the progress I ...

The data type 'string | boolean | null' cannot be assigned to type 'boolean'. Specifically, the type 'null' cannot be assigned to type 'boolean'

Can you spot the issue in this code snippet? isAuthenticated(): boolean { var token = localStorage.getItem(ACCESS_TOKEN_KEY); return token && !this.jwtHelper.isTokenExpired(token); } Error: The variable is returning a type of 'string | bo ...

Save the file to a specific folder and compress the entire folder into a

I have encountered an issue while attempting to write a file to a directory named templates and then stream a zip file with the content that was just written. The problem arises when the zip file is returned, displaying an error message "Failed - Network E ...

When considering Angular directives, which is more suitable for this scenario: structural or attribute?

In the process of developing an Angular 5 directive, I aim to incorporate various host views (generated from a component) into the viewContainer. However, I find myself at a crossroads as to whether I should opt for an attribute directive or a structural ...

What Google Domain Verification APIs are needed for verifying domains in Pub/Sub?

I've written this code that utilizes a Domain token to verify a domain with Google using the Site Verification API: const auth = await this.gcp.getApplicationCredential(accountId, projectId,[ 'https://www.googleapis.com/auth/siteverification ...

The `forEach` method cannot be called on an undefined node.js

I have been developing a small study website but encountered an issue with the title. Despite extensive internet research, I have not been able to find a solution. The system I am using is Ubuntu with node.js, express, and mysql. app.js var fs = requir ...

Issue with special characters while exporting PHP data to Excel

I found some code on a website that is supposed to convert data from MySQL to Excel. Here is the link to the code I'm trying to use: When I try to save the Excel file, it works fine if there are no special characters. However, when it converts the ...

The authentication method "discord" is not recognized

Currently, I am working on implementing discord authentication using passport. Originally, everything was functioning correctly, but now it seems to have encountered an issue which I cannot identify. auth.js const express = require('express') co ...