Resources & Relationships

Defining Resources

Resources are the core building blocks of your api.vision API. This guide explains how to define resources, their fields, and the options available to you.

What is a Resource?

A resource represents an entity or object in your API, such as a user, product, or post. In RESTful terms, resources are the nouns in your API that clients can interact with via CRUD operations (Create, Read, Update, Delete).

Each resource in api.vision:

  • Has a unique name (usually plural, like "users" or "products")
  • Contains a set of fields that define its structure
  • Can have relationships with other resources
  • Automatically gets RESTful endpoints for CRUD operations

Basic Resource Definition

To define a resource, you need to specify its name and fields:

{
  "name": "products",
  "fields": [
    {
      "name": "id",
      "type": "number",
      "defaultValue": "$increment"
    },
    {
      "name": "name",
      "type": "string",
      "required": true
    },
    {
      "name": "price",
      "type": "number",
      "required": true,
      "min": 0
    },
    {
      "name": "description",
      "type": "string"
    },
    {
      "name": "inStock",
      "type": "boolean",
      "defaultValue": true
    }
  ]
}

Resource Properties

PropertyTypeRequiredDescription
nameStringYesThe resource name (plural recommended)
fieldsArrayYesArray of field definitions
relationshipsArrayNoArray of relationship definitions
descriptionStringNoHuman-readable description of the resource

Naming Conventions

Following proper naming conventions helps create a consistent, intuitive API. Here are some recommendations:

Use Plural Nouns for Resources

Good:

users, products, orders

Avoid:

user, product, order

Use camelCase for Field Names

Good:

firstName, createdAt, phoneNumber

Avoid:

first_name, created_at, PhoneNumber

Use Descriptive, Clear Names

Good:

shippingAddress, isActive, paymentMethod

Avoid:

addr, flag, pmt

Field Definitions

Fields define the properties of your resources. Each field requires a name and type, plus optional constraints:

PropertyTypeRequiredDescription
nameStringYesThe field name (camelCase)
typeStringYesData type (string, number, boolean, date, object, array)
requiredBooleanNoWhether the field is required (default: false)
defaultValueAnyNoDefault value if not specified
uniqueBooleanNoWhether the field value must be unique across all records
descriptionStringNoHuman-readable description of the field

Additionally, different field types support their own specific validation properties:

String Fields

PropertyTypeDescription
minLengthNumberMinimum string length
maxLengthNumberMaximum string length
patternStringRegular expression pattern for validation
enumArrayList of allowed values

Number Fields

PropertyTypeDescription
minNumberMinimum value
maxNumberMaximum value
multipleOfNumberValue must be a multiple of this number

Array Fields

PropertyTypeDescription
itemsObjectSchema for array items
minItemsNumberMinimum number of items
maxItemsNumberMaximum number of items
uniqueItemsBooleanWhether items in the array must be unique

Object Fields

PropertyTypeDescription
propertiesObjectSchema for object properties
requiredArrayList of required property names

Example Resource Definitions

User Resource

{
  "name": "users",
  "fields": [
    {
      "name": "id",
      "type": "number",
      "defaultValue": "$increment"
    },
    {
      "name": "username",
      "type": "string",
      "required": true,
      "minLength": 3,
      "maxLength": 30,
      "unique": true
    },
    {
      "name": "email",
      "type": "string",
      "required": true,
      "pattern": "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$",
      "unique": true
    },
    {
      "name": "password",
      "type": "string",
      "required": true,
      "minLength": 8
    },
    {
      "name": "role",
      "type": "string",
      "enum": ["user", "admin"],
      "defaultValue": "user"
    },
    {
      "name": "createdAt",
      "type": "date",
      "defaultValue": "$now"
    },
    {
      "name": "isActive",
      "type": "boolean",
      "defaultValue": true
    }
  ]
}

Product Resource

{
  "name": "products",
  "fields": [
    {
      "name": "id",
      "type": "number",
      "defaultValue": "$increment"
    },
    {
      "name": "name",
      "type": "string",
      "required": true
    },
    {
      "name": "description",
      "type": "string"
    },
    {
      "name": "price",
      "type": "number",
      "required": true,
      "min": 0
    },
    {
      "name": "category",
      "type": "string",
      "enum": ["electronics", "clothing", "books", "furniture", "other"],
      "required": true
    },
    {
      "name": "tags",
      "type": "array",
      "items": {
        "type": "string"
      }
    },
    {
      "name": "inStock",
      "type": "boolean",
      "defaultValue": true
    },
    {
      "name": "attributes",
      "type": "object",
      "properties": {
        "color": { "type": "string" },
        "size": { "type": "string" },
        "weight": { "type": "number" },
        "dimensions": {
          "type": "object",
          "properties": {
            "length": { "type": "number" },
            "width": { "type": "number" },
            "height": { "type": "number" }
          }
        }
      }
    }
  ]
}

Best Practices for Resource Design

Include ID Fields

Always include an ID field for each resource, preferably using auto-increment or UUID generation. This makes resource referencing and relationships easier.

Use Timestamps

Including createdAt and updatedAt fields helps track when resources were created or modified, which is useful for debugging and filtering.

Define Validation Constraints

Always add appropriate validation rules to fields to ensure data integrity and realistic behavior in your API.

Keep Resources Focused

Each resource should represent a single concept. Break down complex entities into multiple related resources rather than creating monolithic resources.

Next Steps

Now that you understand how to define resources, you can:

Tip: Resource Templates

api.vision provides pre-built resource templates for common entities like users, products, and posts. You can use these as starting points and customize them for your specific needs.