How to migrate from localForage to localspace.
localspace is designed as a drop-in replacement for localForage. In most cases, you can simply change your import statement:
-import localforage from 'localforage';
+import localspace from 'localspace';
// Your existing code works unchanged
await localspace.setItem('key', value);
const data = await localspace.getItem('key');Before upgrading, note these differences:
dropInstance() throws a real Error when arguments are invalid. Examine error.message instead of comparing string literals.
try {
await localspace.dropInstance({ name: 'invalid' });
} catch (error) {
// error is an Error instance with proper message
console.error(error.message);
}Blob capability checks run on each request instead of being cached. Cache the result in your application if repeated blob writes dominate your workload:
// Cache the blob support check if needed
const supportsBlobs = await localspace.supports(localspace.INDEXEDDB);WebSQL is intentionally unsupported. Migrate any WebSQL-only code to IndexedDB or localStorage before switching.
If you maintain older code that expects separate success and error callbacks for driver setup methods (setDriver, defineDriver), enable compatibilityMode when creating an instance.
Warning
Use compatibility mode only for migrations. Prefer native Promises going forward.
const legacy = localspace.createInstance({
name: 'legacy-store',
storeName: 'pairs',
compatibilityMode: true,
});
legacy.setDriver(
[legacy.LOCALSTORAGE],
() => {
// Success callback receives no arguments.
},
(error) => {
// Error callback receives the Error object only.
}
);Storage methods like setItem, getItem, removeItem, etc. always use Node-style (error, value) callbacks regardless of compatibilityMode. This matches localForage's original behavior.
localspace.setItem('key', 'value', (err, value) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Saved:', value);
}
});
localspace.getItem('key', (err, value) => {
if (err) {
console.error('Error:', err);
} else {
console.log('Retrieved:', value);
}
});When compatibilityMode is off (default), driver setup methods also use Node-style callbacks:
localspace.setDriver([localspace.INDEXEDDB], (err) => {
if (err) {
console.error('Driver setup failed:', err);
} else {
console.log('Driver ready');
}
});When compatibilityMode is on, driver setup methods use separate success/error callbacks (localForage style):
const legacy = localspace.createInstance({
compatibilityMode: true,
});
legacy.setDriver(
[legacy.INDEXEDDB],
() => console.log('Success!'),
(err) => console.error('Error:', err)
);- Update imports: Change
localforagetolocalspace - Run tests: Your existing test suite should pass without changes
- Update error handling: Check for
Errorinstances instead of string comparisons - Remove WebSQL: If you were using WebSQL driver, migrate to IndexedDB
- Adopt new features: Gradually adopt plugins, batch operations, and other new features