Possible to have a Create Object Action with an auto-incrementing primary key?

427 Views Asked by At

When using a “Create Object” Action, is it possible to configure an incrementing primary key? That is, each new objects primary key is the most recently created object's primary key +1.

1

There are 1 best solutions below

0
On

It is possible to configure an incrementing number based on the current highest value found in existing objects. However, I would not recommend using an incrementing integer as your primary key for the object.

Generating an auto-incrementing integer

There are two approaches to generating an auto-incrementing number. Both involve writing a custom Function to create the object within the Action. Within the Function, you can either:

  1. Have an instance of a "Counting" object that contains the value of the highest known id for the object type you are creating. Read that maximum value, add 1, set this max as the value on your new object, and update the value on the base "Counting" object.
  2. Perform an aggregation over your object type to find the highest value, add 1, and set the value on the new object.

This is a useful pattern to leverage in many workflows, such as ticketing applications.

Choosing a Primary Key

As a best practice, primary keys should not be coupled to semantically meaningful information about an object. Your primary key should not contain information that is relevant to user workflows. For example, in a ticketing workflow, ticket number formats may change. Updating the value of primary keys is an unnecessary amount of work, while updating the value of a typical string or integer column is feasible.

Therefore, it's recommended that primary keys are uniquely generated UUIDs stored in string columns (ex: ticket_id), while auto-incrementing numbers are stored in column(s) named after what the number represents (ex: ticket_number).

Generating a UUID

The best practice for generating primary keys when creating new objects via Actions is to create a UUID String.

When using the Create Object rule inside of an Action, you can specify the typeclass actions::generate_uuid on the parameter to generate a random UUID every time the Action is submitted.

To do this inside of a Function-backed Action, you can follow the Functions documentation on adding a dependency. For uuid package, this means adding the following to your package.json:

 "dependencies": {
    "uuid": "^3.4.0"
  },
  "devDependencies": {
    "@types/uuid": "^3.4.9"
  }

If you use newer versions of the uuid package it will fail to execute in Functions because of a feature removed in the library here.

After doing this, make sure to click the small banner in the package.json editor window that allows you to get packages from public npm. By default this is disabled.

Then in your code you can add something like:

import { v4 as uuidv4 } from "uuid";
...
@OntologyEditFunction()
public createObject(): void {
    var newObject = Objects.create().myObjectType(uuidv4());
}