Skip to content

Instantly share code, notes, and snippets.

@bethropolis
Created July 5, 2023 13:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bethropolis/1f642da2d97e89bf46477f5bf420db56 to your computer and use it in GitHub Desktop.
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
// 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();
}
}
@bethropolis
Copy link
Author

use cases:

// Create a new instance of DexieWrapper with a database name and a schema object
const db = new DexieWrapper("MyDatabase", {
  friends: "++id,name,age",
});

// Add a new record to the friends table
db.add("friends", { name: "Alice", age: 20 }).then((key) => {
  console.log("Added friend with key:", key);
});

// Get a record by key from the friends table
db.get("friends", 1).then((record) => {
  console.log("Got friend with key 1:", record);
});

// Update a record by key in the friends table
db.update("friends", 1, { age: 21 }).then((count) => {
  console.log("Updated", count, "friend(s) with key 1");
});

// Delete a record by key from the friends table
db.delete("friends", 1).then((count) => {
  console.log("Deleted", count, "friend(s) with key 1");
});

// Query records from the friends table using Dexie's query syntax
db.query("friends", { age: { "<": 25 } }).then((result) => {
  console.log("Found young friends:", result);
});

// Close the database connection
db.close();

happy coding 💜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment