1
0
Fork 0
mirror of synced 2024-06-26 18:20:43 +12:00

Use database filter to encode default value as JSON string to preseve type

This commit is contained in:
kodumbeats 2021-08-27 13:12:16 -04:00
parent ddde87f32a
commit a619c26aff
3 changed files with 16 additions and 15 deletions

View file

@ -174,7 +174,7 @@ $collections = [
'required' => false,
'default' => null,
'array' => false,
'filters' => [],
'filters' => ['defaultValue'],
],
[
'$id' => 'signed',

View file

@ -55,8 +55,6 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
$formatOptions = $attribute->getAttribute('formatOptions', []);
$filters = $attribute->getAttribute('filters', []); // filters are hidden from the endpoint
$default = $attribute->getAttribute('default', null);
// $default = (empty($default)) ? null : (int)$default;
$defaultEncoded = (\is_null($default)) ? '' : json_encode(['value'=>$default]);
$collection = $dbForInternal->getDocument('collections', $collectionId);
@ -85,7 +83,7 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
'size' => $size,
'required' => $required,
'signed' => $signed,
'default' => $defaultEncoded,
'default' => $default,
'array' => $array,
'format' => $format,
'formatOptions' => $formatOptions,
@ -97,9 +95,6 @@ function createAttribute($collectionId, $attribute, $response, $dbForInternal, $
$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
// so we can later modify Document to fit response model
$clone = clone $attribute;
@ -805,13 +800,7 @@ App::get('/v1/database/collections/:collectionId/attributes')
throw new Exception('Collection not found', 404);
}
$attributes = $collection->getAttributes();
$attributes = array_map(function ($attribute) use ($collection) {
return new Document([\array_merge($attribute, [
'collectionId' => $collection->getId(),
])]);
}, $attributes);
$attributes = $collection->getAttribute('attributes', []);
$response->dynamic(new Document([
'sum' => \count($attributes),

View file

@ -145,7 +145,7 @@ if(!empty($user) || !empty($pass)) {
}
/**
* DB Filters
* Old DB Filters
*/
DatabaseOld::addFilter('json',
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',
function($value) {
return null;