diff --git a/README.md b/README.md index 6511f52..19d34b2 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ I'm a reasonable man however, and am prepared for a discussion on it, if any wer Note: This module's query API is based on a relatively simple JSON to array conversion principle. It does *not* use Postgres' or MySQL's JSON operators at the ORM level. The aim however is to allow dev's to use their preferred DB's syntax, and to this end you can set -the module into `mysql` or `postgres` mode using SS config, see [Configuration Docs](configuration.md). +the module into `mysql` or `postgres` mode using SS config, see [Configuration Docs](docs/en/configuration.md). ## Installation @@ -42,6 +42,10 @@ the module into `mysql` or `postgres` mode using SS config, see [Configuration D See: [Configuration Docs](docs/en/configuration.md). +## Usage + +See: [Usage Docs](docs/en/usage.md). + ## Stability This is currently *alpha software*. At time of writing (June 2016) there is @@ -61,10 +65,6 @@ See: [CONTRIBUTING.md](CONTRIBUTING.md). Please include all details, no matter how small. If it were *your module*, what would you need to know from a bug/reature request? :-) -## Usage - -See: [Usage Docs](docs/en/usage.md). - ## TODO * Lose the fugly way that data is queried via `$this->dbObject()` diff --git a/docs/en/usage.md b/docs/en/usage.md index e3cc379..c068367 100644 --- a/docs/en/usage.md +++ b/docs/en/usage.md @@ -1,12 +1,6 @@ # Usage -The module can be put into `mysql` or `postgres` query mode using SS config thus: - - - JSONText: - backend: mysql - -You can also stipulate what format you want your query results back as; JSON or Array, thus: +You can stipulate what format you want your query results back as via passing one of **json** or **array** to `setReturnType()`, thus: // JSON $field = JSONText\Fields\JSONText::create('MyJSON'); @@ -18,10 +12,7 @@ You can also stipulate what format you want your query results back as; JSON or $field->setValue('{"a": {"b":{"c": "foo"}}}'); $field->setReturnType('array'); -## Examples - -In the examples below, if you pass invalid queries or malformed JSON (where applicable) an exception is thrown. - +In the examples below, if you pass invalid queries or malformed JSON (where applicable) an instnce of `JSONTextException` is thrown. class MyDataObject extends DataObject { @@ -52,3 +43,34 @@ In the examples below, if you pass invalid queries or malformed JSON (where appl */ public function getNthJSONVal($n) { + return $this->dbObject('MyJSON')->nth($n); + } + + /** + * Returns a key=>value pair based on a strict integer -> key match. + * If a string is passed, an empty array is returned. + */ + public function getNestedByIntKey($int) + { + return $this->dbObject('MyJSON')->query('->', $int); + } + + /** + * Returns a key=>value pair based on a strict string -> key match. + * If an integer is passed, an empty array is returned. + */ + public function getNestedByStrKey($str) + { + return $this->dbObject('MyJSON')->query('->>', $str); + } + + /** + * Returns a value based on a strict string/int match of the key-as-array + * Given source JSON ala: '{"a": {"b":{"c": "foo"}}}' will return '{"c": "foo"}' + */ + public function getByPathMatch('{"a":"b"}') + { + return $this->dbObject('MyJSON')->query('#>', '{"a":"b"}'; + } + + }