/** * Data Store: a customer's own MongoDB, registered with the platform. * * A store is one of two things, and `type` says which: * * - **`self`** -- a database **you** own, registered with a credential. The * platform holds that credential and nothing else: no copy, no mirror, and * deleting the store never touches your data. * - **`cloud`** -- a database the **platform** provisions for you, on one of its * clusters, in a location you pick. No credential to manage, nothing to open * to the internet -- and the platform is then the system of record, so * deleting the store deletes the records with it. * * Everything else is identical: the same collections, the same records, the same * calls. Pick `cloud` to store data you do not already have somewhere, and * `self` to work with a database you do. * * Three nested things: * * 1. **A data store** -- the database, yours or ours. For a `self` store the * platform holds the credential and nothing else: no copy of the data, no * mirror. * 2. **A collection** -- a schema you declare, naming a collection inside that * database and the fields in it. * 3. **Records** -- the documents. They live in your database, and every record * call is a live round trip to your cluster. * * The thing to keep in mind throughout: the platform is not the system of * record. Anything else holding those credentials can change the same data, so * a read is always current and a schema is a *declaration* rather than a * guarantee -- a document may carry fields the schema never mentioned, or lack * ones it does. */ /** Whose database the records are in. */ export type DataStoreType = 'self' | 'cloud'; /** * One cluster the platform can provision a cloud store on. * * `code` identifies the cluster and is what you send on a create; `location` is * the country it is in, which is the half worth showing a user. New locations * appear in this list rather than in a release note -- read it, do not hardcode * it -- and an existing store never moves when one is added, which is why its * `code` is returned on the store itself. */ export interface CloudDataStoreRegion { code: string; location: string; } /** The five storage types a field can declare. */ export type DataStoreFieldType = 'String' | 'Number' | 'Boolean' | 'Date' | 'Array'; /** * One declared field. * * `name` is **slugged** by the server: punctuation is dropped, spaces and * hyphens become underscores, and the result is lowercased, so `"First Name"` * comes back as `first_name`. Use the name the response gives you as the key in * every record payload -- it is the key the document actually has. */ export interface DataStoreField { name: string; type: DataStoreFieldType; /** For display. Never used as a key, so it can be whatever reads well. */ label?: string; /** * Applied when a record omits the field. `String`, `Number` and `Boolean` * only: a `Date` default would have to mean "now", and an `Array` default * shared between documents is a trap. */ default?: string | number | boolean; } /** The two ways to name a database. */ export type DataStoreCredentials = { type: 'field'; /** Hostname only: `cluster0.ab12c.mongodb.net`. No scheme, no port, no path. */ host: string; username: string; password: string; database: string; } | { type: 'string'; /** * A full connection string. Must name a database -- * `mongodb+srv://user:pass@host/` -- because a URI without one * silently puts every record in a database called `test`. */ uri: string; }; /** * Register your own database, or ask for one of the platform's. * * ```ts * // yours * await crm.dataStores.create({ title: 'Orders DB', credentials: { … } }); * * // theirs, in Germany * await crm.dataStores.create({ title: 'Leads', type: 'cloud' }); * ``` * * `type` defaults to `self`, so existing code is unaffected. The two halves do * not mix: a `self` store needs `credentials` and takes no `code`, a `cloud` * store takes a `code` and no credentials, and anything else is a `422`. */ export interface CreateDataStoreInput { title: string; /** `self` -- your database. `cloud` -- one of the platform's. Default `self`. */ type?: DataStoreType; /** Required for `self`, and refused for `cloud`. */ credentials?: DataStoreCredentials; /** * `cloud` only: which cluster to put it on, from * {@link DataStores.regions}. Omitted, the platform picks its default. */ code?: string; } /** * Rename, re-point, or both. * * Credentials are replaced whole or not at all: a host without its matching * password is not a partial credential, it is a broken one. New credentials are * tested before anything is stored. */ export interface UpdateDataStoreInput { title?: string; credentials?: DataStoreCredentials; } /** * A registered data store. * * **No credential is ever returned.** The password is encrypted at rest and does * not come back in any form; `summary` is the connection with its password * replaced by `***`, which is what an edit screen needs to show which database * it is looking at. */ export interface DataStore { id: string; title: string; /** Whose database it is. Stores created before cloud stores existed are `self`. */ type: DataStoreType; /** `cloud` only: the cluster it is on. Quote it in a support request. */ code?: string; /** `cloud` only: where that cluster is -- `Germany`. */ location?: string; /** How the credential was given. Absent on a `cloud` store, which has none. */ credentialType?: 'field' | 'string'; engine: 'mongodb'; /** Your host. Empty on a `cloud` store: the platform's own is not disclosed. */ host: string; database: string; /** Your database user. Empty on a `cloud` store. */ username: string; /** * For display. The connection string with the password removed for a `self` * store, and `cloud:///` for a cloud one -- a label, not * something to connect with. */ summary: string; /** When the connection was last proven to work: a create, an update, or a test. */ verifiedAt?: string; createdAt: string; updatedAt: string; } /** What `test()` found. */ export interface DataStoreTestResult { ok: boolean; /** The database the credential actually lands in. */ database: string; /** * Collections already in that database, up to 100, `system.*` excluded. * * Empty when the database user has no `listCollections` privilege, which a * least-privilege credential legitimately may not -- it does not mean the * database is empty. */ collections: string[]; /** Round-trip milliseconds for the ping. A rough health signal, not a metric. */ latencyMs: number; } export interface CreateDataStoreCollectionInput { title: string; fields: DataStoreField[]; /** * An existing collection to use instead of a new one. * * Omitted, the server generates `_`, which cannot * collide with anything already in the database. Supplied, this points at a * collection you already have -- which is how a database with data in it gets * used rather than duplicated. * * Immutable once set. A schema points at one collection for its whole life. */ collection?: string; /** Whether the chat panel offers this collection beside a conversation. */ show_in_chat?: boolean; /** Presentation only. Nothing here changes what is stored or queryable. */ display?: { theme?: string; fields?: string[]; }; } /** * Edits to a declaration. `collection` is deliberately not editable. * * Changing `fields` changes validation and coercion from the next call onward * and rewrites nothing already stored. Dropping a field hides the values under * that key rather than deleting them; re-declaring it shows them again. */ export interface UpdateDataStoreCollectionInput { title?: string; fields?: DataStoreField[]; show_in_chat?: boolean; display?: { theme?: string; fields?: string[]; }; } export interface DataStoreCollection { id: string; storeId: string; title: string; /** The collection this reads and writes inside your database. */ collection: string; fields: DataStoreField[]; showInChat: boolean; display: { theme?: string; fields: string[]; }; createdAt: string; updatedAt: string; } /** * One document, as it reads back. * * **Every stored key is returned**, declared or not: a collection you already * had, or one another application also writes to, holds fields this schema has * never heard of, and hiding your own data because the declaration is incomplete * would be worse than surprising. The schema governs what can be *written* * through the API; it does not filter what comes back. * * `_id` reads back as `id`, and dates as ISO strings. */ export type DataStoreRecord = Record & { id: string; }; /** * A record body on the way in. * * Keys must be declared field names. An undeclared key is a **422 naming it**, * not a silent drop -- which is the one behaviour here that differs from the * legacy endpoint, where a typo meant data quietly went nowhere. * * `null` clears a field. `id`, `_id`, `created_on` and `updated_on` are the * server's: the timestamps are ignored if sent, the ids are refused. */ export type DataStoreRecordBody = Record; /** Insert one record, or up to 1000. */ export type InsertDataStoreRecordsInput = { record: DataStoreRecordBody; records?: never; } | { records: DataStoreRecordBody[]; record?: never; }; /** * A Mongo query, as you would write it for the driver. * * The full language -- `$in`, `$gt`, `$or`, `$regex`, `$elemMatch` -- because it * is your database and there is nothing in it you do not already own. Three * operators are refused wherever they appear: `$where`, `$function` and * `$accumulator`, all of which run JavaScript on your cluster. * * Operand values are coerced against the declared field type before the query is * sent, so `{ created_on: { $gte: '2026-01-01' } }` against a `Date` field * matches real dates rather than comparing a string to a date and finding * nothing. `id` works as an alias for `_id` and takes the string form. */ export type DataStoreFilter = Record; /** * Paging and ordering for a record list. * * A type alias rather than an interface deliberately: only aliases of object * literals get TypeScript's implicit index signature, which is what lets this * be passed as the query record the HTTP client takes. An interface here fails * to compile at the call site with a message about a missing index signature. */ export type ListDataStoreRecordsQuery = { page?: number; /** 1-100, default 25. */ rows?: number; /** A field name. Defaults to `created_on`. */ sort?: string; /** Defaults to `desc`. */ order?: 'asc' | 'desc'; }; export type SearchDataStoreRecordsInput = ListDataStoreRecordsQuery & { filter: DataStoreFilter; }; export interface UpdateDataStoreRecordInput { record: DataStoreRecordBody; } /** The filter cannot be empty: `{}` would rewrite the whole collection. */ export interface BulkUpdateDataStoreRecordsInput { filter: DataStoreFilter; update: DataStoreRecordBody; } /** The filter cannot be empty, for the same reason. */ export interface BulkDeleteDataStoreRecordsInput { filter: DataStoreFilter; } export interface DataStoreWriteResult { /** * Documents inserted. Can be fewer than were sent: a batch is unordered, so * one document rejected by an index of yours does not abandon the rest. */ inserted: number; /** Ids of the inserted documents, in the order they were sent. */ ids: string[]; } export interface DataStoreUpdateResult { matched: number; /** Lower than `matched` when a record already held the values being written. */ modified: number; } export interface DataStoreDeleteResult { deleted: number; }