Skip to content

Commit

Permalink
Migration to latest version of TypeOrm (#447)
Browse files Browse the repository at this point in the history
  • Loading branch information
antoinejaussoin authored Dec 27, 2022
1 parent d7051a6 commit e39cecd
Show file tree
Hide file tree
Showing 31 changed files with 329 additions and 315 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: [v4180/deps2]
branches: [v4190/typeorm]

jobs:
build:
Expand Down
17 changes: 9 additions & 8 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
"scripts": {
"build": "rimraf dist && tsc --build && yarn copy-templates",
"start": "nodemon --exec 'yarn fix & ts-node' --files ./src/index.ts",
"create-migration": "ts-node ./node_modules/typeorm/cli.js --config src/db/orm-config.ts migration:generate -n ",
"create-empty-migration": "ts-node ./node_modules/typeorm/cli.js --config src/db/orm-config.ts migration:create -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",
"create-migration": "scripty",
"create-empty-migration": "scripty",
"migrate": "typeorm-ts-node-commonjs -d src/db/index.ts migration:run",
"revert": "typeorm-ts-node-commonjs -d src/db/index.ts migration:revert",
"lint": "eslint 'src/**/*.ts'",
"test": "yarn jest",
"ci-test": "CI=true yarn test",
"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",
"migrate-production": "typeorm -d dist/src/db/index.js migration:run",
"copy-templates": "copyfiles -u 0 src/**/*.html dist/"
},
"dependencies": {
Expand Down Expand Up @@ -80,15 +80,16 @@
"rate-limiter-flexible": "2.4.1",
"redis": "3.1.2",
"rimraf": "3.0.2",
"scripty": "^2.1.1",
"shortid": "2.2.16",
"socket.io": "4.5.4",
"socket.io-redis": "6.1.1",
"stripe": "11.1.0",
"ts-jest": "29.0.3",
"ts-node": "10.9.1",
"typeorm": "0.2.45",
"typeorm-naming-strategies": "3.0.0",
"typescript": "4.9.3",
"typeorm": "0.3.11",
"typeorm-naming-strategies": "4.1.0",
"typescript": "4.9.4",
"uuid": "9.0.0"
},
"resolutions": {
Expand Down
2 changes: 2 additions & 0 deletions backend/scripts/create-empty-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
./node_modules/.bin/typeorm-ts-node-commonjs migration:create src/db/migrations/$1
2 changes: 2 additions & 0 deletions backend/scripts/create-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
./node_modules/.bin/typeorm-ts-node-commonjs -d src/db/index.ts migration:generate src/db/migrations/$1
2 changes: 1 addition & 1 deletion backend/src/db/actions/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function saveChatMessage(
message: Message
): Promise<Message | null> {
return await transaction(async (manager) => {
const postRepository = manager.getCustomRepository(MessageRepository);
const postRepository = manager.withRepository(MessageRepository);
const entity = await postRepository.saveFromJson(
sessionId,
userId,
Expand Down
8 changes: 4 additions & 4 deletions backend/src/db/actions/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ async function delVotes(
user: UserView,
anon: UserIdentityEntity
) {
const repo = manager.getCustomRepository(VoteRepository);
const repo = manager.withRepository(VoteRepository);
if (hardDelete) {
await repo.delete({ user: { id: user.id } });
return true;
Expand All @@ -85,8 +85,8 @@ async function delPosts(
user: UserView,
anon: UserIdentityEntity
) {
const repo = manager.getCustomRepository(PostRepository);
const groupRepo = manager.getCustomRepository(PostGroupRepository);
const repo = manager.withRepository(PostRepository);
const groupRepo = manager.withRepository(PostGroupRepository);
if (hardDelete) {
await manager.query(
`
Expand Down Expand Up @@ -116,7 +116,7 @@ async function delSessions(
user: UserView,
anon: UserIdentityEntity
) {
const repo = manager.getCustomRepository(SessionRepository);
const repo = manager.withRepository(SessionRepository);
if (hardDelete) {
await manager.query(
`
Expand Down
5 changes: 3 additions & 2 deletions backend/src/db/actions/licences.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { v4 } from 'uuid';
import { sendSelfHostWelcome } from '../../email/emailSender';
import { LicenceRepository } from '../repositories';
import { LicenceMetadata } from './../../types';
import { saveAndReload } from '../repositories/BaseRepository';

export async function registerLicence(
email: string | null,
Expand All @@ -12,11 +13,11 @@ export async function registerLicence(
sessionId: string
): Promise<boolean> {
return await transaction(async (manager) => {
const repository = manager.getCustomRepository(LicenceRepository);
const repository = manager.withRepository(LicenceRepository);
const key = v4();
const licence = new LicenceEntity(v4(), email, key, customerId, sessionId);
try {
const savedLicence = await repository.saveAndReload(licence);
const savedLicence = await saveAndReload(repository, licence);
if (savedLicence) {
if (email) {
await sendSelfHostWelcome(email, name || '', key);
Expand Down
8 changes: 4 additions & 4 deletions backend/src/db/actions/merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ async function migrateOne(main: UserView, target: UserView) {
` > Migrating data from ${target.id} (${target.name}) to ${main.id} (${main.name})`
);
return await transaction(async (manager) => {
const voteRepo = manager.getCustomRepository(VoteRepository);
const postRepo = manager.getCustomRepository(PostRepository);
const groupRepo = manager.getCustomRepository(PostGroupRepository);
const sessionRepo = manager.getCustomRepository(SessionRepository);
const voteRepo = manager.withRepository(VoteRepository);
const postRepo = manager.withRepository(PostRepository);
const groupRepo = manager.withRepository(PostGroupRepository);
const sessionRepo = manager.withRepository(SessionRepository);

await manager.query('update messages set user_id = $1 where user_id = $2', [
main.id,
Expand Down
35 changes: 18 additions & 17 deletions backend/src/db/actions/posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { transaction } from './transaction';

export async function getNumberOfPosts(userId: string): Promise<number> {
return await transaction(async (manager) => {
const postRepository = manager.getCustomRepository(PostRepository);
const postRepository = manager.withRepository(PostRepository);
return await postRepository.count({ where: { user: { id: userId } } });
});
}
Expand All @@ -20,7 +20,7 @@ export async function savePost(
post: Post
): Promise<Post | null> {
return await transaction(async (manager) => {
const postRepository = manager.getCustomRepository(PostRepository);
const postRepository = manager.withRepository(PostRepository);
const entity = await postRepository.saveFromJson(sessionId, userId, post);
if (entity) {
return entity.toJson();
Expand All @@ -36,9 +36,12 @@ export async function updatePost(
groupId: string | null
): Promise<Post | null> {
return await transaction(async (manager) => {
const postRepository = manager.getCustomRepository(PostRepository);
const entity = await postRepository.findOne(postData.id, {
where: { session: { id: sessionId } },
const postRepository = manager.withRepository(PostRepository);
const entity = await postRepository.findOne({
where: {
id: postData.id,
session: { id: sessionId },
},
});
if (entity) {
const post = entity.toJson();
Expand All @@ -62,8 +65,7 @@ export async function savePostGroup(
group: PostGroup
): Promise<PostGroup | null> {
return await transaction(async (manager) => {
const postGroupRepository =
manager.getCustomRepository(PostGroupRepository);
const postGroupRepository = manager.withRepository(PostGroupRepository);
const entity = await postGroupRepository.saveFromJson(
sessionId,
userId,
Expand All @@ -82,10 +84,9 @@ export async function updatePostGroup(
groupData: Omit<Omit<PostGroup, 'user'>, 'posts'>
) {
return await transaction(async (manager) => {
const postGroupRepository =
manager.getCustomRepository(PostGroupRepository);
const entity = await postGroupRepository.findOne(groupData.id, {
where: { session: { id: sessionId } },
const postGroupRepository = manager.withRepository(PostGroupRepository);
const entity = await postGroupRepository.findOne({
where: { id: groupData.id, session: { id: sessionId } },
});
if (entity) {
const group = entity.toJson();
Expand All @@ -111,7 +112,7 @@ export async function saveVote(
vote: Vote
): Promise<void> {
return await transaction(async (manager) => {
const voteRepository = manager.getCustomRepository(VoteRepository);
const voteRepository = manager.withRepository(VoteRepository);
await voteRepository.saveFromJson(postId, userId, vote);
});
}
Expand All @@ -123,7 +124,7 @@ export async function deletePost(
): Promise<boolean> {
return await transaction(async (manager) => {
try {
const postRepository = manager.getCustomRepository(PostRepository);
const postRepository = manager.withRepository(PostRepository);
const result = await postRepository.delete({
id: postId,
user: { id: userId },
Expand All @@ -142,10 +143,10 @@ export async function deletePostGroup(
): Promise<boolean> {
return await transaction(async (manager) => {
try {
const postGroupRepository =
manager.getCustomRepository(PostGroupRepository);
const sessionRepository = manager.getCustomRepository(SessionRepository);
const session = await sessionRepository.findOne(sessionId, {
const postGroupRepository = manager.withRepository(PostGroupRepository);
const sessionRepository = manager.withRepository(SessionRepository);
const session = await sessionRepository.findOne({
where: { id: sessionId },
relations: ['visitors'],
});
if (
Expand Down
Loading

0 comments on commit e39cecd

Please sign in to comment.