Skip to content

Commit

Permalink
feat(@lvs/env): add universal env
Browse files Browse the repository at this point in the history
  • Loading branch information
azu committed Aug 8, 2020
1 parent 523be98 commit 36eef88
Show file tree
Hide file tree
Showing 11 changed files with 265 additions and 0 deletions.
5 changes: 5 additions & 0 deletions packages/env/.mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": [
"ts-node-test-register"
]
}
19 changes: 19 additions & 0 deletions packages/env/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.
46 changes: 46 additions & 0 deletions packages/env/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# @kvs/env

Universal library for KVS.

It detects the running platform and use suitable storage engine.

## Install

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

npm install @kvs/env

## Usage

- [ ] Write usage instructions

## 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
66 changes: 66 additions & 0 deletions packages/env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "@kvs/env",
"version": "1.0.0",
"description": "Universal Storage for KVS.",
"keywords": [
"kvs",
"browser",
"env"
],
"homepage": "https://github.com/azu/kvs/tree/master/packages/env/",
"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/node.js",
"module": "module/node.js",
"types": "lib/node.d.ts",
"browser": "lib/browser.js",
"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": "mocha \"test/**/*.ts\"",
"watch": "tsc -p . --watch"
},
"prettier": {
"printWidth": 120,
"singleQuote": false,
"tabWidth": 4,
"trailingComma": "none"
},
"dependencies": {
"@kvs/indexeddb": "^1.0.0",
"@kvs/node-localstorage": "^1.0.0"
},
"devDependencies": {
"@jsdevtools/karma-config": "^3.1.7",
"@types/mocha": "^8.0.1",
"@types/node": "^14.0.27",
"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"
}
}
9 changes: 9 additions & 0 deletions packages/env/src/browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { JsonValue, KvsStorage } from "@kvs/storage";
import { kvsIndexedDB } from "@kvs/indexeddb";
import { KvsEnvStorageOptions } from "./share";

export const kvsEnvStorage = async <K extends string, V extends JsonValue>(
options: KvsEnvStorageOptions<K, V>
): Promise<KvsStorage<K, V>> => {
return kvsIndexedDB(options);
};
9 changes: 9 additions & 0 deletions packages/env/src/node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { JsonValue, KvsStorage } from "@kvs/storage";
import { kvsLocalStorage } from "@kvs/node-localstorage";
import { KvsEnvStorageOptions } from "./share";

export const kvsEnvStorage = async <K extends string, V extends JsonValue>(
options: KvsEnvStorageOptions<K, V>
): Promise<KvsStorage<K, V>> => {
return kvsLocalStorage(options);
};
6 changes: 6 additions & 0 deletions packages/env/src/share.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { JsonValue, KVSStorageKey } from "@kvs/storage";
import { KVS } from "@kvs/types";
import { KvsLocalStorageOptions } from "@kvs/node-localstorage";

export type KvsEnvStorage<K extends KVSStorageKey, V extends JsonValue> = KVS<K, V>;
export type KvsEnvStorageOptions<K extends KVSStorageKey, V extends JsonValue> = KvsLocalStorageOptions<K, V>;
51 changes: 51 additions & 0 deletions packages/env/test/node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { kvsEnvStorage } from "../src/node";
import { createKVSTestCase } from "@kvs/common-test-case";

const databaseName = "kvs-test";
const kvsTestCase = createKVSTestCase(
(options) =>
kvsEnvStorage({
...options,
name: databaseName,
debug: true
}),
{
setTestDataList: [
{
name: "string",
value: "str"
},
{
name: "number",
value: 42
},
{
name: "boolean",
value: false
},
{
name: "object",
value: {
prop: "propValue"
},
type: "object"
}
]
}
);
const deleteAllDB = async () => {
if (!kvsTestCase.ref.current) {
return;
}
try {
await kvsTestCase.ref.current.clear();
// await kvs.dropInstance();
} catch (error) {
console.error("deleteAllDB", error);
}
};
describe("@kvs/localstorage", () => {
before(deleteAllDB);
afterEach(deleteAllDB);
kvsTestCase.run();
});
11 changes: 11 additions & 0 deletions packages/env/test/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"declaration": false,
"noEmit": true
},
"include": [
"../src/**/*",
"./**/*"
]
}
36 changes: 36 additions & 0 deletions packages/env/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"compilerOptions": {
/* Basic Options */
"module": "commonjs",
"moduleResolution": "node",
"esModuleInterop": true,
"newLine": "LF",
"outDir": "./lib/",
"target": "ES2015",
"sourceMap": true,
"declaration": true,
"jsx": "preserve",
"lib": [
"esnext",
"dom"
],
/* Strict Type-Checking Options */
"strict": true,
/* Additional Checks */
/* Report errors on unused locals. */
"noUnusedLocals": true,
/* Report errors on unused parameters. */
"noUnusedParameters": true,
/* Report error when not all code paths in function return a value. */
"noImplicitReturns": true,
/* Report errors for fallthrough cases in switch statement. */
"noFallthroughCasesInSwitch": true
},
"include": [
"src/**/*"
],
"exclude": [
".git",
"node_modules"
]
}
7 changes: 7 additions & 0 deletions packages/env/tsconfig.module.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"outDir": "./module/",
}
}

0 comments on commit 36eef88

Please sign in to comment.