Using Node.js to connect to SQL Server database

Viewed 45

I want to create a website that has access to a database, where you can basically update insert and delete. I have the latest version of SQL Server and I am planning to use node.js to connect my database to my front-end since I expect a lot of traffic and noe.js is good with handling it. I was reading some documentation and I saw that streaming is the best way to go about when dealing with high traffic.

Here is my database

CREATE TABLE [dbo].[Users](
    [id] [int] IDENTITY(101,1) NOT NULL,
    [user_name] [nchar](255) NOT NULL,
    [user_lname] [nchar](255) NOT NULL,
    [user_username] [nchar](255) NOT NULL,
    [user_email] [nchar](255) NOT NULL,
    [user_phonenumber] [nchar](50) NOT NULL,
    [user_location] [nvarchar](255) NOT NULL,
    [subscription_type] [nvarchar](100) NULL,
    [sub_duration_in_months] [int] NULL,
    [subscription_date] [datetime] NULL,
    [subscription_deadline] [datetime] NULL,
PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Clients](
    [id] [int] IDENTITY(101,1) NOT NULL,
    [c_account_username] [nchar](255) NULL,
    [c_account_password] [nvarchar](255) NULL,
    [Client_or_Brand_name] [nvarchar](255) NOT NULL,
    [branch_location] [nchar](255) NOT NULL,
    [business_email] [nchar](255) NOT NULL,
    [business_phone] [nchar](100) NULL,
    [business_website] [nchar](255) NULL,
    [percentage_of_earnings] [float] NOT NULL,
    [number_of_affiliated_users] [int] NULL,
    [total_earnings_via_users] [numeric](18, 2) NULL,
    [total_earnings_via_users_this_month] [numeric](18, 2) NULL,
PRIMARY KEY CLUSTERED 

CREATE TABLE [dbo].[transactions](
    [id] [int] IDENTITY(101,1) NOT NULL,
    [user_id] [int] NOT NULL,
    [client_id] [int] NOT NULL,
    [time_of_transaction] [datetime] NULL,
    [receit_id] [nvarchar](255) NULL,
    [receit_total] [nchar](10) NULL
) ON [PRIMARY]
GO

and here is the JavaScript code:

const sql = require('mssql')

const config = {
    user: 'DESKTOP-9JKA425\\user',
    password: '',
    server: 'localhost\\DESKTOP-9JKA425', // You can use 'localhost\\instance' to connect to named instance
    database: 'PD',
}

sql.connect(config, err => {

    console.log(err)
    // ... error checks

    const request = new sql.Request()
    request.stream = true // You can set streaming differently for each request
    request.query('select * from Users') // or request.execute(procedure)

    request.on('recordset', columns => {
        // Emitted once for each recordset in a query
    })

    request.on('row', row => {
        // Emitted for each row in a recordset
    })

    request.on('rowsaffected', rowCount => {
        // Emitted for each `INSERT`, `UPDATE` or `DELETE` statement
        // Requires NOCOUNT to be OFF (default)
    })

    request.on('error', err => {
        // May be emitted multiple times
    })

    request.on('done', result => {
        // Always emitted as the last one
    })
})

sql.on('error', err => {
    // ... error handler
})

now, when I run this code, I get the following error:

ConnectionError: Failed to connect to localhost\DESKTOP-9JKA425 in 15000ms
    at C:\Users\user\Desktop\newnew\node_modules\mssql\lib\tedious\connection-pool.js:70:17
    at Connection.onConnect (C:\Users\user\Desktop\newnew\node_modules\tedious\lib\connection.js:1020:9)
    at Object.onceWrapper (node:events:628:26)
    at Connection.emit (node:events:513:28)
    at Connection.emit 
(C:\Users\user\Desktop\newnew\node_modules\tedious\lib\connection.js:1048:18)
    at Connection.connectTimeout 
(C:\Users\user\Desktop\newnew\node_modules\tedious\lib\connection.js:1258:10)
    at Timeout._onTimeout 
(C:\Users\user\Desktop\newnew\node_modules\tedious\lib\connection.js:1203:12)
    at listOnTimeout (node:internal/timers:559:17)
    at processTimers (node:internal/timers:502:7) {
  code: 'ETIMEOUT',
  originalError: ConnectionError: Failed to connect to localhost\DESKTOP-9JKA425 in 15000ms
      at Connection.connectTimeout 
(C:\Users\user\Desktop\newnew\node_modules\tedious\lib\connection.js:1258:26)
      at Timeout._onTimeout 
(C:\Users\user\Desktop\newnew\node_modules\tedious\lib\connection.js:1203:12)
      at listOnTimeout (node:internal/timers:559:17)
      at processTimers (node:internal/timers:502:7) {
    code: 'ETIMEOUT',
    isTransient: undefined
  }
}

I can tell that it's probably because of my connection, its either I'm not getting my the server right or something along those lines.

Any help would be appreciated.

0 Answers
Related