<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 xKeyboard Shortcuts
Shortcut | Action |
---|---|
ctrl + [num] | Toggle nth panel |
ctrl + 0 | Close focused panel |
ctrl + enter | Re-render output. If console visible: run JS in console |
Ctrl + l | Clear the console |
ctrl + / | Toggle comment on selected lines |
ctrl + ] | Indents selected lines |
ctrl + [ | Unindents selected lines |
tab | Code complete & Emmet expand |
ctrl + shift + L | Beautify code in active panel |
ctrl + s | Save & lock current Bin from further changes |
ctrl + shift + s | Open the share options |
ctrl + y | Archive Bin |
Complete list of JS Bin shortcuts |
JS Bin URLs
URL | Action |
---|---|
/ | Show the full rendered output. This content will update in real time as it's updated from the /edit url. |
/edit | Edit the current bin |
/watch | Follow a Code Casting session |
/embed | Create an embeddable version of the bin |
/latest | Load the very latest bin (/latest goes in place of the revision) |
/[username]/last | View the last edited bin for this user |
/[username]/last/edit | Edit the last edited bin for this user |
/[username]/last/watch | Follow the Code Casting session for the latest bin for this user |
/quiet | Remove analytics and edit button from rendered output |
.js | Load only the JavaScript for a bin |
.css | Load only the CSS for a bin |
Except for username prefixed urls, the url may start with http://jsbin.com/abc and the url fragments can be added to the url to view it differently. |