@helenejs/data
Advanced tools
Comparing version 1.4.5 to 1.4.10-alpha.1
import { IStorage } from '../types'; | ||
export declare const CHUNK_SIZE: number; | ||
export declare const STORE_NAME = "chunks"; | ||
export declare const DB_NAME = "helene_data"; | ||
export declare const DB_VERSION = 1; | ||
export declare class IDBStorage implements IStorage { | ||
private readonly prefix; | ||
private db; | ||
private getDB; | ||
read(name: string): Promise<string>; | ||
write(name: string, data: string): Promise<void>; | ||
append(name: string, data: string): Promise<void>; | ||
clear(): Promise<void>; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IDBStorage = void 0; | ||
const idb_keyval_1 = require("idb-keyval"); | ||
exports.IDBStorage = exports.DB_VERSION = exports.DB_NAME = exports.STORE_NAME = exports.CHUNK_SIZE = void 0; | ||
const idb_1 = require("idb"); | ||
exports.CHUNK_SIZE = 512 * 1024; // 512 KB | ||
exports.STORE_NAME = 'chunks'; | ||
exports.DB_NAME = 'helene_data'; | ||
exports.DB_VERSION = 1; | ||
const dbPromise = (0, idb_1.openDB)(exports.DB_NAME, exports.DB_VERSION, { | ||
upgrade(db) { | ||
const store = db.createObjectStore(exports.STORE_NAME, { keyPath: 'id' }); | ||
store.createIndex('docId', 'docId'); | ||
}, | ||
}); | ||
class IDBStorage { | ||
constructor() { | ||
this.prefix = 'helene:data:'; | ||
this.db = null; | ||
} | ||
async getDB() { | ||
if (!this.db) { | ||
this.db = await dbPromise; | ||
} | ||
return this.db; | ||
} | ||
async read(name) { | ||
name = `helene:data:${name}`; | ||
const value = await (0, idb_keyval_1.get)(name); | ||
return value || ''; | ||
try { | ||
const docId = `${this.prefix}${name}`; | ||
const db = await this.getDB(); | ||
const chunks = await db.getAllFromIndex(exports.STORE_NAME, 'docId', docId); | ||
if (!chunks.length) { | ||
return ''; | ||
} | ||
chunks.sort((a, b) => a.chunkIndex - b.chunkIndex); | ||
console.log({ chunks }); | ||
return chunks.map(chunk => chunk.content).join(''); | ||
} | ||
catch (error) { | ||
console.error('Error reading from IndexedDB:', error); | ||
throw new Error('Failed to read data from storage'); | ||
} | ||
} | ||
async write(name, data) { | ||
name = `helene:data:${name}`; | ||
await (0, idb_keyval_1.set)(name, data); | ||
try { | ||
const docId = `${this.prefix}${name}`; | ||
const db = await this.getDB(); | ||
// Split into chunks | ||
const chunks = []; | ||
for (let i = 0; i < data.length; i += exports.CHUNK_SIZE) { | ||
chunks.push(data.slice(i, i + exports.CHUNK_SIZE)); | ||
} | ||
const tx = db.transaction(exports.STORE_NAME, 'readwrite'); | ||
const store = tx.objectStore(exports.STORE_NAME); | ||
// Delete existing chunks first | ||
const existingChunks = await store.index('docId').getAllKeys(docId); | ||
await Promise.all(existingChunks.map(key => store.delete(key))); | ||
// Write new chunks | ||
await Promise.all(chunks.map((chunk, i) => store.put({ | ||
id: `${docId}-${i}`, | ||
docId, | ||
chunkIndex: i, | ||
content: chunk, | ||
}))); | ||
await tx.done; | ||
} | ||
catch (error) { | ||
console.error('Error writing to IndexedDB:', error); | ||
throw new Error('Failed to write data to storage'); | ||
} | ||
} | ||
async append(name, data) { | ||
name = `helene:data:${name}`; | ||
const existingData = await (0, idb_keyval_1.get)(name); | ||
const newData = existingData ? existingData + data : data; | ||
await (0, idb_keyval_1.set)(name, newData); | ||
try { | ||
const existingData = await this.read(name); | ||
const newData = existingData + data; | ||
await this.write(name, newData); | ||
} | ||
catch (error) { | ||
console.error('Error appending data:', error); | ||
throw new Error('Failed to append data'); | ||
} | ||
} | ||
async clear() { | ||
try { | ||
const db = await this.getDB(); | ||
const tx = db.transaction(exports.STORE_NAME, 'readwrite'); | ||
await tx.objectStore(exports.STORE_NAME).clear(); | ||
await tx.done; | ||
} | ||
catch (error) { | ||
console.error('Error clearing storage:', error); | ||
throw new Error('Failed to clear storage'); | ||
} | ||
} | ||
} | ||
exports.IDBStorage = IDBStorage; | ||
//# sourceMappingURL=idb-storage.js.map |
{ | ||
"name": "@helenejs/data", | ||
"version": "1.4.5", | ||
"version": "1.4.10-alpha.1", | ||
"description": "Real-time Web Apps for Node.js", | ||
@@ -18,3 +18,3 @@ "main": "lib/index.js", | ||
"eventemitter2": "^6.4.7", | ||
"idb-keyval": "^6.2.1", | ||
"idb": "^8.0.0", | ||
"immutable": "^4.3.5", | ||
@@ -51,3 +51,3 @@ "lodash": "^4.17.21", | ||
}, | ||
"gitHead": "d97fe2a4acc5e157fbd8372eb584f633794f10bf" | ||
"gitHead": "d516bbac05424966ea4123b3bc081d5ee4809dd4" | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
250194
4069
2
+ Addedidb@^8.0.0
+ Addedidb@8.0.0(transitive)
- Removedidb-keyval@^6.2.1
- Removedidb-keyval@6.2.1(transitive)