Database
Overview of database service
CodeMash Database service provides many operations for database management like CRUD operations, schema builder, and data validation.
When you enable a database service in your CodeMash dashboard page we create a new MongoDb database for you. When that is done you are able to create collections using our provided tools in the dashboard. After you create a collection you can start adding records through the dashboard or calling to API endpoints.
Features included in database service:
  1. 1.
    Collections - just like any database collection.
  2. 2.
    Taxonomies - special kind of collections used mostly for basic select type data.
  3. 3.
    Imports - importing records to collections.
  4. 4.
    Exports - exporting records from collections.
  5. 5.
    Backups - dumping and restoring collections.

Using SDK

If you decide to use one of our provided SDK, the following code shows how to initialize database service.
.NET
Node
PHP
1
var projectId = Guid.Parse("{YOUR_PROJECT_ID}");
2
var apiKey = "{YOUR_SECRET_KEY}";
3
4
var client = new CodeMashClient(apiKey, projectId);
5
var service = new CodeMashRepository<Person>(client);
Copied!
Here Person is a class which extends the interface IEntity. The example below shows a class extendingEntityclass which extendsIEntityinterface. The attributeCollectionNameis a helper for a client. It takes one parameter - the name of your collection in CodeMash.
1
[Collection("persons")]
2
public class Person : Entity
3
{
4
[Field("name")]
5
public string Name { get; set; }
6
}
Copied!
Here UniqueName is an attribute that is used to set the unique name of a field (the name that is used inside a database). This name can be found in your collection, field details. If this attribute is not used, then your field will be serialized and deserialized as a lowercase property name.
1
import { db } from 'codemash';
Copied!
1
$secretKey = '{YOUR_SECRET_KEY}';
2
$projectId = '{YOUR_PROJECT_ID}';
3
4
$client = new CodemashClient($secretKey, $projectId);
5
$codemashDb = new CodemashDb($client);
Copied!
The following are examples of database SDK using different languages and frameworks.
.NET
Node
PHP
1
using System;
2
using CodeMash.Client;
3
using CodeMash.Repository;
4
using MongoDB.Driver;
5
6
namespace ConsoleApplication
7
{
8
[CollectionName("persons")]
9
public class Person : Entity
10
{
11
public string Name { get; set; }
12
}
13
14
class Program
15
{
16
static void Main(string[] args)
17
{
18
// 1. Get your Project ID and Secret Key
19
var projectId = Guid.Parse("{YOUR_PROJECT_ID}");
20
var apiKey = "{YOUR_SECRET_KEY}";
21
22
// 2. Create a general client for API calls
23
var client = new CodeMashClient(apiKey, projectId);
24
25
// 3. Create a service object
26
var dbService = new CodeMashRepository<Person>(client);
27
28
// 4. Call an API method
29
var filter = Builders<Person>.Filter.Eq(x => x.Name, "John");
30
var result = dbService.Find(filter);
31
}
32
}
33
}
Copied!
1
export async function getHolidaysOfEmployee(userId) {
2
3
const filter = JSON.stringify({
4
application_user: userId,
5
status: 'Approved',
6
});
7
8
// gets first 100 records - all approved holidays sorted out by start date
9
const response =
10
await db.getRecords('holidays', 0, 100, { start: -1 }, filter);
11
12
return response;
13
}
Copied!
1
use Codemash\CodemashClient;
2
use Codemash\CodemashDb;
3
4
class CodemashService
5
{
6
protected CodemashDb $codemashDb;
7
protected string $collectionName = '{YOUR_COLLECTION_NAME}';
8
9
public function __construct()
10
{
11
$secretKey = '{YOUR_SECRET_KEY}';
12
$projectId = '{YOUR_PROJECT_ID}';
13
14
$client = new CodemashClient($secretKey, $projectId);
15
$this->codemashDb = new CodemashDb($client);
16
}
17
18
public function getEmployees()
19
{
20
$responseData = $this->codemashDb->findMany([
21
'collectionName' => 'employees',
22
'filter' => [
23
'address' => 'New York',
24
],
25
]);
26
}
27
}
Copied!

Working with Database Service

The following links explore the usage of database service:
Last modified 1yr ago