OData datasources — ERP records, live, with no driver of your own
Declare a datasource over an OData v4 or v2 service such as SAP S/4HANA, SAP Gateway or Dynamics 365, bind objects to its entity sets, and read and write them live, with no code per entity set.
Many business systems already publish their records over OData v4: SAP S/4HANA,
Microsoft Dynamics 365 and Dataverse, and any service that serves a CSDL 4.0
$metadata document. Many others still speak OData v2: most SAP Gateway services,
the older Dynamics CRM OrganizationData.svc and SharePoint 2013 REST. For those you
need no driver of your own. Mount
@objectstack/driver-odata once per service, declare a datasource, and bind an object
to each entity set you want. Every view, form, permission set, formula and hook then
works on those records, and no row is stored on the platform side.
The OData driver is a plugin driver, so everything Virtual objects
says about plugin drivers holds here too: the engine re-applies every query, a read
matches at most 1000 rows unless the datasource raises external.rowCap, and the
sharing model needs a deliberate choice. The one
difference is that this driver is already written.
Which way in
says when to choose it over a driver of your own or a synced copy.
Everything on this page is what the driver's own tests assert, against an in-process
OData service and behind the real engine
(packages/drivers/driver-odata/src/odata-driver.floor.test.ts,
odata-driver.conformance.test.ts, which runs over a v4 and a v2 service, and
odata-driver-v2.test.ts). Read them when this page and the runtime seem to disagree.
Check the service first
- Which OData version it speaks. Open
<serviceRoot>$metadata. A root element that saysVersion="4.0"is a v4 service, the default. One that saysVersion="1.0"(EDMX 1.0, withm:DataServiceVersion="2.0"or"1.0"below it) is a v2 service: declareversion: '2.0'(OData v2 services). A document of the other version is refused at the first request, with the version that reads it named. - Each entity set you bind has a key. The key becomes the record's
id. An entity type with no usable key is refused at the first request. - You know how it authenticates. The driver sends no credential, a bearer token, or HTTP basic, or, borrowed from a connector instance, an API key too. It does not run an OAuth2 flow (Credentials).
1. Mount the plugin
The plugin builds the driver from its options and publishes it as the service
driver.<name>. Mount it in the stack's plugins, one plugin per service:
import { defineStack } from '@objectstack/spec';
import { createODataDriverPlugin } from '@objectstack/driver-odata';
export default defineStack({
// …the datasource and objects of step 2…
plugins: [
createODataDriverPlugin({
name: 'erp', // = the datasource name
serviceRoot: 'https://erp.example.com/odata/v4/sales/',
auth: { scheme: 'bearer' }, // under os serve, no `secrets`: see Credentials
}),
],
});| Option | Default | Meaning |
|---|---|---|
name | odata | The datasource it serves. It is also the driver name and the driver.<name> service suffix. |
serviceRoot | (required) | The service root URL, http or https, with no credentials and no query string. |
version | 4.0 | The protocol version: 4.0 or 2.0 (OData v2 services). |
auth | { scheme: 'none' } | none, bearer, or basic with a username. The secret is the datasource's external.credentialsRef. Leave it none when the datasource borrows a connector instance's credential (Credentials). |
secrets | (none) | Where credentialsRef is resolved: an object with resolve(credentialsRef). When absent, the host's datasource-secret-resolver service is used; os serve registers one. |
pageSize | 500 | Rows asked for per server page (Prefer: odata.maxpagesize). A v2 service has no such preference and pages by its own size. |
requestTimeoutMs | 30000 | The timeout of each request. |
The options are checked when the plugin is built. An unknown key, a serviceRoot with
a user name or a query in it, or a version other than 4.0 or 2.0 refuses the boot
with every problem named.
2. Declare the datasource and bind objects
import { defineStack } from '@objectstack/spec';
export default defineStack({
manifest: { id: 'com.example.sales', name: 'Sales', version: '1.0.0', type: 'app' },
datasources: [
{
name: 'erp',
label: 'ERP sales service (OData v4)',
driver: 'erp', // the plugin's name, i.e. the `driver.erp` service
schemaMode: 'external',
config: {}, // the plugin's options configure the driver, not this
external: {
allowWrites: true,
credentialsRef: 'sys_secret:7c1e…', // the bearer token, as a secret-store handle
validation: { onMismatch: 'warn', checkOnBoot: false },
},
},
],
objects: [
{
name: 'sales_order',
label: 'Sales order',
datasource: 'erp',
sharingModel: 'public_read_write', // see "Choose the sharing model" below
external: {
remoteName: 'SalesOrders', // the entity set
writable: true,
// remote property → local field; a property not named keeps its own name
columnMap: {
SalesOrderID: 'order_number',
CustomerName: 'customer_name',
NetAmount: 'net_amount',
OverallStatus: 'status',
DeliveryNote: 'delivery_note',
},
},
fields: {
order_number: { type: 'text', label: 'Order' },
customer_name: { type: 'text', label: 'Customer' },
net_amount: { type: 'number', label: 'Net amount' },
status: { type: 'text', label: 'Status' },
delivery_note: { type: 'text', label: 'Delivery note' },
},
},
],
});What each piece does on this driver:
- Three names stay equal: the plugin's
name, the datasourcename, and the datasourcedriver. The connect step findsdriver.erpalready registered and uses it as it is.configis left empty, because the plugin's options already configured the driver. remoteNamenames the entity set. Without it the driver looks for an entity set named like the object.- This driver applies
external.columnMapitself, in both directions: rows arrive under your field names, and filters, sorts and writes go out under the service's property names. That is a property of this driver, not of plugin drivers in general. idis the entity key. A single-property key is read as text ('4711'). A composite key is the body of its key predicate ('OrderID=1,ProductID=7'), which is also whatGET /data/<object>/<id>takes.- Or import the objects from
$metadata. Once the datasource is saved, Studio's datasource page lists its entity sets on the Tables tab. Import saves one, and Import selected saves several, referenced entity sets first. An MCP client whose token carriesmetadata:writereaches the same import throughlist_remote_tablesandimport_object. An import saves an object into the environment's metadata, so under thegroupandisolatedtenancy postures only the platform operator imports, as only the platform operator saves any other metadata there (tenancy modes). A refresh of the datasource's catalog saves its snapshot there too, so the same holds for it. The draft maps each EDM type to a field type, makes the key theid, and writescolumnMapfor every property whose name is not a snake_case field name (CategoryIDbecomescategory_id). EDM types with no field type, such asEdm.Durationor a service's own enum, are listed for review. An imported object is read-only unless the datasource allows writes and you tick Allow writes to this object. - Navigation properties are never followed, so a related entity set is an object of
its own. The import turns a single-valued navigation into a
lookupon its foreign-key property when itsReferentialConstraintis one property pair onto the target's key and the target is already imported on the same datasource. Every other navigation is left out and named in the review, so import the target first. The lookup reads the related record by key, like any other lookup. These rules are whatpackages/services/service-datasource/src/__tests__/external-object-draft-odata-metadata.test.tsasserts, over a recorded Northwind$metadata. - The service document is read lazily.
connect()asks the service nothing, and$metadatais read once, at the first request. A wrongserviceRootor an unknown entity set shows up on the first read of the object, not at boot. checkOnBoot: falsekeeps the boot from depending on the service being up. The driver's tests do not cover the boot-time drift check over an OData service, so test it against your service before you turn it on.
3. Credentials
⛔ Never put a credential in the stack, in the plugin options, or in the serviceRoot
URL. The datasource names exactly one of two channels:
- A secret of its own,
external.credentialsRef, resolved through the host's secret store. This is the example in step 2. - A connector instance's credential,
external.connector, when a connector already authorizes the same service (Borrowing a connector instance's credential).
A secret of its own: credentialsRef
The plugin resolves external.credentialsRef through the host's secret store:
bearersendsAuthorization: Bearer <secret>.basictakesusernamefrom the options and the password from the secret.nonesends nothing. AcredentialsRefdeclared besidenoneis refused, so a credential you meant to send is never left out by mistake.
The plugin resolves the secret at the first request, not at boot, and again after a
401. So a token rotated in the secret store is picked up by the next request that the
old token fails. The driver does not obtain or refresh tokens itself. A service that
issues short-lived OAuth2 access tokens needs something that keeps the stored secret
current.
Which store resolves it:
- Under
os serve, pass nosecrets. The server publishes its own datasource secret store as the kernel servicedatasource-secret-resolver, and the plugin reads it at the first request. Asys_secret:<id>handle that the Add Datasource wizard minted resolves, as on any federated datasource (Credentials). - A host that composes its own kernel passes
secrets: the same binder it givesDatasourceAdminServicePlugin, or any object withresolve(credentialsRef)answering the secret orundefined. It can instead register thatresolveas thedatasource-secret-resolverservice (DATASOURCE_SECRET_RESOLVER_SERVICEin@objectstack/types), which every plugin driver reading acredentialsRefuses the same way. Asecretsoption passed to the plugin always wins over the service.
Resolution fails closed. Each of these refuses the request rather than send it without
the credential: an auth scheme with no credentialsRef, a credentialsRef with neither
secrets nor the service, and a handle that does not resolve. The caller is answered
503 ERR_DATASOURCE_UNAVAILABLE, and the cause goes to the server log.
Borrowing a connector instance's credential
When a connector instance
already authorizes the ERP for your flows, the datasource can borrow its credential instead
of holding a secret of its own (ADR-0015 amendment D7). One credential, set where the
connector's is set, then serves the connector's actions and the datasource's records.
Name the instance in external.connector, leave credentialsRef out, and create the
plugin with auth left at none:
connectors: [
{
name: 'erp_api', label: 'ERP API', type: 'api',
provider: 'rest', providerConfig: { baseUrl: 'https://erp.example.com' },
auth: { type: 'bearer', credentialRef: 'ERP_API_TOKEN' },
},
],
datasources: [
{
name: 'erp', driver: 'erp', schemaMode: 'external', config: {},
external: { connector: 'erp_api', allowWrites: true }, // no credentialsRef
},
],
plugins: [
createODataDriverPlugin({
name: 'erp',
serviceRoot: 'https://erp.example.com/odata/v4/sales/', // no auth, no secrets
}),
],- The instance's
authdecides how the credential is sent:Authorization: Bearer,Authorization: Basic, or an API key in itsheaderNameheader (defaultX-API-Key) or, withparamName, a query parameter. - The plugin borrows at the first request, from the
connector-credential-sourceservice that@objectstack/service-automationregisters, and keeps the handle. Every request then presents the current credential: one past its expiry is obtained afresh before it is sent, and one the service refuses with a401is obtained afresh once and the request retried. A second401answers503, naming the instance in the server log. - It fails closed. Each of these refuses the request with nothing sent, answered
503 ERR_DATASOURCE_UNAVAILABLEwith the cause in the server log: no automation service in the stack, an instance that cannot lend (not declared, disabled, noauth), a credential that is not set yet, acredentialsRefbeside the connector, and anauthscheme other thannoneon the plugin, which would send a second credential. An instance that cannot lend also fails the datasource's connect, so Setup → Integrations → Datasources shows why (Borrowing a connector instance's credential has the rules the stack is validated against, and how the datasource recovers without a restart once the instance can lend).
4. What the service is asked
Behind the floor, a list read is one GET on the entity set:
GET <serviceRoot>SalesOrders?$filter=OverallStatus eq 'open'&$orderby=NetAmount desc,SalesOrderID&$top=1001
Prefer: odata.maxpagesize=500$filtercarries every part of thewherethe driver can translate:$eq,$ne,$gt,$gte,$lt,$lte,$between,$in,$nin,$null,$contains,$startsWith,$endsWithand$icontains(ASCII comparands), under$andand$or.- The rest is left to the engine, which re-applies the whole query to the rows the
service returns:
$not,$like,$exists, field references, and ordering or negation on text. The driver only ever asks a wider question. It drops a member of an$andit cannot translate, and drops a whole$orwhen one branch cannot be translated, so the service never returns fewer rows than match. This is also why a service that compares text case-insensitively still answers correctly. $orderbycarries the sort, then the entity key, so pages partition the result.- Paging follows
@odata.nextLinkuntil the cap plus one row is read ($top=1001above, at the default cap). A link to another origin is refused, so the credential never goes to a host you did not configure. - The page size and the total are the engine's, applied to the rows read. A count is computed the same way. The engine aggregates in memory.
The row cap applies. A read that matches more rows than the cap (1000 unless the
datasource sets external.rowCap, at most 10,000) answers
422 EXTERNAL_ROW_CAP_EXCEEDED rather than a partial set
(the row cap). Every
$filter the driver can push down makes the service return only matching rows, so the cap
counts matching rows, not the whole entity set. Give list views a filter the service
can run. The driver honours whatever $top it is sent, so a raised cap needs no
driver change. For an entity set users must browse whole past the ceiling, mirror it
into a real object instead.
5. Writes
Writes are the same double opt-in as federation: external.allowWrites on the
datasource and external.writable on the object
(Writes).
| The platform does | The service is sent |
|---|---|
POST /data/sales_order | POST SalesOrders |
PATCH /data/sales_order/:id | PATCH SalesOrders(<key>) |
DELETE /data/sales_order/:id | DELETE SalesOrders(<key>) |
If-Matchcarries the entity's@odata.etagwhen the driver's last read of that record returned one, so a write over someone else's change is refused by the service.- Only properties the entity type declares are sent. The fields the engine stamps
itself (
updated_at,updated_by) are not the service's and are left out. - The service's refusal reaches the caller. A
400,409,412or422from the service answers422 EXTERNAL_WRITE_REFUSEDwith the service's own message. Let the ERP's validation be the rule, and do not retry around it. - Refused by name:
upsert, the bulk writes, and transactions. Without$batchthere is no atomic multi-row write. A write that selects its rows by a filter is refused too. Read the ids, then write each one.
Choose the sharing model
A record from an OData service has no owner_id and no created_by, so the default
sharing model admits no row for a member, and an owner-keyed write is refused. The rule
and the ways out are the same as on any plugin driver
(Choose the sharing model on purpose):
- Read-only data: leave
allowWritesoff and declaresharingModel: 'public_read'. Who may read is the permission sets' decision. - Editable data: declare
sharingModel: 'public_read_write', and decide who may edit in abeforeUpdatehook. A member's delete is still refused on this model.
OData v2 services
Declare the version on the plugin; nothing else in the datasource or the objects changes:
createODataDriverPlugin({
name: 'gateway',
serviceRoot: 'https://erp.example.com/sap/opu/odata/sap/ZSALES_SRV/',
version: '2.0',
auth: { scheme: 'basic', username: 'SVC_PLATFORM' },
});The driver then speaks the public v2 protocol. Everything above holds, spelled the v2 way:
| v4 | v2 | |
|---|---|---|
| Request headers | OData-Version: 4.0 | DataServiceVersion: 2.0, Accept: application/json (verbose JSON) |
| A collection | value, @odata.nextLink, @odata.count | d.results, __next, __count (a 1.0 service's bare d array is read too) |
| The count | $count=true | $inlinecount=allpages |
$contains | contains(P,'x') | substringof('x',P) |
| Literals | 2024-03-01T10:00:00Z, a bare GUID, 1.5 | datetime'…', datetimeoffset'…', time'PT13H20M0S', guid'…', 7L, 1.5M, 1.5d, 1.5f |
| Keys | Orders(4711) | the same typed literals: Shipments(guid'…'), Items(7L) |
| A partial update | PATCH | POST with X-HTTP-Method: MERGE |
| The ETag | @odata.etag | __metadata.etag |
$metadata | CSDL 4.0, navigation with its ReferentialConstraint | EDMX 1.0: Association / AssociationSet, the constraint on the Association |
- Values are read the way the engine reads them.
Edm.DateTimearrives as/Date(<ms>)/and is read as that UTC instant (1996-07-04T00:00:00.000Z).Edm.Int64andEdm.Decimalarrive as text and are read as numbers, except anEdm.Int64beyond JavaScript's safe range, which stays text.Edm.Timeis a duration since midnight (PT13H20M) and is read as the time of day (13:20:00). Writes go out in the same forms. - v2 has no
Edm.Date. A v2 service keeps a date in anEdm.DateTimeat midnight. Declare the fielddate, and the driver reads the date alone (1996-07-04). - A zone-less date comparand is left to the engine. A filter on an
Edm.DateTimeis pushed down only when its value names its instant (…Z, an offset, or aDate). A bare date or a date-time without a zone is applied by the engine instead, so the service is never asked a narrower question. - The import reads Associations. An imported
Edm.DateTimebecomes adatetimefield and anEdm.Timeatimefield. A navigation whose Association carries aReferentialConstraint, with this entity type on its dependent end, becomes the samelookupa v4 navigation does. - What stays out of this package. Only the public v2 protocol ships here. Vendor
mechanics stay out: SAP Gateway's CSRF token handshake, the
sap-clientparameter and thesap:*annotations, for example. A service that needs one is served by a vendor plugin.
When the service fails
| What happened | The caller is answered |
|---|---|
A read the service refuses for its own sake (400, 404, 422) | 400 INVALID_QUERY, with a message naming the entity set, the status and the service's own text |
| A write the service refuses | 422 EXTERNAL_WRITE_REFUSED, with the service's message |
A 5xx, a timeout, an unreachable service, a 401 after the retry, a 403 or a 429 | 503 ERR_DATASOURCE_UNAVAILABLE; the cause goes to the server log |
A read that matches more rows than the cap (1000, or external.rowCap) | 422 EXTERNAL_ROW_CAP_EXCEEDED |
An outage is never answered as an empty list. A read refusal's message is withheld, and goes to the server log, when the read carries a predicate the caller did not write, such as a sharing scope. The full mapping is in Virtual objects.
What it does not do
- Vendor-specific mechanics on a v2 service, such as SAP's CSRF token handshake or
sap-client(OData v2 services). $expandand navigation properties. Bind each related entity set as an object of its own; an imported foreign key becomes alookuponto it.- Actions and functions of the service.
executeis refused. - Transactions and
$batch. Each write is one request. - More matching rows than the cap in one read (the cap above: 1000, or the
datasource's
external.rowCap, at most 10,000). - A read scoped by a parent's access (an object
controlled_by_parent): it is refused, because$filterhas no form of that join.
See also
- Virtual objects: which way in, the engine's floor, the sharing-model callout, and writing a driver for a system with no OData.
- External datasources: the datasource and
externalbinding, federation over SQL, the write gates and credentials. - Pulling a mapping: a real object fed from a remote API on a schedule.
packages/drivers/driver-odata/README.md: the package and its exports.
External Datasources (Federation)
Declare an external database as a datasource and query its tables as ObjectStack objects — visible, auto-connected, and queryable with zero app code.
Database Drivers
Connect ObjectStack to PostgreSQL, MySQL, SQLite, MongoDB, Turso or memory — URL-based driver inference, per-driver config keys and dialect caveats.