Skip to content

Commit

Permalink
migrate to drizzle, GL reviewing this shit
Browse files Browse the repository at this point in the history
  • Loading branch information
kahlstrm committed May 15, 2024
1 parent e323e32 commit ad11d3a
Show file tree
Hide file tree
Showing 80 changed files with 4,021 additions and 3,056 deletions.
4 changes: 1 addition & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ PORT=3000

# Database settings

# Choose mysql or postgres
DB_DIALECT=<mysql|postgres>
DB_HOST=localhost
#DB_PORT=<3306|5432>
DB_PORT=5432
DB_USER=ilmo_user
DB_PASSWORD=password
DB_DATABASE=ilmomasiina
Expand Down
104 changes: 49 additions & 55 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
module.exports = {
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": [
parser: "@typescript-eslint/parser",
parserOptions: {
project: [
"packages/ilmomasiina-models/tsconfig.json",
"packages/ilmomasiina-components/tsconfig.json",
"packages/ilmomasiina-frontend/tsconfig.json",
"packages/ilmomasiina-backend/tsconfig.json"
"packages/ilmomasiina-backend/tsconfig.json",
],
"tsconfigRootDir": __dirname,
tsconfigRootDir: __dirname,
// https://github.com/typescript-eslint/typescript-eslint/issues/2094
"EXPERIMENTAL_useSourceOfProjectReferenceRedirect": true
EXPERIMENTAL_useSourceOfProjectReferenceRedirect: true,
},
"ignorePatterns": [
ignorePatterns: [
"**/node_modules/**",
"**/dist/**",
"**/build/**",
".eslintrc.js",
"jest.config.js",
"*.scss",
"*.json"
"*.json",
],
"settings": {
"react": {
"pragma": "React",
"version": "16.12"
settings: {
react: {
pragma: "React",
version: "16.12",
},
},
"extends": [
"airbnb",
"airbnb/hooks",
"airbnb-typescript"
],
"plugins": [
"@typescript-eslint",
"promise",
"simple-import-sort",
"jest"
],
"env": {
"browser": true
extends: ["airbnb", "airbnb/hooks", "airbnb-typescript"],
plugins: ["@typescript-eslint", "promise", "simple-import-sort", "jest"],
env: {
browser: true,
},
"rules": {
"max-len": ["error", 120, 2],
rules: {
"max-len": "off",
"@typescript-eslint/semi": ["error", "always"],
"@typescript-eslint/quotes": ["error", "single"],
// To allow grouping of class members - especially for Models.
Expand All @@ -51,7 +42,7 @@ module.exports = {
// Allow i++ in for loops.
"no-plusplus": ["error", { allowForLoopAfterthoughts: true }],
// We are targeting ES5 or higher.
"radix": ["error", "as-needed"],
radix: ["error", "as-needed"],
// ...I know what I'm doing.
"no-control-regex": "off",
// Not usable with formik.
Expand All @@ -61,53 +52,56 @@ module.exports = {
// Definitely a valid performance concern, but implementing this correctly is
// a giant PITA - the default config ignores arrow functions but they don't solve
// the problem at all, and useCallback is just plain ugly.
"react/jsx-no-bind": "off",
"react/jsx-no-bind": "off", // Note: you must disable the base rule as it can report incorrect errors
"no-shadow": "off",
"@typescript-eslint/no-shadow": "off",
// Add any custom hooks here
"react-hooks/exhaustive-deps": ["error", {
additionalHooks: "useAbortableEffect|useAbortablePromise",
}],
"react-hooks/exhaustive-deps": [
"error",
{
additionalHooks: "useAbortableEffect|useAbortablePromise",
},
],
// Prefer arrow functions to functions expressions, as that's what was done
// when this rule was introduced.
"react/function-component-definition": ["error", {
namedComponents: ["function-declaration", "arrow-function"],
unnamedComponents: "arrow-function",
}],
// Allow dev deps in test files.
"import/no-extraneous-dependencies": ["error", {
devDependencies: [
"**/test/**",
"**/vite.config.ts",
"**/vitest.config.ts",
"**/.eslintrc.js",
"**/rollupMomentPlugin.ts"
],
}],
"react/function-component-definition": [
"error",
{
namedComponents: ["function-declaration", "arrow-function"],
unnamedComponents: "arrow-function",
},
],
// Sort imports: React first, then npm packages, then local files, then CSS.
"simple-import-sort/imports": [
"error",
{
"groups": [
groups: [
["^react$"],
["^@?\\w"],
// Anything that does not start with a dot.
["^[^.]"],
// Anything that starts with a dot, or is from one of our packages.
["^@tietokilta/", "^"],
// Css
["css$"]
]
}
["css$"],
],
},
],
"import/prefer-default-export": "off",
"import/no-extraneous-dependencies": "off",
// Prevent imports from "src/...". VS Code adds these automatically, but they
// break when compiled.
"no-restricted-imports": [
"error",
{
"patterns": [{
group: ["src/*"],
message: "This import will break when compiled by tsc. Use a relative path instead, or \"../src/\" in test files."
}],
patterns: [
{
group: ["src/*"],
message:
'This import will break when compiled by tsc. Use a relative path instead, or "../src/" in test files.',
},
],
},
],
}
},
};
2 changes: 1 addition & 1 deletion packages/ilmomasiina-backend/.eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@
// Used server-side.
"no-console": "off"
},
"ignorePatterns": ["loadTest.js"]
"ignorePatterns": ["loadTest.js", "src/drizzle/**/*.ts"]
}
22 changes: 22 additions & 0 deletions packages/ilmomasiina-backend/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { Config } from 'drizzle-kit';

import envConfig from './src/config';

const config: Config = {
dialect: 'postgresql',
out: './src/drizzle',
schema: './src/drizzle/schema.ts',
dbCredentials: {
host: envConfig.dbHost,
port: envConfig.dbPort ?? 5432,
user: envConfig.dbUser,
password: envConfig.dbPassword,
database: envConfig.dbDatabase,
ssl: envConfig.dbSsl,
},
// Print all statements
verbose: envConfig.debugDbLogging,
// Always ask for confirmation
strict: true,
};
export default config;
8 changes: 3 additions & 5 deletions packages/ilmomasiina-backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"better-npm-run": "^0.1.1",
"debug": "^4.3.4",
"dotenv-flow": "^4.1.0",
"drizzle-orm": "^0.30.10",
"email-templates": "^11.1.1",
"fast-jwt": "^4.0.0",
"fastify": "^4.26.1",
Expand All @@ -68,14 +69,10 @@
"lodash": "^4.17.21",
"moment": "^2.30.1",
"moment-timezone": "^0.5.45",
"mysql2": "^3.9.1",
"node-cron": "^3.0.3",
"nodemailer": "^6.9.10",
"nodemailer-mailgun-transport": "^2.1.5",
"pg": "^8.11.3",
"pg-hstore": "^2.3.4",
"sequelize": "^6.37.1",
"umzug": "^3.7.0"
"postgres": "^3.4.4"
},
"devDependencies": {
"@faker-js/faker": "^8.4.1",
Expand All @@ -90,6 +87,7 @@
"@types/node-cron": "^3.0.11",
"@types/nodemailer": "^6.4.14",
"@types/nodemailer-mailgun-transport": "^1.4.6",
"drizzle-kit": "^0.21.1",
"rimraf": "^5.0.5",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
Expand Down
6 changes: 4 additions & 2 deletions packages/ilmomasiina-backend/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import anonymizeOldSignups from './cron/anonymizeOldSignups';
import deleteOldAuditLogs from './cron/deleteOldAuditLogs';
import deleteUnconfirmedSignups from './cron/deleteUnconfirmedSignups';
import removeDeletedData from './cron/removeDeletedData';
import { runMigrations } from './drizzle/db';
import enforceHTTPS from './enforceHTTPS';
import setupDatabase from './models';
import setupRoutes from './routes';
import { isInitialSetupDone } from './routes/admin/users/createInitialUser';

Expand All @@ -40,7 +40,9 @@ const defaultCompiler = new Ajv({
ajvFormats(defaultCompiler);

export default async function initApp(): Promise<FastifyInstance> {
await setupDatabase();
await runMigrations();

// This command run all migrations from the migrations folder and apply changes to the database

const server = fastify({
trustProxy: config.isAzure || config.trustProxy, // Get IPs from X-Forwarded-For
Expand Down
22 changes: 10 additions & 12 deletions packages/ilmomasiina-backend/src/auditlog/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { FastifyInstance } from 'fastify';
import { Transaction } from 'sequelize';

import type { AuditEvent } from '@tietokilta/ilmomasiina-models';
import { AuditLog } from '../models/auditlog';
import type { Event } from '../models/event';
import type { Signup } from '../models/signup';
import { db, Transaction } from '../drizzle/db';
import { auditlogTable } from '../drizzle/schema';

/**
* Creates an {@link AuditLogger}
Expand All @@ -16,24 +14,24 @@ function eventLogger(ipAddress: string, user?: () => (string | null)) {
return async (
action: AuditEvent,
{
transaction, event, signup, extra,
event, signup, extra, transaction,
}: {
event?: Pick<Event, 'id' | 'title'>,
signup?: Signup,
transaction?: Transaction,
event?: { id: string, title: string },
signup?: { id: string, firstName: string | null, lastName: string | null },
extra?: object,
transaction?: Transaction,
},
) => {
await AuditLog.create({
(transaction ?? db).insert(auditlogTable).values({
user: user ? user() : null,
action,
eventId: event?.id || signup?.quota?.event?.id || null,
eventName: event?.title || signup?.quota?.event?.title || null,
eventId: event?.id || null,
eventName: event?.title || null,
signupId: signup?.id || null,
signupName: signup ? `${signup.firstName} ${signup.lastName}` : null,
extra: extra ? JSON.stringify(extra) : null,
ipAddress,
}, { transaction });
}).execute();
};
}

Expand Down
12 changes: 5 additions & 7 deletions packages/ilmomasiina-backend/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,18 @@ const config = {

/** ClearDB connection string. */
clearDbUrl: envString('CLEARDB_DATABASE_URL', null),
/** `mysql` and `postgres` are supported. */
dbDialect: envString('DB_DIALECT', null),
/** Hostname for the database. */
dbHost: envString('DB_HOST', null),
dbHost: envString('DB_HOST', undefined),
/** Port for the database. */
dbPort: envInteger('DB_PORT', null),
dbPort: envInteger('DB_PORT', 5432),
/** Whether to use SSL for the database. */
dbSsl: envBoolean('DB_SSL', false),
/** Username for the database. */
dbUser: envString('DB_USER', null),
dbUser: envString('DB_USER', undefined),
/** Password for the database. */
dbPassword: envString('DB_PASSWORD', null),
dbPassword: envString('DB_PASSWORD', undefined),
/** Database name. */
dbDatabase: envString('DB_DATABASE', null),
dbDatabase: envString('DB_DATABASE', undefined),
/** Required to run tests, as they reset the test database for every test. */
allowTestsToResetDb: envBoolean('THIS_IS_A_TEST_DB_AND_CAN_BE_WIPED', false),

Expand Down
Loading

0 comments on commit ad11d3a

Please sign in to comment.