Skip to content

CRUD Source

Configuration for automatic CRUD endpoint generation from PostgreSQL tables and views.

Overview

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "Enabled": true,
      "SchemaSimilarTo": null,
      "SchemaNotSimilarTo": null,
      "IncludeSchemas": null,
      "ExcludeSchemas": null,
      "NameSimilarTo": null,
      "NameNotSimilarTo": null,
      "IncludeNames": null,
      "ExcludeNames": null,
      "CommentsMode": "OnlyWithHttpTag",
      "ReturningUrlPattern": "{0}/returning",
      "OnConflictDoNothingUrlPattern": "{0}/on-conflict-do-nothing",
      "OnConflictDoNothingReturningUrlPattern": "{0}/on-conflict-do-nothing/returning",
      "OnConflictDoUpdateUrlPattern": "{0}/on-conflict-do-update",
      "OnConflictDoUpdateReturningUrlPattern": "{0}/on-conflict-do-update/returning",
      "CrudTypes": ["All"]
    }
  }
}

General Settings

SettingTypeDefaultDescription
EnabledbooltrueEnable CRUD endpoint generation for tables and views.
CommentsModestring"OnlyWithHttpTag"How comment annotations affect endpoint creation.

Comments Mode

ModeDescription
IgnoreCreate all endpoints, ignore comment annotations.
ParseAllCreate all endpoints, parse comment annotations to modify them.
OnlyWithHttpTagOnly create endpoints for tables/views with HTTP tag in comments (default).

When using ParseAll, you can use the DISABLED, ENABLED, and TAGS annotations to control which CRUD operations are exposed for each table.

Schema and Name Filtering

Filter which tables and views are exposed as CRUD endpoints.

SettingTypeDefaultDescription
SchemaSimilarTostringnullInclude schemas matching this SQL SIMILAR TO pattern.
SchemaNotSimilarTostringnullExclude schemas matching this SQL SIMILAR TO pattern.
IncludeSchemasarraynullList of schema names to include.
ExcludeSchemasarraynullList of schema names to exclude.
NameSimilarTostringnullInclude table/view names matching this SQL SIMILAR TO pattern.
NameNotSimilarTostringnullExclude table/view names matching this SQL SIMILAR TO pattern.
IncludeNamesarraynullList of table/view names to include.
ExcludeNamesarraynullList of table/view names to exclude.

Filtering Examples

Include only specific schemas:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "IncludeSchemas": ["api", "public"]
    }
  }
}

Exclude system tables:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "ExcludeSchemas": ["pg_catalog", "information_schema"],
      "NameNotSimilarTo": "%_audit"
    }
  }
}

URL Patterns

Configure URL patterns for different CRUD operation variants. Use {0} as placeholder for the default URL.

SettingTypeDefaultDescription
ReturningUrlPatternstring"{0}/returning"URL pattern for INSERT/UPDATE/DELETE with RETURNING clause.
OnConflictDoNothingUrlPatternstring"{0}/on-conflict-do-nothing"URL pattern for INSERT ON CONFLICT DO NOTHING.
OnConflictDoNothingReturningUrlPatternstring"{0}/on-conflict-do-nothing/returning"URL pattern for INSERT ON CONFLICT DO NOTHING with RETURNING.
OnConflictDoUpdateUrlPatternstring"{0}/on-conflict-do-update"URL pattern for INSERT ON CONFLICT DO UPDATE (upsert).
OnConflictDoUpdateReturningUrlPatternstring"{0}/on-conflict-do-update/returning"URL pattern for INSERT ON CONFLICT DO UPDATE with RETURNING.

Custom URL Patterns

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "ReturningUrlPattern": "{0}-returning",
      "OnConflictDoUpdateUrlPattern": "{0}-upsert"
    }
  }
}

CRUD Types

Enable or disable specific CRUD operations using the CrudTypes array.

TypeHTTP MethodDescription
SelectGETRead records from tables/views.
InsertPOSTCreate new records.
InsertReturningPOSTCreate records and return inserted data.
InsertOnConflictDoNothingPOSTInsert or ignore on conflict.
InsertOnConflictDoNothingReturningPOSTInsert or ignore with returning.
InsertOnConflictDoUpdatePOSTUpsert (insert or update on conflict).
InsertOnConflictDoUpdateReturningPOSTUpsert with returning.
UpdatePUTUpdate existing records.
UpdateReturningPUTUpdate and return modified data.
DeleteDELETERemove records.
DeleteReturningDELETERemove and return deleted data.
All-Enable all CRUD types.

Examples

Enable all operations:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "CrudTypes": ["All"]
    }
  }
}

Read-only access:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "CrudTypes": ["Select"]
    }
  }
}

Basic CRUD without conflict handling:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "CrudTypes": [
        "Select",
        "Insert",
        "InsertReturning",
        "Update",
        "UpdateReturning",
        "Delete",
        "DeleteReturning"
      ]
    }
  }
}

Complete Example

Production configuration with filtering and specific CRUD types:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "Enabled": true,
      "IncludeSchemas": ["api"],
      "ExcludeNames": ["audit_log", "system_config"],
      "CommentsMode": "OnlyWithHttpTag",
      "ReturningUrlPattern": "{0}/returning",
      "OnConflictDoUpdateUrlPattern": "{0}/upsert",
      "OnConflictDoUpdateReturningUrlPattern": "{0}/upsert/returning",
      "CrudTypes": [
        "Select",
        "Insert",
        "InsertReturning",
        "InsertOnConflictDoUpdate",
        "InsertOnConflictDoUpdateReturning",
        "Update",
        "UpdateReturning",
        "Delete"
      ]
    }
  }
}

Minimal read-only configuration:

json
{
  "NpgsqlRest": {
    "CrudSource": {
      "Enabled": true,
      "CommentsMode": "ParseAll",
      "CrudTypes": ["Select"]
    }
  }
}

Controlling CRUD Operations with Annotations

When using "CommentsMode": "ParseAll", table comments can control which CRUD operations are exposed.

Example: Read-Only Table

sql
comment on table audit_log is '
@disabled insert, update, delete
';

Example: Selective Operations

sql
comment on table users is '
@disabled
@enabled select, delete
';

Only SELECT and DELETE are enabled; INSERT and UPDATE return 405.

Example: Different Settings per Operation

sql
comment on table products is '
for select
@allow_anonymous
for update
@authorize admin
for returning
@disabled
';

See the annotation documentation for complete details on controlling CRUD operations.

Next Steps

Comments

Released under the MIT License.