Use database filter to encode default value as JSON string to preseve type
This commit is contained in:
parent
ddde87f32a
commit
a619c26aff
3 changed files with 16 additions and 15 deletions
|
@ -174,7 +174,7 @@ $collections = [
|
||||||
'required' => false,
|
'required' => false,
|
||||||
'default' => null,
|
'default' => null,
|
||||||
'array' => false,
|
'array' => false,
|
||||||
'filters' => [],
|
'filters' => ['defaultValue'],
|
||||||
],
|
],
|
||||||
[
|
[
|
||||||
'$id' => 'signed',
|
'$id' => 'signed',
|
||||||
|
|
|
@ -55,8 +55,6 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
|
||||||
$formatOptions = $attribute->getAttribute('formatOptions', []);
|
$formatOptions = $attribute->getAttribute('formatOptions', []);
|
||||||
$filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint
|
$filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint
|
||||||
$default = $attribute->getAttribute('default', null);
|
$default = $attribute->getAttribute('default', null);
|
||||||
// $default = (empty($default)) ? null : (int)$default;
|
|
||||||
$defaultEncoded = (\is_null($default)) ? '' : json_encode(['value'=>$default]);
|
|
||||||
|
|
||||||
$collection = $dbForInternal->getDocument('collections', $collectionId);
|
$collection = $dbForInternal->getDocument('collections', $collectionId);
|
||||||
|
|
||||||
|
@ -85,7 +83,7 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
|
||||||
'size' => $size,
|
'size' => $size,
|
||||||
'required' => $required,
|
'required' => $required,
|
||||||
'signed' => $signed,
|
'signed' => $signed,
|
||||||
'default' => $defaultEncoded,
|
'default' => $default,
|
||||||
'array' => $array,
|
'array' => $array,
|
||||||
'format' => $format,
|
'format' => $format,
|
||||||
'formatOptions' => $formatOptions,
|
'formatOptions' => $formatOptions,
|
||||||
|
@ -97,9 +95,6 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
|
||||||
|
|
||||||
$dbForInternal->purgeDocument('collections', $collectionId);
|
$dbForInternal->purgeDocument('collections', $collectionId);
|
||||||
|
|
||||||
// Only attributes table needs $default encoded as a string, reset before response
|
|
||||||
$attribute->setAttribute('default', $default);
|
|
||||||
|
|
||||||
// Pass clone of $attribute object to workers
|
// Pass clone of $attribute object to workers
|
||||||
// so we can later modify Document to fit response model
|
// so we can later modify Document to fit response model
|
||||||
$clone = clone $attribute;
|
$clone = clone $attribute;
|
||||||
|
@ -805,13 +800,7 @@ App::get('/v1/database/collections/:collectionId/attributes')
|
||||||
throw new Exception('Collection not found', 404);
|
throw new Exception('Collection not found', 404);
|
||||||
}
|
}
|
||||||
|
|
||||||
$attributes = $collection->getAttributes();
|
$attributes = $collection->getAttribute('attributes', []);
|
||||||
|
|
||||||
$attributes = array_map(function ($attribute) use ($collection) {
|
|
||||||
return new Document([\array_merge($attribute, [
|
|
||||||
'collectionId' => $collection->getId(),
|
|
||||||
])]);
|
|
||||||
}, $attributes);
|
|
||||||
|
|
||||||
$response->dynamic(new Document([
|
$response->dynamic(new Document([
|
||||||
'sum' => \count($attributes),
|
'sum' => \count($attributes),
|
||||||
|
|
14
app/init.php
14
app/init.php
|
@ -145,7 +145,7 @@ if(!empty($user) || !empty($pass)) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* DB Filters
|
* Old DB Filters
|
||||||
*/
|
*/
|
||||||
DatabaseOld::addFilter('json',
|
DatabaseOld::addFilter('json',
|
||||||
function($value) {
|
function($value) {
|
||||||
|
@ -181,6 +181,18 @@ DatabaseOld::addFilter('encrypt',
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* New DB Filters
|
||||||
|
*/
|
||||||
|
Database::addFilter('defaultValue',
|
||||||
|
function($value) {
|
||||||
|
return \json_encode(['value' => $value]);
|
||||||
|
},
|
||||||
|
function($value) {
|
||||||
|
return \json_decode($value, true)['value'];
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
Database::addFilter('subQueryAttributes',
|
Database::addFilter('subQueryAttributes',
|
||||||
function($value) {
|
function($value) {
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in a new issue