Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip structure validation for now in createDocuments #316

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
de020a8
copy internalId when running createDocuments
lohanidamodar Aug 22, 2023
c2146e1
mongo and adapter update
lohanidamodar Aug 22, 2023
a03ba11
make default behavior to copy internal ID
lohanidamodar Aug 22, 2023
a49d9ec
fix mongo
lohanidamodar Aug 22, 2023
41802d4
fix mongo empty internalId
lohanidamodar Aug 22, 2023
f9453fa
disable structure validation in createDocuments for now
lohanidamodar Aug 27, 2023
68c8f1b
Merge remote-tracking branch 'origin/fix-create-documents-use-interna…
lohanidamodar Aug 27, 2023
54dac44
mariadb check if internalId is not empty
lohanidamodar Aug 27, 2023
cec3816
Merge branch 'fix-create-documents-use-internal-id' into feat-option-…
lohanidamodar Aug 27, 2023
c1ed979
add database name prefix to cache
lohanidamodar Aug 28, 2023
7ca5ac3
adds internal id support for create document
fanatic75 Aug 29, 2023
375baa1
Merge pull request #318 from utopia-php/feat-internal-id-support-old-…
lohanidamodar Aug 30, 2023
7f5202f
fix remove attribute for null values
lohanidamodar Aug 30, 2023
de40116
add document security
lohanidamodar Sep 13, 2023
69928e0
adds code to use createdAt and updatedAt in createDocuments from docu…
fanatic75 Sep 14, 2023
66dfebb
Remove document security
Meldiron Sep 18, 2023
f947e14
chore: revert documentSecurity attribute
christyjacob4 Sep 18, 2023
6797814
chore: make document Security optional
christyjacob4 Sep 19, 2023
1128f52
chore: patch for migrations
christyjacob4 Sep 19, 2023
01c1751
chore: patch for migrations
christyjacob4 Sep 19, 2023
e503039
fix remove attribute
lohanidamodar Sep 19, 2023
8920b94
modify encode,decode and casting for document security
lohanidamodar Sep 19, 2023
c614b85
add console check
lohanidamodar Sep 19, 2023
d79bc87
fix typo
lohanidamodar Sep 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Database/Adapter/MariaDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ public function createDocuments(string $collection, array $documents, int $batch
$attributes['_createdAt'] = $document->getCreatedAt();
$attributes['_updatedAt'] = $document->getUpdatedAt();
$attributes['_permissions'] = \json_encode($document->getPermissions());
if(!empty($document->getInternalId())) {
$attributes['_id'] = $document->getInternalId();
}

$columns = [];
foreach (\array_keys($attributes) as $key => $attribute) {
Expand Down
3 changes: 3 additions & 0 deletions src/Database/Adapter/Mongo/MongoDBAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ public function createDocuments(string $collection, array $documents, int $batch

$records = [];
foreach ($documents as $document) {
if(!empty($document->getInternalId())) {
$document->setAttribute('_id', $document->getInternalId());
}
$document->removeAttribute('$internalId');

$records[] = $this->replaceChars('$', '_', (array)$document);
Expand Down
28 changes: 16 additions & 12 deletions src/Database/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class Database
*/
protected Cache $cache;

protected string $name;

/**
* @var array
*/
Expand Down Expand Up @@ -236,11 +238,12 @@ class Database
* @param Adapter $adapter
* @param Cache $cache
*/
public function __construct(Adapter $adapter, Cache $cache, array $filters = [])
public function __construct(Adapter $adapter, Cache $cache, array $filters = [], String $name = 'default')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String should be lowercase. It would make more sense to have nam as the first param later.

{
$this->adapter = $adapter;
$this->cache = $cache;
$this->instanceFilters = $filters;
$this->name = $name;

self::addFilter(
'json',
Expand Down Expand Up @@ -1303,7 +1306,7 @@ public function getDocument(string $collection, string $id): Document
$validator = new Authorization(self::PERMISSION_READ);

// TODO@kodumbeats Check if returned cache id matches request
if ($cache = $this->cache->load('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id, self::TTL)) {
if ($cache = $this->cache->load($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id, self::TTL)) {
$document = new Document($cache);

if ($collection->getId() !== self::METADATA
Expand All @@ -1330,7 +1333,7 @@ public function getDocument(string $collection, string $id): Document
$document = $this->casting($collection, $document);
$document = $this->decode($collection, $document);

$this->cache->save('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id, $document->getArrayCopy()); // save to cache after fetching from db
$this->cache->save($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id, $document->getArrayCopy()); // save to cache after fetching from db

$this->trigger(self::EVENT_DOCUMENT_READ, $document);
return $document;
Expand Down Expand Up @@ -1407,10 +1410,11 @@ public function createDocuments(string $collection, array $documents, int $batch

$document = $this->encode($collection, $document);

$validator = new Structure($collection);
if (!$validator->isValid($document)) {
throw new StructureException($validator->getDescription());
}
// TODO find a way to skip with special parameter for migrations
// $validator = new Structure($collection);
// if (!$validator->isValid($document)) {
// throw new StructureException($validator->getDescription());
// }

$documents[$key] = $document;
}
Expand Down Expand Up @@ -1466,7 +1470,7 @@ public function updateDocument(string $collection, string $id, Document $documen
$document = $this->adapter->updateDocument($collection->getId(), $document);
$document = $this->decode($collection, $document);

$this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id);
$this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id);

$this->trigger(self::EVENT_DOCUMENT_UPDATE, $document);
return $document;
Expand Down Expand Up @@ -1516,7 +1520,7 @@ public function updateDocuments(string $collection, array $documents, int $batch
foreach ($documents as $key => $document) {
$documents[$key] = $this->decode($collection, $document);

$this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $document->getId() . ':*');
$this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $document->getId() . ':*');
}

$this->trigger(self::EVENT_DOCUMENTS_UPDATE, $documents);
Expand Down Expand Up @@ -1546,7 +1550,7 @@ public function deleteDocument(string $collection, string $id): bool
throw new AuthorizationException($validator->getDescription());
}

$this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id);
$this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection->getId() . ':' . $id);

$deleted = $this->adapter->deleteDocument($collection->getId(), $id);
$this->trigger(self::EVENT_DOCUMENT_DELETE, $document);
Expand All @@ -1562,7 +1566,7 @@ public function deleteDocument(string $collection, string $id): bool
*/
public function deleteCachedCollection(string $collection): bool
{
return $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection . ':*');
return $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection . ':*');
}

/**
Expand All @@ -1575,7 +1579,7 @@ public function deleteCachedCollection(string $collection): bool
*/
public function deleteCachedDocument(string $collection, string $id): bool
{
return $this->cache->purge('cache-' . $this->getNamespace() . ':' . $collection . ':' . $id);
return $this->cache->purge($this->name . '-cache-' . $this->getNamespace() . ':' . $collection . ':' . $id);
}

/**
Expand Down