import/

directory
v0.0.0-...-7a64e88 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: AGPL-3.0

README

Import API

The import API loads patron-request aggregates, batch actions, and templates from a newline-delimited JSON (NDJSON) stream.

Migration export scripts

The migration folder contains SQL scripts that export a mod-rs tenant as NDJSON accepted by the CrossLink import APIs:

  1. export-crosslink-directory.sql exports directory entries, tiers, and networks for POST /directory/import.
  2. export-crosslink-config.sql exports templates and compatible scheduled actions for POST /import.
  3. export-crosslink-open-patron-requests.sql exports open borrowing and lending requests for POST /import.

Run the scripts in this order so directory peers exist before configuration and patron requests are imported. Each script documents its required psql variables, export command, validation, and migration limitations.

Request

POST /import?conflictPolicy=fail
Content-Type: application/x-ndjson

conflictPolicy is optional and defaults to fail. Its supported values are fail, skip, and update.

The import endpoint refreshes a 10-minute read deadline before each request-body read and clears it after the read. It clears the write deadline during processing, then sets fresh 10-minute read and write deadlines before the final response (including any unread-body drain by the HTTP server). Progressing imports can run longer than 10 minutes, while stalled network I/O still times out. Client and reverse-proxy timeouts still apply while waiting for the response.

The request body must contain one complete JSON object per line. Blank lines are ignored. Do not wrap the records in a JSON array and do not pretty-print a record across multiple lines.

Each line has this envelope:

{"type":"template","owner":"ISIL:US-RS1","data":{}}
Field Meaning
type patronRequest, batchAction, or template
owner Symbol of the institution that owns the imported resource; it must resolve to a known peer
data A JSON object matching the schema for the selected resource type

Unknown envelope fields and unknown fields inside data are rejected. The authoritative schemas are ImportResourceRecord, ImportPatronRequestBundle, CreateBatchAction, and CreateTemplate in broker/oapi/open-api.yaml.

Import a file with curl
curl --fail-with-body \
  -X POST \
  -H 'Content-Type: application/x-ndjson' \
  --data-binary @resources.ndjson \
  'http://localhost:8081/import?conflictPolicy=fail'

Use --data-binary, rather than -d, so curl preserves the line boundaries. Change the host and port to match the deployment.

Mixed-resource example

Save the following as resources.ndjson. Each record remains on one physical line.

{"type":"template","owner":"ISIL:US-RS1","data":{"title":"Request reminder","purpose":"email","subject":"Request update","body":"Your request has been updated.","contentType":"text","labels":["request-reminder"],"audience":"patron"}}
{"type":"batchAction","owner":"ISIL:US-RS1","data":{"schedule":"FREQ=DAILY;BYHOUR=6;BYMINUTE=0","actionName":"request-aging","title":"Daily request aging","batchQuery":"state==NEW","actionParams":{"interval":"24h"}}}
{"type":"patronRequest","owner":"ISIL:US-RS1","data":{"patronRequest":{"id":"US-RS1-1001","createdAt":"2026-09-15T08:00:00Z","updatedAt":"2026-09-15T08:00:00Z","illRequest":{"header":{"requestingAgencyId":{"agencyIdType":{"#text":"ISIL"},"agencyIdValue":"US-RS1"},"supplyingAgencyId":{"agencyIdType":{"#text":"ISIL"},"agencyIdValue":"US-RS2"},"timestamp":"2026-09-15T08:00:00Z","requestingAgencyRequestId":"US-RS1-1001"},"bibliographicInfo":{"title":"Example title"},"serviceInfo":{"serviceType":"Loan"}},"state":"SENT","stateModel":"default","side":"borrowing","requesterSymbol":"ISIL:US-RS1","supplierSymbol":"ISIL:US-RS2","requesterRequestId":"US-RS1-1001","needsAttention":false},"items":[],"notifications":[],"locatedSuppliers":[]}}

The example assumes that ISIL:US-RS1 is a known peer and that SENT is valid for the deployment's default borrowing/loan state model.

Resource details

Templates

Required data fields are title, purpose, body, contentType, and a non-empty labels array.

  • purpose: email or pullslip
  • contentType: text or html
  • optional audience: patron or staff; omit it for a generic template

Example:

{"type":"template","owner":"ISIL:US-RS1","data":{"title":"Ready for pickup","purpose":"email","subject":"Your item is ready","body":"Please collect {{.Title}}.","contentType":"text","labels":["received-notification"],"audience":"patron"}}
Batch actions

Required data fields are schedule, actionName, title, and batchQuery. schedule is an RRULE expression and batchQuery is a CQL query. Supported action names are email-pullslips, request-aging, and overdue.

Imported batch actions are created inactive, including updates to matching actions. Review the migrated schedule, query, and parameters, then enable the action through the batch-action API when it is ready to run.

For request-aging, actionParams.interval is required by the action and uses a Go duration such as 24h or 168h.

Example:

{"type":"batchAction","owner":"ISIL:US-RS1","data":{"schedule":"FREQ=WEEKLY;BYDAY=MO;BYHOUR=6;BYMINUTE=0","actionName":"request-aging","title":"Weekly request aging","batchQuery":"state==NEW","actionParams":{"interval":"168h"}}}
Patron requests

A patron-request record imports an aggregate:

  • patronRequest — required root object
  • items — required array
  • notifications — required array
  • locatedSuppliers — required array
  • illTransaction — optional; allowed only for borrowing requests

Important validation rules include:

  • createdAt and updatedAt must be timestamps, and needsAttention must be present.
  • The request's state must exist in stateModel for its service type and side.
  • requesterRequestId must equal illRequest.header.requestingAgencyRequestId.
  • requesterSymbol must match the requesting-agency symbol in the ISO 18626 header.
  • tenant is optional and is stored as the request's FOLIO tenant ID; omit it to store no tenant. The envelope owner still identifies the owning institution.
  • requesterPickupLocationId is optional and allowed only for borrowing requests. It is stored as the request's pickup location without being checked against the directory, so it must be the UUID of the requesting institution or one of its branches.
  • For a borrowing request, id must equal requesterRequestId, and the requester must be the envelope owner or one of its branches.
  • For a lending request, supplierSymbol is required, must match the supplying-agency symbol in the ISO 18626 header, and must be the envelope owner or one of its branches.
  • An illTransaction, when supplied, must use the same requester symbol and requester request ID. locatedSuppliers require an illTransaction.
  • Item, notification, and located-supplier IDs must be unique within their respective arrays. At most one located supplier may have status selected.

When a borrowing request's id has the PREFIX-N form that the broker generates for its requester, the import reserves the ID so new requests cannot reuse it. PREFIX must be the requester's own request-ID prefix, compared case-insensitively: its Directory requestIdPrefix, or the value of requesterSymbol (US-RS1 in US-RS1-1001) when Directory sets none. N must have at most 15 digits. Other IDs, such as UUIDs or IDs with another prefix, are imported without a reservation; they cannot collide with IDs the broker generates for the requester. A requester outside the envelope owner and its branches reserves nothing, so an import cannot move another institution's sequence. An envelope owner that Directory does not know reserves nothing either, and its records are reported as per-record errors. A requester that Directory does not know uses its requesterSymbol value as its prefix, as request creation does. Any other Directory or database failure while looking up owners or requesters fails the whole upload before any record is imported, because guessing a prefix could leave the real prefix's IDs unreserved.

The broker first receives the whole stream into a temporary file, without making lookups while it reads. It then looks up each envelope owner and requester once and raises the request-ID sequence of every requester prefix past the highest N among the stream's reservable borrowing requests, in one transaction, before it imports any record. A request created while the records are imported therefore cannot take an ID that a later record uses. Requests created before the upload finishes are not protected, just like requests created before the import started. Such a request fails the record that uses its ID under fail, and is never replaced under update (see update). Request creation for a prefix waits while an import is advancing its sequence, so imports and new requests can run at the same time. The broker's temporary directory (/tmp, or TMPDIR when set) must have room for the whole request body of every concurrent import, up to 2 GB each. The broker does not limit concurrent imports. The Helm chart mounts a size-limited emptyDir there (tmpSizeLimit, default 4Gi, enough for two maximum-size imports at once). Set tmpSizeLimit to at least 2 GB times the number of imports that may run at once on one replica: when the emptyDir grows past its limit, Kubernetes evicts the whole pod, which stops every request and task on that replica rather than failing only the import.

Request-ID prefixes cannot contain /, but imported legacy IDs, such as mod-rs HRIDs, may. Such IDs must be percent-encoded (%2F) in /patron_requests/{id} URLs.

Minimal borrowing example:

{"type":"patronRequest","owner":"ISIL:US-RS1","data":{"patronRequest":{"id":"US-RS1-1001","createdAt":"2026-09-15T08:00:00Z","updatedAt":"2026-09-15T08:00:00Z","illRequest":{"header":{"requestingAgencyId":{"agencyIdType":{"#text":"ISIL"},"agencyIdValue":"US-RS1"},"supplyingAgencyId":{"agencyIdType":{"#text":"ISIL"},"agencyIdValue":"US-RS2"},"timestamp":"2026-09-15T08:00:00Z","requestingAgencyRequestId":"US-RS1-1001"},"bibliographicInfo":{"title":"Example title"},"serviceInfo":{"serviceType":"Loan"}},"state":"SENT","stateModel":"default","side":"borrowing","requesterSymbol":"ISIL:US-RS1","supplierSymbol":"ISIL:US-RS2","requesterRequestId":"US-RS1-1001","needsAttention":false},"items":[],"notifications":[],"locatedSuppliers":[]}}

Conflict policies

The selected policy applies independently to every record in the stream. A record-level conflict or validation error does not stop later records, and successful earlier records remain committed. Every imported resource is written in its own database transaction, after the request-ID reservation described under Patron requests.

fail (default)

If the resource identity already exists, that record is not changed. It increments the resource's failed count and adds a detail to errors; processing then continues with the next line.

curl --fail-with-body \
  -H 'Content-Type: application/x-ndjson' \
  --data-binary @resources.ndjson \
  'http://localhost:8081/import'

Example: if owner ISIL:US-RS1 already has a batch action titled Daily request aging, importing another batch action with that owner and title fails, even if its schedule is different.

skip

If the resource identity already exists, the existing resource is left unchanged. The record increments skipped, and a diagnostic is included in errors so the caller can identify the skipped line.

curl --fail-with-body \
  -H 'Content-Type: application/x-ndjson' \
  --data-binary @resources.ndjson \
  'http://localhost:8081/import?conflictPolicy=skip'

Example: suppose this template already exists:

{"type":"template","owner":"ISIL:US-RS1","data":{"title":"Original title","purpose":"email","body":"Original body","contentType":"text","labels":["request-reminder"],"audience":"patron"}}

Importing the following with skip finds the overlapping request-reminder label but keeps the original title and body:

{"type":"template","owner":"ISIL:US-RS1","data":{"title":"Replacement title","purpose":"email","body":"Replacement body","contentType":"text","labels":["request-reminder"],"audience":"patron"}}
update

If exactly one matching resource exists, its mutable data is replaced by the incoming data. The record increments imported, not a separate updated count.

curl --fail-with-body \
  -H 'Content-Type: application/x-ndjson' \
  --data-binary @resources.ndjson \
  'http://localhost:8081/import?conflictPolicy=update'

Using update with the template example above preserves the existing database ID and creation timestamp, but changes its title, body, and other submitted template fields.

Updates still fail when applying them would make identity resolution unsafe. Examples include changing the immutable identity of a patron request, matching multiple templates, or reusing a nested aggregate ID that belongs to a different aggregate.

How a conflict is identified
Resource Conflict identity update behavior
Borrowing patron request Patron-request id Replaces the root data and synchronizes the submitted aggregate collections. The requester request ID, side, and requester owner symbol must still match the existing aggregate. Only a request created by an import is replaced; a request the broker created, such as a live request that took the ID before the import reserved it, fails the record. Requests imported before this rule was introduced are not recorded as imported and are treated the same way.
Lending patron request Patron-request id, or the routing identity (supplierSymbol, requesterRequestId) Updates the matched local aggregate, preserving its local ID when the match was by routing identity. The requester request ID, side, and supplier owner symbol must still match. If the incoming ID and routing identity match two different aggregates, the record fails under every policy.
Template Any label overlap for the same owner, purpose, and audience Updates the one matching template while preserving its ID and creation timestamp. If the labels overlap more than one template, the record fails as ambiguous. A generic template (no audience) and an audience-specific template do not conflict.
Batch action Same owner and title among batch-action scheduled tasks Updates that batch action while preserving its ID and creation timestamp, then notifies the scheduler. A different kind of scheduled task with the same title does not conflict.

For a patron-request update, the submitted arrays are the desired final contents:

  • Existing items, notifications, and located suppliers absent from the incoming arrays are deleted.
  • Existing entries with matching IDs are updated, and new IDs are inserted.
  • An existing associated ILL transaction cannot be omitted from the update.
  • An item, notification, located supplier, or ILL transaction already owned by another aggregate causes the entire record transaction to roll back.

For example, updating a patron request with "notifications":[] removes all of that request's existing notifications. Use skip rather than update when the import must never remove or replace existing aggregate data.

Response

A completed stream returns 200 OK, even when individual records failed or were skipped. For example, an import using conflictPolicy=skip could return:

{
  "patronRequests": {"imported": 1, "failed": 0, "skipped": 0},
  "batchActions": {"imported": 0, "failed": 0, "skipped": 1},
  "templates": {"imported": 1, "failed": 0, "skipped": 1},
  "errors": [
    {
      "line": 2,
      "type": "batchAction",
      "owner": "ISIL:US-RS1",
      "identifier": "Daily request aging",
      "error": "batch action \"Daily request aging\" already exists"
    },
    {
      "line": 4,
      "type": "template",
      "owner": "ISIL:US-RS1",
      "identifier": "request-reminder",
      "error": "template labels overlap existing template \"68d48b0b-8781-41b5-bfea-a7c86f37194d\""
    }
  ],
  "errorsOmitted": 0
}

line is the one-based physical line number in the NDJSON body, including blank lines. At most 100 error or skip details are retained; errorsOmitted reports any additional details. Counters still include all recognized records.

Always inspect the counters and errors array instead of treating HTTP 200 as proof that every record was imported.

Request-level errors and limits

Status Meaning
400 Bad Request Missing body, unsupported conflictPolicy, a content type other than application/x-ndjson, or borrowing request IDs from more than 10,000 combinations of envelope owner, requester, and request-ID prefix
413 Request Entity Too Large The whole body exceeds 2 GiB or one NDJSON record exceeds 1 MiB
500 Internal Server Error The server could not read or store the request stream, resolve an envelope owner, or reserve request IDs

The handler applies the network I/O deadlines described above. Any request-level error occurs before the first record is imported, so a fatal stream error such as an oversized record imports nothing.

Directories

Path Synopsis
Package importdb contains generated import database models and queries.
Package importdb contains generated import database models and queries.
Package importoapi contains generated import API bindings.
Package importoapi contains generated import API bindings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL