mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 06:23:06 +08:00
label type
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { LabelRepo } from '@docmost/db/repos/label/label.repo';
|
||||
import { LabelRepo, LabelType } from '@docmost/db/repos/label/label.repo';
|
||||
import { InjectKysely } from 'nestjs-kysely';
|
||||
import { KyselyDB } from '@docmost/db/types/kysely.types';
|
||||
import { Label } from '@docmost/db/types/entity.types';
|
||||
@@ -22,6 +22,7 @@ export class LabelService {
|
||||
const label = await this.labelRepo.findOrCreate(
|
||||
name.trim(),
|
||||
workspaceId,
|
||||
LabelType.PAGE,
|
||||
trx,
|
||||
);
|
||||
await this.labelRepo.addLabelToPage(pageId, label.id, trx);
|
||||
|
||||
@@ -7,6 +7,7 @@ export async function up(db: Kysely<any>): Promise<void> {
|
||||
col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
|
||||
)
|
||||
.addColumn('name', 'varchar', (col) => col.notNull())
|
||||
.addColumn('type', 'varchar', (col) => col.notNull().defaultTo('page'))
|
||||
.addColumn('workspace_id', 'uuid', (col) =>
|
||||
col.references('workspaces.id').onDelete('cascade').notNull(),
|
||||
)
|
||||
@@ -21,7 +22,7 @@ export async function up(db: Kysely<any>): Promise<void> {
|
||||
await db.schema
|
||||
.createIndex('labels_workspace_id_name_unique')
|
||||
.on('labels')
|
||||
.columns(['workspace_id', 'name'])
|
||||
.columns(['workspace_id', 'name', 'type'])
|
||||
.unique()
|
||||
.execute();
|
||||
|
||||
|
||||
@@ -5,6 +5,13 @@ import { Label } from '@docmost/db/types/entity.types';
|
||||
import { dbOrTx } from '@docmost/db/utils';
|
||||
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo';
|
||||
|
||||
export const LabelType = {
|
||||
PAGE: 'page',
|
||||
SPACE: 'space',
|
||||
} as const;
|
||||
|
||||
export type LabelType = (typeof LabelType)[keyof typeof LabelType];
|
||||
|
||||
@Injectable()
|
||||
export class LabelRepo {
|
||||
constructor(
|
||||
@@ -27,6 +34,7 @@ export class LabelRepo {
|
||||
async findByNameAndWorkspace(
|
||||
name: string,
|
||||
workspaceId: string,
|
||||
type: LabelType,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<Label | undefined> {
|
||||
const db = dbOrTx(this.db, trx);
|
||||
@@ -34,6 +42,7 @@ export class LabelRepo {
|
||||
.selectFrom('labels')
|
||||
.selectAll()
|
||||
.where('name', '=', name.toLowerCase())
|
||||
.where('type', '=', type)
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
@@ -41,6 +50,7 @@ export class LabelRepo {
|
||||
async findOrCreate(
|
||||
name: string,
|
||||
workspaceId: string,
|
||||
type: LabelType,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<Label> {
|
||||
const db = dbOrTx(this.db, trx);
|
||||
@@ -48,9 +58,9 @@ export class LabelRepo {
|
||||
|
||||
const result = await db
|
||||
.insertInto('labels')
|
||||
.values({ name: normalizedName, workspaceId })
|
||||
.values({ name: normalizedName, type, workspaceId })
|
||||
.onConflict((oc) =>
|
||||
oc.columns(['name', 'workspaceId']).doNothing(),
|
||||
oc.columns(['name', 'type', 'workspaceId']).doNothing(),
|
||||
)
|
||||
.returningAll()
|
||||
.executeTakeFirst();
|
||||
@@ -59,15 +69,16 @@ export class LabelRepo {
|
||||
return result;
|
||||
}
|
||||
|
||||
return this.findByNameAndWorkspace(normalizedName, workspaceId, trx);
|
||||
return this.findByNameAndWorkspace(normalizedName, workspaceId, type, trx);
|
||||
}
|
||||
|
||||
async findLabelsByPageId(pageId: string): Promise<Label[]> {
|
||||
return this.db
|
||||
.selectFrom('labels')
|
||||
.innerJoin('pageLabels', 'pageLabels.labelId', 'labels.id')
|
||||
.select(['labels.id', 'labels.name', 'labels.createdAt', 'labels.updatedAt', 'labels.workspaceId'])
|
||||
.select(['labels.id', 'labels.name', 'labels.type', 'labels.createdAt', 'labels.updatedAt', 'labels.workspaceId'])
|
||||
.where('pageLabels.pageId', '=', pageId)
|
||||
.where('labels.type', '=', LabelType.PAGE)
|
||||
.orderBy('labels.name', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
+1
@@ -393,6 +393,7 @@ export interface Watchers {
|
||||
export interface Labels {
|
||||
id: Generated<string>;
|
||||
name: string;
|
||||
type: Generated<string>;
|
||||
workspaceId: string;
|
||||
createdAt: Generated<Timestamp>;
|
||||
updatedAt: Generated<Timestamp>;
|
||||
|
||||
Reference in New Issue
Block a user