diff --git a/examples/todo/database/todo/items.json b/examples/todo/database/todo/items.json new file mode 100644 index 0000000..d295942 --- /dev/null +++ b/examples/todo/database/todo/items.json @@ -0,0 +1,8 @@ +[ + { + "title": "An example", + "id": "5ed559fc3669f", + "createdAt": 1591040508, + "updatedAt": 1591040508 + } +] \ No newline at end of file diff --git a/examples/todo/filerdb.php b/examples/todo/filerdb.php new file mode 100644 index 0000000..22af039 --- /dev/null +++ b/examples/todo/filerdb.php @@ -0,0 +1,28 @@ + __DIR__ . '/database', + + // Optional configurations + 'includeTimestamps' => true, + + // Specify database + 'database' => 'todo', + + // Configs + 'createRootIfNotExist' => true, + 'createDatabaseIfNotExist' => true, + 'createCollectionIfNotExist' => true + ]); + + return $filerdb; +} catch (Exception $e) { + return false; +} diff --git a/examples/todo/index.php b/examples/todo/index.php new file mode 100755 index 0000000..d8a3798 --- /dev/null +++ b/examples/todo/index.php @@ -0,0 +1,50 @@ +collection('items')->insert([ + 'title' => $todo + ]); + } + + header('Location: index.php'); + exit; + +} elseif ($action === 'remove') { + $id = (isset($_GET['id']) ? $_GET['id'] : false); + + if ($id !== false) { + $filerdb->collection('items')->id($id)->delete(); + } + + header('Location: index.php'); + exit; +} + +// Retrieve all todo items +$items = $filerdb->collection('items')->all(); + +?> + +

Todo Items

+ +
+ + +
+ + diff --git a/example/backup.php b/examples/usage/backup.php old mode 100644 new mode 100755 similarity index 80% rename from example/backup.php rename to examples/usage/backup.php index a94ea57..164e998 --- a/example/backup.php +++ b/examples/usage/backup.php @@ -1,7 +1,7 @@ documents; $data = Document::byId($documents, $id); - if ($data === false) return false; + + if ($data === false) { + $this->documents = false; + $this->response = false; + return $this; + } + $this->documents = $documents[$data->index]; $this->response = $this->documents; return $this; @@ -113,13 +119,17 @@ public function id ($id) { */ public function get ($fields = false) { - /** - * If the columns parameter is provided, - * and is an array. - */ - if (is_array($fields)) { - if (count($fields) >= 1) { - return $this->pickFieldsFromData($this->response, $fields); + // Make sure there is a document(s) to iterate through. + if ($this->response !== false) { + + /** + * If the columns parameter is provided, + * and is an array. + */ + if (is_array($fields)) { + if (count($fields) >= 1) { + return $this->pickFieldsFromData($this->response, $fields); + } } } @@ -489,24 +499,21 @@ public function delete () { $documentsToDelete = $this->documents; $originalDocuments = $this->getDocuments(); - /** - * Fail safe, we do not want to delete all records - * with this method. - * - * Warn them to use ->empty() instead. - */ - if (count($documentsToDelete) === count($originalDocuments)) { - throw new FilerDBException("Please use ->empty() to delete all records"); - return false; - } - /** * Filter out records that match the documents * to be deleted. */ - foreach ($documentsToDelete as $deleteDoc) { + if (is_array($documentsToDelete)) { + foreach ($documentsToDelete as $deleteDoc) { + foreach ($originalDocuments as $key => $origDoc) { + if ($origDoc->id === $deleteDoc->id) { + unset($originalDocuments[$key]); + } + } + } + } else { foreach ($originalDocuments as $key => $origDoc) { - if ($origDoc->id === $deleteDoc->id) { + if ($origDoc->id === $documentsToDelete->id) { unset($originalDocuments[$key]); } } diff --git a/src/FilerDB/Core/Libraries/Databases.php b/src/FilerDB/Core/Libraries/Databases.php index bc68af4..1e160f3 100644 --- a/src/FilerDB/Core/Libraries/Databases.php +++ b/src/FilerDB/Core/Libraries/Databases.php @@ -121,13 +121,4 @@ private function retrieveDatabases() { $this->databases = $result; } - /** - * Builds the path for the database in the file system. - * @return string $path - */ - private function path ($database) { - $path = $this->config->root . DIRECTORY_SEPARATOR . $database . DIRECTORY_SEPARATOR; - return $path; - } - }