Skip to content

Commit c1b1160

Browse files
WilsontheWolfRealShadowNova
authored andcommitted
idk anymore
1 parent c2dbf83 commit c1b1160

File tree

6 files changed

+975
-186
lines changed

6 files changed

+975
-186
lines changed

packages/indexeddb/jest.config.mjs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/** @type {import('@jest/types').Config.InitialOptions} */
2+
const config = {
3+
displayName: 'unit test',
4+
preset: 'ts-jest',
5+
testMatch: ['<rootDir>/tests/**/*.test.ts'],
6+
collectCoverageFrom: ['<rootDir>/src/**/*.ts'],
7+
setupFilesAfterEnv: ['jest-extended/all'],
8+
globals: {
9+
'ts-jest': {
10+
tsconfig: '<rootDir>/tests/tsconfig.json'
11+
}
12+
},
13+
coverageDirectory: '../../coverage/json',
14+
coveragePathIgnorePatterns: []
15+
};
16+
17+
export default config;

packages/indexeddb/jest.config.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

packages/indexeddb/package.json

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,56 @@
11
{
2-
"name": "@joshdb/indexeddb",
3-
"version": "2.0.0",
4-
"author": "@joshdb",
5-
"license": "Apache-2.0",
6-
"main": "dist/index.js",
7-
"types": "dist/index.d.ts",
8-
"exports": {
9-
"import": "./dist/index.mjs",
10-
"require": "./dist/index.js"
11-
},
12-
"scripts": {
13-
"test": "jest --verbose",
14-
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
15-
"build": "rollup -c rollup.bundle.ts",
16-
"prepublishOnly": "rollup-type-bundler"
17-
},
18-
"dependencies": {
19-
"@joshdb/core": "RealShadowNova/joshdb-core#build",
20-
"@realware/utilities": "^2.0.2"
21-
},
22-
"repository": {
23-
"type": "git",
24-
"url": "git+https://github.com/eslachance/josh-providers"
25-
},
26-
"files": [
27-
"dist",
28-
"!dist/*tsbuildinfo"
29-
],
30-
"engines": {
31-
"node": ">=16",
32-
"npm": ">=6"
33-
},
34-
"keywords": [],
35-
"bugs": {
36-
"url": "https://github.com/eslachance/josh-providers/issues"
37-
},
38-
"homepage": "https://github.com/eslachance/josh-providers",
39-
"publishConfig": {
40-
"access": "public"
41-
},
42-
"devDependencies": {
43-
"fake-indexeddb": "^3.1.7"
44-
}
2+
"name": "@joshdb/indexeddb",
3+
"version": "2.0.0",
4+
"author": "Évelyne Lachance <[email protected]> (https://evie.codes/)",
5+
"contributors": [
6+
"WilsontheWolf (https://wilson.antti.codes/)",
7+
"DanCodes <[email protected]> (https://dancodes.online/)"
8+
],
9+
"license": "Apache-2.0",
10+
"main": "dist/index.js",
11+
"module": "dist/index.mjs",
12+
"types": "dist/index.d.ts",
13+
"exports": {
14+
"import": "./dist/index.mjs",
15+
"require": "./dist/index.js",
16+
"types": "./dist/index.d.ts"
17+
},
18+
"sideEffects": false,
19+
"scripts": {
20+
"test": "jest",
21+
"docs": "typedoc-json-parser",
22+
"lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
23+
"build": "rollup -c rollup.config.ts",
24+
"prepack": "rollup-type-bundler",
25+
"bump": "cliff-jumper",
26+
"check-update": "cliff-jumper --dry-run"
27+
},
28+
"dependencies": {
29+
"@joshdb/core": "next",
30+
"@realware/utilities": "^2.0.2"
31+
},
32+
"repository": {
33+
"type": "git",
34+
"url": "git+https://github.com/eslachance/josh-providers"
35+
},
36+
"files": [
37+
"dist",
38+
"!dist/*tsbuildinfo"
39+
],
40+
"engines": {
41+
"node": ">=16",
42+
"npm": ">=6"
43+
},
44+
"keywords": [],
45+
"bugs": {
46+
"url": "https://github.com/josh-development/providers/issues"
47+
},
48+
"homepage": "https://josh.evie.dev",
49+
"publishConfig": {
50+
"access": "public"
51+
},
52+
"devDependencies": {
53+
"fake-indexeddb": "^3.1.7",
54+
"jest": "^28.1.3"
55+
}
4556
}

packages/indexeddb/src/lib/IndexedDBProvider.ts

Lines changed: 108 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -2,157 +2,155 @@
22
// // @ts-nocheck
33
// Import the JoshProvider class.
44
import {
5-
ClearPayload,
6-
GetAllPayload,
7-
GetManyPayload,
8-
GetPayload,
9-
HasPayload,
10-
JoshProvider,
11-
Method,
12-
SetManyPayload,
13-
SetPayload,
14-
SizePayload
5+
ClearPayload,
6+
GetAllPayload,
7+
GetManyPayload,
8+
GetPayload,
9+
HasPayload,
10+
JoshProvider,
11+
Method,
12+
SetManyPayload,
13+
SetPayload,
14+
SizePayload
1515
} from '@joshdb/core';
16-
import { deleteFromObject, getFromObject, hasFromObject, setToObject } from '@realware/utilities';
16+
import { getFromObject, hasFromObject, setToObject } from '@realware/utilities';
1717
import DbHandler from './DbHandler';
1818

1919
// Create your provider class.
2020
export class IndexedDBProvider<StoredValue = unknown> extends JoshProvider<StoredValue> {
21-
private db: DbHandler;
22-
public constructor(options: JoshProvider.Options) {
23-
super(options);
24-
this.db = new DbHandler();
25-
}
21+
private db: DbHandler;
22+
public constructor(options: JoshProvider.Options) {
23+
super(options);
24+
this.db = new DbHandler();
25+
}
2626

27-
public async init(context: JoshProvider.Context<StoredValue>): Promise<JoshProvider.Context<StoredValue>> {
28-
context = await super.init(context);
29-
await this.db.init();
27+
public async init(context: JoshProvider.Context<StoredValue>): Promise<JoshProvider.Context<StoredValue>> {
28+
context = await super.init(context);
29+
await this.db.init();
3030

31-
return context;
32-
}
31+
return context;
32+
}
3333

34-
public async [Method.Get]<Value = StoredValue>(payload: GetPayload<Value>): Promise<GetPayload<Value>> {
35-
await this.check();
34+
public async [Method.Get]<Value = StoredValue>(payload: GetPayload<Value>): Promise<GetPayload<Value>> {
35+
await this.check();
3636

37-
const { key, path } = payload;
38-
const value = await this.db.get(key);
37+
const { key, path } = payload;
38+
const value = await this.db.get(key);
3939

40-
payload.data = path.length === 0 ? value : getFromObject(value, path);
40+
payload.data = path.length === 0 ? value : getFromObject(value, path);
4141

42-
return payload;
43-
}
42+
return payload;
43+
}
4444

45-
public async [Method.GetAll](payload: GetAllPayload<StoredValue>): Promise<GetAllPayload<StoredValue>> {
46-
await this.check();
45+
public async [Method.GetAll](payload: GetAllPayload<StoredValue>): Promise<GetAllPayload<StoredValue>> {
46+
await this.check();
4747

48-
payload.data = await this.db.getAll();
48+
payload.data = await this.db.getAll();
4949

50-
return payload;
51-
}
50+
return payload;
51+
}
5252

53-
public async [Method.GetMany](payload: GetManyPayload<StoredValue>): Promise<GetManyPayload<StoredValue>> {
54-
await this.check();
55-
// according to old method this could be made into an index search
53+
public async [Method.GetMany](payload: GetManyPayload<StoredValue>): Promise<GetManyPayload<StoredValue>> {
54+
await this.check();
55+
// according to old method this could be made into an index search
5656

57-
const { keys } = payload;
57+
const { keys } = payload;
58+
const data: { [key: string]: StoredValue } = await this.db.getAll();
5859

59-
const data: { [key: string]: StoredValue } = await this.db.getAll();
60+
payload.data = {};
6061

61-
payload.data = {};
62+
Object.entries(data).forEach(([key, val]) => {
63+
if (keys.includes(key)) {
64+
payload.data[key] = val;
65+
}
66+
});
6267

63-
Object.entries(data).forEach(([key, val]) => {
64-
if (keys.includes(key)) {
65-
payload.data[key] = val;
66-
}
67-
});
68+
return payload;
69+
}
6870

69-
return payload;
70-
}
71+
public async [Method.Set]<Value = StoredValue>(payload: SetPayload<Value>): Promise<SetPayload<Value>> {
72+
await this.check();
7173

72-
public async [Method.Set]<Value = StoredValue>(payload: SetPayload<Value>): Promise<SetPayload<Value>> {
73-
await this.check();
74+
const { key, value, path } = payload;
75+
let data = (await this.db.get(key)) || {};
7476

75-
const { key, value, path } = payload;
77+
if (path.length === 0) {
78+
data = value;
79+
} else {
80+
setToObject(data, path, value);
81+
}
7682

77-
let data = (await this.db.get(key)) || {};
83+
await this.db.set(key, data);
7884

79-
if (path.length === 0) {
80-
data = value;
81-
} else {
82-
setToObject(data, path, value);
83-
}
85+
return payload;
86+
}
8487

85-
await this.db.set(key, data);
88+
public async [Method.SetMany](payload: SetManyPayload<StoredValue>): Promise<SetManyPayload<StoredValue>> {
89+
await this.check();
8690

87-
return payload;
88-
}
91+
const { keys, value } = payload;
8992

90-
public async [Method.SetMany](payload: SetManyPayload<StoredValue>): Promise<SetManyPayload<StoredValue>> {
91-
await this.check();
93+
for (const key of keys) {
94+
await this.set({ key, value, path: [], method: Method.Set });
95+
// Incase overwrite is re-added.
96+
// const found = await this.get({ key, method: Method.Get, path: [] });
9297

93-
const { keys, value } = payload;
98+
// if (!found || (found && overwrite)) {
99+
// await this.set(key, null, val);
100+
// }
101+
}
94102

95-
for (const key of keys) {
96-
await this.set({ key, value, path: [], method: Method.Set });
97-
// Incase overwrite is re-added.
98-
// const found = await this.get({ key, method: Method.Get, path: [] });
103+
return payload;
104+
}
99105

100-
// if (!found || (found && overwrite)) {
101-
// await this.set(key, null, val);
102-
// }
103-
}
106+
public async [Method.Clear](payload: ClearPayload): Promise<ClearPayload> {
107+
await this.check();
108+
await this.db.clear();
104109

105-
return payload;
106-
}
110+
return payload;
111+
}
107112

108-
public async [Method.Clear](payload: ClearPayload): Promise<ClearPayload> {
109-
await this.check();
110-
await this.db.clear();
113+
public async [Method.Has](payload: HasPayload): Promise<HasPayload> {
114+
await this.check();
111115

112-
return payload;
113-
}
116+
const { key, path } = payload;
114117

115-
public async [Method.Has](payload: HasPayload): Promise<HasPayload> {
116-
await this.check();
118+
if (await this.db.has(key)) {
119+
payload.data = true;
117120

118-
const { key, path } = payload;
121+
if (path.length !== 0) payload.data = hasFromObject(await this.db.get(key), path);
122+
}
119123

120-
if (await this.db.has(key)) {
121-
payload.data = true;
124+
return payload;
125+
}
122126

123-
if (path.length !== 0) payload.data = hasFromObject(await this.db.get(key), path);
124-
}
127+
public async [Method.Size](payload: SizePayload): Promise<SizePayload> {
128+
await this.check();
129+
payload.data = await this.db.count();
125130

126-
return payload;
127-
}
131+
return payload;
132+
}
128133

129-
public async [Method.Size](payload: SizePayload): Promise<SizePayload> {
130-
await this.check();
131-
payload.data = await this.db.count();
134+
private async check(key: string | null = null, type: string[] | null = null, path: string[] = []) {
135+
if (!this.db) throw new Error('Database has been closed');
136+
if (!key || !type) return;
132137

133-
return payload;
134-
}
138+
const value = await this.get({ method: Method.Get, key, path });
135139

136-
private async check(key: string | null = null, type: string[] | null = null, path: string[] = []) {
137-
if (!this.db) throw new Error('Database has been closed');
138-
if (!key || !type) return;
140+
if (value === null) {
141+
throw new Error(
142+
`The document "${key}" of path "${path}" was not found in the database`
143+
// 'JoshTypeError',
144+
);
145+
}
139146

140-
const value = await this.get({ method: Method.Get, key, path });
147+
const valueType = value.constructor.name;
141148

142-
if (value === null) {
143-
throw new Error(
144-
`The document "${key}" of path "${path}" was not found in the database`
145-
// 'JoshTypeError',
146-
);
147-
}
148-
149-
const valueType = value.constructor.name;
150-
151-
if (!type.includes(valueType)) {
152-
throw new Error(
153-
`The property ${path ? `${path} ` : ''}in key "${key}" is not of type "${type.join('" or "')}"(key was of type "${valueType}")`
154-
// 'JoshTypeError',
155-
);
156-
}
157-
}
149+
if (!type.includes(valueType)) {
150+
throw new Error(
151+
`The property ${path ? `${path} ` : ''}in key "${key}" is not of type "${type.join('" or "')}"(key was of type "${valueType}")`
152+
// 'JoshTypeError',
153+
);
154+
}
155+
}
158156
}

0 commit comments

Comments
 (0)