{"size": 1780, "ext": "js", "max_stars_repo_path": "js/Module.js", "max_stars_repo_name": "Oseias-maker/crud-javascript", "max_stars_repo_stars_event_min_datetime": "2021-05-13T20:35:00.000Z", "max_stars_repo_stars_event_max_datetime": "2021-05-13T20:35:00.000Z", "max_issues_repo_path": "js/Module.js", "max_issues_repo_name": "Oseias-maker/crud-javascript", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "js/Module.js", "max_forks_repo_name": "Oseias-maker/crud-javascript", "max_forks_repo_forks_event_min_datetime": "2021-05-06T13:04:47.000Z", "max_forks_repo_forks_event_max_datetime": "2022-03-08T00:58:33.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 4, "avg_line_length": 22.8205128205, "max_line_length": 79, "alphanum_fraction": 0.5415730337}
const productsdb = (dbname, table) => {
    const db = new Dexie(dbname);
    db.version(1).stores(table);
    db.open();

    return db;
};

const bulkcreate = (dbtable, data) => {
    let flag = empty(data);
    if (flag) {
        dbtable.bulkAdd([data]);
        console.log("data inserted successfully...!");
    } else {
        console.log("Por favor, forneça dados..!");
    }
    return flag;
};

// criando elementos dinâmicos
const createEle = (tagname, appendTo, fn) => {
    const element = document.createElement(tagname);
    if (appendTo) appendTo.appendChild(element);
    if (fn) fn(element);
};

// checando a validação da caixa de texto
const empty = object => {
    let flag = false;
    for (const value in object) {
        if (object[value] != "" && object.hasOwnProperty(value)) {
            flag = true;
        } else {
            flag = false;
        }
    }
    return flag;
};

// getData direto do database
const getData = (dbname, fn) => {
    let index = 0;
    let obj = {};
    dbname.count(count => {
        // contando as linhas da tabela usando um método de conta
        if (count) {
            dbname.each(table => {
                // tabela => retornando um objeto data da tabela
                // para organizar a ordem vamos criar um looping
                obj = SortObj(table);
                fn(obj, index++); // chamando uma função com um argumento data 
            });
        } else {
            fn(0);
        }
    });
};

const SortObj = (sortobj) => {
    let obj = {};
    obj = {
        id: sortobj.id,
        name: sortobj.name,
        seller: sortobj.seller,
        price: sortobj.price
    };
    return obj;
}


export default productsdb;
export {
    bulkcreate,
    createEle,
    getData,
    SortObj
};
