denoDB

denoDB

  • Docs
  • GitHub
  • Help

›Models

Guides

  • Getting started
  • Connect to a database
  • Clients

    • Using MariaDB
    • Using MongoDB
    • Using MySQL
    • Using PostgreSQL
    • Using SQLite
  • Create models
  • Synchronize database
  • Query models
  • Transactions
  • Relationships

    • Foreign key
    • One-to-one
    • One-to-many
    • Many-to-many
  • Model events

API Reference

    Models

    • Data types
    • Field descriptors
    • Model methods
    • Model records

Model records

Model records are handy if you don't want to use Model.create, Model.delete or Model.update with where(...) clauses every time.

However, it is important to note that in order to use model records, you will need to edit your models a little bit by adding every field to your model:

class Flight extends Model {
  static fields = {
    _id: { primaryKey: true },
    departure: DataTypes.STRING,
    destination: DataTypes.STRING,
    flightDuration: DataTypes.FLOAT,
  };

  _id!: string;
  departure!: string;
  destination!: string;
  flightDuration!: number;

  // `createdAt` and `updatedAt` should not be added
  // as they will automatically be created by the system
  // if `timestamps = true`
}

The API should not change anytime soon. However, the way fields are added could switch to a different approach as it now comes with "field duplicates".

Create a new record

You can create a single record without using Model.create(), but by instantiating a record and setting its fields one by one. The record will only be created into the database when save() is called.

Any missing field will be matched to its default if provided through the model static defaults property.

const flight = new Flight();
flight.departure = 'London';
flight.destination = 'Roma';
flight.flightDuration = 3;
await flight.save();

Update a record

If you already have a record instance, you can update any of its fields and then call update().

// flight is an instance of Flight which extends Model
flight.flightDuration = 2;
await flight.update();

Delete a record

If you already have a record instance, you can delete it from the database using delete().

// flight is an instance of Flight which extends Model
await flight.delete();
Last updated on 1/2/2021 by eveningkid
← Model methods
Docs
Getting StartedAPI Reference
More
GitHubStar