Skip to content

Commit f3ceba6

Browse files
added new internal attributes to the adapters
1 parent 87fb55e commit f3ceba6

6 files changed

Lines changed: 68 additions & 6 deletions

File tree

src/Database/Adapter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,6 +1218,7 @@ protected function escapeWildcards(string $value): string
12181218
* @param string $attribute
12191219
* @param int|float $value
12201220
* @param string $updatedAt
1221+
* @param string|null $updatedBy
12211222
* @param int|float|null $min
12221223
* @param int|float|null $max
12231224
* @return bool
@@ -1229,6 +1230,7 @@ abstract public function increaseDocumentAttribute(
12291230
string $attribute,
12301231
int|float $value,
12311232
string $updatedAt,
1233+
?string $updatedBy,
12321234
int|float|null $min = null,
12331235
int|float|null $max = null
12341236
): bool;

src/Database/Adapter/MariaDB.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ public function createCollection(string $name, array $attributes = [], array $in
160160
_uid VARCHAR(255) NOT NULL,
161161
_createdAt DATETIME(3) DEFAULT NULL,
162162
_updatedAt DATETIME(3) DEFAULT NULL,
163+
_createdBy VARCHAR(255) DEFAULT NULL,
164+
_updatedBy VARCHAR(255) DEFAULT NULL,
163165
_permissions MEDIUMTEXT DEFAULT NULL,
164166
PRIMARY KEY (_id),
165167
" . \implode(' ', $attributeStrings) . "
@@ -828,6 +830,8 @@ public function createDocument(Document $collection, Document $document): Docume
828830
$attributes = $document->getAttributes();
829831
$attributes['_createdAt'] = $document->getCreatedAt();
830832
$attributes['_updatedAt'] = $document->getUpdatedAt();
833+
$attributes['_createdBy'] = $document->getCreatedBy();
834+
$attributes['_updatedBy'] = $document->getUpdatedBy();
831835
$attributes['_permissions'] = \json_encode($document->getPermissions());
832836

833837
if ($this->sharedTables) {
@@ -954,6 +958,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
954958
$attributes = $document->getAttributes();
955959
$attributes['_createdAt'] = $document->getCreatedAt();
956960
$attributes['_updatedAt'] = $document->getUpdatedAt();
961+
$attributes['_updatedBy'] = $document->getUpdatedBy();
957962
$attributes['_permissions'] = json_encode($document->getPermissions());
958963

959964
$name = $this->filter($collection);
@@ -1242,6 +1247,7 @@ public function getUpsertStatement(
12421247
* @param string $attribute
12431248
* @param int|float $value
12441249
* @param string $updatedAt
1250+
* @param string|null $updatedBy
12451251
* @param int|float|null $min
12461252
* @param int|float|null $max
12471253
* @return bool
@@ -1253,6 +1259,7 @@ public function increaseDocumentAttribute(
12531259
string $attribute,
12541260
int|float $value,
12551261
string $updatedAt,
1262+
?string $updatedBy,
12561263
int|float|null $min = null,
12571264
int|float|null $max = null
12581265
): bool {
@@ -1266,7 +1273,8 @@ public function increaseDocumentAttribute(
12661273
UPDATE {$this->getSQLTable($name)}
12671274
SET
12681275
`{$attribute}` = `{$attribute}` + :val,
1269-
`_updatedAt` = :updatedAt
1276+
`_updatedAt` = :updatedAt,
1277+
`_updatedBy` = :updatedBy
12701278
WHERE _uid = :_uid
12711279
{$this->getTenantQuery($collection)}
12721280
";
@@ -1279,6 +1287,7 @@ public function increaseDocumentAttribute(
12791287
$stmt->bindValue(':_uid', $id);
12801288
$stmt->bindValue(':val', $value);
12811289
$stmt->bindValue(':updatedAt', $updatedAt);
1290+
$stmt->bindValue(':updatedBy', $updatedBy);
12821291

12831292
if ($this->sharedTables) {
12841293
$stmt->bindValue(':_tenant', $this->tenant);
@@ -1530,6 +1539,14 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
15301539
$results[$index]['$updatedAt'] = $document['_updatedAt'];
15311540
unset($results[$index]['_updatedAt']);
15321541
}
1542+
if (\array_key_exists('_createdBy', $document)) {
1543+
$results[$index]['$createdBy'] = $document['_createdBy'];
1544+
unset($results[$index]['_createdBy']);
1545+
}
1546+
if (\array_key_exists('_updatedBy', $document)) {
1547+
$results[$index]['$updatedBy'] = $document['_updatedBy'];
1548+
unset($results[$index]['_updatedBy']);
1549+
}
15331550
if (\array_key_exists('_permissions', $document)) {
15341551
$results[$index]['$permissions'] = \json_decode($document['_permissions'] ?? '[]', true);
15351552
unset($results[$index]['_permissions']);

src/Database/Adapter/Pool.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ protected function getAttributeProjection(array $selections, string $prefix, arr
481481
return $this->delegate(__FUNCTION__, \func_get_args());
482482
}
483483

484-
public function increaseDocumentAttribute(string $collection, string $id, string $attribute, float|int $value, string $updatedAt, float|int|null $min = null, float|int|null $max = null): bool
484+
public function increaseDocumentAttribute(string $collection, string $id, string $attribute, float|int $value, string $updatedAt, ?string $updatedBy, float|int|null $min = null, float|int|null $max = null): bool
485485
{
486486
return $this->delegate(__FUNCTION__, \func_get_args());
487487
}

src/Database/Adapter/Postgres.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,8 @@ public function createCollection(string $name, array $attributes = [], array $in
238238
" . $sqlTenant . "
239239
\"_createdAt\" TIMESTAMP(3) DEFAULT NULL,
240240
\"_updatedAt\" TIMESTAMP(3) DEFAULT NULL,
241+
\"_createdBy\" VARCHAR(255) DEFAULT NULL,
242+
\"_updatedBy\" VARCHAR(255) DEFAULT NULL,
241243
_permissions TEXT DEFAULT NULL,
242244
" . \implode(' ', $attributeStrings) . "
243245
PRIMARY KEY (_id)
@@ -963,6 +965,8 @@ public function createDocument(Document $collection, Document $document): Docume
963965
$attributes = $document->getAttributes();
964966
$attributes['_createdAt'] = $document->getCreatedAt();
965967
$attributes['_updatedAt'] = $document->getUpdatedAt();
968+
$attributes['_createdBy'] = $document->getCreatedBy();
969+
$attributes['_updatedBy'] = $document->getUpdatedBy();
966970
$attributes['_permissions'] = \json_encode($document->getPermissions());
967971

968972
if ($this->sharedTables) {
@@ -1077,6 +1081,8 @@ public function updateDocument(Document $collection, string $id, Document $docum
10771081
$attributes = $document->getAttributes();
10781082
$attributes['_createdAt'] = $document->getCreatedAt();
10791083
$attributes['_updatedAt'] = $document->getUpdatedAt();
1084+
$attributes['_createdBy'] = $document->getCreatedBy();
1085+
$attributes['_updatedBy'] = $document->getUpdatedBy();
10801086
$attributes['_permissions'] = json_encode($document->getPermissions());
10811087

10821088
$name = $this->filter($collection);
@@ -1345,12 +1351,13 @@ protected function getUpsertStatement(
13451351
* @param string $attribute
13461352
* @param int|float $value
13471353
* @param string $updatedAt
1354+
* @param string|null $updatedBy
13481355
* @param int|float|null $min
13491356
* @param int|float|null $max
13501357
* @return bool
13511358
* @throws DatabaseException
13521359
*/
1353-
public function increaseDocumentAttribute(string $collection, string $id, string $attribute, int|float $value, string $updatedAt, int|float|null $min = null, int|float|null $max = null): bool
1360+
public function increaseDocumentAttribute(string $collection, string $id, string $attribute, int|float $value, string $updatedAt, ?string $updatedBy, int|float|null $min = null, int|float|null $max = null): bool
13541361
{
13551362
$name = $this->filter($collection);
13561363
$attribute = $this->filter($attribute);
@@ -1362,7 +1369,8 @@ public function increaseDocumentAttribute(string $collection, string $id, string
13621369
UPDATE {$this->getSQLTable($name)}
13631370
SET
13641371
\"{$attribute}\" = \"{$attribute}\" + :val,
1365-
\"_updatedAt\" = :updatedAt
1372+
\"_updatedAt\" = :updatedAt,
1373+
\"_updatedBy\" = :updatedBy
13661374
WHERE _uid = :_uid
13671375
{$this->getTenantQuery($collection)}
13681376
";
@@ -1375,6 +1383,7 @@ public function increaseDocumentAttribute(string $collection, string $id, string
13751383
$stmt->bindValue(':_uid', $id);
13761384
$stmt->bindValue(':val', $value);
13771385
$stmt->bindValue(':updatedAt', $updatedAt);
1386+
$stmt->bindValue(':updatedBy', $updatedBy);
13781387

13791388
if ($this->sharedTables) {
13801389
$stmt->bindValue(':_tenant', $this->tenant);
@@ -1617,6 +1626,14 @@ public function find(Document $collection, array $queries = [], ?int $limit = 25
16171626
$results[$index]['$updatedAt'] = $document['_updatedAt'];
16181627
unset($results[$index]['_updatedAt']);
16191628
}
1629+
if (\array_key_exists('_createdBy', $document)) {
1630+
$results[$index]['$createdBy'] = $document['_createdBy'];
1631+
unset($results[$index]['_createdBy']);
1632+
}
1633+
if (\array_key_exists('_updatedBy', $document)) {
1634+
$results[$index]['$updatedBy'] = $document['_updatedBy'];
1635+
unset($results[$index]['_updatedBy']);
1636+
}
16201637
if (\array_key_exists('_permissions', $document)) {
16211638
$results[$index]['$permissions'] = \json_decode($document['_permissions'] ?? '[]', true);
16221639
unset($results[$index]['_permissions']);

src/Database/Adapter/SQL.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,14 @@ public function getDocument(Document $collection, string $id, array $queries = [
407407
$document['$updatedAt'] = $document['_updatedAt'];
408408
unset($document['_updatedAt']);
409409
}
410+
if (\array_key_exists('_createdBy', $document)) {
411+
$document['$createdBy'] = $document['_createdBy'];
412+
unset($document['_createdBy']);
413+
}
414+
if (\array_key_exists('_updatedBy', $document)) {
415+
$document['$updatedBy'] = $document['_updatedBy'];
416+
unset($document['_updatedBy']);
417+
}
410418
if (\array_key_exists('_permissions', $document)) {
411419
$document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true);
412420
unset($document['_permissions']);
@@ -467,6 +475,14 @@ public function updateDocuments(Document $collection, Document $updates, array $
467475
$attributes['_createdAt'] = $updates->getCreatedAt();
468476
}
469477

478+
if (!empty($updates->getCreatedBy())) {
479+
$attributes['_createdBy'] = $updates->getCreatedBy();
480+
}
481+
482+
if (!empty($updates->getUpdatedBy())) {
483+
$attributes['_updatedBy'] = $updates->getUpdatedBy();
484+
}
485+
470486
if ($updates->offsetExists('$permissions')) {
471487
$attributes['_permissions'] = json_encode($updates->getPermissions());
472488
}
@@ -1052,10 +1068,12 @@ public function getAttributeWidth(Document $collection): int
10521068
* `_tenant` int => 4 bytes
10531069
* `_createdAt` datetime(3) => 7 bytes
10541070
* `_updatedAt` datetime(3) => 7 bytes
1071+
* `_createdBy` varchar(255) => 1021 (4 * 255 + 1) bytes
1072+
* `_updatedBy` varchar(255) => 1021 (4 * 255 + 1) bytes
10551073
* `_permissions` mediumtext => 20
10561074
*/
10571075

1058-
$total = 1067;
1076+
$total = 3109;
10591077

10601078
$attributes = $collection->getAttributes()['attributes'];
10611079

@@ -1848,7 +1866,7 @@ protected function getAttributeProjection(array $selections, string $prefix, arr
18481866
$projections = [];
18491867
$projections[] = "{$this->quote($prefix)}.*";
18501868

1851-
$internalColumns = ['_id', '_uid', '_createdAt', '_updatedAt', '_permissions'];
1869+
$internalColumns = ['_id', '_uid', '_createdAt', '_updatedAt', '_permissions', '_createdBy', '_updatedBy'];
18521870
if ($this->sharedTables) {
18531871
$internalColumns[] = '_tenant';
18541872
}
@@ -1905,6 +1923,8 @@ protected function getInternalKeyForAttribute(string $attribute): string
19051923
'$tenant' => '_tenant',
19061924
'$createdAt' => '_createdAt',
19071925
'$updatedAt' => '_updatedAt',
1926+
'$createdBy' => '_createdBy',
1927+
'$updatedBy' => '_updatedBy',
19081928
'$permissions' => '_permissions',
19091929
default => $attribute
19101930
};
@@ -1999,6 +2019,8 @@ public function createDocuments(Document $collection, array $documents): array
19992019
$attributes['_createdAt'] = $document->getCreatedAt();
20002020
$attributes['_updatedAt'] = $document->getUpdatedAt();
20012021
$attributes['_permissions'] = \json_encode($document->getPermissions());
2022+
$attributes['_createdBy'] = $document->getCreatedBy();
2023+
$attributes['_updatedBy'] = $document->getUpdatedBy();
20022024

20032025
if (!empty($document->getSequence())) {
20042026
$attributes['_id'] = $document->getSequence();
@@ -2113,6 +2135,8 @@ public function createOrUpdateDocuments(
21132135
$attributes['_uid'] = $document->getId();
21142136
$attributes['_createdAt'] = $document->getCreatedAt();
21152137
$attributes['_updatedAt'] = $document->getUpdatedAt();
2138+
$attributes['_createdBy'] = $document->getCreatedBy();
2139+
$attributes['_updatedBy'] = $document->getUpdatedBy();
21162140
$attributes['_permissions'] = \json_encode($document->getPermissions());
21172141

21182142
if (!empty($document->getSequence())) {

src/Database/Adapter/SQLite.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ public function createCollection(string $name, array $attributes = [], array $in
168168
{$tenantQuery}
169169
`_createdAt` DATETIME(3) DEFAULT NULL,
170170
`_updatedAt` DATETIME(3) DEFAULT NULL,
171+
`_createdBy` VARCHAR(255) DEFAULT NULL,
172+
`_updatedBy` VARCHAR(255) DEFAULT NULL,
171173
`_permissions` MEDIUMTEXT DEFAULT NULL".(!empty($attributes) ? ',' : '')."
172174
" . \substr(\implode(' ', $attributeStrings), 0, -2) . "
173175
)

0 commit comments

Comments
 (0)