feat(core): template-doc settings for user-worksapce scope (#9566)

This commit is contained in:
CatsJuice
2025-01-14 02:10:34 +00:00
parent 10196f6785
commit 57b89b5ad4
11 changed files with 237 additions and 46 deletions

View File

@@ -1,5 +1,6 @@
import type { Framework } from '@toeverything/infra';
import { WorkspaceServerService } from '../cloud';
import { WorkspaceScope, WorkspaceService } from '../workspace';
import { WorkspaceDB } from './entities/db';
import { WorkspaceDBTable } from './entities/table';
@@ -11,7 +12,7 @@ export { WorkspaceDBService } from './services/db';
export function configureWorkspaceDBModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(WorkspaceDBService, [WorkspaceService])
.service(WorkspaceDBService, [WorkspaceService, WorkspaceServerService])
.entity(WorkspaceDB)
.entity(WorkspaceDBTable, [WorkspaceService]);
}

View File

@@ -48,6 +48,10 @@ export const AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA = {
key: f.string().primaryKey(),
index: f.string(),
},
settings: {
key: f.string().primaryKey(),
value: f.json(),
},
} as const satisfies DBSchemaBuilder;
export type AFFiNEWorkspaceUserdataDbSchema =
typeof AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA;

View File

@@ -1,11 +1,13 @@
import {
createORMClient,
LiveData,
ObjectPool,
Service,
YjsDBAdapter,
} from '@toeverything/infra';
import { Doc as YDoc } from 'yjs';
import { AuthService, type WorkspaceServerService } from '../../cloud';
import type { WorkspaceService } from '../../workspace';
import { WorkspaceDB, type WorkspaceDBWithTables } from '../entities/db';
import {
@@ -31,7 +33,10 @@ export class WorkspaceDBService extends Service {
},
});
constructor(private readonly workspaceService: WorkspaceService) {
constructor(
private readonly workspaceService: WorkspaceService,
private readonly workspaceServerService: WorkspaceServerService
) {
super();
this.db = this.framework.createEntity(
WorkspaceDB<AFFiNEWorkspaceDbSchema>,
@@ -95,6 +100,25 @@ export class WorkspaceDBService extends Service {
return newDB as WorkspaceDBWithTables<AFFiNEWorkspaceUserdataDbSchema>;
}
authService = this.workspaceServerService.server?.scope.get(AuthService);
public get userdataDB$() {
// if is local workspace or no account, use __local__ userdata
// sometimes we may have cloud workspace but no account for a short time, we also use __local__ userdata
if (
this.workspaceService.workspace.meta.flavour === 'local' ||
!this.authService
) {
return new LiveData(this.userdataDB('__local__'));
} else {
return this.authService.session.account$.map(account => {
if (!account) {
return this.userdataDB('__local__');
}
return this.userdataDB(account.id);
});
}
}
static isDBDocId(docId: string) {
return docId.startsWith('db$') || docId.startsWith('userdata$');
}