Skip to content

Commit f77eb9a

Browse files
added new internal attributes to the adapters
1 parent dbecdf8 commit f77eb9a

6 files changed

Lines changed: 52 additions & 6 deletions

File tree

src/Database/Adapter.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1233,6 +1233,7 @@ protected function escapeWildcards(string $value): string
12331233
* @param string $attribute
12341234
* @param int|float $value
12351235
* @param string $updatedAt
1236+
* @param string|null $updatedBy
12361237
* @param int|float|null $min
12371238
* @param int|float|null $max
12381239
* @return bool
@@ -1244,6 +1245,7 @@ abstract public function increaseDocumentAttribute(
12441245
string $attribute,
12451246
int|float $value,
12461247
string $updatedAt,
1248+
?string $updatedBy,
12471249
int|float|null $min = null,
12481250
int|float|null $max = null
12491251
): bool;

src/Database/Adapter/MariaDB.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ public function createCollection(string $name, array $attributes = [], array $in
159159
_uid VARCHAR(255) NOT NULL,
160160
_createdAt DATETIME(3) DEFAULT NULL,
161161
_updatedAt DATETIME(3) DEFAULT NULL,
162+
_createdBy VARCHAR(255) DEFAULT NULL,
163+
_updatedBy VARCHAR(255) DEFAULT NULL,
162164
_permissions MEDIUMTEXT DEFAULT NULL,
163165
PRIMARY KEY (_id),
164166
" . \implode(' ', $attributeStrings) . "
@@ -827,6 +829,8 @@ public function createDocument(Document $collection, Document $document): Docume
827829
$attributes = $document->getAttributes();
828830
$attributes['_createdAt'] = $document->getCreatedAt();
829831
$attributes['_updatedAt'] = $document->getUpdatedAt();
832+
$attributes['_createdBy'] = $document->getCreatedBy();
833+
$attributes['_updatedBy'] = $document->getUpdatedBy();
830834
$attributes['_permissions'] = \json_encode($document->getPermissions());
831835

832836
if ($this->sharedTables) {
@@ -953,6 +957,7 @@ public function updateDocument(Document $collection, string $id, Document $docum
953957
$attributes = $document->getAttributes();
954958
$attributes['_createdAt'] = $document->getCreatedAt();
955959
$attributes['_updatedAt'] = $document->getUpdatedAt();
960+
$attributes['_updatedBy'] = $document->getUpdatedBy();
956961
$attributes['_permissions'] = json_encode($document->getPermissions());
957962

958963
$name = $this->filter($collection);
@@ -1241,6 +1246,7 @@ public function getUpsertStatement(
12411246
* @param string $attribute
12421247
* @param int|float $value
12431248
* @param string $updatedAt
1249+
* @param string|null $updatedBy
12441250
* @param int|float|null $min
12451251
* @param int|float|null $max
12461252
* @return bool
@@ -1252,6 +1258,7 @@ public function increaseDocumentAttribute(
12521258
string $attribute,
12531259
int|float $value,
12541260
string $updatedAt,
1261+
?string $updatedBy,
12551262
int|float|null $min = null,
12561263
int|float|null $max = null
12571264
): bool {
@@ -1265,7 +1272,8 @@ public function increaseDocumentAttribute(
12651272
UPDATE {$this->getSQLTable($name)}
12661273
SET
12671274
`{$attribute}` = `{$attribute}` + :val,
1268-
`_updatedAt` = :updatedAt
1275+
`_updatedAt` = :updatedAt,
1276+
`_updatedBy` = :updatedBy
12691277
WHERE _uid = :_uid
12701278
{$this->getTenantQuery($collection)}
12711279
";
@@ -1278,6 +1286,7 @@ public function increaseDocumentAttribute(
12781286
$stmt->bindValue(':_uid', $id);
12791287
$stmt->bindValue(':val', $value);
12801288
$stmt->bindValue(':updatedAt', $updatedAt);
1289+
$stmt->bindValue(':updatedBy', $updatedBy);
12811290

12821291
if ($this->sharedTables) {
12831292
$stmt->bindValue(':_tenant', $this->tenant);

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: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,8 @@ public function createCollection(string $name, array $attributes = [], array $in
237237
" . $sqlTenant . "
238238
\"_createdAt\" TIMESTAMP(3) DEFAULT NULL,
239239
\"_updatedAt\" TIMESTAMP(3) DEFAULT NULL,
240+
\"_createdBy\" VARCHAR(255) DEFAULT NULL,
241+
\"_updatedBy\" VARCHAR(255) DEFAULT NULL,
240242
_permissions TEXT DEFAULT NULL,
241243
" . \implode(' ', $attributeStrings) . "
242244
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);

src/Database/Adapter/SQL.php

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,14 @@ public function getDocument(Document $collection, string $id, array $queries = [
409409
$document['$updatedAt'] = $document['_updatedAt'];
410410
unset($document['_updatedAt']);
411411
}
412+
if (\array_key_exists('_createdBy', $document)) {
413+
$document['$createdBy'] = $document['_createdBy'];
414+
unset($document['_createdBy']);
415+
}
416+
if (\array_key_exists('_updatedBy', $document)) {
417+
$document['$updatedBy'] = $document['_updatedBy'];
418+
unset($document['_updatedBy']);
419+
}
412420
if (\array_key_exists('_permissions', $document)) {
413421
$document['$permissions'] = json_decode($document['_permissions'] ?? '[]', true);
414422
unset($document['_permissions']);
@@ -469,6 +477,14 @@ public function updateDocuments(Document $collection, Document $updates, array $
469477
$attributes['_createdAt'] = $updates->getCreatedAt();
470478
}
471479

480+
if (!empty($updates->getCreatedBy())) {
481+
$attributes['_createdBy'] = $updates->getCreatedBy();
482+
}
483+
484+
if (!empty($updates->getUpdatedBy())) {
485+
$attributes['_updatedBy'] = $updates->getUpdatedBy();
486+
}
487+
472488
if ($updates->offsetExists('$permissions')) {
473489
$attributes['_permissions'] = json_encode($updates->getPermissions());
474490
}
@@ -1054,10 +1070,12 @@ public function getAttributeWidth(Document $collection): int
10541070
* `_tenant` int => 4 bytes
10551071
* `_createdAt` datetime(3) => 7 bytes
10561072
* `_updatedAt` datetime(3) => 7 bytes
1073+
* `_createdBy` varchar(255) => 1021 (4 * 255 + 1) bytes
1074+
* `_updatedBy` varchar(255) => 1021 (4 * 255 + 1) bytes
10571075
* `_permissions` mediumtext => 20
10581076
*/
10591077

1060-
$total = 1067;
1078+
$total = 3109;
10611079

10621080
$attributes = $collection->getAttributes()['attributes'];
10631081

@@ -1891,7 +1909,7 @@ protected function getAttributeProjection(array $selections, string $prefix, arr
18911909
$projections = [];
18921910
$projections[] = "{$this->quote($prefix)}.*";
18931911

1894-
$internalColumns = ['_id', '_uid', '_createdAt', '_updatedAt', '_permissions'];
1912+
$internalColumns = ['_id', '_uid', '_createdAt', '_updatedAt', '_permissions', '_createdBy', '_updatedBy'];
18951913
if ($this->sharedTables) {
18961914
$internalColumns[] = '_tenant';
18971915
}
@@ -1950,6 +1968,8 @@ protected function getInternalKeyForAttribute(string $attribute): string
19501968
'$tenant' => '_tenant',
19511969
'$createdAt' => '_createdAt',
19521970
'$updatedAt' => '_updatedAt',
1971+
'$createdBy' => '_createdBy',
1972+
'$updatedBy' => '_updatedBy',
19531973
'$permissions' => '_permissions',
19541974
default => $attribute
19551975
};
@@ -2044,6 +2064,8 @@ public function createDocuments(Document $collection, array $documents): array
20442064
$attributes['_createdAt'] = $document->getCreatedAt();
20452065
$attributes['_updatedAt'] = $document->getUpdatedAt();
20462066
$attributes['_permissions'] = \json_encode($document->getPermissions());
2067+
$attributes['_createdBy'] = $document->getCreatedBy();
2068+
$attributes['_updatedBy'] = $document->getUpdatedBy();
20472069

20482070
if (!empty($document->getSequence())) {
20492071
$attributes['_id'] = $document->getSequence();
@@ -2158,6 +2180,8 @@ public function createOrUpdateDocuments(
21582180
$attributes['_uid'] = $document->getId();
21592181
$attributes['_createdAt'] = $document->getCreatedAt();
21602182
$attributes['_updatedAt'] = $document->getUpdatedAt();
2183+
$attributes['_createdBy'] = $document->getCreatedBy();
2184+
$attributes['_updatedBy'] = $document->getUpdatedBy();
21612185
$attributes['_permissions'] = \json_encode($document->getPermissions());
21622186

21632187
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)