async function writeCards(page = 1) {
    try {
        let totalCards = 0;
        let totalTime = 0;
        let lastPageTime = new Date();
        console.log(`Started card import at ${lastPageTime.toString()}`);
        for (let pageCards; pageCards = await scryfall.getAllCards(page); page++, totalCards += pageCards.length) {
            console.log(`Cards in page: ${pageCards.length}`);
            if (!pageCards.length) {
                break;
            }
            await knex.batchInsert("scryfall.card", pageCards.map((card) => {
                cards.push(card);
                for (let key in card) {
                    if (card[key] && typeof (card[key]) === "object") {
                        card[key] = JSON.stringify(card[key]);
                    }
                }
                if (card.mana_cost === "") {
                    card.mana_cost = null;
                }
                return card;
            })).then();
            let thisPageTime = new Date();
            let delta = thisPageTime - lastPageTime;
            totalTime += delta;
            lastPageTime = thisPageTime;
            console.log(`Page ${page} imported in ${delta} ms.`);
        }
        console.log(`Cards imported. ${totalCards} total cards processed in ${(totalTime / 60000).toPrecision(4)} minutes.`);
    } catch (err) {
        console.log(err);
        console.log(`Card import error - restarting at page ${page}.`);
        await writeCards(page)
    }
}
/**
 * Pages through all available cards and writes them to the DB.
 * @param {number?} page The page number to start on.
 */
Pages through all available cards and writes them to the DB.
@param {number?} page The page number to start on.
Pages through all available cards and writes them to the DB . @param { number? } page The page number to start on .
async function writeCards(page = 1) {
    try {
        let totalCards = 0;
        let totalTime = 0;
        let lastPageTime = new Date();
        console.log(`Started card import at ${lastPageTime.toString()}`);
        for (let pageCards; pageCards = await scryfall.getAllCards(page); page++, totalCards += pageCards.length) {
            console.log(`Cards in page: ${pageCards.length}`);
            if (!pageCards.length) {
                break;
            }
            await knex.batchInsert("scryfall.card", pageCards.map((card) => {
                cards.push(card);
                for (let key in card) {
                    if (card[key] && typeof (card[key]) === "object") {
                        card[key] = JSON.stringify(card[key]);
                    }
                }
                if (card.mana_cost === "") {
                    card.mana_cost = null;
                }
                return card;
            })).then();
            let thisPageTime = new Date();
            let delta = thisPageTime - lastPageTime;
            totalTime += delta;
            lastPageTime = thisPageTime;
            console.log(`Page ${page} imported in ${delta} ms.`);
        }
        console.log(`Cards imported. ${totalCards} total cards processed in ${(totalTime / 60000).toPrecision(4)} minutes.`);
    } catch (err) {
        console.log(err);
        console.log(`Card import error - restarting at page ${page}.`);
        await writeCards(page)
    }
}
async function writeCards ( page = 1 ) { try { let totalCards = 0 ; let totalTime = 0 ; let lastPageTime = new Date ( ) ; console . log ( ` ${ lastPageTime . toString ( ) } ` ) ; for ( let pageCards ; pageCards = await scryfall . getAllCards ( page ) ; page ++ , totalCards += pageCards . length ) { console . log ( ` ${ pageCards . length } ` ) ; if ( ! pageCards . length ) { break ; } await knex . batchInsert ( "scryfall.card" , pageCards . map ( ( card ) => { cards . push ( card ) ; for ( let key in card ) { if ( card [ key ] && typeof ( card [ key ] ) === "object" ) { card [ key ] = JSON . stringify ( card [ key ] ) ; } } if ( card . mana_cost === "" ) { card . mana_cost = null ; } return card ; } ) ) . then ( ) ; let thisPageTime = new Date ( ) ; let delta = thisPageTime - lastPageTime ; totalTime += delta ; lastPageTime = thisPageTime ; console . log ( ` ${ page } ${ delta } ` ) ; } console . log ( ` ${ totalCards } ${ ( totalTime / 60000 ) . toPrecision ( 4 ) } ` ) ; } catch ( err ) { console . log ( err ) ; console . log ( ` ${ page } ` ) ; await writeCards ( page ) } }
Pages through all available cards and writes them to the DB.
Pages through all available cards and writes them to the DB .