Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update typeorm #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
# production
**/build
**/npm-debug.log*
.idea
6,810 changes: 6,367 additions & 443 deletions api/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
"jsonwebtoken": "^8.5.1",
"lodash": "^4.17.15",
"module-alias": "^2.2.2",
"pg": "^7.14.0",
"pg": "^8.7.3",
"reflect-metadata": "^0.1.13",
"striptags": "^3.1.1",
"typeorm": "^0.2.20"
"typeorm": "^0.3.7"
},
"devDependencies": {
"@types/cors": "^2.8.6",
Expand All @@ -43,7 +43,7 @@
"lint-staged": "^9.4.3",
"nodemon": "^2.0.0",
"prettier": "^1.19.1",
"ts-node": "^8.5.2",
"ts-node": "^10.8.2",
"tsconfig-paths": "^3.9.0",
"typescript": "^3.7.2"
},
Expand Down
5 changes: 3 additions & 2 deletions api/src/controllers/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const getProjectIssues = catchErrors(async (req, res) => {
});

export const getIssueWithUsersAndComments = catchErrors(async (req, res) => {
const issue = await findEntityOrThrow(Issue, req.params.issueId, {
const issue = await findEntityOrThrow(Issue, {
where: { id: req.params.issueId },
relations: ['users', 'comments', 'comments.user'],
});
res.respond({ issue });
Expand All @@ -44,7 +45,7 @@ export const remove = catchErrors(async (req, res) => {
});

const calculateListPosition = async ({ projectId, status }: Issue): Promise<number> => {
const issues = await Issue.find({ projectId, status });
const issues = await Issue.find({ select: { id: projectId, status } });

const listPositions = issues.map(({ listPosition }) => listPosition);

Expand Down
5 changes: 4 additions & 1 deletion api/src/controllers/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { findEntityOrThrow, updateEntity } from 'utils/typeorm';
import { issuePartial } from 'serializers/issues';

export const getProjectWithUsersAndIssues = catchErrors(async (req, res) => {
const project = await findEntityOrThrow(Project, req.currentUser.projectId, {
const project = await findEntityOrThrow(Project, {
where: {
id: req.currentUser.projectId,
},
relations: ['users', 'issues'],
});
res.respond({
Expand Down
8 changes: 4 additions & 4 deletions api/src/database/createConnection.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createConnection, Connection } from 'typeorm';
import { DataSource } from 'typeorm';

import * as entities from 'entities';

const createDatabaseConnection = (): Promise<Connection> =>
createConnection({
const createDatabaseConnection = (): Promise<DataSource> =>
new DataSource({
type: 'postgres',
host: process.env.DB_HOST,
port: Number(process.env.DB_PORT),
Expand All @@ -12,6 +12,6 @@ const createDatabaseConnection = (): Promise<Connection> =>
database: process.env.DB_DATABASE,
entities: Object.values(entities),
synchronize: true,
});
}).initialize();

export default createDatabaseConnection;
4 changes: 3 additions & 1 deletion api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ const initializeExpress = (): void => {
app.use((req, _res, next) => next(new RouteNotFoundError(req.originalUrl)));
app.use(handleError);

app.listen(process.env.PORT || 3000);
app.listen(process.env.PORT || 3000, () => {
console.log('Server start');
});
};

const initializeApp = async (): Promise<void> => {
Expand Down
2 changes: 1 addition & 1 deletion api/src/middleware/authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const authenticateUser = catchErrors(async (req, _res, next) => {
if (!userId) {
throw new InvalidTokenError('Authentication token is invalid.');
}
const user = await User.findOne(userId);
const user = await User.findOne({ where: { id: userId } });
if (!user) {
throw new InvalidTokenError('Authentication token is invalid: User not found.');
}
Expand Down
9 changes: 4 additions & 5 deletions api/src/utils/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ const entities: { [key: string]: EntityConstructor } = { Comment, Issue, Project

export const findEntityOrThrow = async <T extends EntityConstructor>(
Constructor: T,
id: number | string,
options?: FindOneOptions,
options: FindOneOptions,
): Promise<InstanceType<T>> => {
const instance = await Constructor.findOne(id, options);
const instance = await Constructor.findOne(options);
if (!instance) {
throw new EntityNotFoundError(Constructor.name);
}
Expand Down Expand Up @@ -47,7 +46,7 @@ export const updateEntity = async <T extends EntityConstructor>(
id: number | string,
input: Partial<InstanceType<T>>,
): Promise<InstanceType<T>> => {
const instance = await findEntityOrThrow(Constructor, id);
const instance = await findEntityOrThrow(Constructor, { where: { id } });
Object.assign(instance, input);
return validateAndSaveEntity(instance);
};
Expand All @@ -56,7 +55,7 @@ export const deleteEntity = async <T extends EntityConstructor>(
Constructor: T,
id: number | string,
): Promise<InstanceType<T>> => {
const instance = await findEntityOrThrow(Constructor, id);
const instance = await findEntityOrThrow(Constructor, { where: { id } });
await instance.remove();
return instance;
};