Guía rápida de InceptionDB

Esta guía crea una colección, inserta tres documentos JSON y consulta documentos filtrados.

Los endpoints de acceso a la base de datos aceptan Api-Key y Api-Secret. También se puede usar un token Glue de owner cuando el propietario de la base de datos está permitido.

Paso 1: Crear una colección

curl -X POST "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections" \
  -H "Api-Key: tu-api-key" \
  -H "Api-Secret: tu-api-secret" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "mi-coleccion"
  }'
POST /v1/databases/tu-id-de-base-de-datos/collections HTTP/1.1
Host: api.hola.cloud
Api-Key: tu-api-key
Api-Secret: tu-api-secret
Content-Type: application/json

{
    "name": "mi-coleccion"
  }
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"name": "mi-coleccion"}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "tu-api-key")
	req.Header.Set("Api-Secret", "tu-api-secret")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['name' => 'mi-coleccion'];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: tu-api-key',
        'Api-Secret: tu-api-secret',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/json",
}

payload = {"name": "mi-coleccion"}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"name": "mi-coleccion"};

const response = await fetch("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections", {
  method: "POST",
  headers: {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"name": "mi-coleccion"};

const response = await fetch("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections", {
  method: "POST",
  headers: {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("name", "mi-coleccion");
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "tu-api-key")
            .header("Api-Secret", "tu-api-secret")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Respuesta esperada:

1{
2  "name": "mi-coleccion",
3  "total": 0,
4  "indexes": 0,
5  "defaults": {
6    "id": "uuid()"
7  }
8}

Paso 2: Insertar documentos

Usa el endpoint de acción de colección y envía JSONL: un documento JSON por línea.

curl -X POST "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert" \
  -H "Api-Key: tu-api-key" \
  -H "Api-Secret: tu-api-secret" \
  -H "Content-Type: application/jsonl" \
  --data-binary '{"name":"Elemento 1","value":100}
{"name":"Elemento 2","value":200}
{"name":"Elemento 3","value":300}'
POST /v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert HTTP/1.1
Host: api.hola.cloud
Api-Key: tu-api-key
Api-Secret: tu-api-secret
Content-Type: application/jsonl

{"name":"Elemento 1","value":100}
{"name":"Elemento 2","value":200}
{"name":"Elemento 3","value":300}
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"name": "Elemento 1", "value": 100}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "tu-api-key")
	req.Header.Set("Api-Secret", "tu-api-secret")
	req.Header.Set("Content-Type", "application/jsonl")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['name' => 'Elemento 1', 'value' => 100];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: tu-api-key',
        'Api-Secret: tu-api-secret',
        'Content-Type: application/jsonl',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/jsonl",
}

payload = {"name": "Elemento 1", "value": 100}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"name": "Elemento 1", "value": 100};

const response = await fetch("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert", {
  method: "POST",
  headers: {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/jsonl"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"name": "Elemento 1", "value": 100};

const response = await fetch("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert", {
  method: "POST",
  headers: {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/jsonl"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("name", "Elemento 1", "value", 100);
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:insert"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "tu-api-key")
            .header("Api-Secret", "tu-api-secret")
            .header("Content-Type", "application/jsonl")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Respuesta esperada:

1{"id":"document-id-1","name":"Elemento 1","value":100}
2{"id":"document-id-2","name":"Elemento 2","value":200}
3{"id":"document-id-3","name":"Elemento 3","value":300}

Paso 3: Buscar documentos filtrados

curl -X POST "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find" \
  -H "Api-Key: tu-api-key" \
  -H "Api-Secret: tu-api-secret" \
  -H "Content-Type: application/json" \
  -d '{
    "filter": {
      "value": { "$gte": 200 }
    }
  }'
POST /v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find HTTP/1.1
Host: api.hola.cloud
Api-Key: tu-api-key
Api-Secret: tu-api-secret
Content-Type: application/json

{
    "filter": {
      "value": { "$gte": 200 }
    }
  }
package main

import (
	"fmt"
	"io"
	"net/http"
	"encoding/json"
	"strings"
)

func main() {
	payload := map[string]any{"filter": map[string]any{"value": map[string]any{"$gte": 200}}}
	bodyBytes, err := json.Marshal(payload)
	if err != nil {
		panic(err)
	}
	body := string(bodyBytes)

	req, err := http.NewRequest("POST", "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find", strings.NewReader(body))
	if err != nil {
		panic(err)
	}
	req.Header.Set("Api-Key", "tu-api-key")
	req.Header.Set("Api-Secret", "tu-api-secret")
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	responseBody, err := io.ReadAll(resp.Body)
	if err != nil {
		panic(err)
	}
	fmt.Println(string(responseBody))
}
<?php
$payload = ['filter' => ['value' => ['$gte' => 200]]];
$body = json_encode($payload);

$ch = curl_init();

curl_setopt_array($ch, [
    CURLOPT_URL => 'https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find',
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_POSTFIELDS => $body,
    CURLOPT_HTTPHEADER => [
        'Api-Key: tu-api-key',
        'Api-Secret: tu-api-secret',
        'Content-Type: application/json',
    ],
]);

$response = curl_exec($ch);
if ($response === false) {
    throw new Exception(curl_error($ch));
}
curl_close($ch);

echo $response;
import requests

import json

headers = {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/json",
}

payload = {"filter": {"value": {"$gte": 200}}}
body = json.dumps(payload)

response = requests.request(
    "POST",
    "https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find",
    headers=headers,
    data=body
)

print(response.text)
const payload = {"filter": {"value": {"$gte": 200}}};

const response = await fetch("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find", {
  method: "POST",
  headers: {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

console.log(await response.text());
const payload = {"filter": {"value": {"$gte": 200}}};

const response = await fetch("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find", {
  method: "POST",
  headers: {
    "Api-Key": "tu-api-key",
    "Api-Secret": "tu-api-secret",
    "Content-Type": "application/json"
  },
  body: JSON.stringify(payload)
});

const text = await response.text();
console.log(text);
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.Map;
import java.util.List;

public class Main {
    public static void main(String[] args) throws Exception {
        var payload = Map.of("filter", Map.of("value", Map.of("$gte", 200)));
        var body = new ObjectMapper().writeValueAsString(payload);

        var request = HttpRequest.newBuilder()
            .uri(URI.create("https://api.hola.cloud/v1/databases/tu-id-de-base-de-datos/collections/mi-coleccion:find"))
            .method("POST", HttpRequest.BodyPublishers.ofString(body))
            .header("Api-Key", "tu-api-key")
            .header("Api-Secret", "tu-api-secret")
            .header("Content-Type", "application/json")
            .build();

        var response = HttpClient.newHttpClient().send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println(response.body());
    }
}

Respuesta esperada:

1{"id":"document-id-2","name":"Elemento 2","value":200}
2{"id":"document-id-3","name":"Elemento 3","value":300}

Comentarios

Deja un comentario