feat(nbstore): add sqlite implementation (#8811)

This commit is contained in:
forehalo
2024-12-13 06:13:05 +00:00
parent 932e1da7f3
commit 8c24f2b906
66 changed files with 2932 additions and 397 deletions
@@ -2,17 +2,17 @@ import path from 'node:path';
import fs from 'fs-extra';
import { ensureSQLiteDB } from '../db/ensure-db';
import { logger } from '../logger';
import { ensureSQLiteDB } from '../nbstore/v1/ensure-db';
import type { WorkspaceMeta } from '../type';
import {
getDeletedWorkspacesBasePath,
getWorkspaceBasePath,
getWorkspaceBasePathV1,
getWorkspaceMeta,
} from './meta';
export async function deleteWorkspace(id: string) {
const basePath = await getWorkspaceBasePath('workspace', id);
const basePath = await getWorkspaceBasePathV1('workspace', id);
const movedPath = path.join(await getDeletedWorkspacesBasePath(), `${id}`);
try {
const db = await ensureSQLiteDB('workspace', id);
@@ -30,7 +30,7 @@ export async function storeWorkspaceMeta(
meta: Partial<WorkspaceMeta>
) {
try {
const basePath = await getWorkspaceBasePath('workspace', workspaceId);
const basePath = await getWorkspaceBasePathV1('workspace', workspaceId);
await fs.ensureDir(basePath);
const metaPath = path.join(basePath, 'meta.json');
const currentMeta = await getWorkspaceMeta('workspace', workspaceId);
@@ -1,9 +1,9 @@
import path from 'node:path';
import type { SpaceType } from '@affine/nbstore';
import fs from 'fs-extra';
import { isWindows } from '../../shared/utils';
import type { SpaceType } from '../db/types';
import { logger } from '../logger';
import { mainRPC } from '../main-rpc';
import type { WorkspaceMeta } from '../type';
@@ -22,7 +22,7 @@ export async function getWorkspacesBasePath() {
return path.join(await getAppDataPath(), 'workspaces');
}
export async function getWorkspaceBasePath(
export async function getWorkspaceBasePathV1(
spaceType: SpaceType,
workspaceId: string
) {
@@ -33,6 +33,34 @@ export async function getWorkspaceBasePath(
);
}
export async function getSpaceBasePath(spaceType: SpaceType) {
return path.join(
await getAppDataPath(),
spaceType === 'userspace' ? 'userspaces' : 'workspaces'
);
}
export function escapeFilename(name: string) {
// replace all special characters with '_' and replace repeated '_' with a single '_' and remove trailing '_'
return name
.replaceAll(/[\\/!@#$%^&*()+~`"':;,?<>|]/g, '_')
.replaceAll(/_+/g, '_')
.replace(/_+$/, '');
}
export async function getSpaceDBPath(
peer: string,
spaceType: SpaceType,
id: string
) {
return path.join(
await getSpaceBasePath(spaceType),
escapeFilename(peer),
id,
'storage.db'
);
}
export async function getDeletedWorkspacesBasePath() {
return path.join(await getAppDataPath(), 'deleted-workspaces');
}
@@ -42,7 +70,7 @@ export async function getWorkspaceDBPath(
workspaceId: string
) {
return path.join(
await getWorkspaceBasePath(spaceType, workspaceId),
await getWorkspaceBasePathV1(spaceType, workspaceId),
'storage.db'
);
}
@@ -52,7 +80,7 @@ export async function getWorkspaceMetaPath(
workspaceId: string
) {
return path.join(
await getWorkspaceBasePath(spaceType, workspaceId),
await getWorkspaceBasePathV1(spaceType, workspaceId),
'meta.json'
);
}
@@ -66,7 +94,7 @@ export async function getWorkspaceMeta(
workspaceId: string
): Promise<WorkspaceMeta> {
try {
const basePath = await getWorkspaceBasePath(spaceType, workspaceId);
const basePath = await getWorkspaceBasePathV1(spaceType, workspaceId);
const metaPath = await getWorkspaceMetaPath(spaceType, workspaceId);
if (
!(await fs