label type

This commit is contained in:
Philipinho
2026-02-17 02:42:30 +00:00
parent 75f7f9b296
commit 2f99cd8f7d
4 changed files with 20 additions and 6 deletions
+2 -1
View File
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common'; 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 { InjectKysely } from 'nestjs-kysely';
import { KyselyDB } from '@docmost/db/types/kysely.types'; import { KyselyDB } from '@docmost/db/types/kysely.types';
import { Label } from '@docmost/db/types/entity.types'; import { Label } from '@docmost/db/types/entity.types';
@@ -22,6 +22,7 @@ export class LabelService {
const label = await this.labelRepo.findOrCreate( const label = await this.labelRepo.findOrCreate(
name.trim(), name.trim(),
workspaceId, workspaceId,
LabelType.PAGE,
trx, trx,
); );
await this.labelRepo.addLabelToPage(pageId, label.id, 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()`), col.primaryKey().defaultTo(sql`gen_uuid_v7()`),
) )
.addColumn('name', 'varchar', (col) => col.notNull()) .addColumn('name', 'varchar', (col) => col.notNull())
.addColumn('type', 'varchar', (col) => col.notNull().defaultTo('page'))
.addColumn('workspace_id', 'uuid', (col) => .addColumn('workspace_id', 'uuid', (col) =>
col.references('workspaces.id').onDelete('cascade').notNull(), col.references('workspaces.id').onDelete('cascade').notNull(),
) )
@@ -21,7 +22,7 @@ export async function up(db: Kysely<any>): Promise<void> {
await db.schema await db.schema
.createIndex('labels_workspace_id_name_unique') .createIndex('labels_workspace_id_name_unique')
.on('labels') .on('labels')
.columns(['workspace_id', 'name']) .columns(['workspace_id', 'name', 'type'])
.unique() .unique()
.execute(); .execute();
@@ -5,6 +5,13 @@ import { Label } from '@docmost/db/types/entity.types';
import { dbOrTx } from '@docmost/db/utils'; import { dbOrTx } from '@docmost/db/utils';
import { SpaceMemberRepo } from '@docmost/db/repos/space/space-member.repo'; 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() @Injectable()
export class LabelRepo { export class LabelRepo {
constructor( constructor(
@@ -27,6 +34,7 @@ export class LabelRepo {
async findByNameAndWorkspace( async findByNameAndWorkspace(
name: string, name: string,
workspaceId: string, workspaceId: string,
type: LabelType,
trx?: KyselyTransaction, trx?: KyselyTransaction,
): Promise<Label | undefined> { ): Promise<Label | undefined> {
const db = dbOrTx(this.db, trx); const db = dbOrTx(this.db, trx);
@@ -34,6 +42,7 @@ export class LabelRepo {
.selectFrom('labels') .selectFrom('labels')
.selectAll() .selectAll()
.where('name', '=', name.toLowerCase()) .where('name', '=', name.toLowerCase())
.where('type', '=', type)
.where('workspaceId', '=', workspaceId) .where('workspaceId', '=', workspaceId)
.executeTakeFirst(); .executeTakeFirst();
} }
@@ -41,6 +50,7 @@ export class LabelRepo {
async findOrCreate( async findOrCreate(
name: string, name: string,
workspaceId: string, workspaceId: string,
type: LabelType,
trx?: KyselyTransaction, trx?: KyselyTransaction,
): Promise<Label> { ): Promise<Label> {
const db = dbOrTx(this.db, trx); const db = dbOrTx(this.db, trx);
@@ -48,9 +58,9 @@ export class LabelRepo {
const result = await db const result = await db
.insertInto('labels') .insertInto('labels')
.values({ name: normalizedName, workspaceId }) .values({ name: normalizedName, type, workspaceId })
.onConflict((oc) => .onConflict((oc) =>
oc.columns(['name', 'workspaceId']).doNothing(), oc.columns(['name', 'type', 'workspaceId']).doNothing(),
) )
.returningAll() .returningAll()
.executeTakeFirst(); .executeTakeFirst();
@@ -59,15 +69,16 @@ export class LabelRepo {
return result; return result;
} }
return this.findByNameAndWorkspace(normalizedName, workspaceId, trx); return this.findByNameAndWorkspace(normalizedName, workspaceId, type, trx);
} }
async findLabelsByPageId(pageId: string): Promise<Label[]> { async findLabelsByPageId(pageId: string): Promise<Label[]> {
return this.db return this.db
.selectFrom('labels') .selectFrom('labels')
.innerJoin('pageLabels', 'pageLabels.labelId', 'labels.id') .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('pageLabels.pageId', '=', pageId)
.where('labels.type', '=', LabelType.PAGE)
.orderBy('labels.name', 'asc') .orderBy('labels.name', 'asc')
.execute(); .execute();
} }
+1
View File
@@ -393,6 +393,7 @@ export interface Watchers {
export interface Labels { export interface Labels {
id: Generated<string>; id: Generated<string>;
name: string; name: string;
type: Generated<string>;
workspaceId: string; workspaceId: string;
createdAt: Generated<Timestamp>; createdAt: Generated<Timestamp>;
updatedAt: Generated<Timestamp>; updatedAt: Generated<Timestamp>;