Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(storage): use name as namespace #21

Merged
merged 13 commits into from
Feb 19, 2022
Merged

fix(storage): use name as namespace #21

merged 13 commits into from
Feb 19, 2022

Conversation

azu
Copy link
Owner

@azu azu commented Feb 16, 2022

Previously, @kvs/storage does not separate key per storage.
This PR aim to fix this issue #20

However, It need to change saved storage's key. It is a BREAKING CHANGE.
We need to bump it as major update.

Affected Packages

  • @kvs/env in Node.js
    • 馃摑 Browser is not affected because it uses IndexedDB
  • @kvs/storage
  • @kvs/localstorage
  • @kvs/memorystorage
  • @kvs/node-localstorage
  • @kvs/storage-sync

Previous: storage save value with key

key: value

This PR: storage save value with ${name}.__.${key}.

${name}.__.${key}: value

It means that kvs package can not load value which is saved by previous version.

Migration

You need to write manuall migration code using upgrade.

If you have used @kvs/localstorage, you can write following.
Unfortunately, this migration should use raw localStorage API because @kvs/localstorage can not access the key which is created in kvs 1.x.

import { kvsLocalStorage } from "@kvs/localstorage";
type StorageSchema = {
    a1: string;
    b2: number;
    c3: boolean;
};
const databaseName = "kvs-database";
const storage = kvsStorageSync<StorageSchema>({
    name: databaseName,
    version: 2, // If previous `version` is 1, you need to update the version to 2
    storage: localStorage,
    upgrade({ oldVersion }: { kvs: KVSSync<StorageSchema>; oldVersion: number; newVersion: number }): any {
        if (oldVersion < 2) {
            // manually migration to new key format
            ["a1", "b2", "c3"].forEach((key) => {
                // CAUTION: use raw localStorage for migration
                const item = localStorage.getItem(key);
                if (item) {
                    localStorage.setItem(`${databaseName}.__.${key}`, item);
                }
            });
        }
    }
});
KVS raw localStorage
@kvs/localstorage localStorage
@kvs/memorystorage NONE
@kvs/node-localstorage node-localstorage
@kvs/env in Node.js node-localstorage

TODO

  • fix storage
  • fix storage-sync
  • create migrateion plan as possible
    • It may be difficult

fix #20

@azu azu changed the title fix(storage): add name as namespace fix(storage): use name as namespace Feb 16, 2022
@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 16, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit b5857bd:

Sandbox Source
kvs-example-basic Configuration

@azu azu marked this pull request as ready for review February 18, 2022 13:19
@azu azu merged commit 2a8d783 into master Feb 19, 2022
@azu azu deleted the feature/20 branch February 19, 2022 01:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Breaking Change Includes breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Iterating a specific kvsEnvStorage returns all key-value pairs for all databases
1 participant