Skip to content

Commit

Permalink
Change DB naming convention, simplify ORM Config (#344)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinejaussoin committed Mar 3, 2022
1 parent 7dac4e1 commit 7c39cbb
Show file tree
Hide file tree
Showing 25 changed files with 1,126 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/alpha.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: 'Alpha Build'

on:
push:
branches: [v4110/force-dep]
branches: [v4120/db_conventions]

jobs:
build:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ This will run a demo version, which you can turn into a fully licenced version b

## Versions History

### Version 4.12.0 (unreleased)

- Changing naming convention for the database. All fields and tables are now `snake_case`.
- Simplified the configuration of TypeORM, removed the generation of `ormconfig.json`.

### Version 4.11.5 (hotfix)

- Making secure cookies an optional setting, as they won't work unless it is hosted on HTTPS.
Expand Down
3 changes: 2 additions & 1 deletion backend/.dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules
npm-debug.log
persist
Dockerfile*
dist
dist
yarn-error.log
1 change: 1 addition & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ RUN yarn --network-timeout 1000000 install
COPY ./ ./

RUN yarn build
RUN rm -rf ./src

EXPOSE ${BACKEND_PORT}
CMD [ "yarn", "backend-production" ]
2 changes: 2 additions & 0 deletions backend/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
build:
docker build -t retrospected/backend:local .
22 changes: 9 additions & 13 deletions backend/package.json
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
{
"name": "@retrospected/backend",
"version": "4.11.5",
"version": "4.12.0",
"license": "GNU GPLv3",
"private": true,
"scripts": {
"build": "rimraf dist && tsc --build",
"start": "nodemon --exec 'yarn fix & ts-node' --files ./src/index.ts",
"backend-production": "yarn migrate && cd ./dist/src && node index.js",
"create-migration": "ts-node ./src/init.ts --ts && ts-node ./node_modules/typeorm/cli.js migration:generate -n ",
"migrate": "node ./dist/src/init.js && node ./node_modules/typeorm/cli.js migration:run",
"revert": "node ./dist/src/init.js && node ./node_modules/typeorm/cli.js migration:revert",
"create-migration": "ts-node ./node_modules/typeorm/cli.js --config src/db/orm-config.ts migration:generate -n ",
"migrate": "ts-node ./node_modules/typeorm/cli.js --config src/db/orm-config.ts migration:run",
"revert": "ts-node ./node_modules/typeorm/cli.js --config src/db/orm-config.ts migration:revert",
"lint": "eslint 'src/**/*.ts'",
"test": "yarn jest",
"ci-test": "CI=true yarn test",
"fix": "eslint 'src/**/*.ts' --fix"
"fix": "eslint 'src/**/*.ts' --fix",
"backend-production": "yarn migrate-production && cd ./dist/src && node index.js",
"migrate-production": "node ./node_modules/typeorm/cli.js --config dist/src/db/orm-config.js migration:run"
},
"dependencies": {
"@sendgrid/mail": "7.6.0",
Expand All @@ -25,11 +26,9 @@
"@types/express": "4.17.13",
"@types/express-mung": "0.5.2",
"@types/express-rate-limit": "6.0.0",
"@types/express-serve-static-core": "4.17.28",
"@types/express-session": "1.17.4",
"@types/jest": "27.4.0",
"@types/lodash": "4.14.178",
"@types/md5": "2.3.1",
"@types/node": "17.0.10",
"@types/node-fetch": "2.5.12",
"@types/passport": "1.0.7",
Expand All @@ -44,10 +43,8 @@
"@typescript-eslint/eslint-plugin": "5.10.0",
"@typescript-eslint/parser": "5.10.0",
"bcryptjs": "2.4.3",
"body-parser": "1.19.1",
"chalk": "4.1.2",
"connect-redis": "6.0.0",
"cross-env": "7.0.3",
"crypto-js": "4.1.1",
"csurf": "1.11.0",
"date-fns": "2.28.0",
Expand All @@ -63,7 +60,6 @@
"jest": "27.4.7",
"lexorank": "1.0.4",
"lodash": "4.17.21",
"md5": "2.3.0",
"moment": "2.29.1",
"node-fetch": "2.6.1",
"nodemon": "2.0.15",
Expand All @@ -87,9 +83,9 @@
"ts-jest": "27.1.3",
"ts-node": "10.4.0",
"typeorm": "0.2.41",
"typeorm-naming-strategies": "^2.0.0",
"typescript": "4.5.4",
"uuid": "8.3.2",
"yargs": "17.3.1"
"uuid": "8.3.2"
},
"resolutions": {
"@types/connect-redis": "0.0.18",
Expand Down
46 changes: 0 additions & 46 deletions backend/src/build-config.ts

This file was deleted.

26 changes: 13 additions & 13 deletions backend/src/db/actions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ async function deleteVisits(
anon: UserIdentityEntity
) {
if (hardDelete) {
await manager.query('delete from visitors where "usersId" = $1', [user.id]);
await manager.query('delete from visitors where users_id = $1', [user.id]);
} else {
await manager.query(
'update visitors set "usersId" = $1 where "usersId" = $2',
'update visitors set users_id = $1 where users_id = $2',
[anon.user.id, user.id]
);
}
Expand Down Expand Up @@ -78,13 +78,13 @@ async function deletePosts(
if (hardDelete) {
await manager.query(
`
delete from votes where "postId" in (select id from posts where "userId" = $1)
delete from votes where post_id in (select id from posts where user_id = $1)
`,
[user.id]
);
await manager.query(
`
update posts set "groupId" = null where "groupId" in (select id from groups where "userId" = $1)
update posts set group_id = null where group_id in (select id from groups where user_id = $1)
`,
[user.id]
);
Expand All @@ -108,25 +108,25 @@ async function deleteSessions(
if (hardDelete) {
await manager.query(
`
delete from votes where "postId" in (select id from posts where "sessionId" in (select id from sessions where "createdById" = $1))
delete from votes where post_id in (select id from posts where session_id in (select id from sessions where created_by_id = $1))
`,
[user.id]
);
await manager.query(
`
delete from posts where "sessionId" in (select id from sessions where "createdById" = $1)
delete from posts where session_id in (select id from sessions where created_by_id = $1)
`,
[user.id]
);
await manager.query(
`
delete from groups where "sessionId" in (select id from sessions where "createdById" = $1)
delete from groups where session_id in (select id from sessions where created_by_id = $1)
`,
[user.id]
);
await manager.query(
`
delete from columns where "sessionId" in (select id from sessions where "createdById" = $1)
delete from columns where session_id in (select id from sessions where created_by_id = $1)
`,
[user.id]
);
Expand All @@ -141,21 +141,21 @@ async function deleteSessions(
async function deleteUserAccount(manager: EntityManager, user: UserView) {
await manager.query(
`
update users set "defaultTemplateId" = null where "defaultTemplateId" in (select id from templates where "createdById" = $1)
update users set default_template_id = null where default_template_id in (select id from templates where created_by_id = $1)
`,
[user.id]
);
await manager.query(
'delete from "templates-columns" where "templateId" in (select id from templates where "createdById" = $1)',
'delete from templates_columns where template_id in (select id from templates where created_by_id = $1)',
[user.id]
);
await manager.query('delete from templates where "createdById" = $1', [
await manager.query('delete from templates where created_by_id = $1', [
user.id,
]);
await manager.query('delete from subscriptions where "ownerId" = $1', [
await manager.query('delete from subscriptions where owner_id = $1', [
user.id,
]);
await manager.query('delete from users_identities where "userId" = $1', [
await manager.query('delete from users_identities where user_id = $1', [
user.id,
]);
await manager.query('delete from users where id = $1', [user.id]);
Expand Down
19 changes: 9 additions & 10 deletions backend/src/db/actions/sessions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,20 +227,19 @@ export async function deleteSessions(
return false;
}
await sessionRepository.query(
`delete from visitors where "sessionsId" = $1;`,
`delete from visitors where sessions_id = $1;`,
[sessionId]
);
await sessionRepository.query(`delete from posts where "sessionId" = $1;`, [
await sessionRepository.query(`delete from posts where session_id = $1;`, [
sessionId,
]);
await sessionRepository.query(
`delete from columns where "sessionId" = $1;`,
[sessionId]
);
await sessionRepository.query(
`delete from groups where "sessionId" = $1;`,
`delete from columns where session_id = $1;`,
[sessionId]
);
await sessionRepository.query(`delete from groups where session_id = $1;`, [
sessionId,
]);
await sessionRepository.query(`delete from sessions where id = $1;`, [
sessionId,
]);
Expand All @@ -255,16 +254,16 @@ export async function previousSessions(
return await transaction(async (manager) => {
const sessionsAsVisitors: { sessionsId: string }[] = await manager.query(
`
select distinct v."sessionsId" from visitors v
where v."usersId" = $1
select distinct v.sessions_id from visitors v
where v.users_id = $1
`,
[userId]
);

const sessionsAsOwner: { id: string }[] = await manager.query(
`
select s.id from sessions s
where s."createdById" = $1
where s.created_by_id = $1
`,
[userId]
);
Expand Down
5 changes: 4 additions & 1 deletion backend/src/db/entities/ColumnDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
ManyToOne,
CreateDateColumn,
UpdateDateColumn,
Index,
} from 'typeorm';
import { IconName, ColumnDefinition, ColumnDefinitionType } from '../../common';
import SessionEntity from './Session';
Expand Down Expand Up @@ -59,6 +60,7 @@ class ColumnDefinitionEntityBase {
@Entity({ name: 'columns' })
export class ColumnDefinitionEntity extends ColumnDefinitionEntityBase {
@ManyToOne(() => SessionEntity, { nullable: false })
@Index()
public session: SessionEntity;
constructor(
id: string,
Expand All @@ -74,9 +76,10 @@ export class ColumnDefinitionEntity extends ColumnDefinitionEntityBase {
}
}

@Entity({ name: 'templates-columns' })
@Entity({ name: 'templates_columns' })
export class TemplateColumnDefinitionEntity extends ColumnDefinitionEntityBase {
@ManyToOne(() => SessionTemplateEntity, { nullable: false })
@Index()
public template: SessionTemplateEntity;
constructor(
id: string,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/db/entities/Licence.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import {
PrimaryColumn,
CreateDateColumn,
UpdateDateColumn,
Index,
} from 'typeorm';

@Entity({ name: 'licences' })
export default class LicenceEntity {
@PrimaryColumn({ primary: true, generated: false, unique: true })
public id: string;
@Column({ nullable: true, type: 'character varying' })
@Index()
public email: string | null;
@Column({ nullable: false })
public key: string;
Expand Down
20 changes: 10 additions & 10 deletions backend/src/db/entities/SessionView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,28 @@ select
(
select to_jsonb(cb) from (
select cbu.id, cbu.name, cbu.photo from users cbu
where cbu.id = s."createdById"
where cbu.id = s.created_by_id
) as cb
) as "createdBy",
) as created_by,
s.encrypted,
s.locked,
(select count(*) from posts p where p."sessionId" = s.id and p.action is not null) as "numberOfActions",
(select count(*) from posts p where p."sessionId" = s.id) as "numberOfPosts",
(select count(*) from posts p where p.session_id = s.id and p.action is not null) as "number_of_actions",
(select count(*) from posts p where p.session_id = s.id) as "number_of_posts",
(
select count(*) from votes vv
left join posts vp on vp.id = vv."postId"
where vp."sessionId" = s.id
) as "numberOfVotes",
left join posts vp on vp.id = vv.post_id
where vp.session_id = s.id
) as "number_of_votes",
(
select json_agg(vis) from (
select vu.id, vu.name, vu.photo from visitors v
join users vu on vu.id = v."usersId"
where v."sessionsId" = s.id
join users vu on vu.id = v.users_id
where v.sessions_id = s.id
) as vis
) as participants
from sessions s
left join users u on s."createdById" = u.id
left join users u on s.created_by_id = u.id
order by s.updated desc
`,
Expand Down
Loading

0 comments on commit 7c39cbb

Please sign in to comment.