Published
Edited
1 fork
2 stars
Insert cell
Insert cell
Insert cell
lokiVersion = "1.5.12"
Insert cell
loki = require(`lokijs@${lokiVersion}/build/lokijs.min.js`)
Insert cell
LokiIndexedAdapter = require(`lokijs@${lokiVersion}/build/loki-indexed-adapter.min.js`)
Insert cell
db = {
return new Promise((resolve, reject) => {
try {
const db = new loki('experiment.db', {
adapter: new LokiIndexedAdapter(),
autoload: true,
autoloadCallback: () => resolve(db)
});
} catch (error) {
reject(error);
}
});
}
Insert cell
Insert cell
Inputs.button("Delete Database", {
reduce: () =>
db.close(() => {
db.deleteDatabase(() => {
mutable users.clear();
mutable users = users;
});
})
})
Insert cell
Insert cell
mutable users = {
const users = db.getCollection("users");
if (users) return users;
return db.addCollection('users', { indices: ['email'] });
}
Insert cell
Inputs.table(users.data)
Insert cell
Insert cell
Inputs.button("Clear Rows", {
reduce: () => {
users.clear();
db.save();
mutable users = users;
}
})
Insert cell
Insert cell
Inputs.button("Insert Rows", {
reduce: () => {
users.insert({ name: 'odin', email: 'odin@lokijs.org', age: 38 });
users.insert({ name: 'thor', email: 'thor@lokijs.org', age: 25 });
users.insert({ name: 'stan', email: 'stan@lokijs.org', age: 29 });
users.insert({ name: 'oliver', email: 'oliver@lokijs.org', age: 31 });
users.insert({ name: 'hector', email: 'hector@lokijs.org', age: 15 });
users.insert({ name: 'achilles', email: 'achilles@lokijs.org', age: 31 });
db.save();
mutable users = users;
}
})
Insert cell
Insert cell
csvParser = require("papaparse")
Insert cell
chunkSize = 5000
Insert cell
viewof file = html`<input type=file accept=".csv">`
Insert cell
uploadResult = {
return new Promise((resolve, reject) => {
try {
const workings = { chunkCount: 0, errors: [], meta: {}, rowCount: 0 };
csvParser.parse(file, {
chunk: chunk => processChunk(workings, chunk),
chunkSize: chunkSize,
complete: () => resolve(buildSummary(workings)),
header: true,
skipEmptyLines: true
});
} catch (error) {
reject(error);
}
});
}
Insert cell
processChunk = (workings, chunk) => {
workings.chunkCount++;
workings.errors = chunk.errors;
workings.meta = chunk.meta;
workings.rowCount += chunk.data.length;

users.insert(chunk.data);
db.save();
}
Insert cell
buildSummary = workings => {
return {
aborted: workings.meta.aborted,
chunkCount: workings.chunkCount,
chunkSize: chunkSize,
delimiter: workings.meta.delimiter,
errors: workings.errors,
fieldNames: workings.meta.fields,
lastModified: file.lastModified,
lastModifiedDate: file.lastModifiedDate,
linebreak: workings.meta.linebreak,
name: file.name,
rowCount: workings.rowCount,
size: file.size,
type: file.type,
truncated: workings.meta.truncated
};
}
Insert cell
Insert cell
result = users
.chain()
.find({ age: { $gt: 25 } })
.where(function(obj) {
return obj.name.indexOf("in") != -1;
})
.simplesort("age")
.offset(0)
.limit(10)
.data()
Insert cell
Inputs.table(result)
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more