dangoDB: A MongoDB ODM for Deno
Since the late 2000’s, the proliferation of high traffic internet web applications demonstrated the need for database systems that excelled in scalability and performance when handling massive data and high concurrency. NoSQL database solutions such as MongoDB were developed to address these performance and scalability issues. To gain these benefits, they store data in an unstructured or semi-structured form, often in key-value pairs or JSON documents. They also prioritize availability and allow for horizontal scaling. One of the downsides to this solution was the lack of data structure. As a result, Object Document Modeling libraries were developed to provide a schema-based structure to documents as well as schema/model validation at the application layer.
Just as database modeling has evolved over the years, so have other technologies, including runtime environments. Although Node.js currently dominates the runtime “scene”, its creator, Ryan Dahl, introduced an alternative in 2018 known as Deno. He introduced Deno as a better half of Node.js. Deno is a runtime environment but it also acts as a package manager within a single executable. More and more developers are building applications in Deno but few of these apps are actually in production because Deno still lacks the established library infrastructure that Node has built throughout the years. dangoDB is an attempt to address this lack of support by providing a library similar to Mongoose, built specifically for Deno.
What is dangoDB?
dangoDB (pronounced don-go DB) is a free, open-source, easy to use Object Data Modeling library for Deno which enables developers to enforce schema validation to structure their data within MongoDB. By providing the look and feel of existing established ODM libraries from other runtime environments such as Node.js, dangoDB makes it simple for users to adapt quickly.
Features
- Schema-based validation
- Data type-casting
- MongoDB queries
Getting Started
First things first. Be sure to import the dangoDB module into your Deno project.
import { dango } from "https://deno.land/x/dangodb@v1.0.2/mod.ts";
Connect To Your Database
Next, open a connection to your MongoDB database using your URI string.
await dango.connect(‘YOUR_URI’);
Now that you’re connected, you can create a schema for your documents.
Creating A Schema
Each property defined in the schema typically requires a set of schema options assigned to it before being inserted into the database. However, we’ve given the user the option to specify the type at a minimum or more as necessary. dangoDB will assign default values to any schema options not provided.
Simply pass in an object with key-value pairs which specify the properties and their respective types.
const dinosaurSchema = dango.schema({ name: ‘string’ });
Under the hood, dangoDB will take your property type, and include it in an object with the other options set to default.
{
name: {
type: 'string',
required: false,
default: null,
unique: false,
validator: false
}
}
You may also create your schema using the format listed above with the type property and any other properties you choose. dangoDB will handle the rest.
Data types such as number, string or boolean may be assigned as the type. However, if you want to assign an object with its own schema, you can directly assign a subschema.
{
name: {
type: 'string',
required: false,
default: null,
unique: false,
validator: false
},
other: otherSchema,
}
Creating a Model
Next, compile your schema into a Model.
const Dinosaur = dango.model(‘Dinosaur’, dinosaurSchema);
Now that your Model is set up, you can begin sending queries to the database.
Ready to Query
You are ready to insert a document into the Dinosaur collection.
await Dinosaur.insertOne({ name: ‘Stegosaurus’ });
After running this code, dangoDB will take the passed-in object, run a validation check to compare it to the model’s schema and make any necessary type conversions. If all tests pass, it will be formatted into a new object and sent to the database to be inserted.
Now, let’s say you wanted to display all the dinosaurs in your collection. You can access all of the dinosaur documents through our Dinosaur model.
const dinosaurs = await Dinosaur.find({ });console.log(dinosaurs);// [ { name: 'Triceratops', age: 70,000,000 }, { name: 'Brontosaurus', age: 150,000,000 }, { name: 'Stegosaurus', age: null }];
Now you’ve successfully inserted a document into the Dinosaur collection at your MongoDB database.
Congratulations! You’ve seen firsthand just how easy-to-use and powerful the dangoDB library can be if used in your code.
Schema Code Generator
If you head on over to dangodb.land, you can navigate to the dangoDB schema generator. The first thing you will notice is a Property Form on which you can add a property name and type, you can specify if the property is required or unique, or if it should have a set default value. Lastly, you can choose to check a box ‘Validation Function’ if you plan on requiring your value to pass a user-defined validation test before being added to the schema. When you’re done with your selections, click the ‘Save Property’ button to add this new property to your schema. When you’re ready, hit the ‘Generate Schema’ button to see your newly generated schema. Copy and paste your schema with a click of the copy button in the corner of the generator and you’re all set to go! Schema-generate away!
Future Features
- Additional queries
- Creating pre and post hooks for more functionality and control during execution of asynchronous functions
- Implementing arrays as a data schema type making configuration easier for clients
Issues?
If you would like to report any problems using dangoDB, please submit an issue using this link.
Connect with the dangoDB team
- Celeste Knopf | LinkedIn | GitHub
- Emilia Yoffie | LinkedIn | GitHub
- Stephen Jue | LinkedIn | GitHub
- William Greco | LinkedIn | GitHub
Continuing Development
We welcome contributions to dangoDB as they are essential for growing and advancing the Deno ecosystem. If you’d like to contribute, please reach out to a dangoDB team member so you can be incorporated into the development process.