mirror of
https://github.com/docmost/docmost.git
synced 2025-11-18 18:51:46 +10:00
* feat(EE): fulltext search in attachments * feat: global search - search filters - attachments search ui - and more * fix import * fix import * rename migration * add GIN index * fix table name * sanitize
114 lines
2.6 KiB
TypeScript
114 lines
2.6 KiB
TypeScript
import { Injectable } from '@nestjs/common';
|
|
import { InjectKysely } from 'nestjs-kysely';
|
|
import { KyselyDB, KyselyTransaction } from '@docmost/db/types/kysely.types';
|
|
import { dbOrTx } from '@docmost/db/utils';
|
|
import {
|
|
Attachment,
|
|
InsertableAttachment,
|
|
UpdatableAttachment,
|
|
} from '@docmost/db/types/entity.types';
|
|
|
|
@Injectable()
|
|
export class AttachmentRepo {
|
|
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
|
|
|
private baseFields: Array<keyof Attachment> = [
|
|
'id',
|
|
'fileName',
|
|
'filePath',
|
|
'fileSize',
|
|
'fileExt',
|
|
'mimeType',
|
|
'type',
|
|
'creatorId',
|
|
'pageId',
|
|
'spaceId',
|
|
'workspaceId',
|
|
'createdAt',
|
|
'updatedAt',
|
|
'deletedAt',
|
|
];
|
|
|
|
async findById(
|
|
attachmentId: string,
|
|
opts?: {
|
|
trx?: KyselyTransaction;
|
|
},
|
|
): Promise<Attachment> {
|
|
const db = dbOrTx(this.db, opts?.trx);
|
|
|
|
return db
|
|
.selectFrom('attachments')
|
|
.select(this.baseFields)
|
|
.where('id', '=', attachmentId)
|
|
.executeTakeFirst();
|
|
}
|
|
|
|
async insertAttachment(
|
|
insertableAttachment: InsertableAttachment,
|
|
trx?: KyselyTransaction,
|
|
): Promise<Attachment> {
|
|
const db = dbOrTx(this.db, trx);
|
|
|
|
return db
|
|
.insertInto('attachments')
|
|
.values(insertableAttachment)
|
|
.returning(this.baseFields)
|
|
.executeTakeFirst();
|
|
}
|
|
|
|
async findBySpaceId(
|
|
spaceId: string,
|
|
opts?: {
|
|
trx?: KyselyTransaction;
|
|
},
|
|
): Promise<Attachment[]> {
|
|
const db = dbOrTx(this.db, opts?.trx);
|
|
|
|
return db
|
|
.selectFrom('attachments')
|
|
.select(this.baseFields)
|
|
.where('spaceId', '=', spaceId)
|
|
.execute();
|
|
}
|
|
|
|
updateAttachmentsByPageId(
|
|
updatableAttachment: UpdatableAttachment,
|
|
pageIds: string[],
|
|
trx?: KyselyTransaction,
|
|
) {
|
|
return dbOrTx(this.db, trx)
|
|
.updateTable('attachments')
|
|
.set(updatableAttachment)
|
|
.where('pageId', 'in', pageIds)
|
|
.returning(this.baseFields)
|
|
.executeTakeFirst();
|
|
}
|
|
|
|
async updateAttachment(
|
|
updatableAttachment: UpdatableAttachment,
|
|
attachmentId: string,
|
|
): Promise<Attachment> {
|
|
return await this.db
|
|
.updateTable('attachments')
|
|
.set(updatableAttachment)
|
|
.where('id', '=', attachmentId)
|
|
.returning(this.baseFields)
|
|
.executeTakeFirst();
|
|
}
|
|
|
|
async deleteAttachmentById(attachmentId: string): Promise<void> {
|
|
await this.db
|
|
.deleteFrom('attachments')
|
|
.where('id', '=', attachmentId)
|
|
.executeTakeFirst();
|
|
}
|
|
|
|
async deleteAttachmentByFilePath(attachmentFilePath: string): Promise<void> {
|
|
await this.db
|
|
.deleteFrom('attachments')
|
|
.where('filePath', '=', attachmentFilePath)
|
|
.executeTakeFirst();
|
|
}
|
|
}
|