-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(app): add idb package, update indedex-db service
- Loading branch information
Showing
11 changed files
with
81 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 26 additions & 73 deletions
99
apps/app/src/app/service/indexed-db/indexed-db.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,112 +1,65 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { DBSchema, IDBPDatabase, openDB } from 'idb'; | ||
|
||
import { LocalStorageKey } from './definition/indexed-db-key.enum'; | ||
|
||
interface KeyValueStoreSchema extends DBSchema { | ||
KeyValueStore: { | ||
key: LocalStorageKey; | ||
value: unknown; | ||
}; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root', | ||
}) | ||
export class IndexedDBService { | ||
private dbName = 'PlaySetOnline'; | ||
private storeName = 'KeyValueStore'; | ||
private dbVersion = 1; | ||
private dbPromise: Promise<IDBDatabase>; | ||
private readonly dbName = 'PlaySetOnline'; | ||
private readonly storeName = 'KeyValueStore'; | ||
private readonly dbVersion = 1; | ||
private readonly dbPromise: Promise<IDBPDatabase<KeyValueStoreSchema>>; | ||
|
||
constructor() { | ||
this.dbPromise = this.initDB(); | ||
} | ||
|
||
async setItem<T>(key: LocalStorageKey, data: T): Promise<void> { | ||
async setItem<T>(key: LocalStorageKey, value: T): Promise<void> { | ||
const db = await this.dbPromise; | ||
|
||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction([this.storeName], 'readwrite'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.put({ key, value: data }); | ||
|
||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
|
||
request.onerror = (event) => { | ||
reject((event.target as IDBRequest).error); | ||
}; | ||
}); | ||
await db.put(this.storeName, { key, value }); | ||
} | ||
|
||
async getItem<T>(key: LocalStorageKey): Promise<T | undefined> { | ||
const db = await this.dbPromise; | ||
|
||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction([this.storeName], 'readonly'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.get(key); | ||
const item = await db.get(this.storeName, key); | ||
|
||
request.onsuccess = (event) => { | ||
const result = (event.target as IDBRequest).result; | ||
resolve(result ? (result.value as T) : undefined); | ||
}; | ||
const typedItem = item as { value: T } | undefined; | ||
|
||
request.onerror = (event) => { | ||
reject((event.target as IDBRequest).error); | ||
}; | ||
}); | ||
return typedItem?.value; | ||
} | ||
|
||
async deleteItem(key: LocalStorageKey): Promise<void> { | ||
const db = await this.dbPromise; | ||
|
||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction([this.storeName], 'readwrite'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.delete(key); | ||
|
||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
|
||
request.onerror = (event) => { | ||
reject((event.target as IDBRequest).error); | ||
}; | ||
}); | ||
await db.delete(this.storeName, key); | ||
} | ||
|
||
async clearStore(): Promise<void> { | ||
const db = await this.dbPromise; | ||
|
||
return new Promise((resolve, reject) => { | ||
const transaction = db.transaction([this.storeName], 'readwrite'); | ||
const store = transaction.objectStore(this.storeName); | ||
const request = store.clear(); | ||
|
||
request.onsuccess = () => { | ||
resolve(); | ||
}; | ||
|
||
request.onerror = (event) => { | ||
reject((event.target as IDBRequest).error); | ||
}; | ||
}); | ||
await db.clear(this.storeName); | ||
} | ||
|
||
private initDB(): Promise<IDBDatabase> { | ||
return new Promise((resolve, reject) => { | ||
const request = indexedDB.open(this.dbName, this.dbVersion); | ||
private initDB(): Promise<IDBPDatabase<KeyValueStoreSchema>> { | ||
const storeName = this.storeName; | ||
|
||
request.onupgradeneeded = (event) => { | ||
const db = (event.target as IDBOpenDBRequest).result; | ||
if (!db.objectStoreNames.contains(this.storeName)) { | ||
db.createObjectStore(this.storeName, { keyPath: 'key' }); | ||
return openDB<KeyValueStoreSchema>(this.dbName, this.dbVersion, { | ||
upgrade(db) { | ||
if (!db.objectStoreNames.contains(storeName)) { | ||
db.createObjectStore(storeName, { keyPath: 'key' }); | ||
} | ||
}; | ||
|
||
request.onsuccess = (event) => { | ||
const db = (event.target as IDBOpenDBRequest).result; | ||
resolve(db); | ||
}; | ||
|
||
request.onerror = (event) => { | ||
reject((event.target as IDBOpenDBRequest).error); | ||
}; | ||
}, | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
"codegen", | ||
"commitlint", | ||
"devkit", | ||
"IDBP", | ||
"mikro", | ||
"nestjs", | ||
"ngneat", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export default function eslintErrorsToWarnings(rules) { | ||
return Object.fromEntries( | ||
Object.entries(rules).map(([ruleName, ruleValue]) => { | ||
ruleValue = | ||
typeof ruleValue === 'string' | ||
? ruleValue?.replace('error', 'warn') | ||
: ruleValue[0]?.replace('error', 'warn'); | ||
|
||
return [ruleName, ruleValue]; | ||
}), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
const path = require('path'); | ||
import { relative } from 'node:path'; | ||
|
||
/* | ||
* Temporary workaround for debugging Node applications being built with webpack and Nx. | ||
* See: https://github.com/nrwl/nx/issues/14708#issuecomment-1457996600 | ||
*/ | ||
module.exports = function patchNxSourceMapPaths(config) { | ||
config.output.devtoolModuleFilenameTemplate = function (info) { | ||
const rel = path.relative(process.cwd(), info.absoluteResourcePath); | ||
return `webpack:///./${rel}`; | ||
}; | ||
config.output.devtoolModuleFilenameTemplate = function (info) { | ||
const rel = relative(process.cwd(), info.absoluteResourcePath); | ||
return `webpack:///./${rel}`; | ||
}; | ||
|
||
config.devtool = 'source-map'; | ||
config.devtool = 'source-map'; | ||
|
||
return config; | ||
return config; | ||
}; |