Docs - https://github.com/dannyconnell/localbase
Playground - https://github.com/dannyconnell/localbase#localbase-playground
Localbase is an libray which controls a data structure indexedDB. This is aprto fthe browser and allows developers to store small amounts of data in an organised structure. Each record of data has a unique key given to it to make it unique, and can also have flexible data structures. This storage application has a schema and allows for searching of data. The localbase library allows developers to create, read, update and delete data. Its important to note that indexedb is only local storage on the device you are using but a great way to be introduced to how bigger databases may operate.
Localbase works on the following structures:
Databsaes contain Collections - these are like individual tables of data about one entity (users, books, cars etc)
Each collection contains documents. These are the information about each enity. In databases we call these records.
All types of databases store and organise data to create information. The main processes include:
Create records or documents - adding information to the database through input and collection
Read - Look up and find the infromation that the user requires
Update existing records/documents
Delete exisiting records/documents
Within any database structure we like have a key field that is unique to every other record in the database. It is sometimes referred to as a Primary Key. These keys can be generated by the system automatically or manually by the user. But they must be unique and it should never change.
Allows for duplication of peoples names to be identified - what of we have two John Smiths?
Creates an easy way to reference and search for records i.e. Search for rego plate number
In localbase, they automaically generate a random key of characters. in other databases it may auto incremeted numbers
Each collection is made up of properties called fields. These are consistent in each collection to make storing and filtering data much simpler. Localbase has a nosql structure which means that the fields are not strict. This means that each record does not have to have all the same properties which is great for easy development.
When designing a collection which is normally a collection of data about similar entities, its important to think about what fields are required to allow your application to function properly. For example when storing an address, will you have just one field that stores street, suburb, postcode and state or will you have seperate fields for these? In most cases the second option is better as it allows you to filter the database more easily and can be more helpful in ensuring the data collected is valid and not missing.
Include in the header of your HTML - <script src="https://unpkg.com/localbase/dist/localbase.dev.js"></script>
In your JS file you create a new instance with - let db = new Localbase('db')
db.collection('users').add({
id: 1,
name: 'Bill',
age: 47
})
db.collection('users').get().then(users => {
console.log(users)
})
// [
// { id: 1, name: 'Bill', age: 47 },
// { id: 2, name: 'Paul', age: 34 }
// ]
Get a collection and order it by a particular field (ascending).
db.collection('users').orderBy('age').get().then(users => {
console.log('users: ', users)
})
// [
// { id: 2, name: 'Paul', age: 34 },
// { id: 1, name: 'Bill', age: 47 }
// ]
Descending order
db.collection('users').orderBy('name', 'desc').get().then(users => {
console.log('users: ', users)
})
// [
// { id: 2, name: 'Paul', age: 34 },
// { id: 1, name: 'Bill', age: 47 }
// ]
Limit how many documents are returned
db.collection('users').orderBy('name', 'desc').limit(1).get().then(users => {
console.log('users: ', users)
})
// [
// { id: 2, name: 'Paul', age: 34 }
// ]
db.collection('users').doc({ id: 1 }).update({
name: 'William'
})
// [
// { id: 1, name: 'William', age: 47 },
// { id: 2, name: 'Paul', age: 34 }
// ]
Localbase cannot filter collections of data very easily. So you will need to use a function called filter to search a collection after grabbing all the data from the database collection.
db.collection('users').get().then(users => {
var findSmith = users.filter(function (el) {
return el.lastname == "Smith";
});
console.log(paidJobs);
})
})