Deleting a Model

A common scenario is to delete a single model by ID. That’s done using the DataContext.delete method. The usage is pretty much identical to updating a model by ID, so be sure to review the Updating a Model section. It has more detailed documentation.

Delete a PhoneNumber Instance

Deleting a record by ID is simple. Below we’ll delete a single PhoneNumber record.

// Delete the PhoneNumber with ID 1.
const phone = new PhoneNumber();

phone.id = 1;

const query: DeleteModel<PhoneNumber> = dataContext
  .delete(PhoneNumber, phone);

console.log(query.toString());

// If no rows are affected (i.e. if a phone number with an id of 1 does not
// exist), then an error will be raised.
await query.execute();

First, we instantiate a PhoneNumber and set the id to 1. Then we call DataContext.delete, supplying two parameters:

  1. An EntityType, which is some Table-decorated class. (See the Models section.)
  2. A model, which is an instance of the EntityType. The model must have at least the primary key set.

That returns a DeleteModel instance, which is a type of Query. Like all Fomrn queries, we can log the SQL that Formn generates using DeleteModel.toString, and execute it.

Note that if there is no record with an id of 1, then an error is raised.

Full Example

The code discussed above is in the formn-example repository under src/delete/delete-phone-number.ts. It’s also printed below for convenience.

import {
  MySQLDataContext, ConnectionOptions, DeleteModel
} from 'formn';

import { PhoneNumber } from '../entity/phone-number.entity';

async function main() {
  const connOpts: ConnectionOptions = require('../../connections.json');
  const dataContext = new MySQLDataContext();

  try {
    await dataContext.connect(connOpts);

    // Delete the PhoneNumber with ID 1.
    const phone = new PhoneNumber();

    phone.id = 1;

    const query: DeleteModel<PhoneNumber> = dataContext
      .delete(PhoneNumber, phone);

    console.log(query.toString());

    // If no rows are affected (i.e. if a phone number with an id of 1 does not
    // exist), then an error will be raised.
    await query.execute();
    await dataContext.end();
  }
  catch(err) {
    console.error(err);
  }
}

main();

Run it using ts-node.

npx ts-node ./src/delete/delete-phone-number.ts

Here’s the output:

DELETE  `phone_numbers`
FROM    `phone_numbers` AS `phone_numbers`
WHERE   (`phone_numbers`.`phoneNumberID` = :phone_numbers_id_0)