mirror of
https://github.com/docmost/docmost.git
synced 2026-05-21 09:14:07 +08:00
feat: synced blocks (transclusion) (#2163)
* feat: synced blocks (transclusion) * fix:remove name * make placeholders smaller * feat: enforce strict transclusion schema * fix: scope synced blocks to workspace, gate unsync on edit permission * fix collab module error
This commit is contained in:
@@ -89,6 +89,22 @@ export class AttachmentRepo {
|
||||
.execute();
|
||||
}
|
||||
|
||||
async findByIds(
|
||||
ids: string[],
|
||||
opts?: {
|
||||
trx?: KyselyTransaction;
|
||||
},
|
||||
): Promise<Attachment[]> {
|
||||
if (ids.length === 0) return [];
|
||||
const db = dbOrTx(this.db, opts?.trx);
|
||||
|
||||
return db
|
||||
.selectFrom('attachments')
|
||||
.select(this.baseFields)
|
||||
.where('id', 'in', ids)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async findByAiChatId(
|
||||
aiChatId: string,
|
||||
opts?: {
|
||||
|
||||
+98
@@ -0,0 +1,98 @@
|
||||
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 {
|
||||
InsertablePageTransclusionReference,
|
||||
PageTransclusionReference,
|
||||
} from '@docmost/db/types/entity.types';
|
||||
|
||||
export type TransclusionReferenceKey = {
|
||||
sourcePageId: string;
|
||||
transclusionId: string;
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class PageTransclusionReferencesRepo {
|
||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||
|
||||
async findByReferencePageId(
|
||||
referencePageId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<PageTransclusionReference[]> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('pageTransclusionReferences')
|
||||
.selectAll()
|
||||
.where('referencePageId', '=', referencePageId)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async findReferencePageIdsByTransclusion(
|
||||
sourcePageId: string,
|
||||
transclusionId: string,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<string[]> {
|
||||
const rows = await dbOrTx(this.db, trx)
|
||||
.selectFrom('pageTransclusionReferences')
|
||||
.select('referencePageId')
|
||||
.distinct()
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.where('sourcePageId', '=', sourcePageId)
|
||||
.where('transclusionId', '=', transclusionId)
|
||||
.execute();
|
||||
return rows.map((r) => r.referencePageId);
|
||||
}
|
||||
|
||||
async insertMany(
|
||||
rows: InsertablePageTransclusionReference[],
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
if (rows.length === 0) return;
|
||||
await dbOrTx(this.db, trx)
|
||||
.insertInto('pageTransclusionReferences')
|
||||
.values(rows)
|
||||
.onConflict((oc) =>
|
||||
oc
|
||||
.columns(['referencePageId', 'sourcePageId', 'transclusionId'])
|
||||
.doNothing(),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async deleteByReferenceAndKeys(
|
||||
referencePageId: string,
|
||||
keys: TransclusionReferenceKey[],
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
if (keys.length === 0) return;
|
||||
await dbOrTx(this.db, trx)
|
||||
.deleteFrom('pageTransclusionReferences')
|
||||
.where('referencePageId', '=', referencePageId)
|
||||
.where((eb) =>
|
||||
eb.or(
|
||||
keys.map((k) =>
|
||||
eb.and([
|
||||
eb('sourcePageId', '=', k.sourcePageId),
|
||||
eb('transclusionId', '=', k.transclusionId),
|
||||
]),
|
||||
),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async deleteOne(
|
||||
referencePageId: string,
|
||||
sourcePageId: string,
|
||||
transclusionId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
await dbOrTx(this.db, trx)
|
||||
.deleteFrom('pageTransclusionReferences')
|
||||
.where('referencePageId', '=', referencePageId)
|
||||
.where('sourcePageId', '=', sourcePageId)
|
||||
.where('transclusionId', '=', transclusionId)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
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 {
|
||||
InsertablePageTransclusion,
|
||||
PageTransclusion,
|
||||
UpdatablePageTransclusion,
|
||||
} from '@docmost/db/types/entity.types';
|
||||
|
||||
@Injectable()
|
||||
export class PageTransclusionsRepo {
|
||||
constructor(@InjectKysely() private readonly db: KyselyDB) {}
|
||||
|
||||
async findByPageId(
|
||||
pageId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<PageTransclusion[]> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('pageTransclusions')
|
||||
.selectAll()
|
||||
.where('pageId', '=', pageId)
|
||||
.orderBy('createdAt', 'asc')
|
||||
.execute();
|
||||
}
|
||||
|
||||
async findByPageAndTransclusion(
|
||||
pageId: string,
|
||||
transclusionId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<PageTransclusion | undefined> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('pageTransclusions')
|
||||
.selectAll()
|
||||
.where('pageId', '=', pageId)
|
||||
.where('transclusionId', '=', transclusionId)
|
||||
.executeTakeFirst();
|
||||
}
|
||||
|
||||
async findManyByPageAndTransclusion(
|
||||
keys: Array<{ pageId: string; transclusionId: string }>,
|
||||
workspaceId: string,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<PageTransclusion[]> {
|
||||
if (keys.length === 0) return [];
|
||||
return dbOrTx(this.db, trx)
|
||||
.selectFrom('pageTransclusions')
|
||||
.selectAll()
|
||||
.where('workspaceId', '=', workspaceId)
|
||||
.where((eb) =>
|
||||
eb.or(
|
||||
keys.map((k) =>
|
||||
eb.and([
|
||||
eb('pageId', '=', k.pageId),
|
||||
eb('transclusionId', '=', k.transclusionId),
|
||||
]),
|
||||
),
|
||||
),
|
||||
)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async insert(
|
||||
data: InsertablePageTransclusion,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<PageTransclusion> {
|
||||
return dbOrTx(this.db, trx)
|
||||
.insertInto('pageTransclusions')
|
||||
.values(data)
|
||||
.returningAll()
|
||||
.executeTakeFirstOrThrow();
|
||||
}
|
||||
|
||||
async insertMany(
|
||||
data: InsertablePageTransclusion[],
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
if (data.length === 0) return;
|
||||
await dbOrTx(this.db, trx)
|
||||
.insertInto('pageTransclusions')
|
||||
.values(data)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async update(
|
||||
pageId: string,
|
||||
transclusionId: string,
|
||||
data: UpdatablePageTransclusion,
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
await dbOrTx(this.db, trx)
|
||||
.updateTable('pageTransclusions')
|
||||
.set({ ...data, updatedAt: new Date() })
|
||||
.where('pageId', '=', pageId)
|
||||
.where('transclusionId', '=', transclusionId)
|
||||
.execute();
|
||||
}
|
||||
|
||||
async deleteByPageAndTransclusionIds(
|
||||
pageId: string,
|
||||
transclusionIds: string[],
|
||||
trx?: KyselyTransaction,
|
||||
): Promise<void> {
|
||||
if (transclusionIds.length === 0) return;
|
||||
await dbOrTx(this.db, trx)
|
||||
.deleteFrom('pageTransclusions')
|
||||
.where('pageId', '=', pageId)
|
||||
.where('transclusionId', 'in', transclusionIds)
|
||||
.execute();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -100,6 +100,30 @@ export class PageRepo {
|
||||
return query.executeTakeFirst();
|
||||
}
|
||||
|
||||
async findManyByIds(
|
||||
pageIds: string[],
|
||||
opts?: {
|
||||
trx?: KyselyTransaction;
|
||||
workspaceId?: string;
|
||||
},
|
||||
): Promise<Page[]> {
|
||||
if (pageIds.length === 0) return [];
|
||||
const db = dbOrTx(this.db, opts?.trx);
|
||||
|
||||
let query = db
|
||||
.selectFrom('pages')
|
||||
.select(this.baseFields)
|
||||
.where('id', 'in', pageIds);
|
||||
|
||||
if (opts?.workspaceId) {
|
||||
query = query
|
||||
.where('workspaceId', '=', opts.workspaceId)
|
||||
.where('deletedAt', 'is', null);
|
||||
}
|
||||
|
||||
return query.execute();
|
||||
}
|
||||
|
||||
async updatePage(
|
||||
updatablePage: UpdatablePage,
|
||||
pageId: string,
|
||||
|
||||
Reference in New Issue
Block a user