Insert
Inserts a single document
Inserts a new document into database. https://docs.codemash.io/api/database/collections/insert
Collection name - unique, lowercased, collection name without whitespace. E.g., if your collection title you have entered in the CodeMash dashboard is "Business Trips" then collection name would be "business-trips".
The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2
Accept Header
API key of your cluster. Can be passed in a header as X-CM-Cluster.
ID of your project. Can be passed in a header as X-CM-ProjectId.
Specify culture code when your response from the API should be localised. E.g.: en
Records are validated against CodeMash JSON Schema before each insert. That can slow down API performance. If you have already validated your data with JSON Schema and you can assure of data integrity, you can bypass document validation by setting this property to true.
By default file uploads are done after the record is inserted, set to true in case you need to wait for files to be uploaded
Triggers are called each time you insert the document. You can disable triggers if it's not needed. This is really important when you insert in database from trigger itself, so then you can avoid infinite loop.
Set responsible user for document, but be sure the caller of API has right set of permissions.
If your document has file field(s), then you set field value with Id that comes from your file storage provider. E.g.: When your file provider is AWS S3, so then you can set field value to S3 file key, and this will be automatically combined between your file service provider and CodeMash.
Success
POST /{version}/db/{CollectionName} HTTP/1.1
Host: api.codemash.io/
Authorization: YOUR_API_KEY
Accept: application/json
X-CM-ProjectId: text
Content-Type: application/x-www-form-urlencoded
Content-Length: 135
"BypassDocumentValidation=true&WaitForFileUpload=true&IgnoreTriggers=true&ResponsibleUserId='text'&ResolveProviderFiles=true"Success
{
"response_status": {
"error_code": "text",
"message": "text",
"stack_trace": "text",
"errors": [
{
"error_code": "text",
"field_name": "text",
"message": "text",
"meta": {
"ANY_ADDITIONAL_PROPERTY": "text"
}
}
],
"meta": {
"ANY_ADDITIONAL_PROPERTY": "text"
}
},
"result": "text"
}var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);
var person = new Person { Name = "John" };
await service.InsertOneAsync(person, new DatabaseInsertOneOptions());import { db } from 'codemash';
const request = {
start: '1588855312059', // Unix time stamp in miliseconds
end: '1588855340191', // Unix time stamp in miliseconds
employee: 'some_user_id',
type: 'paid',
};
export async function saveHolidaysRequest(request) {
const response = await db.saveRecord('holidays', request);
return response;
}
use Codemash\CodemashClient;
use Codemash\CodemashDb;
class CodemashService
{
protected CodemashDb $codemashDb;
protected string $collectionName = '{YOUR_COLLECTION_NAME}';
public function __construct()
{
$secretKey = '{YOUR_SECRET_KEY}';
$projectId = '{YOUR_PROJECT_ID}';
$client = new CodemashClient($secretKey, $projectId);
$this->codemashDb = new CodemashDb($client);
}
public function insertEmployee()
{
$responseData = $this->codemashDb->insertOne([
'collectionName' => 'employees',
'document' => [
'name' => 'John',
'email' => '[email protected]',
'address' => 'New York',
],
]);
}
}Last updated
Was this helpful?