ProGress with DenoGres

What you need to know about DenoGres 4.0

Jacob Martin
4 min readJan 25, 2023

Very brief overview of Deno

Deno is a fast emerging JavaScript, TypeScript, and WebAssembly runtime built in Rust on top of Chrome’s V8 engine, with secure defaults, built-in tooling, and URL imports for dependency management. It was introduced by the creator of Node, Ryan Dahl, at the JSconf EU in 2018 during his talk famously titled “10 Things I Regret About Node.

Why ORMs?

For those who aren’t familiar, ORM stands for Object-Relational Mapper. “What does that mean?” you might ask. Simply put, an ORM allows developers to work with relational databases through objects, rather than query strings. Switching between object-oriented programming and the declarative syntax of SQL can become tedious and interrupt the workflow during development. ORMs allow a developer to interact with their database as classes and instances. To our benefit, there are a ton of ORM tools out there — unfortunately, open-source options that are designed with first-class TypeScript support are limited and even fewer are designed for the Deno runtime.

DenoGres, A Solution

DenoGres provides a TypeScript ORM for developers working with PostgreSQL in the Deno runtime. The tool works by introspecting a user’s database — including its labels, types, and constraints — and transposing each table onto a class that extends an abstract “Model” class. This gives these table classes access to a set of static properties and methods which securely handle database querying under the hood, while a user experiences database interactions through the familiar mechanism of OOP.

To start working with DenoGres in your Deno project, follow these command line prompts:

Import DenoGres into your project:

deno install --allow-read --allow-write --allow-net --allow-env 
--allow-run --name denogres https://deno.land/x/denogres/mod.ts

Initialize DenoGres:

denogres --init

This command creates a .env file, to which you can then add your database connection URI.

Introspect your database and create models:

denogres --db-pull

DenoGres also features a GUI to visually interact with your database connections, models, and queries. To access the GUI run:

denogres --gui

Further information on how to use the DenoGres CLI and GUI can be found on our website.

DenoGres 4.0 features

DenoGres 4.0 brings to the table a wide variety of updates to the already powerful ORM. Perhaps most excitingly, it adds a transaction and rollback function to allow users to batch multiple queries, maintaining transaction integrity and ACID compliance.

Atomicity, the first principle in the ACID acronym, is essentially the rule that when batching multiple queries to the database, either all changes should be committed, or none at all. This protects important tables in the database from unwanted changes in the case of crashes, connection issues, or unsuccessful queries.

To use transactions in DenoGres, a user can chain .transaction() to the end of other query methods to create or continue a transaction. Transactions can be used across several models to batch queries. This allows you to ensure that important tables stay in sync when the manipulation of one table relies on another. To end the transaction, chain .endTransaction() onto the last query. If any query in the transaction fails, all of the previous queries will be rolled back and those tables will return to their original state.

Here’s an example:

await Person.insert('name = Alex').transaction();
await Animal.delete().where('name = Spot').transaction();
await Person.insert('nae = Rachel').endTransaction();
// throws an error since 'nae' isn't a column, rolls back all previous queries and returns the error for the query that failed

In addition to transactions, more robust error handling was added to provide faster feedback to the user without waiting for errors to be thrown by PostgreSQL. This allows users to address issues before a connection to the database is established and saves the database from an extra connection.

DenoGres 4.0 also significantly increases code coverage to ensure a streamlined experience for both users and developers. Rigorous attention has been given to any potential use case, which means that users can employ DenoGres for more sophisticated queries without worrying that data will fall through the cracks.

The Future of DenoGres

Since DenoGres is an open source product developed under OSLabs, the tool is ever being improved upon and there are a number of features to look forward to in the future:

  • SavePoint functionality will allow users to have larger transactions that can be sectioned out and allow partial transaction rollback to a savepoint. This is useful for implementing complex error recovery.
  • A setter method that can be appended to models when an association is established with another model, allowing for quick additions to related rows.
  • A manyToMany function will create a new junction table, allowing users of the ORM to initiate more complex relationships between tables.

If you have features you’d like to see added or problems that need to be resolved, be sure to open an issue or make a pull request on GitHub — we always welcome feedback!

DenoGres 4.0 Contributors:

--

--