mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-14 13:25:12 +00:00
feat(core): init organize (#7456)
This commit is contained in:
10
packages/common/infra/src/modules/db/index.ts
Normal file
10
packages/common/infra/src/modules/db/index.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
import type { Framework } from '../../framework';
|
||||
import { WorkspaceScope, WorkspaceService } from '../workspace';
|
||||
import { DBService } from './services/db';
|
||||
|
||||
export { AFFiNE_DB_SCHEMA } from './schema';
|
||||
export { DBService } from './services/db';
|
||||
|
||||
export function configureDBModule(framework: Framework) {
|
||||
framework.scope(WorkspaceScope).service(DBService, [WorkspaceService]);
|
||||
}
|
||||
1
packages/common/infra/src/modules/db/schema/index.ts
Normal file
1
packages/common/infra/src/modules/db/schema/index.ts
Normal file
@@ -0,0 +1 @@
|
||||
export { AFFiNE_DB_SCHEMA } from './schema';
|
||||
14
packages/common/infra/src/modules/db/schema/schema.ts
Normal file
14
packages/common/infra/src/modules/db/schema/schema.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import { nanoid } from 'nanoid';
|
||||
|
||||
import { type DBSchemaBuilder, f } from '../../../orm';
|
||||
|
||||
export const AFFiNE_DB_SCHEMA = {
|
||||
folders: {
|
||||
id: f.string().primaryKey().optional().default(nanoid),
|
||||
parentId: f.string().optional(),
|
||||
data: f.string(),
|
||||
type: f.string(),
|
||||
index: f.string(),
|
||||
},
|
||||
} as const satisfies DBSchemaBuilder;
|
||||
export type AFFiNE_DB_SCHEMA = typeof AFFiNE_DB_SCHEMA;
|
||||
32
packages/common/infra/src/modules/db/services/db.ts
Normal file
32
packages/common/infra/src/modules/db/services/db.ts
Normal file
@@ -0,0 +1,32 @@
|
||||
import { Doc as YDoc } from 'yjs';
|
||||
|
||||
import { Service } from '../../../framework';
|
||||
import { createORMClient, type TableMap, YjsDBAdapter } from '../../../orm';
|
||||
import type { WorkspaceService } from '../../workspace';
|
||||
import { AFFiNE_DB_SCHEMA } from '../schema';
|
||||
|
||||
export class DBService extends Service {
|
||||
db: TableMap<AFFiNE_DB_SCHEMA>;
|
||||
|
||||
constructor(private readonly workspaceService: WorkspaceService) {
|
||||
super();
|
||||
const Client = createORMClient(AFFiNE_DB_SCHEMA);
|
||||
this.db = new Client(
|
||||
new YjsDBAdapter(AFFiNE_DB_SCHEMA, {
|
||||
getDoc: guid => {
|
||||
const ydoc = new YDoc({
|
||||
// guid format: db${workspaceId}${guid}
|
||||
guid: `db$${this.workspaceService.workspace.id}$${guid}`,
|
||||
});
|
||||
this.workspaceService.workspace.engine.doc.addDoc(ydoc, false);
|
||||
this.workspaceService.workspace.engine.doc.setPriority(ydoc.guid, 50);
|
||||
return ydoc;
|
||||
},
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
static isDBDocId(docId: string) {
|
||||
return docId.startsWith('db$');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user