Skip to content

Instantly share code, notes, and snippets.

@vasa-develop
Created May 3, 2020 10:38
Show Gist options
  • Save vasa-develop/c93f8354bcf111cd0d50c0dbdc809d97 to your computer and use it in GitHub Desktop.
Save vasa-develop/c93f8354bcf111cd0d50c0dbdc809d97 to your computer and use it in GitHub Desktop.
Using AvionDB with Browser
<!--Using AvionDB in Browser-->
<!--IPFS CDN Link-->
<script src="https://cdn.jsdelivr.net/npm/ipfs/dist/index.min.js"></script>
<!--AvionDB CDN Link-->
<script src="https://unpkg.com/aviondb/dist/aviondb.min.js"></script>
<script type="text/javascript">
const runExample = async () => {
const ipfs = await window.Ipfs.create();
// 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()
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment