Compare commits

...

2 Commits

Author SHA1 Message Date
a08b0c4eea Fixed database init loop
All checks were successful
Build / Build NPM Project (push) Successful in 40s
Build / Tag Version (push) Successful in 8s
Build / Publish Documentation (push) Successful in 34s
2025-05-27 15:42:00 -04:00
1b03ae875b Fixed database includes
All checks were successful
Build / Build NPM Project (push) Successful in 41s
Build / Tag Version (push) Successful in 7s
Build / Publish Documentation (push) Successful in 35s
2025-05-27 15:13:53 -04:00
3 changed files with 12 additions and 6 deletions

View File

@ -3,7 +3,8 @@
<script type="module"> <script type="module">
import {Cache, Database} from './dist/index.mjs'; import {Cache, Database} from './dist/index.mjs';
const db = new Database('test', []); const db = new Database('test', [123]);
window['table'] = db.table(123);
</script> </script>
</body> </body>
</html> </html>

View File

@ -1,6 +1,6 @@
{ {
"name": "@ztimson/utils", "name": "@ztimson/utils",
"version": "0.25.5", "version": "0.25.7",
"description": "Utility library", "description": "Utility library",
"author": "Zak Timson", "author": "Zak Timson",
"license": "MIT", "license": "MIT",

View File

@ -7,11 +7,16 @@ export type TableOptions = {
export class Database { export class Database {
connection!: Promise<IDBDatabase>; connection!: Promise<IDBDatabase>;
tables!: TableOptions[];
constructor(public readonly database: string, public readonly tables: (string | TableOptions)[], public version?: number) { constructor(public readonly database: string, tables: (string | TableOptions)[], public version?: number) {
this.connection = new Promise((resolve, reject) => { this.connection = new Promise((resolve, reject) => {
const req = indexedDB.open(this.database, this.version); const req = indexedDB.open(this.database, this.version);
const tableNames = new ASet(tables.map(t => (typeof t == 'object' ? t.name : t).toString())); this.tables = tables.map(t => {
t = typeof t == 'object' ? t : {name: t};
return {...t, name: t.name.toString()};
});
const tableNames = new ASet(this.tables.map(t => t.name));
req.onerror = () => reject(req.error); req.onerror = () => reject(req.error);
@ -35,8 +40,8 @@ export class Database {
}); });
} }
includes(name: string): boolean { includes(name: any): boolean {
return this.tables.some(t => (typeof t === 'string' ? name === t : name === t.name)); return !!this.tables.find(t => t.name == name.toString());
} }
table<K extends IDBValidKey = any, T = any>(name: any): Table<K, T> { table<K extends IDBValidKey = any, T = any>(name: any): Table<K, T> {