delta-sync
Version:
A lightweight framework for bi-directional database synchronization with automatic version tracking and conflict resolution.
125 lines (124 loc) • 4.05 kB
JavaScript
// core/adapters/memory.ts
// 内存数据库适配器
export class MemoryAdapter {
constructor() {
this.stores = new Map();
this.fileStore = new Map();
this.fileStoreName = '_files';
// Initialize with empty stores
}
async isAvailable() {
return true; // Memory adapter is always available
}
async initSync() {
this.ensureStoreExists('_sync_change');
this.ensureStoreExists('_sync_meta');
this.ensureStoreExists(this.fileStoreName);
return Promise.resolve();
}
ensureStoreExists(storeName) {
if (!this.stores.has(storeName)) {
this.stores.set(storeName, new Map());
}
}
// 实现新的read方法,支持分页和since参数
async read(storeName, options = {}) {
this.ensureStoreExists(storeName);
const store = this.stores.get(storeName);
// 获取所有记录
let items = Array.from(store.values());
// 如果指定了since参数,则按时间戳筛选
if (options.since) {
items = items.filter(item => (item._ver || 0) > options.since);
}
// 按版本号排序
items.sort((a, b) => (a._ver || 0) - (b._ver || 0));
// 计算分页结果
const offset = options.offset || 0;
const limit = options.limit || items.length;
const paginatedItems = items.slice(offset, offset + limit);
const hasMore = offset + limit < items.length;
return {
items: paginatedItems,
hasMore
};
}
async readBulk(storeName, ids) {
this.ensureStoreExists(storeName);
const store = this.stores.get(storeName);
const results = [];
for (const id of ids) {
const item = store.get(id);
if (item !== undefined) {
results.push(item);
}
}
return results;
}
async putBulk(storeName, items) {
this.ensureStoreExists(storeName);
const store = this.stores.get(storeName);
const now = Date.now();
const processedItems = items.map(item => {
const processedItem = {
...item,
_ver: item._ver || now,
_store: storeName
};
store.set(processedItem._delta_id, processedItem);
return processedItem;
});
return processedItems;
}
// 批量删除接口
async deleteBulk(storeName, ids) {
this.ensureStoreExists(storeName);
const store = this.stores.get(storeName);
for (const id of ids) {
store.delete(id);
}
return Promise.resolve();
}
// 文件操作实现
async readFile(fileId) {
const file = this.fileStore.get(fileId);
if (!file) {
throw new Error(`File not found: ${fileId}`);
}
return file.content;
}
async saveFile(content, fileId) {
const now = Date.now();
let fileContent;
if (typeof content === 'string') {
fileContent = new Blob([content], { type: 'text/plain' });
}
else {
fileContent = content;
}
const attachment = {
id: fileId,
mimeType: fileContent instanceof Blob ? fileContent.type : 'application/octet-stream',
size: fileContent instanceof Blob ? fileContent.size : fileContent.byteLength,
createdAt: now,
updatedAt: now,
metadata: {}
};
// 存储文件
this.fileStore.set(fileId, {
_delta_id: fileId,
content: fileContent,
metadata: attachment,
_created_at: now,
_updated_at: now
});
return attachment;
}
async deleteFile(fileId) {
if (!this.fileStore.has(fileId)) {
throw new Error(`File not found: ${fileId}`);
}
this.fileStore.delete(fileId);
return Promise.resolve();
}
}