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

First step for typeorm upgrade! #2

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
"reflect-metadata": "^0.1.13",
"sharp": "^0.25.4",
"socket.io": "^3.0.1",
"typeorm": "0.2.25",
"typeorm": "0.3.12",
"typeorm-seeding": "^1.6.1",
"uuid": "^8.3.0",
"ws": "^7.4.0",
Expand Down
24 changes: 12 additions & 12 deletions api/src/commands/spaces/DeleteCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export class DeleteCommand {

private static async deleteSpace(id: number) {
const spaceRepository = getCustomRepository(SpaceRepository)
const space = await spaceRepository.findOne(id)
const space = await spaceRepository.findOne({where: {id}})

if (space) {
// tslint:disable-next-line:no-console
Expand All @@ -54,7 +54,7 @@ export class DeleteCommand {

// Docs
const docsRepository = getCustomRepository(DocRepository)
const docs = await docsRepository.find({spaceId: id})
const docs = await docsRepository.find({where: {spaceId: id}})
if (docs) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting docs ...`))
Expand All @@ -64,7 +64,7 @@ export class DeleteCommand {

// Docs Revisions
const docsRevisionsRepository = getCustomRepository(DocRevisionRepository)
const docsRevisions = await docsRevisionsRepository.find({spaceId: id})
const docsRevisions = await docsRevisionsRepository.find({where: {spaceId: id}})
if (docsRevisions) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting doc revisions ...`))
Expand All @@ -74,7 +74,7 @@ export class DeleteCommand {

// Embeds
const embedsRepository = getCustomRepository(EmbedRepository)
const embeds = await embedsRepository.find({spaceId: id})
const embeds = await embedsRepository.find({where: {spaceId: id}})
if (embeds) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting embeds ...`))
Expand All @@ -84,7 +84,7 @@ export class DeleteCommand {

// Favorites
const favoritesRepository = getCustomRepository(FavoriteRepository)
const favorites = await favoritesRepository.find({spaceId: id})
const favorites = await favoritesRepository.find({where: {spaceId: id}})
if (favorites) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting favorites ...`))
Expand All @@ -94,7 +94,7 @@ export class DeleteCommand {

// Folders
const foldersRepository = getCustomRepository(FolderRepository)
const folders = await foldersRepository.find({spaceId: id})
const folders = await foldersRepository.find({where: {spaceId: id}})
if (folders) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting folders ...`))
Expand All @@ -107,7 +107,7 @@ export class DeleteCommand {

// Invites
const invitesRepository = getCustomRepository(InviteRepository)
const invites = await invitesRepository.find({spaceId: id})
const invites = await invitesRepository.find({where: {spaceId: id}})
if (invites) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting invites ...`))
Expand All @@ -117,7 +117,7 @@ export class DeleteCommand {

// Links
const linksRepository = getCustomRepository(LinkRepository)
const links = await linksRepository.find({spaceId: id})
const links = await linksRepository.find({where: {spaceId: id}})
if (links) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting links ...`))
Expand All @@ -127,7 +127,7 @@ export class DeleteCommand {

// Nodes
const nodesRepository = getCustomRepository(NodeRepository)
const nodes = await nodesRepository.find({spaceId: id})
const nodes = await nodesRepository.find({where: {spaceId: id}})
if (nodes) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting nodes ...`))
Expand All @@ -137,7 +137,7 @@ export class DeleteCommand {

// Notifications
const notificationsRepository = getCustomRepository(NotificationRepository)
const notifications = await notificationsRepository.find({spaceId: id})
const notifications = await notificationsRepository.find({where: {spaceId: id}})
if (notifications) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting notifications ...`))
Expand All @@ -148,7 +148,7 @@ export class DeleteCommand {
// Uploads
// Todo: Delete files from remote storage
const uploadsRepository = getCustomRepository(UploadRepository)
const uploads = await uploadsRepository.find({spaceId: id})
const uploads = await uploadsRepository.find({where: {spaceId: id}})
if (uploads) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting uploads ...`))
Expand All @@ -158,7 +158,7 @@ export class DeleteCommand {

// User Settings
const userSettingsRepository = getCustomRepository(UserSettingRepository)
const settings = await userSettingsRepository.find({spaceId: id})
const settings = await userSettingsRepository.find({where: {spaceId: id}})
if (settings) {
// tslint:disable-next-line:no-console
console.log(chalk.yellow(`Deleting user settings ...`))
Expand Down
8 changes: 4 additions & 4 deletions api/src/commands/tasks/OverdueCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ export class OverdueCommand {
private static async updateTasks() {
// tslint:disable-next-line:no-console
console.log(chalk.yellow('Updating isOverdue...'))
const yesterday = moment().subtract(1, 'days')
const yesterday = moment().subtract(1, 'days').toDate()

const repository = getCustomRepository(TaskRepository)
const tasks = await repository.find(
{
const tasks = await repository.find({
where: {
isOverdue: false,
dueDate: LessThan(yesterday)
})
}})

for (const task of tasks) {
task.isOverdue = OverdueCommand.isOverdue(task)
Expand Down
2 changes: 1 addition & 1 deletion api/src/database/commands/tasks/BoardIdCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class BoardIdCommand {
.getMany()

for (const task of tasks) {
const taskList = await getCustomRepository(TaskListRepository).findOne(task.listId)
const taskList = await getCustomRepository(TaskListRepository).findOne({where: {id: task.listId}})

task.boardId = taskList.boardId
}
Expand Down
10 changes: 5 additions & 5 deletions api/src/database/entities/UserToSpace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, Index, CreateDateColumn, UpdateDateColumn } from 'typeorm'
import { Entity, PrimaryGeneratedColumn, Column, ManyToOne, Index, CreateDateColumn, UpdateDateColumn, JoinColumn } from 'typeorm'
import { User } from './User'
import { Space } from './Space'

Expand Down Expand Up @@ -29,15 +29,15 @@ export class UserToSpace {

@ManyToOne(
(type) => User,
(user) => user.userSpaces,
{ primary: true }
(user) => user.userSpaces
)
@JoinColumn({ name: 'userId' })
user: User

@ManyToOne(
(type) => Space,
(space) => space.userSpaces,
{ primary: true }
(space) => space.userSpaces
)
@JoinColumn({ name: 'spaceId' })
space: Space
}
8 changes: 4 additions & 4 deletions api/src/database/repositories/BaseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Repository } from 'typeorm'
import { FindOptionsWhere, Repository } from 'typeorm'

interface WithId { id: number }

export abstract class BaseRepository<T> extends Repository<T> {
public reload<ET extends WithId>(entity: ET): Promise<ET> {
return this.findOneOrFail(entity.id) as any
export abstract class BaseRepository<T extends WithId> extends Repository<T> {
public reload(entity: T): Promise<T> {
return this.findOneByOrFail({id:entity.id} as FindOptionsWhere<T>) as any
}
}
2 changes: 1 addition & 1 deletion api/src/database/seeder/dev/Seeder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export class DevSeeder implements Seeder {

public async run(factory: Factory, connection: Connection): Promise<any> {
this.factory = factory
const userCount = await connection.manager.count(User, {email: this.demoUserData.email})
const userCount = await connection.manager.count(User, {where:{ email: this.demoUserData.email}})
if (userCount===0){
await this.createUser()
}
Expand Down
8 changes: 4 additions & 4 deletions api/src/libs/cron/MarkOverdueTasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import moment from 'moment'

export class MarkOverdueTasks {
static async run() {
const yesterday = moment().subtract(1, 'days')
const tasks = await getCustomRepository(TaskRepository).find(
{
const yesterday = moment().subtract(1, 'days').toDate();
const tasks = await getCustomRepository(TaskRepository).find({
where: {
isOverdue: false,
dueDate: LessThan(yesterday)
}
}}
)

for (const task of tasks) {
Expand Down
22 changes: 11 additions & 11 deletions api/src/services/FollowService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ export class FollowService {
}

async getById(id: number): Promise<Follow> {
return this.getFollowRepository().findOneOrFail(id)
return this.getFollowRepository().findOneOrFail({where: {id}})
}

async getFollowsForActivity(activity: Activity): Promise<Follow[]> {
const follows = await this.getFollowRepository().find({
const follows = await this.getFollowRepository().find({where:{
entityId: activity.entityId,
entity: activity.entity,
})
}})

return follows.filter((follow) => {
return follow.userId !== activity.actorId
Expand All @@ -50,31 +50,31 @@ export class FollowService {
}

async followFromRequest(userId: number, entity: any): Promise<Follow> {
const user = await this.userService.getUserRepository().findOneOrFail(userId)
const user = await this.userService.getUserRepository().findOneOrFail({where: {id: userId}})
return this.follow(user, entity)
}

async unfollowFromRequest(userId: number, entity: any): Promise<Follow> {
const user = await this.userService.getUserRepository().findOneOrFail(userId)
const user = await this.userService.getUserRepository().findOneOrFail({where: {id: userId}})
return this.unfollow(user, entity)
}

async followEntity(userId: number, entity: any): Promise<Follow> {
const user = await this.userService.getUserRepository().findOneOrFail(userId)
const user = await this.userService.getUserRepository().findOneOrFail({where: {id: userId}})
return this.follow(user, entity)
}

async unfollowEntity(userId: number, entity: any): Promise<Follow> {
const user = await this.userService.getUserRepository().findOneOrFail(userId)
const user = await this.userService.getUserRepository().findOneOrFail({where: {id: userId}})
return this.unfollow(user, entity)
}

async follow(user: User, entity: any): Promise<Follow> {
const follow = await this.getFollowRepository().findOne({
const follow = await this.getFollowRepository().findOne({where: {
userId: user.id,
entityId: entity.id,
entity: entity.constructor.name,
})
}})

if (follow) {
return follow
Expand All @@ -91,11 +91,11 @@ export class FollowService {
}

async unfollow(user: User, entity: any): Promise<Follow> {
const follow = await this.getFollowRepository().findOne({
const follow = await this.getFollowRepository().findOne({where: {
userId: user.id,
entityId: entity.id,
entity: entity.constructor.name,
})
}})

if (!follow) {
return
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/NotificationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class NotificationService {
throw clientError('Notification not found', HttpErrName.EntityNotFound, HttpStatusCode.NotFound)
}

return this.getNotificationRepository().findOne(id)
return this.getNotificationRepository().findOne({where: {id}})
}

async create(activity: Activity, userId: number): Promise<Notification> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/activity/ActivityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export class ActivityService implements IActivityObserver {
}

getById(id: number): Promise<Activity | undefined> {
return this.getActivityRepository().findOne(id)
return this.getActivityRepository().findOne({where: {id}})
}

async getBySpaceId(spaceId: number, filter: any = {}, options: any = {}): Promise<Activity[]> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/content-access/ContentAccessService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class ContentAccessService extends Service {
}

getById(id: number): Promise<ContentAccess> {
return this.getRepository().findOne(id)
return this.getRepository().findOne({where: {id}})
}

async requireById(id: number): Promise<ContentAccess> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/content/FavoriteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class FavoriteService {
}

getById(id: number): Promise<Favorite | undefined> {
return this.getFavoriteRepository().findOne(id)
return this.getFavoriteRepository().findOne({where: {id}})
}

async requireById(id: number): Promise<Favorite> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/content/doc/DocService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export class DocService extends NodeContentService {
}

getDocRevisionById(id: number): Promise<DocRevision | undefined> {
return this.getDocRevisionRepository().findOne(id)
return this.getDocRevisionRepository().findOne({where: {id}})
}

async requireDocRevisionById(id: number): Promise<DocRevision> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/content/tasks/TaskBoardService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export class TaskBoardService extends NodeContentService {
}

async save(data: any, parentId?: number): Promise<TaskBoard & Node> {
data.space = await this.getSpaceRepository().findOneOrFail(data.spaceId)
data.space = await this.getSpaceRepository().findOneOrFail({where: {id: data.spaceId}})
let taskBoard = await this.getTaskBoardRepository().save(data)

let value = NodeCreateValue.fromObject({
Expand Down
4 changes: 2 additions & 2 deletions api/src/services/content/tasks/TaskBoardTagService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ export class TaskBoardTagService {
}

async getById(id: number): Promise<Tag> {
return this.getTagRepository().findOneOrFail(id)
return this.getTagRepository().findOneOrFail({where: {id}})
}

async getByTaskBoardId(id: number): Promise<Tag[]> {
return this.getTagRepository().find({ boardId: id })
return this.getTagRepository().find({where: { boardId: id }})
}

async create(data: any): Promise<Tag> {
Expand Down
2 changes: 1 addition & 1 deletion api/src/services/content/tasks/TaskCommentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class TaskCommentService extends Service {
}

async getById(id: number): Promise<TaskComment> {
return this.getTaskCommentRepository().findOneOrFail(id)
return this.getTaskCommentRepository().findOneOrFail({where: {id}})
}

async create(data: any): Promise<TaskComment> {
Expand Down
6 changes: 3 additions & 3 deletions api/src/services/content/tasks/TaskListService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class TaskListService extends Service {
}

async getById(id: number): Promise<TaskList | undefined> {
return this.getTaskListRepository().findOne(id)
return this.getTaskListRepository().findOne({where: {id}})
}

async requireById(id: number): Promise<TaskList> {
Expand Down Expand Up @@ -71,8 +71,8 @@ export class TaskListService extends Service {
}

async create(data: any): Promise<TaskList> {
data.space = await this.getSpaceRepository().findOneOrFail(data.spaceId)
data.board = await this.getTaskBoardRepository().findOneOrFail(data.boardId)
data.space = await this.getSpaceRepository().findOneOrFail({where: {id: data.spaceId}})
data.board = await this.getTaskBoardRepository().findOneOrFail({where: {id: data.boardId}})

const taskList = await this.getTaskListRepository().save(data)
await this.notifyActivity(TaskListActivity.created(taskList, taskList.userId))
Expand Down
Loading