Skip to content

Commit

Permalink
Move to ts (#25)
Browse files Browse the repository at this point in the history
* Update several components and backend setup

Several frontend components were updated to fix minor spelling issues and import order. The backend node script has been updated to use TypeScript files instead of JavaScript. The backend package.json has been updated with newer dependencies and included build script. Some string literals were also changed in the backend server to lower case.

* Update Docker and package configurations for TypeScript

This update focuses on enhancing TypeScript compatibility by introducing a new dev dependency "ts-node" for better TypeScript runtime support. The "type" field is added to package.json making the project a module, and a new command "dev:ts" has been added to allow running the project using ts-node. A "built" exception is placed in .dockerignore. Command for running the development server inside Docker was adjusted accordingly.

* Update build configuration and ignore files

Updated Docker and ignore files to better handle typescript build files, and switched to SWC for faster compilation. The changes include updating the output directory from 'built' to 'dist', adding SWC as a dev dependency, and adding 'dist' to the list of docker ignored paths.

* Delete all database migration files

This commit completely removes all database migration files in the backend/src/db/migrations directory. The files include migrations for creating tables such as "projects", "users", "boards", "issue_statuses", and more. It seems like some foundational changes are planned for the structure of the backend database necessitating the removal of all these migrations.

* Updating .gitignore

* Update backend configurations and fix issues in various files

This commit includes several updates across multiple files for the backend, including revising import paths, updating build and execution scripts, and refining TypeScript configuration. The primary focus has been improving compatibility between files and making sure necessary configurations are appropriately updated.

* Delete all database migration files

This commit completely removes all database migration files in the backend/src/db/migrations directory. The files include migrations for creating tables such as "projects", "users", "boards", "issue_statuses", and more. It seems like some foundational changes are planned for the structure of the backend database necessitating the removal of all these migrations.

* Refactor backend code and remove docker-compose-clay.yml

The `docker-compose-clay.yml` file was removed as it's no longer needed. The backend code on `index.ts` was refactored, moving several interface definitions to a new typings file at `backend/src/typings/index.ts`. A minor adjustment was also made to handling multipart requests. A `pnpm-lock.yaml` file was created for project dependencies.

* Update backend codebase, introduce Fastify handlers

The update includes major restructuring in the backend codebase, primarily focused on introducing individual Fastify handlers for different server functionalities. Also, backend typings and model definitions have been moved to a different file. Dependencies and scripts in package.json have been updated, and new files fastify-ws.ts and hocuspocus.ts have been added. Finally, the Dockerfile.local and .tool-versions files have been adjusted accordingly.

* Refactor backend database models with enhanced Sequelize typings

This code revision refactors the database models on the backend. The refactoring process involves enhancing and integrating Sequelize TypeScript typings, replacing 'DataTypes' with more specific Sequelize model types, and improving the correct typing of certain variables and return types. This will for sure facilitate scaling and ongoing development by boosting type safety, understanding, and editor-aided inspections of the models.

* Add MinioGetObjectResponse type and update file handling

We have introduced a new type, MinioGetObjectResponse, in the server index for better data structure comprehension. This change reflects an update in the way we handle files in the '/uploads/:file' route, removing the previously needed '@ts-ignore' and enhancing TypeScript integration. This should streamline file processing in the backend and improve overall code clarity.

* Refactor CORS configuration in server index

The CORS settings in the server index file have been refactored. The previous CORS settings relied on hard-coded conditions, this update introduces the usage of a separate handler function for setting up CORS OPTIONS. This will ensure modularity and improves the flexibility of the CORS configuration.

* Refactor import and adjust request handling in server

The User import was updated in apollo.ts to reflect its new location. In index.ts, the Fastify request import was expanded to enable a fleshed out request handler for 'multipart' content types. In fastify-ws.ts, the handling of 'multipart' requests was refined and moved to occur earlier in the preValidation hook. This helps ensure that multipart requests, notably for file uploads, are processed properly across the application.

* Add caching to handle user authentication

A new caching mechanism has been introduced using NodeCache to handle user authentication and reduce database queries. The changes refactored the authentication process in the backend by moving the user authentication from Apollo and Fastify-WS to Fastify hooks. This change facilitates more efficient request handling in the server, improving performance. Furthermore, a new utility function, `hash`, is added to aid in creating cache keys.

* Refactor hocuspocus-server.ts and add caching

The code refactor primarily targets improving readability by breaking the server configuration into discrete functions. Additionally, a cache mechanism has been introduced for token verification process. This adjustment aims to boost performance by reducing redundant requests to JWT verification API whenever a valid token is presented.

* Update fetch and store methods in hocuspocus-server

Added type annotations to 'fetch' and 'store' methods in hocuspocus-server.ts. Additionally, simplified a condition check in 'store' method for 'issueComment' entityType.

* Refactor hocuspocus-server.ts for better typing and exception handling

The code updates in hocuspocus-server.ts include the usage of types for better data handling and enhanced exception handling. It also includes changing the way JWT is verified with the addition of the VerifyJwtResponse type for more precise data extraction. Improvements finalize on refining the user fetching process from the database using the User type.

* Refactor Backend DB Models and Adjust Resolvers

Optimized Sequelize usage by using the `init` function instead of `define` in backend database models. This improvement led to more strict TypeScript typings, which increases code stability and makes development easier. Naming conventions were also adjusted for better semantic clarity. Manual updates on model associations and import paths were done accordingly. Resolvers were then adjusted to match the updated models.

* Refactor Backend DB Models and Adjust Resolvers

Optimized Sequelize usage by using the `init` function instead of `define` in backend database models. This improvement led to more strict TypeScript typings, which increases code stability and makes development easier. Naming conventions were also adjusted for better semantic clarity. Manual updates on model associations and import paths were done accordingly. Resolvers were then adjusted to match the updated models.

* updating

* Update ESLint config file reference for backend

The ESLint configuration file for the backend has been renamed from .eslintrc to .eslintrc.json. Consequently, the reference to this file in .lintstagedrc has been updated to reflect this change.

* Update optional properties to CreationOptional

Several properties across different models in `backend/src/db/models/types.ts` have been updated to use `CreationOptional`. This change provides better clarity that these properties can be undefined during instance creation. Additionally, the build script in `backend/package.json` has been simplified and the unnecessary `--resolve-extensions=.js` flag removed.

* Add GraphQL code generation configuration and generated types

This commit introduces a new `codegen.ts` file for configuring GraphQL code generation using `@graphql-codegen/cli`. Besides, it includes a large set of generated TypeScript types for existing GraphQL queries, mutations, and input types. This supports better type safety and autocompletion when working with GraphQL on the backend.

* Update package versions in pnpm-lock.yaml

The versions of '@graphql-codegen/cli', '@graphql-codegen/typescript', '@graphql-codegen/typescript-resolvers', and '@babel/traverse' have been fixed and updated in the pnpm-lock.yaml. The changes remove the range (caret) from the version specification, which ensures that the exact listed versions are used, improving the reliability of the build process.

* Update import statements and enhance linting

The code has been refactored to enhance TypeScript type safety by changing import statements to import type where applicable. Linting scripts have been updated with the integration of 'oxlint' and 'eslint'. This leads to improved code quality and adherence to coding standards. Additionally, the 'hocuspocus-server.ts' file has been refactored to improve the overall readability of the code.

* Add '@types/uuid' to backend dependencies

The type definitions for 'uuid' have been added to the project's backend dependencies. This addition is visible in both package.json and pnpm-lock.yaml files, ensuring consistency and addressing possible type checking needs for 'uuid' in the backend.

* Remove unused dependencies from pnpm-lock.yaml

The 'module-alias', 'sharp', and '@types/module-alias' dependencies along with their related entries have been deleted from the pnpm-lock.yaml file. This commit also includes the removal of several additional subdependencies, improving the project's overall dependency management and reducing the install size.

* Remove resolver types file

The file 'backend/src/__generated__/resolvers-types.ts' was deleted. This file was auto-generated and used types definitions for GraphQL resolvers. Its removal suggests a clean up or a change in the approach for handling schema and type definitions.

* Update backend dependencies and refactor resolvers

Dependencies for the backend have been updated with the addition of "dataloader-sequelize". Also, the code for resolving graphql queries in the backend has been refactored to improve readability and maintainability. This includes the use of database calls for fetching data and using a custom "Date" scalar for date values in the graphql schema.

* Refactor resolvers and types with Date to DateTime scalar

This update changes 'Date' to 'DateTime' across GraphQL resolvers and generated types. The code now utilizes 'graphql-scalars' for the DateTime scalar implementation, resulting in improved clarity and consistency. The 'Date' scalar used previously was manually crafted hence changed for better clarity and functionality. Several query and mutation resolvers are updated to accommodate this change.

* Convert IDs to numbers across multiple resolvers

The identifiers used in project/index, issue-comment/index, board/index and user/index resolvers have been converted to numbers. This change aids in uniformity and prevents potential type-related bugs. Resolvers are also updated to use DataLoader for caching, reducing the number of database queries executing a significant performance enhancement.

* Import and use AssetModel in user resolver

AssetModel is now being imported into the user resolver in the backend. This has been used to properly type the variable findOldAvatarAsset. This will ensure more accurate and safer type-checking within the resolvers.

* Refactor data mapping and date handling in resolvers and frontend components

This commit focuses on two main changes to the codebase. First, it streamlines the way data is mapped from database queries in resolver functions - unnecessary array spreads are removed, simplifying the array returns. Secondly, it modifies how dates are handled in a number of frontend components to use 'DateTime.fromISO' instead of 'DateTime.fromMillis', reflecting the way dates are stored and returned from the backend.

* Refactor resolver logic in multiple files

Removed 'include' query parameter from the project resolver to simplify the database query. Updated the issue resolver to handle issueId as a Number type to ensure correct issue-board relationship. Modified the board resolver to fetch issues from viewState and handle data accordingly. These changes help improve code maintainability and consistency.

* Extend MyWebSocket interface and refactor usage

The MyWebSocket interface has been extended by adding 'id', 'user', and 'namespace' optional properties. Following this extension, the way connection sockets are handled in the 'wsServer.handleConnection' method was also refactored. Instead of directly injecting the properties into the socket, a separate handleConnectionSocket object was created, which then gets passed to the method.

* Refactor websocket handler in Fastify server

The handshake connection and message handling is now encapsulated within a separate `wsPlugin` function. This streamlines the main `fastifyWsHandler` function by allowing it to focus on fastify-specific configuration and registration tasks, improving code readability and maintainability.
  • Loading branch information
claygorman authored Jan 12, 2024
1 parent a19e592 commit 27ea86a
Show file tree
Hide file tree
Showing 105 changed files with 8,899 additions and 2,075 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,8 @@ assets/

/frontend/.pnpm-store/
/backend/.pnpm-store/
.history
.history
/backend/dist/

docker-compose-dev.yml
/backend/.env.local
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env sh
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
7 changes: 7 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"backend/**/*.ts": [
"backend/node_modules/.bin/eslint --config backend/.eslintrc.json",
"backend/node_modules/.bin/oxlint --config backend/.eslintrc.json"
],
"frontend/**/*.{ts,tsx}": "frontend/node_modules/.bin/eslint --config frontend/.eslintrc.json",
}
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"singleQuote": true,
"overrides": [
{
"files": ["backend/**/*.js"],
"files": ["backend/**/*.ts"],
"options": {
"printWidth": 120,
"importOrder": ["^[./]"],
Expand Down
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
nodejs 20.9.0
nodejs 20.10.0
2 changes: 2 additions & 0 deletions backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,5 @@ node_modules
.vercel
next-env.d.ts
.pnpm-store
dist
built
12 changes: 12 additions & 0 deletions backend/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 2022,
"sourceType": "module"
},
"plugins": ["import", "oxlint"],
"rules": {
"import/extensions": ["error", "always"]
},
"ignorePatterns": ["dist/", "node_modules/"]
}
10 changes: 4 additions & 6 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@ RUN apk add --no-cache libc6-compat

FROM base AS build
WORKDIR /app

ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache g++ make python3 py3-pip && ln -sf python3 /usr/bin/python
# pnpm fetch does require only lockfile
COPY pnpm-lock.yaml ./
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm fetch --prod
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm fetch
COPY . ./
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install -r --offline
RUN pnpm run build
RUN --mount=type=cache,id=pnpm,target=/pnpm/store pnpm install -r --offline --prod


FROM base
WORKDIR /app

COPY --from=build /app /app

RUN npm install pm2 -g

EXPOSE 8080

CMD ["./start.sh"]
2 changes: 1 addition & 1 deletion backend/Dockerfile.local
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ CMD "pnpm install --frozen-lockfile -f"

EXPOSE 8080

CMD sh -c "pnpm migrate:up && pnpm dev"
CMD sh -c "pnpm tsx src/umzug-runner.ts up && pnpm dev"
36 changes: 36 additions & 0 deletions backend/codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
overwrite: true,
schema: 'http://localhost:8080/graphql',
generates: {
'src/__generated__/resolvers-types.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
allowParentTypeOverride: true,
defaultMapper: 'Partial<{T}>',
},
},
},
hooks: {
afterOneFileWrite: [
'sed -i \'\' -e \'1s|import { GraphQLResolveInfo, GraphQLScalarType, GraphQLScalarTypeConfig } from "graphql";|import { type GraphQLResolveInfo, GraphQLScalarType, type GraphQLScalarTypeConfig } from "graphql";|\' src/__generated__/resolvers-types.ts',
],
},
config: {
useIndexSignature: true,
contextType: '../server/apollo.js#ApolloContext',
mappers: {
// Project: '../db/models/types.js#Project as ProjectModel',
// ProjectTag: '../db/models/types.js#ProjectTag as ProjectTagModel',
// CustomField: '../db/models/types.js#ProjectCustomField as ProjectCustomFieldModel',
// User: '../db/models/types.js#User as UserModel',
// Issue: '../db/models/types.js#Issue as IssueModel',
// IssueComment: '../db/models/types.js#IssueComment as IssueCommentModel',
// Board: '../db/models/types.js#Board as BoardModel',
// IssueStatus: '../db/models/types.js#IssueStatus as IssueStatusModel',
},
},
};

export default config;
42 changes: 34 additions & 8 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,83 @@
"name": "backend",
"version": "0.1.0",
"private": true,
"module": "src/index.js",
"type": "module",
"engines": {
"node": ">=20.6.0",
"pnpm": ">=8.7.0"
},
"scripts": {
"dev": "node --env-file .env --watch src/index.js",
"start": "node src/index.js",
"dev": "tsx watch src/index.ts",
"lint": "pnpm oxlint && pnpm eslint && tsc --noEmit",
"start": "node dist",
"format": "prettier --check --ignore-path ../.gitignore .",
"format:fix": "prettier --write --ignore-path ../.gitignore .",
"migrate:up": "node src/umzug-runner.js up"
"build": "esbuild `find src \\( -name '*.ts' \\)` --platform=node --outdir=dist",
"check:types": "tsc --noEmit",
"pretest": "pnpm run build",
"codegen": "graphql-codegen --config codegen.ts"
},
"dependencies": {
"@apollo/server": "^4.9.5",
"@as-integrations/fastify": "^2.1.1",
"@fastify/cors": "^8.4.1",
"@fastify/websocket": "^8.3.0",
"@fastify/websocket": "^8.3.1",
"@graphql-tools/merge": "^9.0.1",
"@hocuspocus/extension-database": "^2.8.1",
"@hocuspocus/server": "^2.8.1",
"axios": "^1.6.0",
"fastify": "^4.24.3",
"dataloader-sequelize": "^2.3.3",
"fastify": "^4.25.2",
"fastify-graceful-shutdown": "^3.5.1",
"graphql": "^16.8.1",
"graphql-scalars": "^1.22.4",
"graphql-tag": "^2.12.6",
"graphql-upload": "^16.0.2",
"lodash-es": "^4.17.21",
"minio": "^7.1.3",
"nanoid": "^5.0.4",
"node-cache": "^5.1.2",
"pg": "^8.11.3",
"pg-hstore": "^2.3.4",
"sequelize": "^6.34.0",
"sharp": "^0.32.6",
"sinon-chai": "^3.7.0",
"tsconfig-paths": "^4.2.0",
"umzug": "^3.4.0",
"uuid": "^9.0.1",
"y-protocols": "^1.0.6",
"yjs": "^13.6.10",
"yn": "^5.0.0"
},
"devDependencies": {
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/typescript": "4.0.1",
"@graphql-codegen/typescript-resolvers": "4.0.1",
"@swc/cli": "^0.1.63",
"@swc/core": "^1.3.102",
"@swc/helpers": "^0.5.3",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.10.6",
"@types/uuid": "^9.0.7",
"@types/ws": "^8.5.10",
"@typescript-eslint/parser": "^6.18.0",
"chai": "^4.3.10",
"esbuild": "^0.19.11",
"eslint": "^8.56.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-oxlint": "^0.1.0",
"husky": "^8.0.3",
"lint-staged": "^15.2.0",
"mocha": "^10.2.0",
"prettier": "^3.0.3",
"oxlint": "^0.1.2",
"prettier": "^3.1.1",
"proxyquire": "^2.1.3",
"sequelize-cli": "^6.6.2",
"sequelize-test-helpers": "^1.4.3",
"sinon": "^17.0.1",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"tsx": "^4.7.0",
"typescript": "^5.3.3"
},
"optionalDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion backend/pm2.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"apps": [
{
"name": "api",
"script": "src/index.js",
"script": "dist/index.js",
"instances": "max",
"exec_mode": "cluster",
"interpreter": "/bin/sh",
Expand Down
Loading

0 comments on commit 27ea86a

Please sign in to comment.