Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
  <script src="http://raw.githubusercontent.com/LokiJS-Forge/LokiDB/master/dist/packages/loki/lokidb.loki.js"></script>
  <script src="http://raw.githubusercontent.com/LokiJS-Forge/LokiDB/master/dist/packages/indexed-storage/lokidb.indexed-storage.js"></script>
  <script src="http://raw.githubusercontent.com/LokiJS-Forge/LokiDB/master/dist/packages/partitioning-adapter/lokidb.partitioning-adapter.js"></script>
  
  <script>
    console.log('test 1');
    
    window.Loki = window['@lokidb/loki'].Loki;
    window.IndexedStorage = window['@lokidb/indexed-storage'].IndexedStorage;
    window.PartitioningAdapter = window['@lokidb/partitioning-adapter'].PartitioningAdapter;
    var dbName = 'test.db';
            
     console.log('test 2');
    let testCollection; // we will assign it after persistence
    IndexedStorage.register();
    PartitioningAdapter.register();
    
    let idbAdapter = new IndexedStorage(dbName);
    let paAdapter = new PartitioningAdapter(
        idbAdapter,
        {
            paging: true, 
            pageSize: 5*1024*1024 
        }
    );
    let options = {
        env: 'BROWSER',
        serializationMethod: 'normal'
    };
    wdb = new Loki(dbName);
    let persistenceOptions = {
        adapter: paAdapter,
        autosave: window.self === window.top,
        autosaveInterval: 1000,
        autoload: window.self === window.top,
        throttledSaves: true,
        persistenceMethod: 'indexed-storage',
    };
    wdb.initializePersistence(persistenceOptions)
    .then(() => {
        console.log('Loki Persistence initialized');
        // Initialize collection
        testCollection = wdb.getCollection('Test');
        if (!testCollection) testCollection = wdb.addCollection('Test');
        // Display number of rows IN collection
        const oldRecords = testCollection.find();
        console.log('Number of rows loaded from IndexedDB collection:',oldRecords.length);
        // Use verbose method to get maximum id of existing records.
        const oldIds = oldRecords.map((r) => parseInt(r.id));
        const oldIdsLength = oldIds.length;
        let maxId = oldIds[0] || 0;
        for (let i = 1; i < oldIdsLength; ++i) {
            if (oldIds[i] > maxId) {
                maxId = oldIds[i];
            }
        }
        // Fetch placeholder data
        return fetch('https://jsonplaceholder.typicode.com/photos') 
            .then(response => response.json())
            .then(json => {
                console.log('Enlarging result set from jsonplaceholder...');
                let bSet = json;
                // Make set 5M (5,000 x 1,000 = 5,000,000)
                bSet = [
                    ...bSet,...bSet,...bSet,...bSet,...bSet,
                    ...bSet,...bSet,...bSet,...bSet,...bSet,
                ];
                bSet = [
                    ...bSet,...bSet,...bSet,...bSet,...bSet,
                    ...bSet,...bSet,...bSet,...bSet,...bSet,
                ];
                // bSet = [
                //  ...bSet,...bSet,...bSet,...bSet,...bSet,
                //  ...bSet,...bSet,...bSet,...bSet,...bSet,
                // ];
                
                console.log('Cloning',bSet.length,'records...');
                bSet = JSON.parse(JSON.stringify(bSet)); // clone so we can reset id field
                console.log('Resetting',bSet.length,'id fields, starting at',maxId+1);
                for (i = 0; i < bSet.length; i += 1) bSet[i].id = i+maxId+1; // reset id field
                console.log('Adding',bSet.length,'rows from jsonplaceholder into LokiDB...');
                testCollection.insert(bSet);
                console.log('Added rows from jsonplaceholder. Refresh to see if they stick around.');
            })
            // return typeof s === 'function' ? s() : null;
    })
    .catch((e) => console.error('initializePersistence error', e.message));
  </script>
  
  
  </head>
<body>
</body>
</html>
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers