CodeMash
  • ๐Ÿš€Backend as a service
  • ๐Ÿฆ„Roadmap
  • ๐Ÿ•Release Notes
  • Installation
    • Managed Service
    • ๐Ÿ—๏ธAWS-CDK
    • ๐Ÿ—๏ธAzure
    • ๐Ÿ—๏ธDocker
    • ๐Ÿ—๏ธGC
    • ๐Ÿ—๏ธTerraform
  • dashboard
    • CodeMash Cloud
    • Project
    • Membership
      • Users
      • Roles
      • Policies
      • Triggers
      • Integrations
        • Apple
        • Azure
        • Facebook
        • Google
        • Twitter
      • Settings
    • Database
      • Collections
        • Schema
          • From Template
          • Structure
        • Tabs
        • Triggers
        • Indexes
        • Shared Forms
      • Taxonomies
        • Terms
        • Schema
        • Settings
      • Aggregation
      • Imports
      • Exports
      • Backups
    • Files
      • Integrations
        • CodeMash
        • S3 Bucket
    • Notifications
      • Push
        • Push Templates
        • Push Templates API
        • Push Notifications
        • Push Notifications API
        • Devices
        • Devices API
        • Integrations
          • Firebase
          • One Signal
          • Expo
      • Email
        • Email Templates
        • Emails
        • Integrations
          • AWS SES
          • Twilio Sendgrid
          • Mailgun
      • ๐Ÿ—๏ธServer events
      • ๐Ÿ—๏ธSms
    • Payments
      • Integrations
        • Apple Pay
        • Google Pay
        • Stripe
        • Kevin.
        • Paysera
        • Decta
    • Scheduler
    • Logs & Monitoring
    • Code
      • Functions
        • Function Inputs
        • Function Templates
          • Node.js
          • Python
          • Ruby
          • Java
          • Go
          • .NET Core
      • CodeMash Functions
        • Google Functions
          • Google Calendar
          • Google Gmail
        • Microsoft Functions
          • Microsoft 365 Users
          • Microsoft 365 Calendar
        • Collection Find
        • Collection Update
        • Users Find
        • Image Resize
        • Html To Pdf
        • Word Document
        • Barcode
        • QR Code
        • Send Email
        • Send Notification
        • Email Reminder
        • Notification Reminder
      • Integrations
        • AWS Lambda
        • Google Cloud Functions
        • Azure Functions
  • Other Topics
    • Apple
      • Developer Portal
      • Bundle Identifier
      • Team ID
      • Service ID
      • Key ID
    • Triggers
    • Tokens Binding
      • Project Tokens
      • Initiator Tokens
      • Receiver Tokens
      • Request Tokens
      • Operation based tokens
      • Template Functions
    • Search parameters
      • Paging
      • Filter
      • Sort
      • Projection
    • data-models
    • Errors
  • SDK
    • Node.js
    • TypeScript
    • .NET
    • ๐Ÿ—๏ธGo Lang
    • ๐Ÿ—๏ธFlutter
    • ๐Ÿ—๏ธSwift
    • ๐Ÿ—๏ธKotlin
  • CLI
    • ๐Ÿ—๏ธCodeMash CLI
  • API
    • Get Started
    • Prerequisites
    • How to test?
    • Cors
    • Project
    • Membership
      • Authentication
      • Users
    • Database
      • Collections
        • Aggregate
        • Change Responsibility
        • Count
        • Delete
        • Delete Many
        • Distinct
        • Find
        • Find One
        • Insert
        • Insert Many
        • References
        • Replace
        • Update
        • Update Many
      • Taxonomies
        • Find
    • Files
    • Code
    • Notifications
      • Push
      • Emails
      • Server Events
      • ๐Ÿ—๏ธSms
    • Payments
    • Scheduler
    • Logs & Monitoring
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. API
  2. Database
  3. Collections

Find One

Gets record by specified database unique id or filter.

PreviousFindNextInsert

Last updated 2 years ago

Was this helpful?

Finds one database record. You can pass projection to return record fields you care about. Also, you can include referenced collections to have all that information in one place.

var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);

var person = await service.FindOneByIdAsync(
    "{RECORD_ID}",
    new DatabaseFindOneOptions()
);

Check the docs about entities on how the response record is deserialized into your class object.

import { db } from 'codemash';

export async function getEmployeeDetails(id) {
    const response = await db.getRecord('employees', id);
    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 findOneById()
    {
        $responseData = $this->codemashDb->get([
        	'collectionName' => 'employees',
        	'id' => '{EMPLOYEE_ID}',
        ]);
    }
}

Find One (by using a filter)

GET https://api.codemash.io/:version/db/:collectionName/findOne

Gets a record by using a filter. This endpoint accepts GET and POST methods.

Path Parameters

Name
Type
Description

version

string

A version of the API endpoint.

collectionName

string

The name of the collection to get a record from.

Query Parameters

Name
Type
Description

filter

string

Filter document. This allows you to find a record by a custom filter. More about filters follow the link below.

referencedFields

array

Fields to left join. More about referencing fields follow the link below.

addReferencesFirst

boolean

If set to true, left joins first before applying other processing to main records. More about referencing fields follow the link below.

cultureCode

string

Language code. If your record has translatable fields, those fields will only include this specified language. If not provided, will take language from the Accept-Language header.

projection

string

Projection document. This allows you to specify what fields to return decreasing the amount of data transferred. More about projections follow the link below.

includeSchema

boolean

If set to true, includes your collection details in the response.

excludeCulture

boolean

Culture code or Accept-Language header will be used for translatable fields. If you want to get values in all languages, set this as true.

Headers

Name
Type
Description

Authorization

string

Secret API key which belongs to your project or user. Not required if using cookies with a session ID.

x-cm-projectid

string

Your project's ID. Can be passed as a query parameter.

{
    "result": "{ '_id': '5e37136bf59f3a3f940b99a4', 'name': 'John' }",
    "schema": null,
}
var client = new CodeMashClient(apiKey, projectId);
var service = new CodeMashRepository<Person>(client);

var person = await service.FindOneAsync(
    x => x.Id == "{RECORD_ID}",
    new DatabaseFindOneOptions()
);

Check the docs about entities on how the response record is deserialized into your class object.

import { db } from 'codemash';

export async function getEmployeeByUserId(id) {    
    
    const filter = { userId : id };    
    return await db.getRecordWithFilter(collectionName, filter, null);
}
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 findOneByFilter()
    {
        $responseData = $this->codemashDb->findOne([
        	'collectionName' => 'employees',
        	'filter' => [
        		'address' => 'Los Angeles',
        	],
        ]);
    }
}

Check the docs on how to form . Check the docs on how to use .

Check the docs on how to form , . Check the docs on how to use .

projections
references
projections
filters
references
  • GET[FindOne] - Gets record by specified database unique id.
  • POST[FindOne] - Gets record by specified database unique id.
  • Find One (by using a filter)

[FindOne] - Gets record by specified database unique id.

get

Finds one database record. You can pass projection to return record fields you care about. Also, you can include referenced collections to have all that information in one place. https://docs.codemash.io/api/database/collections/find-one

Authorizations
Path parameters
IdstringRequired

Id of a record. Required if filter is not set.

CollectionNamestringRequired

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".

versionstringRequired

The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2

Query parameters
ExcludeSchemabooleanOptional

Use the IncludeSchema property when you want to have schema definition in the API response as well. This is useful when you have dynamic data rendering and want to have context over your data structure and how it should be displayed. By default schema is excluded.

ExcludeCulturebooleanOptional

Set ExcludeCulture property to "true" when you want to return all the data translations from translatable fields. This is useful when you want to take care about translations in the front-end side. E.g.: when you want to enter product description in your project supported languages.

IncludeUserNamesbooleanOptional

If your collection record has relationship with the Users collection from the Membership module, you can set IncludeUserNames property to "true" to get full user name and id information altogether without making any extra roundtrip to the server.

IncludeRoleNamesbooleanOptional

If your collection record has relationship with the system Roles from the Membership module, you can set IncludeRoleNames property to "true" to get full role name and id information altogether without making any extra roundtrip to the server.

IncludeCollectionNamesbooleanOptional

If your collection record has relationship with other collections from the Database module, you can set IncludeCollectionNames property to "true" to get display name and id information altogether without making any extra roundtrip to the server.

IncludeTermNamesbooleanOptional

If your collection record has relationship with the taxonomy terms from the Database module, you can set IncludeTermNames property to "true" to get term name and id information altogether without making any extra roundtrip to the server.

AddReferencesFirstbooleanOptional

If true, then references are injected before your list queries are applied

Header parameters
AcceptanyRequired

Accept Header

X-CM-ClusterstringOptional

API key of your cluster. Can be passed in a header as X-CM-Cluster.

X-CM-ProjectIdstringRequired

ID of your project. Can be passed in a header as X-CM-ProjectId.

CultureCodestringOptional

Specify culture code when your response from the API should be localised. E.g.: en

Responses
200
Success
application/json
get
GET //{version}/db/{CollectionName}/{Id} HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Accept: */*
200

Success

{
  "schema": {
    "collection_name_as_title": "text",
    "collection_name": "text",
    "ui_schema": "text",
    "json_schema": "text",
    "translatable_fields": [
      "text"
    ],
    "database_id": "text",
    "schema_id": "text"
  },
  "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"
}

[FindOne] - Gets record by specified database unique id.

post

Finds one database record. You can pass projection to return record fields you care about. Also, you can include referenced collections to have all that information in one place. https://docs.codemash.io/api/database/collections/find-one

Authorizations
Path parameters
IdstringRequired

Id of a record. Required if filter is not set.

CollectionNamestringRequired

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".

versionstringRequired

The CodeMash API version used to fetch data from the API. If not specified, the last version will be used. E.g.: v2

Query parameters
IncludeTermNamesbooleanOptional

If your collection record has relationship with the taxonomy terms from the Database module, you can set IncludeTermNames property to "true" to get term name and id information altogether without making any extra roundtrip to the server.

AddReferencesFirstbooleanOptional

If true, then references are injected before your list queries are applied

Header parameters
AcceptanyRequired

Accept Header

X-CM-ClusterstringOptional

API key of your cluster. Can be passed in a header as X-CM-Cluster.

X-CM-ProjectIdstringRequired

ID of your project. Can be passed in a header as X-CM-ProjectId.

CultureCodestringOptional

Specify culture code when your response from the API should be localised. E.g.: en

Body
anyOptional
Responses
200
Success
application/json
post
POST //{version}/db/{CollectionName}/{Id} HTTP/1.1
Host: api.codemash.io
Authorization: YOUR_API_KEY
X-CM-ProjectId: text
Content-Type: application/json
Accept: */*
200

Success

{
  "schema": {
    "collection_name_as_title": "text",
    "collection_name": "text",
    "ui_schema": "text",
    "json_schema": "text",
    "translatable_fields": [
      "text"
    ],
    "database_id": "text",
    "schema_id": "text"
  },
  "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"
}