Skip to content

Commit eade79c

Browse files
author
Sylvestre
authored
Add MSSQL DB client (#37)
undefined
1 parent 7563330 commit eade79c

15 files changed

+4413
-1089
lines changed

.babelrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"presets": ["@babel/preset-env"]
3+
}

.env.test

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
NODE_ENV=test
2+
MSSQL_CREDENTIALS:Server=mssql,1433;Database=master;User Id=sa;Password=Pass@word;trustServerCertificate=true;
3+
MSSQL_CREDENTIALS_READ_ONLY:Server=mssql,1433;Database=master;User Id=reader;Password=re@derP@ssw0rd;trustServerCertificate=true;

.env.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
export const MSSQL_CREDENTIALS = env("MSSQL_CREDENTIALS");
3+
export const MSSQL_CREDENTIALS_READ_ONLY = env("MSSQL_CREDENTIALS_READ_ONLY");
4+
export const NODE_ENV = env("NODE_ENV");
5+
6+
function env(key, defaultValue) {
7+
const value = process.env[key]; // eslint-disable-line no-process-env
8+
if (value !== undefined) return value;
9+
if (defaultValue !== undefined) return defaultValue;
10+
throw new Error(`Missing environment variable: ${key}`);
11+
}

.mocharc.cjs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = {
2+
color: true,
3+
extension: ["js"],
4+
global: [],
5+
ignore: [],
6+
require: "@babel/register",
7+
spec: ["test/**/*.test.js"],
8+
};

Dockerfile

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
FROM node:16.17.0-alpine
2+
3+
RUN mkdir /app
4+
WORKDIR /app
5+
6+
RUN apk --no-cache add bash
7+
8+
COPY package.json yarn.lock /app/
9+
RUN \
10+
yarn --frozen-lockfile && \
11+
yarn cache clean
12+
13+
ENV PATH="/app/node_modules/.bin:${PATH}"
14+
15+
COPY . /app/
16+
17+
CMD yarn test

data/AdventureWorks2019.bak

8.12 MB
Binary file not shown.

data/seed.mssql.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import mssql from "mssql";
2+
import {MSSQL_CREDENTIALS} from "../.env.test.js";
3+
4+
const credentials = MSSQL_CREDENTIALS;
5+
6+
const seed = async () => {
7+
await mssql.connect(credentials);
8+
9+
await mssql.query`RESTORE DATABASE test
10+
FROM DISK = '/var/opt/mssql/backup/test.bak'
11+
WITH REPLACE, RECOVERY,
12+
MOVE 'AdventureWorksLT2012_Data' TO '/var/opt/mssql/data/aw2019.mdf',
13+
MOVE 'AdventureWorksLT2012_Log' TO '/var/opt/mssql/data/aw2019.ldf';`;
14+
15+
await mssql.query`IF NOT EXISTS(SELECT name
16+
FROM sys.syslogins
17+
WHERE name='reader')
18+
BEGIN
19+
CREATE LOGIN reader WITH PASSWORD = 're@derP@ssw0rd'
20+
CREATE USER reader FOR LOGIN reader
21+
END`;
22+
};
23+
24+
seed()
25+
.then(() => {
26+
console.log(`MS_SQL DB seeded.`);
27+
process.exit(0);
28+
})
29+
.catch((err) => {
30+
console.error(err.message, err);
31+
process.exit(1);
32+
});

docker-compose.local.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
version: "3.7"
2+
3+
services:
4+
mssql:
5+
image: mcr.microsoft.com/azure-sql-edge
6+
expose:
7+
- "1433"

docker-compose.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
version: "3.7"
2+
3+
services:
4+
test:
5+
build: .
6+
depends_on:
7+
- mssql
8+
env_file:
9+
- .env.test
10+
networks:
11+
- db_proxy_test
12+
command: sh -c "set -o pipefail && wait-on -d 10000 -t 30000 tcp:mssql:1433 && node ./data/seed.mssql.js && TZ=UTC NODE_ENV=TEST node_modules/.bin/mocha"
13+
14+
mssql:
15+
image: mcr.microsoft.com/mssql/server:2019-latest
16+
environment:
17+
- MSSQL_SA_PASSWORD=Pass@word
18+
- ACCEPT_EULA=Y
19+
- MSSQL_DATABASE=test
20+
- MSSQL_SLEEP=7
21+
volumes:
22+
- ./data/AdventureWorks2019.bak:/var/opt/mssql/backup/test.bak
23+
ports:
24+
- "1433:1433"
25+
networks:
26+
- db_proxy_test
27+
28+
networks:
29+
db_proxy_test:
30+
name: db_proxy_test

lib/errors.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import {createError} from "micro";
2-
export const unauthorized = error => createError(401, "Unauthorized", error);
3-
export const notFound = error => createError(404, "Not Found", error);
4-
export const exit = message => {
2+
export const unauthorized = (error) => createError(401, "Unauthorized", error);
3+
export const notFound = (error) => createError(404, "Not Found", error);
4+
export const notImplemented = (error) =>
5+
createError(501, "Not Implemented", error);
6+
export const badRequest = (error) =>
7+
createError(400, typeof error === "string" ? error : "Bad request", error);
8+
export const failedCheck = (error) =>
9+
createError(200, typeof error === "string" ? error : "Failed check", error);
10+
export const exit = (message) => {
511
console.error(message); // eslint-disable-line no-console
612
process.exit(1);
713
};

0 commit comments

Comments
 (0)