Skip to content

Commit

Permalink
feat: add @joshdb/indexeddb
Browse files Browse the repository at this point in the history
  • Loading branch information
WilsontheWolf authored and RealShadowNova committed Jun 7, 2023
1 parent 3307e10 commit cea950b
Show file tree
Hide file tree
Showing 19 changed files with 2,902 additions and 27 deletions.
3 changes: 3 additions & 0 deletions packages/indexeddb/.cliff-jumperrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: indexeddb
org: joshdb
packagePath: packages/indexeddb
1 change: 1 addition & 0 deletions packages/indexeddb/.typedoc-json-parserrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json: 'docs/api.json'
3 changes: 3 additions & 0 deletions packages/indexeddb/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Changelog

All notable changes to this project will be documented in this file.
33 changes: 33 additions & 0 deletions packages/indexeddb/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<div align="center">

![Josh Logo](https://evie.codes/josh-light.png)

# @joshdb/indexeddb

**A provider for `@joshdb/core`**

[![GitHub](https://img.shields.io/github/license/josh-development/providers)](https://github.com/josh-development/providers/blob/main/LICENSE.md)
[![codecov](https://codecov.io/gh/josh-development/providers/branch/main/graph/badge.svg?token=JnJcjxqT3k)](https://codecov.io/gh/josh-development/providers)
[![npm](https://img.shields.io/npm/v/@joshdb/indexeddb?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@joshdb/indexeddb)

[![Support Server](https://discord.com/api/guilds/298508738623438848/embed.png?style=banner2)](https://discord.gg/N7ZKH3P)

</div>

## Description

A Josh provider

## Features

- Written in TypeScript
- Offers CommonJS and ESM bundles
- Fully tested

## Installation

You can use the following command to install this package, or replace `npm install` with your package manager of choice.

```sh
npm install @joshdb/indexeddb
```
67 changes: 67 additions & 0 deletions packages/indexeddb/cliff.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
[changelog]
header = """
# Changelog
All notable changes to this project will be documented in this file.
"""
body = """
{% if version %}\
# [{{ version | trim_start_matches(pat="v") }}]\
{% if previous %}\
{% if previous.version %}\
(https://github.com/josh-development/utilities/compare/{{ previous.version }}...{{ version }})\
{% else %}\
(https://github.com/josh-development/utilities/tree/{{ version }})\
{% endif %}\
{% endif %} \
- ({{ timestamp | date(format="%Y-%m-%d") }})
{% else %}\
# [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
## {{ group | upper_first }}
{% for commit in commits %}
- {% if commit.scope %}\
**{{commit.scope}}:** \
{% endif %}\
{{ commit.message | upper_first }} ([{{ commit.id | truncate(length=7, end="") }}](https://github.com/josh-development/utilities/commit/{{ commit.id }}))\
{% if commit.breaking %}\
{% raw %} {% endraw %} ### 💥 Breaking Changes:
\
{% for breakingChange in commit.footers %}\
{% raw %} {% endraw %} - {{ breakingChange }}
\
{% endfor %}\
{% endif %}\
{% endfor %}
{% endfor %}
"""
trim = true
footer = ""

[git]
conventional_commits = true
filter_unconventional = true
commit_parsers = [
{ message = "^feat", group = "🚀 Features" },
{ message = "^fix", group = "🐛 Bug Fixes" },
{ message = "^docs", group = "📝 Documentation" },
{ message = "^perf", group = "🏃 Performance" },
{ message = "^refactor", group = "🏠 Refactor" },
{ message = ".*deprecated", body = ".*deprecated", group = "🚨 Deprecation" },
{ message = "^revert", skip = true },
{ message = "^style", group = "🪞 Styling" },
{ message = "^test", group = "🧪 Testing" },
{ message = "^chore", skip = true },
{ message = "^ci", skip = true },
{ body = ".*security", group = "🛡️ Security" },
]
filter_commits = true
tag_pattern = "@joshdb/indexeddb@[0-9]*"
ignore_tags = ""
topo_order = false
sort_commits = "newest"
16 changes: 16 additions & 0 deletions packages/indexeddb/jest.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @type {import('@jest/types').Config.InitialOptions} */
const config = {
displayName: 'unit test',
preset: 'ts-jest',
testMatch: ['<rootDir>/tests/**/*.test.ts'],
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
setupFilesAfterEnv: ['jest-extended/all'],
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tests/tsconfig.json'
}
},
coveragePathIgnorePatterns: []
};

export default config;
63 changes: 63 additions & 0 deletions packages/indexeddb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@joshdb/indexeddb",
"version": "2.0.0",
"description": "A Josh provider",
"author": "Évelyne Lachance <[email protected]> (https://evie.codes/)",
"contributors": [
"WilsontheWolf (https://wilson.antti.codes/)",
"DanCodes <[email protected]> (https://dancodes.online/)"
],
"license": "Apache-2.0",
"main": "dist/index.js",
"module": "dist/index.mjs",
"browser": "dist/index.umd.js",
"unpkg": "dist/index.umd.js",
"types": "dist/index.d.ts",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/index.d.ts"
},
"sideEffects": false,
"scripts": {
"test": "jest --verbose",
"docs": "typedoc-json-parser",
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
"build": "rollup -c rollup.config.ts",
"prepack": "rollup-type-bundler",
"bump": "cliff-jumper",
"check-update": "cliff-jumper --dry-run"
},
"dependencies": {
"@joshdb/provider": "1.1.0-next.24ef775.0",
"property-helpers": "^1.1.0"
},
"devDependencies": {
"@favware/cliff-jumper": "^1.8.5",
"@favware/rollup-type-bundler": "^1.0.9",
"fake-indexeddb": "^4.0.0",
"jest": "^28.1.3",
"typedoc": "^0.23.9",
"typedoc-json-parser": "^2.0.1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/josh-development/providers.git"
},
"files": [
"dist",
"!dist/*tsbuildinfo"
],
"engines": {
"node": ">=16.6.0",
"npm": ">=7.0.0"
},
"keywords": [],
"bugs": {
"url": "https://github.com/josh-development/providers/issues"
},
"homepage": "https://josh.evie.dev",
"publishConfig": {
"access": "public"
}
}
34 changes: 34 additions & 0 deletions packages/indexeddb/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { resolve } from 'path';
import cleaner from 'rollup-plugin-cleaner';
import typescript from 'rollup-plugin-typescript2';
import versionInjector from 'rollup-plugin-version-injector';

export default {
input: 'src/index.ts',
output: [
{
file: './dist/index.js',
format: 'cjs',
exports: 'named',
sourcemap: true
},
{
file: './dist/index.mjs',
format: 'es',
exports: 'named',
sourcemap: true
},
{
file: './dist/index.umd.js',
format: 'umd',
name: 'JoshIndexedDBProvider',
exports: 'named',
sourcemap: true,
globals: {
'@joshdb/provider': 'JoshProvider'
}
}
],
external: ['@joshdb/provider'],
plugins: [cleaner({ targets: ['./dist'] }), typescript({ tsconfig: resolve(process.cwd(), 'src', 'tsconfig.json') }), versionInjector()]
};
1 change: 1 addition & 0 deletions packages/indexeddb/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './lib/IndexedDBProvider';
123 changes: 123 additions & 0 deletions packages/indexeddb/src/lib/DbHandler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
export default class DbHandler {
private idb: IDBFactory;
private db!: IDBDatabase;

public constructor() {
if (!indexedDB) {
throw new Error("Your browser doesn't support a stable version of IndexedDB. Josh is unable to run without one.");
}

this.idb = indexedDB;
}

public init() {
const request = this.idb.open('josh');

return new Promise<void>((resolve, reject) => {
request.onerror = reject;

request.onupgradeneeded = () => {
const db = request.result;

if (!db.objectStoreNames.contains('store')) {
db.createObjectStore('store', { keyPath: 'key' });
}
};

request.onsuccess = () => {
this.db = request.result;
resolve();
};
});
}

public async set(key: string, value: unknown) {
const all = this.open();
const doc = {
key,
value
};

const request = all.put(doc);

await this.handleEvents(request);
}

public async get(key: string) {
const all = this.open();
const request = all.get(key);
const result = await this.handleEvents(request);

// @ts-ignore it exists f you TS
return result?.value;
}

public async getAll() {
const all = this.open();
const request = all.getAll();
const docs = await this.handleEvents(request);
const final = {};

// @ts-ignore TS GO AWAY
docs.forEach((x) => {
// @ts-ignore TS GO AWAY
final[x.key] = x.value;
});

return final;
}

public async getKeys() {
const all = this.open();
const request = all.getAllKeys();

return this.handleEvents(request);
}

public async count() {
const all = this.open();
const request = all.count();
const data = await this.handleEvents(request);

if (typeof data !== 'number') throw new Error('Something is amiss!!!');

return data;
}

public async delete(key: string) {
const all = this.open();
const request = all.delete(key);

return this.handleEvents(request);
}

public async clear() {
const all = this.open();
const request = all.clear();

return this.handleEvents(request);
}

public async has(key: string) {
return (await this.get(key)) !== undefined;
}

private handleEvents(request: IDBRequest) {
return new Promise((res, rej) => {
request.onsuccess = () => {
res(request.result);
};

request.onerror = () => {
rej(new Error(request.error?.toString()));
};
});
}

private open() {
const transaction = this.db.transaction('store', 'readwrite');
const all = transaction.objectStore('store');

return all;
}
}
Loading

0 comments on commit cea950b

Please sign in to comment.