Skip to content

viniceosm/vinidex

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

36 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

vinidex

Lib to use easily indexedDB

IndexedDB is a low-level API for client-side storage of significant amounts of structured data, including files/blobs.

Nomenclatures of IndexedDB

Object store

The mechanism by which data is stored in the database.

Examples

Lib Methods

schema(objectStores)

This method creates object stores when database is upgraded, you need create object store before put value in.

Call this method before opens database.

// Array de objectStore para onupgradeneeded
let objectStores = [
  ['Students', { keyPath: 'id' }],
  ['Car', { keyPath: 'id' }]
];

vinidex.schema(objectStores);

// after this you can open db
await vinidex.init('myNameIndexedDB');

init(nameDb, versionDb = 1)

await vinidex.init('myNameIndexedDB'); // version will be 1
await vinidex.init('myNameIndexedDB', 7);

model(nameObjectStore)

Method model return an object with methods CRUD for the objectStore, this is helps in writing, because you don't need use every time vinidex

let Students = vinidex.model('Students');

findById(id)

let firstStudent = await Students.findById(0);

find(query = {})

query is condition for search

Equal

let izadora = await Students.find({ name: 'Izadora' });

Find all

let allStudents = await Students.find(); // all students
// or
allStudents = await Students.find({}); // all students

$gt

Greater than >

let greaterThan20 = await Students.find({ age: { $gt: 20} });

$gte

Greater than or equal to >=

let gte21 = await Students.find({ age: { $gte: 21} });

$lt

Less than <

let lessThan24 = await Students.find({ age: { $lt: 24} });

$lte

Less than or equal to <=

let lte30 = await Students.find({ age: { $lte: 30} });

$ne

Not equal !==

let ne30 = await Students.find({ name: { $ne: 'Will'} });

Many condition

name == 'Izadora' && age >= 20 && age < 100

let izadora = await Students.find({
  name: 'Izadora',
  age: { $gte: 20, $lt: 100 },
});

updateById(id, atributos)

Students.updateById(0, {
  name: 'Roberts'
});

update(query, atributos)

Students.update({ name: 'Julia' }, {
  name: 'Roberts'
});

delete(id)

Students.delete(0);

add(valueAdd)

await Students.add({ id: 0, name: 'Vinicius' });

Methods that needs to pass objectStore name

The alternative of this is use an object model

add(nameObjectStore, valueAdd)

await vinidex.add('Car', { id: 0, name: 'Lancer' });

delete(nameObjectStore, id)

vinidex.delete('Car', 0);

select(nameObjectStore, id)

let lancer = await vinidex.select('Car', 0);

find(nameObjectStore, query = {})

query is condition for search

let lancer = await vinidex.find('Car', { name: 'Lancer' });

updateById(nameObjectStore, id, atributos)

vinidex.updateById('Car', 0, {
  name: 'F-Pace'
});

update(nameObjectStore, query, atributos)

vinidex.update('car', { name: 'Lancer' }, {
  name: 'F-Pace'
});