Skip to content

Instantly share code, notes, and snippets.

@vasa-develop
Created May 3, 2020 10:37
Show Gist options
  • Save vasa-develop/6d64761edc01e463d427283d8a96aa81 to your computer and use it in GitHub Desktop.
Save vasa-develop/6d64761edc01e463d427283d8a96aa81 to your computer and use it in GitHub Desktop.
Using AvionDB with Node.js
// Import modules
const AvionDB = require("aviondb");
const IPFS = require("ipfs");
const ipfs = new IPFS();
const runExample = async () => {
await ipfs.ready;
// Creates a db named "DatabaseName"
const aviondb = await AvionDB.init("DatabaseName", ipfs);
// Creates a Collection named "employees"
const collection = await aviondb.initCollection("employees");
// Returns the List of collection names
await aviondb.listCollections()
// prints ['employees']
// Adding an employee document
await collection.insertOne({
hourly_pay: "$15",
name: "Elon",
ssn: "562-48-5384",
weekly_hours: 100,
});
// We also support multi-insert using collection.insert()
// See https://github.com/dappkit/aviondb/blob/master/API.md
// Search by a single field Or many!
var employee = await collection.findOne({
ssn: "562-48-5384",
});
// We also support find(), findById()
// See https://github.com/dappkit/aviondb/blob/master/API.md
// Returns the matching document
console.log(employee);
// Prints the above added JSON document
// Update a document
var updatedEmployee = await collection.update(
{ ssn: "562-48-5384" },
{ $set: { hourly_pay: '$100' } }
);
// We also support updateMany(), findOneAndUpdate()
// See https://github.com/dappkit/aviondb/blob/master/API.md
// Returns the updated document
console.log(updatedEmployee);
// Prints the updated JSON document
await collection.close(); // Collection will be closed.
await aviondb.drop(); // Drops the database
await aviondb.close(); // Closes all collections and binding database.
await ipfs.stop();
};
runExample();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment