Skip to main content

Version 0.3.0

· 3 min read
Lyoha Plotinka

The core library version has reached 0.3.0. Let me briefly explain what's new in Rattus ORM.

TL;DR

  • The database creation process has changed: the recommended approach is to use the createDatabase function;
  • The Query class has been split into two - Query and Constraintor. Constraintor is responsible for applying constraints to data queries, while Query inherits from Constraintor. This provides more flexibility for supporting asynchronous storage;
  • Decorators and attribute type methods have been renamed and moved to a separate export;
  • A @DateField decorator (this.dateField in JS) has been added, which is - obviously - for dates in your models;
  • Dependencies have been updated.

createDatabase

I still entertain thoughts about supporting asynchronous storage. I really want this to be done without "cloning" the core package and to be easy to use.

The abstraction for database creation could very well contribute to this, and although asynchronous storage support hasn’t been introduced in this release, I decided to revamp the database creation process now.

Query and Constraintor

This was done for the same reason - the hope of supporting asynchronous storage in the future. According to the early concepts, constraints for data selection will be applied the same way for both synchronous and asynchronous storage.

New Names and Export of Type Decorators

All methods that create attribute types, as well as decorators for them, have been renamed:

  • this.uid => this.uidField; @Uid => @UidField;
  • this.string => this.stringField; @Str => @StringField;
  • this.number => this.numberField; @Num => @NumberField;
  • this.boolean => this.booleanField; @Bool => @BooleanField;

Full compatibility with Vuex ORM Next is lost, but the code will be cleaner, with fewer conflicts with your IDE’s autocomplete.

In addition, the decorators have been moved from the default export to @rattus-orm/core/decorators. This is for those who use Rattus ORM without TypeScript.

DateField

In recent months, I’ve had the opportunity to work a bit on the backend. I used Nest JS and TypeORM, with Postgresql as the DBMS. While working with TypeORM, I realized what Rattus ORM was missing - a Date type for properties ("columns").

On the frontend, additional solutions for working with dates, like dayjs, date-fns, and so on, are often used. However, if simple interaction is required, the built-in date is quite sufficient. Therefore, I decided to relieve myself (and other users) of the need to manually parse the date every time by assigning this task to the attribute.

By default, the type does not support null (like all other types) - if you pass null to the model class (or any other value that would result in the creation of an Invalid date), Rattus ORM will issue a warning in the console, and a "zero date" will be set in the model, the same as when calling new Date(0). If you explicitly allow the field to accept null - everything will work as usual:

export class User extends Model {
public static entity = 'user'

@DateField(null, { nullable: true })
public createdAt: Date | null
}

Future Plans

There is a possibility that the project will continue moving towards greater compatibility with TypeORM, as I am keen to share more code in projects where both the frontend and backend are written in TypeScript.