Created
July 5, 2023 13:36
-
-
Save bethropolis/1f642da2d97e89bf46477f5bf420db56 to your computer and use it in GitHub Desktop.
a JS class that acts as a wrapper around Dexie.js major functions
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
// A JS class that wraps Dexie.js major functions | |
class DexieWrapper { | |
// Constructor takes a database name and an optional schema object | |
constructor(dbName, schema = {}) { | |
// Create a new Dexie instance with the given name | |
this.db = new Dexie(dbName); | |
// Define the schema if given | |
if (Object.keys(schema).length > 0) { | |
this.db.version(1).stores(schema); | |
} | |
// Open the database | |
this.db.open(); | |
} | |
// A method to add a new record to a table | |
async add(table, data) { | |
// Check if the table exists | |
if (!this.db[table]) { | |
throw new Error(`Table ${table} does not exist`); | |
} | |
// Try to add the data and return the generated key | |
try { | |
const key = await this.db[table].add(data); | |
return key; | |
} catch (error) { | |
// Handle any error | |
console.error(error); | |
throw error; | |
} | |
} | |
// A method to get a record by key from a table | |
async get(table, key) { | |
// Check if the table exists | |
if (!this.db[table]) { | |
throw new Error(`Table ${table} does not exist`); | |
} | |
// Try to get the record and return it | |
try { | |
const record = await this.db[table].get(key); | |
return record; | |
} catch (error) { | |
// Handle any error | |
console.error(error); | |
throw error; | |
} | |
} | |
// A method to update a record by key in a table | |
async update(table, key, data) { | |
// Check if the table exists | |
if (!this.db[table]) { | |
throw new Error(`Table ${table} does not exist`); | |
} | |
// Try to update the record and return the number of updated records | |
try { | |
const count = await this.db[table].update(key, data); | |
return count; | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
} | |
// A method to delete a record by key from a table | |
async delete(table, key) { | |
// Check if the table exists | |
if (!this.db[table]) { | |
throw new Error(`Table ${table} does not exist`); | |
} | |
// Try to delete the record and return the number of deleted records | |
try { | |
const count = await this.db[table].delete(key); | |
return count; | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
} | |
// A method to query records from a table using Dexie's query syntax | |
async query(table, query) { | |
// Check if the table exists | |
if (!this.db[table]) { | |
throw new Error(`Table ${table} does not exist`); | |
} | |
// Try to execute the query and return the result as an array | |
try { | |
const result = await this.db[table].where(query).toArray(); | |
return result; | |
} catch (error) { | |
console.error(error); | |
throw error; | |
} | |
} | |
// A method to close the database connection | |
close() { | |
this.db.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
use cases:
happy coding 💜