fastify-mssql
Advanced tools
Comparing version 1.0.2 to 1.1.0
{ | ||
"name": "fastify-mssql", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Fastify Plugin to manage MSSQL connections", | ||
"main": "plugin.js", | ||
"scripts": { | ||
"test": "jest" | ||
"test": "jest --testTimeout=15000", | ||
"lint": "eslint .", | ||
"test:docker:start": "docker-compose -f tests/docker-compose.dev.yml up -d", | ||
"test:docker:stop": "docker-compose -f tests/docker-compose.dev.yml stop" | ||
}, | ||
@@ -39,5 +42,10 @@ "engines": { | ||
"@types/mssql": "^6.0.7", | ||
"eslint": "^7.27.0", | ||
"eslint-config-prettier": "^8.3.0", | ||
"eslint-plugin-import": "^2.23.3", | ||
"eslint-plugin-prettier": "^3.4.0", | ||
"fastify": "^3.11.0", | ||
"jest": "^26.6.3", | ||
"mssql": "^6.3.1" | ||
"mssql": "^7.1.0", | ||
"prettier": "^2.3.0" | ||
}, | ||
@@ -44,0 +52,0 @@ "peerDependencies": { |
@@ -1,5 +0,5 @@ | ||
'use strict'; | ||
'use strict' | ||
const fp = require('fastify-plugin'); | ||
const MSSql = require('mssql'); | ||
const fp = require('fastify-plugin') | ||
const MSSql = require('mssql') | ||
@@ -13,22 +13,21 @@ const defaults = { | ||
} | ||
const defaultOptions = { | ||
enableArithAbort: true | ||
} | ||
const plugin = async (fastify, options) => { | ||
const config = Object.assign({}, defaults, options) | ||
const { server, port, database, user, password } = config; | ||
const plugin = async (fastify, config) => { | ||
const connectionConfig = Object.assign({}, defaults, config) | ||
connectionConfig.options = Object.assign({}, defaultOptions, config.options) | ||
config.options = { | ||
'enableArithAbort': true | ||
} | ||
const pool = await new MSSql.ConnectionPool(connectionConfig) | ||
const pool = await new MSSql.ConnectionPool(config); | ||
fastify.addHook('onClose', async () => { | ||
await pool.close(); | ||
}); | ||
await pool.close() | ||
}) | ||
fastify.decorate('mssql', { pool }) | ||
}; | ||
} | ||
module.exports = fp(plugin, { | ||
fastify: '>=3', | ||
name: 'fastify-mssql', | ||
}); | ||
name: 'fastify-mssql' | ||
}) |
@@ -26,3 +26,3 @@ # fastify-mssql | ||
app.register(mssqlPlugin, { | ||
host: 'my-host', | ||
server: 'my-host', | ||
port: 1433, | ||
@@ -45,2 +45,2 @@ user: 'my-user', | ||
app.listen(3000) | ||
``` | ||
``` |
@@ -5,3 +5,3 @@ const fastify = require('fastify') | ||
const app = fastify(opts) | ||
app.get('/', async function (request, reply) { | ||
app.get('/', async function () { | ||
return { hello: 'world' } | ||
@@ -13,2 +13,2 @@ }) | ||
module.exports = build | ||
module.exports = build |
const buildServer = require('./build-server') | ||
const plugin = require('../plugin.js'); | ||
const MSSql = require('mssql'); | ||
const plugin = require('../plugin.js') | ||
const { getPool } = require('./utils') | ||
describe('fastify-mssql', () => { | ||
let app | ||
let app; | ||
beforeEach(async () => { | ||
const pool = await getPool(); | ||
try { | ||
beforeAll(async () => { | ||
const pool = await getPool() | ||
app = buildServer() | ||
await pool.query(`CREATE DATABASE TestSuite`); | ||
await pool.query(`USE TestSuite`); | ||
await pool.query("CREATE TABLE [dbo].[Users] ([id] bigint,[name] varchar(25), [email] varchar(25) PRIMARY KEY (id));"); | ||
await pool.query("INSERT INTO [dbo].[Users] ([id], [name], [email]) VALUES ('1', N'Foobar', N'foobar@gmail.com');"); | ||
await pool.query("INSERT INTO [dbo].[Users] ([id], [name], [email]) VALUES ('2', N'fizzbuzz', N'fizzbuzz@gmail.com');"); | ||
} catch (err) { | ||
if (err.message.match(`Database 'TestSuite' already exists`)) { | ||
// do nothing, it's fine | ||
} else { | ||
throw err; | ||
} | ||
} | ||
await pool.query(` | ||
IF NOT EXISTS (SELECT 1 FROM sys.databases WHERE name = 'TestSuite') | ||
CREATE DATABASE TestSuite; | ||
`) | ||
await pool.query(`USE TestSuite`) | ||
await pool.query( | ||
'CREATE TABLE [dbo].[Users] ([id] bigint,[name] varchar(25), [email] varchar(25) PRIMARY KEY (id));' | ||
) | ||
await pool.query( | ||
"INSERT INTO [dbo].[Users] ([id], [name], [email]) VALUES ('1', N'Foobar', N'foobar@gmail.com');" | ||
) | ||
await pool.query( | ||
"INSERT INTO [dbo].[Users] ([id], [name], [email]) VALUES ('2', N'fizzbuzz', N'fizzbuzz@gmail.com');" | ||
) | ||
}) | ||
afterAll(async () => { | ||
const pool = await getPool() | ||
await pool.query(`USE TestSuite`) | ||
await pool.query('DROP TABLE IF EXISTS [dbo].[Users]') | ||
await pool.close() | ||
app.close() | ||
}) | ||
}) | ||
afterEach(async () => { | ||
const pool = await getPool(); | ||
await pool.query(`USE TestSuite`); | ||
await pool.query("DROP TABLE [dbo].[Users]"); | ||
await pool.close(); | ||
app.close(); | ||
}) | ||
test('MSSQL plugin is loaded', async () => { | ||
app.register(plugin, { | ||
user: 'sa', | ||
password: 'S3cretP4ssw0rd!', | ||
database: 'TestSuite', | ||
options: { | ||
trustServerCertificate: true | ||
} | ||
}) | ||
test('MSSQL plugin is loaded', async () => { | ||
app.get('/users', async function () { | ||
try { | ||
await app.mssql.pool.connect() | ||
const res = await app.mssql.pool.query('SELECT * FROM [dbo].[Users];') | ||
return { users: res.recordset } | ||
} catch (err) { | ||
return { error: err.message } | ||
} | ||
}) | ||
const response = await app.inject({ | ||
method: 'GET', | ||
url: '/users' | ||
}) | ||
app.register(plugin, { | ||
user: 'sa', | ||
password: 'S3cretP4ssw0rd!', | ||
database: 'TestSuite' | ||
const body = JSON.parse(response.body) | ||
expect(app.mssql.pool).not.toBe(undefined) | ||
expect(response.statusCode).toBe(200) | ||
expect(body.users.length).toBe(2) | ||
}) | ||
app.get('/users', async function (req, reply) { | ||
try { | ||
await app.mssql.pool.connect(); | ||
const res = await app.mssql.pool.query('SELECT * FROM [dbo].[Users];'); | ||
return { users: res.recordset } | ||
} catch (err) { | ||
return { error: err.message } | ||
} | ||
}) | ||
const response = await app.inject({ | ||
method: 'GET', | ||
url: '/users' | ||
}) | ||
const body = JSON.parse(response.body); | ||
expect(app.mssql.pool).not.toBe(undefined); | ||
expect(response.statusCode).toBe(200); | ||
expect(body.users.length).toBe(2); | ||
}) | ||
}) |
@@ -1,3 +0,3 @@ | ||
const MSSql = require('mssql'); | ||
let pool; | ||
const MSSql = require('mssql') | ||
let pool | ||
@@ -13,3 +13,4 @@ async function getPool() { | ||
options: { | ||
'enableArithAbort': true | ||
enableArithAbort: true, | ||
trustServerCertificate: true | ||
} | ||
@@ -20,7 +21,7 @@ } | ||
if (!pool.connected) { | ||
await pool.connect(); | ||
await pool.connect() | ||
} | ||
return pool; | ||
return pool | ||
} | ||
module.exports = { getPool } | ||
module.exports = { getPool } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
14236
17
143
45
10
1