refactor(infra): migrate to new infra (#5565)

This commit is contained in:
EYHN
2024-01-30 07:16:39 +00:00
parent 1e3499c323
commit 329fc19852
170 changed files with 2007 additions and 4354 deletions
@@ -1,38 +1,36 @@
import { apis } from '@affine/electron-api';
import type { BlobStorage } from '@affine/workspace';
import { assertExists } from '@blocksuite/global/utils';
import { type BlobStorage } from '@toeverything/infra';
import { bufferToBlob } from '../utils/buffer-to-blob';
export const createSQLiteBlobStorage = (workspaceId: string): BlobStorage => {
assertExists(apis);
return {
name: 'sqlite',
readonly: false,
get: async (key: string) => {
assertExists(apis);
const buffer = await apis.db.getBlob(workspaceId, key);
if (buffer) {
return bufferToBlob(buffer);
}
return null;
},
set: async (key: string, value: Blob) => {
assertExists(apis);
await apis.db.addBlob(
workspaceId,
key,
new Uint8Array(await value.arrayBuffer())
);
return key;
},
delete: async (key: string) => {
assertExists(apis);
return apis.db.deleteBlob(workspaceId, key);
},
list: async () => {
assertExists(apis);
return apis.db.getBlobKeys(workspaceId);
},
};
};
export class SQLiteBlobStorage implements BlobStorage {
constructor(private readonly workspaceId: string) {}
name = 'sqlite';
readonly = false;
async get(key: string) {
assertExists(apis);
const buffer = await apis.db.getBlob(this.workspaceId, key);
if (buffer) {
return bufferToBlob(buffer);
}
return null;
}
async set(key: string, value: Blob) {
assertExists(apis);
await apis.db.addBlob(
this.workspaceId,
key,
new Uint8Array(await value.arrayBuffer())
);
return key;
}
delete(key: string) {
assertExists(apis);
return apis.db.deleteBlob(this.workspaceId, key);
}
list() {
assertExists(apis);
return apis.db.getBlobKeys(this.workspaceId);
}
}