Permalink
Browse files

Documents can now be updated and removed

  • Loading branch information...
1 parent e304af9 commit 512f5bca4a4491b2df7333f19d9fae234ca750df @marcqualie committed Feb 7, 2013
View
@@ -2,71 +2,22 @@
[![Build Status](https://travis-ci.org/marcqualie/mongominify.png?branch=master)](https://travis-ci.org/marcqualie/mongominify)
-A drop-in library for applying a simple schema to MongoDB documents and transparently compressing data on the fly.
+A drop-in library which acts as a transparent filter to MongoDB documents and compresses/decompresses data on the fly.
Entirely namespaced, PSR-0 compliant and works with both PHP 5.3 and 5.4.
+
+### Getting started
+
+You should check out the [Getting Started](https://github.com/marcqualie/mongominify/wiki/Getting-Started) page on the Wiki to get up and running
+
+
### Why MongoMinify?
As great as MongoDB is at so many things, it has a downside compared to other data stores that it stores it's keys wih every document.
These keys quickly add up and sometimes double or evem triple the amount of storage required.
Myself and many other developers got around this by adding single letter key names, but this is hard to manage with large projects and documents.
MongoMinify gets around this problem by transparently converting documents as they are transfered between the client and the database leaving readable code with compressed storage.
-### Install
-
-Via composer
-
-```json
-{
- "require": {
- "marcqualie/mongominify": "dev-master"
- }
-}
-```
-
-
-### Usage
-
-I designed the usage pattern to match the native Mongo PHP driver as close as possible so it's a drop in replacement. Nearly all actions are transparent to the application and based around the schema you specify.
-
-```php
-// Connect in the same way you would with the native driver
-$mongo = new MongoMinify\Client('mongodb://localhost:27017');
-$collection = $mongo->db_name->collection_name;
-```
-
-
-#### Apply Schema
-
-The schema is now applied internally based on the collection name. Every action you take on a collection has the schema applied correctly so you see no differences between this library and native library.
-
-```php
-$mongo->schema_dir = 'app/schema/mongominify';
-```
-
-
-#### Inserting documents
-
-When inserting/saving documents they are validated against the schema and translated internally
-
-```php
-$document = [
- 'long_key_name' => 'test data',
- 'long_key_name_2' => 13
-];
-$save = $collection->save($document);
-```
-
-
-#### Querying Schema
-
-You can query data just as you normaly would, the conversion is internal and transparent
-
-```php
-$document = $collection->findOne(['long_key_name' => 'test data'], ['_id' => 0, 'long_key_name_2' => 1]);
-print_r($document); // ['long_key_name_2' => 13]
-```
-
## Feedback / Contributing
View
@@ -1,6 +1,6 @@
{
"name": "marcqualie/mongominify",
- "description": "A drop-in library for applying a simple schema to MongoDB documents and transparently compressing data on the fly",
+ "description": "A drop-in library which acts as a transparent filter to MongoDB documents and compresses/decompresses data on the fly",
"license": "Beerware",
"keywords": [
"mongodb",
@@ -69,6 +69,17 @@ public function update(array $data = array(), array $new_object, array $options
}
+ /**
+ * Remove Documents
+ */
+ public function remove($criteria, array $options = array())
+ {
+ $query = new Query($criteria, $this);
+ $query->compress();
+ return $this->native->remove($query->compressed, $options);
+ }
+
+
/**
* Find document
* @param array $document [description]
@@ -124,6 +135,10 @@ public function setSchemaByName($schema_name = null)
if (file_exists($schema_file))
{
$schema = include $schema_file;
+ if ( ! $schema)
+ {
+ throw new Exception('Possible syntax error in ' . $schema_file);
+ }
$this->setSchema($schema);
}
}
@@ -31,8 +31,16 @@ public function save(array $options = array())
public function update(array $new_object = array(), array $options = array())
{
$this->compress();
+
+ // Apply Rules to special cases
+ if (array_key_exists('$set', $new_object))
+ {
+ $set_document = new Document($new_object['$set'], $this->collection);
+ $set_document->compress();
+ $new_object['$set'] = $set_document->compressed;
+ }
+
$this->collection->native->update($this->compressed, $new_object, $options);
- $this->data['_id'] = $this->compressed['_id'];
}
public function insert(array $options = array())
{
@@ -32,6 +32,7 @@ public function testSave()
}
+
/**
* Test saving a document to the database
*/
@@ -62,6 +63,37 @@ public function testInsert()
}
+ /**
+ * Test updating a document
+ */
+ public function testUpdate()
+ {
+
+ // Create a collection
+ $collection = $this->getTestCollection();
+
+ // Fake Document
+ $document = array(
+ 'user_id' => 1,
+ 'email' => 'test1@example.com',
+ 'tags' => array('tag1', 'tag2')
+ );
+ $new_tags = array('test1', 'test2');
+ $collection->insert($document);
+ $collection->update(array('user_id' => 1), array('$set' => array(
+ 'email' => 'test2@example.com',
+ 'tags' => $new_tags
+ )));
+
+ // Check Data stored in database is compressed
+ $document_native = $collection->native->findOne(array('e' => 'test2@example.com'));
+ $this->assertArrayHasKey('u', $document_native);
+ $this->assertArrayHasKey('e', $document_native);
+ $this->assertEquals($document_native['t'], $new_tags);
+
+ }
+
+
/**
* Make sure we can batch insert
*/
@@ -0,0 +1,33 @@
+<?php
+
+class RemoveTest extends MongoMinifyTest {
+
+
+ /**
+ * Test saving a document to the database
+ */
+ public function testRemove()
+ {
+
+ // Create a collection
+ $collection = $this->getTestCollection();
+
+ // Fake Document
+ $document = array(
+ 'user_id' => 1,
+ 'email' => 'test1@example.com'
+ );
+ $collection->save($document);
+
+ // Check Data stored in database is compressed
+ $document_native = $collection->native->findOne(array('_id' => $document['_id']));
+ $this->assertEquals($document_native['u'], 1);
+
+ // Remove and recheck
+ $collection->remove(array('user_id' => 1));
+ $document_native = $collection->findOne(array('user_id' => 1));
+ $this->assertNull($document_native);
+
+ }
+
+}

0 comments on commit 512f5bc

Please sign in to comment.