Skip to content

Commit

Permalink
feat(infra): doc properties by orm (#8382)
Browse files Browse the repository at this point in the history
create new orm table docConfiguration

move primary store to docConfiguration
  • Loading branch information
EYHN committed Oct 7, 2024
1 parent f5c49a6 commit c26df2e
Show file tree
Hide file tree
Showing 16 changed files with 551 additions and 80 deletions.
2 changes: 1 addition & 1 deletion packages/common/infra/src/modules/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { WorkspaceDB } from './entities/db';
import { WorkspaceDBTable } from './entities/table';
import { WorkspaceDBService } from './services/db';

export { AFFiNE_WORKSPACE_DB_SCHEMA } from './schema';
export type { DocProperties } from './schema';
export { WorkspaceDBService } from './services/db';
export { transformWorkspaceDBLocalToCloud } from './services/db';

Expand Down
6 changes: 5 additions & 1 deletion packages/common/infra/src/modules/db/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export { AFFiNE_WORKSPACE_DB_SCHEMA } from './schema';
export type { DocProperties } from './schema';
export {
AFFiNE_WORKSPACE_DB_SCHEMA,
AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA,
} from './schema';
27 changes: 26 additions & 1 deletion packages/common/infra/src/modules/db/schema/schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { nanoid } from 'nanoid';

import { type DBSchemaBuilder, f } from '../../../orm';
import { type DBSchemaBuilder, f, type ORMEntity, t } from '../../../orm';

export const AFFiNE_WORKSPACE_DB_SCHEMA = {
folders: {
Expand All @@ -10,9 +10,34 @@ export const AFFiNE_WORKSPACE_DB_SCHEMA = {
type: f.string(),
index: f.string(),
},
docProperties: t.document({
// { [`custom:{customPropertyId}`]: any }
id: f.string().primaryKey(),
primaryMode: f.string().optional(),
edgelessColorTheme: f.string().optional(),
journal: f.string().optional(),
}),
docCustomPropertyInfo: {
id: f.string().primaryKey().optional().default(nanoid),
name: f.string().optional(),
type: f.string(),
show: f.string().optional(),
index: f.string().optional(),
additionalData: f.json().optional(),
isDeleted: f.boolean().optional(),
// we will keep deleted properties in the database, for override legacy data
},
} as const satisfies DBSchemaBuilder;
export type AFFiNE_WORKSPACE_DB_SCHEMA = typeof AFFiNE_WORKSPACE_DB_SCHEMA;

export type DocProperties = ORMEntity<
AFFiNE_WORKSPACE_DB_SCHEMA['docProperties']
>;

export type DocCustomPropertyInfo = ORMEntity<
AFFiNE_WORKSPACE_DB_SCHEMA['docCustomPropertyInfo']
>;

export const AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA = {
favorite: {
key: f.string().primaryKey(),
Expand Down
6 changes: 4 additions & 2 deletions packages/common/infra/src/modules/db/services/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import type { DocStorage } from '../../../sync';
import { ObjectPool } from '../../../utils';
import type { WorkspaceService } from '../../workspace';
import { WorkspaceDB, type WorkspaceDBWithTables } from '../entities/db';
import { AFFiNE_WORKSPACE_DB_SCHEMA } from '../schema';
import { AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA } from '../schema/schema';
import {
AFFiNE_WORKSPACE_DB_SCHEMA,
AFFiNE_WORKSPACE_USERDATA_DB_SCHEMA,
} from '../schema';

const WorkspaceDBClient = createORMClient(AFFiNE_WORKSPACE_DB_SCHEMA);
const WorkspaceUserdataDBClient = createORMClient(
Expand Down
1 change: 1 addition & 0 deletions packages/common/infra/src/modules/doc/entities/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export class Doc extends Entity {
public readonly record = this.scope.props.record;

readonly meta$ = this.record.meta$;
readonly properties$ = this.record.properties$;
readonly primaryMode$ = this.record.primaryMode$;
readonly title$ = this.record.title$;
readonly trash$ = this.record.trash$;
Expand Down
27 changes: 27 additions & 0 deletions packages/common/infra/src/modules/doc/entities/property-list.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Entity } from '../../../framework';
import { LiveData } from '../../../livedata';
import type { DocCustomPropertyInfo } from '../../db/schema/schema';
import type { DocPropertiesStore } from '../stores/doc-properties';

export class DocPropertyList extends Entity {
constructor(private readonly docPropertiesStore: DocPropertiesStore) {
super();
}

properties$ = LiveData.from(
this.docPropertiesStore.watchDocPropertyInfoList(),
[]
);

updatePropertyInfo(id: string, properties: Partial<DocCustomPropertyInfo>) {
this.docPropertiesStore.updateDocPropertyInfo(id, properties);
}

createProperty(properties: DocCustomPropertyInfo) {
return this.docPropertiesStore.createDocPropertyInfo(properties);
}

removeProperty(id: string) {
this.docPropertiesStore.removeDocPropertyInfo(id);
}
}
16 changes: 15 additions & 1 deletion packages/common/infra/src/modules/doc/entities/record.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import type { DocMeta } from '@blocksuite/affine/store';

import { Entity } from '../../../framework';
import { LiveData } from '../../../livedata';
import type { DocProperties } from '../../db';
import type { DocPropertiesStore } from '../stores/doc-properties';
import type { DocsStore } from '../stores/docs';

/**
Expand All @@ -12,7 +14,10 @@ import type { DocsStore } from '../stores/docs';
*/
export class DocRecord extends Entity<{ id: string }> {
id: string = this.props.id;
constructor(private readonly docsStore: DocsStore) {
constructor(
private readonly docsStore: DocsStore,
private readonly docPropertiesStore: DocPropertiesStore
) {
super();
}

Expand All @@ -21,6 +26,15 @@ export class DocRecord extends Entity<{ id: string }> {
{}
);

properties$ = LiveData.from<DocProperties>(
this.docPropertiesStore.watchDocProperties(this.id),
{ id: this.id }
);

setProperties(properties: Partial<DocProperties>): void {
this.docPropertiesStore.updateDocProperties(this.id, properties);
}

setMeta(meta: Partial<DocMeta>): void {
this.docsStore.setDocMeta(this.id, meta);
}
Expand Down
15 changes: 8 additions & 7 deletions packages/common/infra/src/modules/doc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,27 @@ export { DocService } from './services/doc';
export { DocsService } from './services/docs';

import type { Framework } from '../../framework';
import {
WorkspaceLocalState,
WorkspaceScope,
WorkspaceService,
} from '../workspace';
import { WorkspaceDBService } from '../db';
import { WorkspaceScope, WorkspaceService } from '../workspace';
import { Doc } from './entities/doc';
import { DocPropertyList } from './entities/property-list';
import { DocRecord } from './entities/record';
import { DocRecordList } from './entities/record-list';
import { DocScope } from './scopes/doc';
import { DocService } from './services/doc';
import { DocsService } from './services/docs';
import { DocPropertiesStore } from './stores/doc-properties';
import { DocsStore } from './stores/docs';

export function configureDocModule(framework: Framework) {
framework
.scope(WorkspaceScope)
.service(DocsService, [DocsStore])
.store(DocsStore, [WorkspaceService, WorkspaceLocalState])
.entity(DocRecord, [DocsStore])
.store(DocPropertiesStore, [WorkspaceService, WorkspaceDBService])
.store(DocsStore, [WorkspaceService, DocPropertiesStore])
.entity(DocRecord, [DocsStore, DocPropertiesStore])
.entity(DocRecordList, [DocsStore])
.entity(DocPropertyList, [DocPropertiesStore])
.scope(DocScope)
.entity(Doc, [DocScope, DocsStore, WorkspaceService])
.service(DocService);
Expand Down
3 changes: 3 additions & 0 deletions packages/common/infra/src/modules/doc/services/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Service } from '../../../framework';
import { type DocProps, initDocFromProps } from '../../../initialization';
import { ObjectPool } from '../../../utils';
import type { Doc } from '../entities/doc';
import { DocPropertyList } from '../entities/property-list';
import { DocRecordList } from '../entities/record-list';
import { DocScope } from '../scopes/doc';
import type { DocsStore } from '../stores/docs';
Expand All @@ -19,6 +20,8 @@ export class DocsService extends Service {
},
});

propertyList = this.framework.createEntity(DocPropertyList);

constructor(private readonly store: DocsStore) {
super();
}
Expand Down
Loading

0 comments on commit c26df2e

Please sign in to comment.