V1.0
LocalDatabase
A simple front-end embedded database that wraps IndexedDB.
Installation
Either:
- Modules: Include the
LocalDatabase.js
,ColumnSchema.js
,TableSchema.js
,DatabaseSchema.js
files in your project and import the LocalDatabase file:
import LocalDatabase from './LocalDatabase';
- HTML Script: Include the LocalDatabase.all.js file and import like so:
<script src="LocalDatabase.all.js"></script>
Inserting & Updating
Inserting and updating are a single combined action and referred to as add
.
You can add a single entry into the database using:
await LocalDatabase.add("PersonsTable",
{id: 1, firstName: "John", lastName: "Doe", age: 42}
);
You can add multiple entries into the database using:
await LocalDatabase.multiAdd("PersonsTable", [
{id: 4, firstName: "David", lastName: "Gray", age: 20},
{id: 6, firstName: "John", lastName: "Gilmore", age: 69},
{id: 5, firstName: "John", lastName: "Robson", age: 69},
{id: 3, firstName: "Harry", lastName: "Gardener", age: 66}
]);
Select Queries
To select someone from the People table that has the firstName John, age 69, and their last name is not Gilmore:
PersonsTable
id | firstName | lastName | age |
---|---|---|---|
1 | John | Doe | 42 |
2 | Bob | Smith | 35 |
4 | David | Gray | 20 |
6 | John | Gilmore | 69 |
5 | John | Robson | 69 |
3 | Harry | Gardener | 66 |
await LocalDatabase.select("PersonsTable", {firstName: "John", age: 69, lastName: {$ne: "Gilmore"}});
The above line of code selects the following row:
6 | John | Gilmore | 69 |
NodeJS
This embedded database is made for use on the front-end of a website although you can modify export default
to module.exports
in all classes:
export default LocalDatabase;
to
module.exports = LocalDatabase;
to use this in NodeJS.
Example usage:
<script src="LocalDatabase.all.js"></script>
<script>
(async() => {
// Create the schema
const peopleTable = new LocalDatabase.Table("People",
new LocalDatabase.Column("id", {unique: true}),
[
new LocalDatabase.Column("firstName"),
new LocalDatabase.Column("lastName"),
new LocalDatabase.Column("age")
]
);
const dbSchema = new LocalDatabase.Database("MyDatabase", [peopleTable]);
// Initialise the database
await LocalDatabase.init(dbSchema);
// Insert some data
await LocalDatabase.add(peopleTable.name, {id: 1, firstName: "John", lastName: "Doe", age: 42});
await LocalDatabase.add("People", {id: 2, firstName: "Bob", lastName: "Smith", age: 35});
await LocalDatabase.multiAdd(peopleTable.name, [
{id: 4, firstName: "David", lastName: "Gray", age: 20},
{id: 6, firstName: "John", lastName: "Gilmore", age: 69},
{id: 5, firstName: "John", lastName: "Robson", age: 69},
{id: 3, firstName: "Harry", lastName: "Gardener", age: 66}
]);
// Query the database
const allEntries = await LocalDatabase.select("People", {age: {$gt: 0}});
console.log("All Entries", allEntries);
const queryResult = await LocalDatabase.select("People", {firstName: "John", age: 69, lastName: {$ne: "Gilmore"}});
console.log("Query result:", queryResult);
})()
</script>