Add complete CocoDB documentation from source code
- CocoDB::get with all options (uploads, relations, redis, groupBy, etc.) - CocoDB::insertRecords and updateRecords with functions parameter - CocoDB::deleteRecords - Advanced where syntax (operators, OR, NOT, IS NULL, raw_key) - Options reference tables for get/insert/update Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -116,9 +116,147 @@ CmsApi.get('tableName', { where: conditions }, function(records) {
|
|||||||
|
|
||||||
## CocoDB
|
## CocoDB
|
||||||
|
|
||||||
Low-level database abstraction layer. Use when CmsApi is too high-level.
|
Low-level database abstraction layer used internally by CmsApi. Use directly from hooks when you need more control.
|
||||||
|
|
||||||
<!-- TODO: Document CocoDB methods (query, insert, update, delete, transactions) -->
|
### `CocoDB::get($table, $where, $order, $limit, $options)`
|
||||||
|
|
||||||
|
Reads records from a table. This is the same method CmsApi::get wraps.
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Basic query
|
||||||
|
$records = CocoDB::get('productos', ['activo' => 1], 'orden ASC', 10);
|
||||||
|
|
||||||
|
// With advanced where (array syntax for operators)
|
||||||
|
$records = CocoDB::get('productos', [
|
||||||
|
['column' => 'precio', 'value' => 100, 'operator' => '>='],
|
||||||
|
['column' => 'categoria_num', 'value' => [1, 2, 3], 'operator' => 'IN'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// OR conditions
|
||||||
|
$records = CocoDB::get('productos', [
|
||||||
|
['column' => 'nombre', 'value' => '%keyword%', 'operator' => 'LIKE'],
|
||||||
|
['column' => 'descripcion', 'value' => '%keyword%', 'operator' => 'LIKE', 'or' => true],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// NOT condition
|
||||||
|
$records = CocoDB::get('productos', [
|
||||||
|
['column' => 'estado', 'value' => 'borrador', 'operator' => '=', 'not' => true],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// IS NULL
|
||||||
|
$records = CocoDB::get('productos', [
|
||||||
|
['column' => 'fecha_baja', 'value' => '', 'operator' => 'IS NULL'],
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Limit with offset
|
||||||
|
$records = CocoDB::get('productos', [], 'num DESC', ['limit' => 10, 'offset' => 20]);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Options for `get()`
|
||||||
|
|
||||||
|
| Option | Type | Default | Description |
|
||||||
|
|--------|------|---------|-------------|
|
||||||
|
| `uploads` | bool | `true` | Include upload field data |
|
||||||
|
| `relations` | bool/array | `true` | Resolve foreign key relations. Pass array to limit: `['category']` |
|
||||||
|
| `relationsDepth` | int | 2 | Depth of nested relation resolution |
|
||||||
|
| `translates` | string | current lang | Language code for translations |
|
||||||
|
| `groupBy` | string | null | GROUP BY clause |
|
||||||
|
| `aggregates` | array | `[]` | Aggregate functions |
|
||||||
|
| `onlyFields` | array | null | Select specific fields only |
|
||||||
|
| `debug` | bool | false | Output SQL query for debugging |
|
||||||
|
| `redis` | bool | null | Force Redis cache |
|
||||||
|
| `redis_expire` | int | 60 | Redis cache TTL in seconds |
|
||||||
|
|
||||||
|
### `CocoDB::insertRecords($table, $records, $functions, $options)`
|
||||||
|
|
||||||
|
Insert one or multiple records.
|
||||||
|
|
||||||
|
```php
|
||||||
|
// Single record
|
||||||
|
$count = CocoDB::insertRecords('contacto', [
|
||||||
|
'nombre' => 'John',
|
||||||
|
'email' => 'john@example.com',
|
||||||
|
]);
|
||||||
|
// Returns number of inserted records. Use mysql_insert_id() to get the new num.
|
||||||
|
|
||||||
|
// Multiple records
|
||||||
|
$count = CocoDB::insertRecords('productos', [
|
||||||
|
['nombre' => 'Product A', 'precio' => 10],
|
||||||
|
['nombre' => 'Product B', 'precio' => 20],
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
#### Options for insert/update
|
||||||
|
|
||||||
|
| Option | Description |
|
||||||
|
|--------|-------------|
|
||||||
|
| `forceNum` | Allow setting the `num` field manually |
|
||||||
|
| `ignoreSchema` | Skip schema validation |
|
||||||
|
| `ignoreFields` | Array of field names to skip |
|
||||||
|
|
||||||
|
### `CocoDB::updateRecords($table, $records, $where, $functions, $options)`
|
||||||
|
|
||||||
|
Update records matching a where condition.
|
||||||
|
|
||||||
|
```php
|
||||||
|
CocoDB::updateRecords('productos',
|
||||||
|
['precio' => 29.99, 'activo' => 1], // fields to update
|
||||||
|
['num' => 42] // where condition
|
||||||
|
);
|
||||||
|
|
||||||
|
// With operator in where
|
||||||
|
CocoDB::updateRecords('productos',
|
||||||
|
['activo' => 0],
|
||||||
|
[['column' => 'stock', 'value' => 0, 'operator' => '<=']]
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
### `CocoDB::deleteRecords($table, $where, $options)`
|
||||||
|
|
||||||
|
Delete records matching a where condition.
|
||||||
|
|
||||||
|
```php
|
||||||
|
CocoDB::deleteRecords('productos', ['num' => 42]);
|
||||||
|
|
||||||
|
// With operator
|
||||||
|
CocoDB::deleteRecords('logs', [
|
||||||
|
['column' => 'fecha', 'value' => '2024-01-01', 'operator' => '<']
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
### Where Clause Syntax
|
||||||
|
|
||||||
|
The `$where` parameter supports two formats:
|
||||||
|
|
||||||
|
**Simple (key-value):**
|
||||||
|
```php
|
||||||
|
['campo' => 'valor'] // campo = 'valor'
|
||||||
|
```
|
||||||
|
|
||||||
|
**Advanced (array of conditions):**
|
||||||
|
```php
|
||||||
|
[
|
||||||
|
'column' => 'field_name', // Required
|
||||||
|
'value' => 'match_value', // Required
|
||||||
|
'operator' => '=', // =, !=, <, >, <=, >=, LIKE, IN, IS NULL
|
||||||
|
'or' => false, // Use OR instead of AND
|
||||||
|
'not' => false, // Negate the condition
|
||||||
|
'raw_key' => false, // Skip column existence check
|
||||||
|
]
|
||||||
|
```
|
||||||
|
|
||||||
|
### Functions Parameter
|
||||||
|
|
||||||
|
The `$functions` parameter lets you apply MySQL functions to values during insert/update:
|
||||||
|
|
||||||
|
```php
|
||||||
|
CocoDB::insertRecords('logs', [
|
||||||
|
'mensaje' => 'Login exitoso',
|
||||||
|
'fecha' => '',
|
||||||
|
], [
|
||||||
|
'fecha' => 'NOW()', // Will use MySQL NOW() instead of the value
|
||||||
|
]);
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Table Schemas
|
## Table Schemas
|
||||||
|
|||||||
Reference in New Issue
Block a user