Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add built-in duration type #1701

Merged
merged 21 commits into from
Jan 23, 2025
Merged

feat: add built-in duration type #1701

merged 21 commits into from
Jan 23, 2025

Conversation

RutZap
Copy link
Contributor

@RutZap RutZap commented Jan 15, 2025

The Duration type is a first-class Keel model type and will capture an interval of time. For example:

model Employee {
	fields {
	  name Text
		startDate Date
		periodOfService Duration
	}
}

The duration field will accept ISO 8601 durations, e.g. P1Y10M4DT10H2M1S. These data types will be stored in the DB as Postgres' Interval types.

When generating the JSON schema for the Keel app's API, these fields will be of type: string and format duration.

Filter support

When a Duration field is defined as a filterable field for a list action, the input message will support the following fields/operations:

{
"myDuration": {
      "equals": "duration",
      "notEquals": "duration",
      "lessThan": "duration",
      "lessThanOrEquals": "duration",
      "greaterThan": "duration",
      "greaterThanOrEquals": "duration",
 }

The resolution of these operations will be deferred to the DB engine.


ModelAPI & Functions Runtime support

We're introducing a new class in the keel functions runtime sdk: Duration and built-in support in the modelAPI.

Considering the following keel model schema:

model MyDuration {
    fields {
        dur Duration?
    }
}

We are able to interact with the dur field through the sdk.Duration class:

import { WriteCustomFunction, models, Duration } from "@teamkeel/sdk";

export default WriteCustomFunction(async (ctx, inputs) => {
  const mod = await models.myDuration.create({ dur: Duration.fromISOString("PT1H") });
  const dbModel = await models.myDuration.findOne({ id: mod.id})
  return {
    isoDuration: dbModel.dur.toISOString(),
  }
});

@RutZap RutZap force-pushed the add-duration-type branch from cc82a57 to 96a5bf5 Compare January 20, 2025 11:39
@RutZap RutZap marked this pull request as ready for review January 20, 2025 14:27
@RutZap RutZap merged commit 98d0c60 into main Jan 23, 2025
10 checks passed
@RutZap RutZap deleted the add-duration-type branch January 23, 2025 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants