feat(core): user data db (#7930)

This commit is contained in:
EYHN
2024-09-11 07:55:37 +00:00
parent 498a69af53
commit d93c3b3719
36 changed files with 1220 additions and 281 deletions
@@ -57,16 +57,16 @@ test('can get a valid WorkspaceSQLiteDB', async () => {
'@affine/electron/helper/db/ensure-db'
);
const workspaceId = v4();
const db0 = await ensureSQLiteDB(workspaceId);
const db0 = await ensureSQLiteDB('workspace', workspaceId);
expect(db0).toBeDefined();
expect(db0.workspaceId).toBe(workspaceId);
const db1 = await ensureSQLiteDB(v4());
const db1 = await ensureSQLiteDB('workspace', v4());
expect(db1).not.toBe(db0);
expect(db1.workspaceId).not.toBe(db0.workspaceId);
// ensure that the db is cached
expect(await ensureSQLiteDB(workspaceId)).toBe(db0);
expect(await ensureSQLiteDB('workspace', workspaceId)).toBe(db0);
});
test('db should be destroyed when app quits', async () => {
@@ -74,8 +74,8 @@ test('db should be destroyed when app quits', async () => {
'@affine/electron/helper/db/ensure-db'
);
const workspaceId = v4();
const db0 = await ensureSQLiteDB(workspaceId);
const db1 = await ensureSQLiteDB(v4());
const db0 = await ensureSQLiteDB('workspace', workspaceId);
const db1 = await ensureSQLiteDB('workspace', v4());
expect(db0.adapter).not.toBeNull();
expect(db1.adapter).not.toBeNull();
@@ -94,8 +94,8 @@ test('db should be removed in db$Map after destroyed', async () => {
'@affine/electron/helper/db/ensure-db'
);
const workspaceId = v4();
const db = await ensureSQLiteDB(workspaceId);
const db = await ensureSQLiteDB('workspace', workspaceId);
await db.destroy();
await setTimeout(100);
expect(db$Map.has(workspaceId)).toBe(false);
expect(db$Map.has(`workspace:${workspaceId}`)).toBe(false);
});
@@ -29,7 +29,7 @@ test('can create new db file if not exists', async () => {
'@affine/electron/helper/db/workspace-db-adapter'
);
const workspaceId = v4();
const db = await openWorkspaceDatabase(workspaceId);
const db = await openWorkspaceDatabase('workspace', workspaceId);
const dbPath = path.join(
appDataPath,
`workspaces/${workspaceId}`,
@@ -44,7 +44,7 @@ test('on destroy, check if resources have been released', async () => {
'@affine/electron/helper/db/workspace-db-adapter'
);
const workspaceId = v4();
const db = await openWorkspaceDatabase(workspaceId);
const db = await openWorkspaceDatabase('workspace', workspaceId);
const updateSub = {
complete: vi.fn(),
next: vi.fn(),
@@ -28,40 +28,6 @@ afterAll(() => {
vi.doUnmock('@affine/electron/helper/main-rpc');
});
describe('list workspaces', () => {
test('listWorkspaces (valid)', async () => {
const { listWorkspaces } = await import(
'@affine/electron/helper/workspace/handlers'
);
const workspaceId = v4();
const workspacePath = path.join(appDataPath, 'workspaces', workspaceId);
const meta = {
id: workspaceId,
};
await fs.ensureDir(workspacePath);
await fs.writeJSON(path.join(workspacePath, 'meta.json'), meta);
const workspaces = await listWorkspaces();
expect(workspaces).toEqual([[workspaceId, meta]]);
});
test('listWorkspaces (without meta json file)', async () => {
const { listWorkspaces } = await import(
'@affine/electron/helper/workspace/handlers'
);
const workspaceId = v4();
const workspacePath = path.join(appDataPath, 'workspaces', workspaceId);
await fs.ensureDir(workspacePath);
const workspaces = await listWorkspaces();
expect(workspaces).toEqual([
[
workspaceId,
// meta file will be created automatically
{ id: workspaceId, mainDBPath: path.join(workspacePath, 'storage.db') },
],
]);
});
});
describe('delete workspace', () => {
test('deleteWorkspace', async () => {
const { deleteWorkspace } = await import(
@@ -93,7 +59,7 @@ describe('getWorkspaceMeta', () => {
};
await fs.ensureDir(workspacePath);
await fs.writeJSON(path.join(workspacePath, 'meta.json'), meta);
expect(await getWorkspaceMeta(workspaceId)).toEqual(meta);
expect(await getWorkspaceMeta('workspace', workspaceId)).toEqual(meta);
});
test('can create meta if not exists', async () => {
@@ -103,9 +69,10 @@ describe('getWorkspaceMeta', () => {
const workspaceId = v4();
const workspacePath = path.join(appDataPath, 'workspaces', workspaceId);
await fs.ensureDir(workspacePath);
expect(await getWorkspaceMeta(workspaceId)).toEqual({
expect(await getWorkspaceMeta('workspace', workspaceId)).toEqual({
id: workspaceId,
mainDBPath: path.join(workspacePath, 'storage.db'),
type: 'workspace',
});
expect(
await fs.pathExists(path.join(workspacePath, 'meta.json'))
@@ -124,9 +91,10 @@ describe('getWorkspaceMeta', () => {
await fs.ensureSymlink(sourcePath, path.join(workspacePath, 'storage.db'));
expect(await getWorkspaceMeta(workspaceId)).toEqual({
expect(await getWorkspaceMeta('workspace', workspaceId)).toEqual({
id: workspaceId,
mainDBPath: path.join(workspacePath, 'storage.db'),
type: 'workspace',
});
expect(
@@ -145,6 +113,7 @@ test('storeWorkspaceMeta', async () => {
const meta = {
id: workspaceId,
mainDBPath: path.join(workspacePath, 'storage.db'),
type: 'workspace',
};
await storeWorkspaceMeta(workspaceId, meta);
expect(await fs.readJSON(path.join(workspacePath, 'meta.json'))).toEqual(