Skip to content

Commit

Permalink
feat(storage-sync): add storage-sync package (#11)
Browse files Browse the repository at this point in the history
* feat(storage-sync): add storage-sync package

* fix package homepage

* chore: remove sync.ts
  • Loading branch information
azu committed Aug 22, 2020
1 parent 8a03a53 commit 5748776
Show file tree
Hide file tree
Showing 17 changed files with 625 additions and 73 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ KVS libraries provide following common features.
- In-Memory
- [@kvs/memorystorage](./packages/memorystorage): In-Memory Storage
- For debug
- Sync Version
- [@kvs/storage-sync](./packages/storage-sync): Sync version of [@kvs/localstorage](./packages/localstorage)

If you want to custom implementation, please see [@kvs/storage](./packages/storage) and test it with [@kvs/common-test-case](./packages/common-test-case).

Expand Down
5 changes: 5 additions & 0 deletions packages/storage-sync/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": [
"ts-node-test-register"
]
}
38 changes: 38 additions & 0 deletions packages/storage-sync/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Change Log

All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [0.2.1](https://github.com/azu/kvs/compare/v0.2.0...v0.2.1) (2020-08-22)

**Note:** Version bump only for package @kvs/storage





# [0.2.0](https://github.com/azu/kvs/compare/v0.1.0...v0.2.0) (2020-08-08)


### Features

* **@kvs/indexeddb:** migrate Schema Type ([ec143e2](https://github.com/azu/kvs/commit/ec143e27d174271f4ce45f657e1ae644ef01591c))
* **@kvs/localstorage:** migrate to Schema type ([0c84640](https://github.com/azu/kvs/commit/0c84640c1c1d28955c60ca83d8a01bdce936d9ef))
* **storage:** use Schema interface ([2560aae](https://github.com/azu/kvs/commit/2560aae28d642c8f2e8ee5920dc1cc15f7c8c3f6))





# 0.1.0 (2020-08-08)


### Bug Fixes

* add deps ([b72e91a](https://github.com/azu/kvs/commit/b72e91aaa2487d69d44200ef0a11cc0b5f8fb904))


### Features

* **@kvs/node-localstorage:** add node implementation ([5160012](https://github.com/azu/kvs/commit/516001286c96ac85cb54d55fbba62549d6d7eb0e))
* add `close()` to interface ([a269d1d](https://github.com/azu/kvs/commit/a269d1dda6ce63388771e6fa4d897a26f284b72c))
19 changes: 19 additions & 0 deletions packages/storage-sync/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2020 azu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
78 changes: 78 additions & 0 deletions packages/storage-sync/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# @kvs/storage-sync

Storage-like for KVS.

You can inject Storage object like localStorage, sessionStorage to this storage.

[@kvs/storage] is async version, but [@kvs/storage-sync] is sync version.

## Feature

- Synchronize version [@kvs/storage]
- Support `[Symbol.iterator]` instead of `[Symbol.asyncIterator]`
- Injectable `localStorage` or `sessionStorage`

## Install

Install with [npm](https://www.npmjs.com/):

npm install @kvs/storage-sync

## Usage

```ts
import assert from "assert";
import { kvsStorageSync } from "@kvs/storage";
(() => {
type StorageSchema = {
a1: string;
b2: number;
c3: boolean;
};
const storage = kvsStorageSync<StorageSchema>({
name: "test",
version: 1,
storage: localStorage
});
storage.set("a1", "string");
storage.set("b2", 42);
storage.set("c3", false);
const a1 = storage.get("a1");
const b2 = storage.get("b2");
const c3 = storage.get("c3");
assert.strictEqual(a1, "string");
assert.strictEqual(b2, 42);
assert.strictEqual(c3, false);
})();
```

## Changelog

See [Releases page](https://github.com/azu/kvs/releases).

## Running tests

Install devDependencies and Run `npm test`:

npm test

## Contributing

Pull requests and stars are always welcome.

For bugs and feature requests, [please create an issue](https://github.com/azu/kvs/issues).

1. Fork it!
2. Create your feature branch: `git checkout -b my-new-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request :D

## Author

- [github/azu](https://github.com/azu)
- [twitter/azu_re](https://twitter.com/azu_re)

## License

MIT © azu
34 changes: 34 additions & 0 deletions packages/storage-sync/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = require("@jsdevtools/karma-config")({
tests: ["src/**/*.ts", "test/**/*.test.ts"],
browsers: Boolean(process.env.CI)
? {
firefox: true,
chrome: true,
safari: false
// MSEdge does not work correctly
// edge: false,
}
: {
// manually attach to test bed
firefox: false,
chrome: false,
edge: false,
safari: false
},
config: {
webpack: {
resolve: {
extensions: [".js", ".jsx", ".ts", ".tsx"]
},
mode: "development",
module: {
rules: [{ test: /\.ts$/, use: "ts-loader" }]
}
},
client: {
mocha: {
timeout: 5 * 1000 // 5 sec
}
}
}
});
67 changes: 67 additions & 0 deletions packages/storage-sync/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"name": "@kvs/storage-sync",
"version": "0.2.1",
"description": "Storage(localStorage, sessionStorage) helper for KVS.",
"keywords": [
"kvs",
"browser",
"localstorage"
],
"homepage": "https://github.com/azu/kvs/tree/master/packages/storage-sync/",
"bugs": {
"url": "https://github.com/azu/kvs/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/azu/kvs.git"
},
"license": "MIT",
"author": "azu",
"sideEffects": false,
"main": "lib/index.js",
"module": "module/index.js",
"types": "lib/index.d.ts",
"directories": {
"lib": "lib",
"test": "test"
},
"files": [
"bin/",
"lib/",
"module"
],
"scripts": {
"build": "tsc -p . && tsc --project ./tsconfig.module.json",
"clean": "rimraf lib/ module/",
"prettier": "prettier --write \"**/*.{js,jsx,ts,tsx,css}\"",
"prepublish": "npm run clean && npm run build",
"test": "karma start --single-run",
"watch": "tsc -p . --watch"
},
"prettier": {
"printWidth": 120,
"singleQuote": false,
"tabWidth": 4,
"trailingComma": "none"
},
"dependencies": {
"@kvs/types": "^0.2.1"
},
"devDependencies": {
"@kvs/common-test-case": "^0.2.1",
"@jsdevtools/karma-config": "^3.1.7",
"@types/mocha": "^8.0.1",
"@types/node": "^14.0.27",
"karma": "^5.1.1",
"karma-cli": "^2.0.0",
"lerna": "^3.22.1",
"mocha": "^8.1.1",
"prettier": "^2.0.5",
"rimraf": "^3.0.2",
"ts-loader": "^8.0.2",
"typescript": "^3.9.7"
},
"publishConfig": {
"access": "public"
}
}
16 changes: 16 additions & 0 deletions packages/storage-sync/src/JSONValue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Based on https://github.com/sindresorhus/type-fest/blob/master/source/basic.d.ts
/**
Matches a JSON object.
This type can be useful to enforce some input to be JSON-compatible or as a super-type to be extended from. Don't use this as a direct return type as the user would have to double-cast it: `jsonObject as unknown as CustomResponse`. Instead, you could extend your CustomResponse type from it to ensure your type only uses JSON-compatible types: `interface CustomResponse extends JsonObject { … }`.
*/
export type JsonObject = { [Key in string]?: JsonValue };

/**
Matches a JSON array.
*/
export interface JsonArray extends Array<JsonValue> {}

/**
Matches any valid JSON value.
*/
export type JsonValue = string | number | boolean | null | JsonObject | JsonArray;
14 changes: 14 additions & 0 deletions packages/storage-sync/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export {
kvsStorageSync,
// internal
deleteItem,
setItem,
getItem,
clearItem,
createIterator,
hasItem,
KvsStorageSync,
KVSStorageKey,
KvsStorageSyncOptions
} from "./storage";
export { JsonValue, JsonArray, JsonObject } from "./JSONValue";

0 comments on commit 5748776

Please sign in to comment.