import type { BulkDeleteDataStoreRecordsInput, CloudDataStoreRegion, BulkUpdateDataStoreRecordsInput, CreateDataStoreCollectionInput, CreateDataStoreInput, DataStore, DataStoreCollection, DataStoreDeleteResult, DataStoreRecord, DataStoreTestResult, DataStoreUpdateResult, DataStoreWriteResult, InsertDataStoreRecordsInput, ListDataStoreRecordsQuery, ListQuery, Paginated, SearchDataStoreRecordsInput, UpdateDataStoreCollectionInput, UpdateDataStoreInput, UpdateDataStoreRecordInput } from '../types'; import type { HttpClient } from '../http'; /** * Data stores: a MongoDB you own, or one the platform runs for you. * * ```ts * // your own cluster, registered with a credential * const store = await crm.dataStores.create({ * title: 'Orders DB', * credentials: { * type: 'field', * host: 'cluster0.ab12c.mongodb.net', * username: 'crm_writer', * password: process.env.ORDERS_DB_PASSWORD!, * database: 'shop', * }, * }); * * // or one of the platform's -- no credential, no cluster to run * const hosted = await crm.dataStores.create({ title: 'Leads', type: 'cloud' }); * * const orders = await crm.dataStoreCollections.create(store.id, { * title: 'Orders', * fields: [ * { name: 'order_no', type: 'String' }, * { name: 'total', type: 'Number' }, * { name: 'paid', type: 'Boolean', default: false }, * ], * }); * * await crm.dataStoreRecords.insert(store.id, orders.id, { * record: { order_no: 'A-1001', total: 49.9 }, * }); * ``` * * ## Which kind to pick * * A **cloud** store (`type: 'cloud'`) is a database the platform provisions on * one of its clusters, in a location you choose from `regions()` -- Germany for * now. There is no credential to manage, nothing to expose to the internet, and * no IP access list to maintain. Use it for data that does not already live * somewhere. * * A **self** store is your own cluster, and the default. Use it when the data is * already there, or has to stay there. * * The difference that matters beyond setup is **who the system of record is**, * and it shows up on delete -- see below. * * ## What the platform holds, and what it does not * * For a `self` store it holds the credential -- encrypted -- and the schemas you * declare. It holds **none of your data**: records are read from and written to * your cluster on every call, so nothing here is a copy that can go stale, and * nothing here survives you revoking the credential. * * For a `cloud` store it holds the database itself. Nothing about that cluster * is returned -- no host, no user, no connection string -- because there is no * second way in: the API is the only door to those records. * * That extends to its failures. On a `self` store an upstream error carries the * database's own message, because it is yours and the detail is what you act on * (`datastore_auth_failed` with the reason, `datastore_unreachable` pointing at * an IP access list). On a `cloud` store every upstream failure is one code -- * `502 datastore_unavailable`, no details -- because the cluster is the * platform's and nothing in that message would be yours to fix. Refusals about * *your request* are reported in full either way. * * ## A record is a row, not a file * * Records hold structured values. **Files are refused**, in either kind of * store: a `data:` URI or base64 that begins with a known file signature comes * back `422` however small it is, and every value and every record is capped -- * see `insert` for the numbers. Put the file in object storage and store its * URL, which is cheaper, cacheable, and does not pass through this API on every * page read. * * Which also means every record call costs a round trip to your database. The * platform keeps the connection open between calls, so that is one query's * latency rather than a fresh TLS handshake each time, but a loop of a thousand * single inserts is a thousand round trips: send them as one `insert` with * `records` instead. * * ## Deleting a `self` store never deletes your data. Deleting a `cloud` store does * * On a **self** store, `remove()` forgets the credential and the schemas, and * `remove()` on a collection forgets the declaration. **Neither touches your * database** -- the collections and every document in them are exactly as they * were, and re-registering the same credential with the same collection name * reads it all straight back. * * On a **cloud** store, `remove()` **destroys the database**. Every record in it * is gone, there is no undo, and re-creating the store gets a fresh empty one. * Ask before you call it. * * ## The host must be reachable from the internet * * A credential pointing at a private or loopback address is refused with * `422 invalid_datastore_host`: the platform would be connecting to its own * network rather than to yours. Atlas users will usually also need to allow the * platform's egress addresses in their project's IP access list -- the symptom * otherwise is `502 datastore_unreachable` with a server-selection timeout. */ export declare class DataStores { private readonly http; constructor(http: HttpClient); /** * `GET /v1/datastores/cloud-regions` -- `{ data: [{ code, location }] }`. * * The clusters a cloud store can be provisioned on. Read it to render the * location choice rather than hardcoding one: locations are added over time, * and the `code` is what a create takes. * * An **empty list** means this deployment provisions none -- an on-premise * install, typically -- and a `type: 'cloud'` create would be refused. * * Scope: `datastores:read`. */ regions(): Promise<{ data: CloudDataStoreRegion[]; }>; /** * `POST /v1/datastores` -- 201 with the store. * * ### Payload * * | Field | Type | Required | Notes | * | --- | --- | --- | --- | * | `title` | string | yes | What you call it. 1-120 characters. | * | `type` | `'self' \| 'cloud'` | no | Default `self`: your own database. | * | `credentials` | object | for `self` | Either form below. Refused for `cloud`. | * | `code` | string | no | `cloud` only: a cluster from `regions()`. Defaults to the platform's. | * * ### A cloud store * * ```ts * await crm.dataStores.create({ title: 'Leads', type: 'cloud' }); * ``` * * That is the whole payload: the platform provisions a database of its own, * in the default location, and the response carries the `code` and `location` * it ended up in. Nothing below about credentials, hosts or IP access lists * applies -- and neither does the 502 pair, because there is no cluster of * yours to be unreachable. * * | Status | Code | Meaning | * | --- | --- | --- | * | 422 | `validation_failed` | Credentials sent with `type: 'cloud'`, or a `code` that is not on offer -- including when this deployment offers none. | * * **`credentials.type: 'field'`** * * | Field | Type | Notes | * | --- | --- | --- | * | `host` | string | Hostname only: `cluster0.ab12c.mongodb.net`. | * | `username` | string | | * | `password` | string | Encrypted at rest; never returned. | * | `database` | string | | * * Assembled into `mongodb+srv://…?retryWrites=true&w=majority`, with both * halves of the credential percent-encoded -- so a password containing `@`, * `/` or `?` works, which it did not in the legacy panel. * * **`credentials.type: 'string'`** * * | Field | Type | Notes | * | --- | --- | --- | * | `uri` | string | `mongodb://` or `mongodb+srv://`, and it must name a database. | * * ### What happens before the row is written * * The credential is checked for shape, the host is resolved and refused if it * is private, and a connection is opened and pinged. Only then is anything * stored -- so a 201 means the database really did accept these credentials, * and a store you can see is a store that worked at least once. * * | Status | Code | Meaning | * | --- | --- | --- | * | 422 | `validation_failed` | Malformed URI, a host with a scheme in it, or no database named. | * | 422 | `invalid_datastore_host` | The host does not resolve, or resolves somewhere private. | * | 502 | `datastore_auth_failed` | The database refused the username or password. | * | 502 | `datastore_unreachable` | No answer in time -- usually an IP access list. | * | 502 | `datastore_unavailable` | A `cloud` store only: the platform's cluster did not answer. Nothing for you to fix; retry. | * * Scope: `datastores:write`. */ create(input: CreateDataStoreInput): Promise; /** * `GET /v1/datastores` -- offset paginated, newest first. * * | Query | Type | Default | Notes | * | --- | --- | --- | --- | * | `page` | number | `1` | 1-based. | * | `rows` | number | `25` | 1-100. | * | `search` | string | — | Case-insensitive match on the title. | * * Scope: `datastores:read`. */ list(query?: Partial): Promise>; /** `GET /v1/datastores/:store`. Scope: `datastores:read`. */ get(store: string): Promise; /** * `PATCH /v1/datastores/:store` -- rename, re-point, or both. * * | Field | Type | Notes | * | --- | --- | --- | * | `title` | string | | * | `credentials` | object | Replaced whole, and tested before it is stored. `self` stores only. | * * `type` and `code` are not editable: they say where the records are, not how * the store is configured. Sending `credentials` for a **cloud** store is a * `422` -- there is nothing to replace, and re-pointing it would leave its * records in a database nothing addresses. Register your own database as a * second store instead. * * A partial credential is not accepted: send all four fields, or the whole * URI. New credentials go through the same checks as `create`, and the old * connection is dropped -- so the next record call reads from the new cluster * rather than from a connection that happens to still be open. * * Scope: `datastores:write`. */ update(store: string, input: UpdateDataStoreInput): Promise; /** * `DELETE /v1/datastores/:store` -- 204. * * **A `self` store:** forgets the credential and every schema declared on it. * Your database is not touched -- the collections and all their documents * remain, and registering the same credential again with the same collection * names reads them straight back. * * **A `cloud` store:** drops every collection in its database, which destroys * the database with them. Every record is gone, there is no undo, and a new * store of the same name is a new empty database. Confirm with the user before * calling this on one. * * | Status | Code | Meaning | * | --- | --- | --- | * | 502 | `datastore_purge_failed` | A collection could not be deleted, named in `details`. Nothing was removed -- the store is intact and the call can be retried. | * * Scope: `datastores:write`. */ remove(store: string): Promise; /** * `POST /v1/datastores/:store/test` -- connect, and report. * * ```ts * const { ok, database, collections, latencyMs } = await crm.dataStores.test(store.id); * ``` * * The answer to "is it us or them". A credential that worked last month may * have been rotated, an Atlas project may be paused, an IP access list may * have been tightened -- each of which this distinguishes by status code * rather than leaving to be inferred from a failing record call. * * `collections` is also what you need in order to point a new schema at a * collection that already exists. It is empty when the database user has no * `listCollections` privilege, which is a perfectly good credential -- not a * sign the database is empty. * * A POST rather than a GET because it opens a connection and updates * `verifiedAt`. Scope: `datastores:read`. */ test(store: string): Promise; } /** * The collections you declare over a data store: a name, and the fields in it. * * ```ts * const customers = await crm.dataStoreCollections.create(store.id, { * title: 'Customers', * fields: [ * { name: 'Email', type: 'String' }, // stored as `email` * { name: 'Signed Up', type: 'Date' }, // stored as `signed_up` * { name: 'Credits', type: 'Number', default: 0 }, * { name: 'Tags', type: 'Array' }, * ], * }); * * customers.fields.map((field) => field.name); // ['email','signed_up','credits','tags'] * ``` * * ## Field names are slugged, once * * The server normalises every name -- punctuation dropped, spaces and hyphens to * underscores, lowercased -- and the normalised name is both what comes back and * what the document actually stores. **Use the returned names as your record * keys.** The legacy panel slugged the name for the database but kept the * original in the schema, so a field called `First Name` stored under * `first_name` while the schema said otherwise, and records written against the * schema's spelling silently went nowhere. * * ## Declaring a collection does not create one * * Mongo creates a collection on its first write, so a new schema addresses a * collection that does not exist yet. Reads against it correctly return an empty * page rather than failing. * * ## Editing fields rewrites nothing * * `update()` changes validation and coercion from the next call onward. It does * not migrate documents: dropping a field hides the values under that key * (re-declare it and they are back), and changing a field's type leaves old * documents holding the old type. The collection a schema points at cannot be * changed at all -- every record ever written through it lives there. */ export declare class DataStoreCollections { private readonly http; constructor(http: HttpClient); /** * `POST /v1/datastores/:store/collections` -- 201. * * | Field | Type | Required | Notes | * | --- | --- | --- | --- | * | `title` | string | yes | 1-120 characters. | * | `fields` | array | yes | 1-100 fields; see below. | * | `collection` | string | no | An existing collection to adopt. Immutable afterwards. | * | `show_in_chat` | boolean | no | Default `false`. Whether the chat panel offers it. | * | `display.theme` | string | no | Presentation only. | * | `display.fields` | string[] | no | Field names to show, in order. Presentation only. | * * **Each field** * * | Field | Type | Notes | * | --- | --- | --- | * | `name` | string | Slugged by the server; the slug is the document key. | * | `type` | `String` `Number` `Boolean` `Date` `Array` | | * | `label` | string | Display only. | * | `default` | string \| number \| boolean | `String`, `Number` and `Boolean` only. | * * Omit `collection` and the server generates `_`, which * cannot collide with anything already in your database. Supply it to point at * a collection you already have -- the way to use a database with data in it * rather than duplicating it. * * | Status | Code | Meaning | * | --- | --- | --- | * | 404 | `not_found` | No such store in this account. | * | 409 | `conflict` | This store already has a schema for that collection name. | * | 422 | `validation_failed` | A field name that slugs to nothing, a duplicate after slugging, or a default of the wrong type. | * * Scope: `datastores:write`. */ create(store: string, input: CreateDataStoreCollectionInput): Promise; /** * `GET /v1/datastores/:store/collections` -- offset paginated, * newest first. `page`, `rows`, and `search` on the title. * * Scope: `datastores:read`. */ list(store: string, query?: Partial): Promise>; /** * `GET /v1/datastores/:store/collections/:collection`. * * Both ids are checked: a collection id belonging to a different store -- even * one of your own -- is a 404 rather than a match. * * Scope: `datastores:read`. */ get(store: string, collection: string): Promise; /** * `PATCH /v1/datastores/:store/collections/:collection`. * * `title`, `fields`, `show_in_chat` and `display`. Not `collection`: a schema * points at one collection for its whole life, and re-pointing it would orphan * every record written through it. Point a new schema at the other collection * instead. * * Scope: `datastores:write`. */ update(store: string, collection: string, input: UpdateDataStoreCollectionInput): Promise; /** * `DELETE /v1/datastores/:store/collections/:collection` -- 204. * * Forgets the declaration. **Your collection and its documents remain** -- * declaring the same collection name again reads the same records back. * * Scope: `datastores:write`. */ remove(store: string, collection: string): Promise; } /** * The records in a data store collection -- documents in **your** database. * * ```ts * await crm.dataStoreRecords.insert(store.id, orders.id, { * records: [{ order_no: 'A-1', total: 10 }, { order_no: 'A-2', total: 20 }], * }); * * const { data, pagination } = await crm.dataStoreRecords.search(store.id, orders.id, { * filter: { paid: false, total: { $gte: 20 } }, * sort: 'total', * order: 'desc', * }); * ``` * * ## Writes are validated against the declaration * * Keys must be declared field names, and values are coerced to the declared * type -- `"42"` into a `Number` field is stored as `42`, an ISO string into a * `Date` field as a date. An **undeclared key is a 422 naming it**, which is the * one deliberate break from the legacy endpoint: there, an unknown key was * dropped silently, so a typo meant data quietly went nowhere. * * `null` clears a field. `created_on` and `updated_on` are stamped by the server * -- unix seconds, or real dates if the collection declared them as `Date` -- * and ignored if you send them. `id` cannot be written. * * ## Reads return everything that is stored * * Including keys the schema does not declare, because the documents are yours * and may have been written by something else. `_id` reads back as `id`. * * ## There is no aggregation endpoint * * Deliberately: a pipeline can read other collections with `$lookup` and write * them with `$merge`, which would make a schema's boundary meaningless, and * `$function` runs JavaScript on your cluster. The same three operators are * refused inside a filter. For anything that needs a pipeline, use your own * credentials and your own driver -- it is your database. */ export declare class DataStoreRecords { private readonly http; /** * ## What a record may hold * * Structured values -- text, numbers, booleans, dates, short lists. **Not * files.** A record is a row: every read of a page carries every byte of it * back through the API, uncached and unstreamed, so one embedded image turns * a 25-row page into a response nobody wants. The file belongs in object * storage with its URL in the record. * * Refused with `422 validation_failed`, keyed by the field: * * | | | * | --- | --- | * | A `data:` URI | At any size. Unambiguously a file. | * | Base64 starting with a file signature | PNG, JPEG, GIF, WebP, PDF, ZIP, MP3, MP4, WebM -- only for values of 64 characters or more, so a short string that happens to start that way is fine. | * | A raw file header | `%PDF-`, `GIF89a`, the PNG and JPEG magic bytes, a ZIP header. | * | `$binary`, `$uuid`, `$code`, `$function`, `$where` | Extended-JSON keys anywhere inside an `Array` value: binary and stored JavaScript, by another route. | * | A value over **16KB** | Serialised. Generous for text, hopeless for media. | * | A record over **64KB** | The finished document, so a hundred legal fields cannot add up to an illegal row. | * | An array over **1000 items**, or nesting deeper than **8** | A list of options, not a dataset. | * * The same rules apply to `insert`, `update` and `bulkUpdate`, and to a * `cloud` store and your own alike -- the cost is the round trip, which both * pay. */ constructor(http: HttpClient); /** * `POST /v1/datastores/:store/collections/:collection/records` -- 201. * * | Field | Type | Notes | * | --- | --- | --- | * | `record` | object | One record. Exactly one of this or `records`. | * | `records` | object[] | 1-1000 records. | * * ```ts * const { inserted, ids } = await crm.dataStoreRecords.insert(store, collection, { * records: rows, * }); * ``` * * A batch is **unordered**: one document rejected by a unique index of yours * does not abandon the rest, so `inserted` may be lower than what you sent. * `ids` are in the order the documents were sent. * * Prefer one call with `records` over a loop of single inserts -- each call is * a round trip to your cluster. * * Scope: `datastore-records:write`. */ insert(store: string, collection: string, input: InsertDataStoreRecordsInput): Promise; /** * `GET /v1/datastores/:store/collections/:collection/records` * * | Query | Type | Default | Notes | * | --- | --- | --- | --- | * | `page` | number | `1` | 1-based. | * | `rows` | number | `25` | 1-100. | * | `sort` | string | `created_on` | Any field name. | * | `order` | `asc` \| `desc` | `desc` | | * * `_id` is appended to the sort as a tiebreaker, so paging is reproducible * even when the sort field repeats -- or does not exist, which it will not on * an adopted collection that something else wrote. * * Scope: `datastore-records:read`. */ list(store: string, collection: string, query?: ListDataStoreRecordsQuery): Promise>; /** * `POST /v1/datastores/:store/collections/:collection/records/search` * * | Field | Type | Notes | * | --- | --- | --- | * | `filter` | object | A Mongo query. Required; `{}` matches everything. | * | `page` `rows` `sort` `order` | | As `list`. | * * ```ts * await crm.dataStoreRecords.search(store, collection, { * filter: { * $or: [{ status: 'open' }, { total: { $gte: 100 } }], * signed_up: { $gte: '2026-01-01' }, // coerced to a Date for a Date field * tags: 'vip', // an Array field contains this * }, * }); * ``` * * A POST because a Mongo filter is a nested object and encoding one into a * query string is a different argument in every HTTP client. It reads and * changes nothing. * * Operand values are coerced per declared field, so dates and numbers written * as JSON strings match. `$where`, `$function` and `$accumulator` are refused * with `422 filter_operator_not_allowed`, and a filter nested deeper than ten * levels or holding more than 500 keys with `422 filter_too_complex`. * * Scope: `datastore-records:read`. */ search(store: string, collection: string, input: SearchDataStoreRecordsInput): Promise>; /** * `GET /v1/datastores/:store/collections/:collection/records/:record` * * 404 when there is no such document. Scope: `datastore-records:read`. */ get(store: string, collection: string, record: string): Promise; /** * `PATCH /v1/datastores/:store/collections/:collection/records/:record` * * | Field | Type | Notes | * | --- | --- | --- | * | `record` | object | The fields to change. Validated exactly as an insert. | * * A `$set` of those fields and nothing else -- keys you leave out are * untouched, and `null` clears a value. `updated_on` is stamped; `created_on` * cannot be moved. Returns the record as it is after the write. * * Scope: `datastore-records:write`. */ update(store: string, collection: string, record: string, input: UpdateDataStoreRecordInput): Promise; /** * `POST /v1/datastores/:store/collections/:collection/records/bulk-update` * * | Field | Type | Notes | * | --- | --- | --- | * | `filter` | object | Required and **non-empty**. | * | `update` | object | The fields to set on every match. | * * ```ts * const { matched, modified } = await crm.dataStoreRecords.bulkUpdate(store, collection, { * filter: { status: 'pending', created_on: { $lt: cutoff } }, * update: { status: 'expired' }, * }); * ``` * * `{}` as the filter is refused: it matches the whole collection, and a * one-character slip that rewrites every record is not something to make * convenient. `modified` is lower than `matched` where a record already held * the values. * * Scope: `datastore-records:write`. */ bulkUpdate(store: string, collection: string, input: BulkUpdateDataStoreRecordsInput): Promise; /** * `DELETE /v1/datastores/:store/collections/:collection/records/:record` * * 404 when there is no such document. This one really does delete your data. * * Scope: `datastore-records:write`. */ remove(store: string, collection: string, record: string): Promise; /** * `POST /v1/datastores/:store/collections/:collection/records/bulk-delete` * * | Field | Type | Notes | * | --- | --- | --- | * | `filter` | object | Required and **non-empty**. | * * Deletes every match, irreversibly, from your database. The empty filter is * refused for the same reason as in `bulkUpdate` -- emptying a collection * deliberately means paging through it. * * Scope: `datastore-records:write`. */ bulkRemove(store: string, collection: string, input: BulkDeleteDataStoreRecordsInput): Promise; }