{"id":17087830,"url":"https://github.com/geraintluff/my-json","last_synced_at":"2025-04-12T21:53:28.019Z","repository":{"id":11375655,"uuid":"13813914","full_name":"geraintluff/my-json","owner":"geraintluff","description":"Use a MySQL database as a JSON document store (in Node)","archived":false,"fork":false,"pushed_at":"2013-11-16T18:43:09.000Z","size":252,"stargazers_count":5,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-26T16:04:27.136Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://npmjs.org/package/my-json","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/geraintluff.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2013-10-23T20:26:50.000Z","updated_at":"2018-03-27T14:00:03.000Z","dependencies_parsed_at":"2022-08-31T06:50:22.559Z","dependency_job_id":null,"html_url":"https://github.com/geraintluff/my-json","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fmy-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fmy-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fmy-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/geraintluff%2Fmy-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/geraintluff","download_url":"https://codeload.github.com/geraintluff/my-json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248325511,"owners_count":21084938,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-14T13:35:05.209Z","updated_at":"2025-04-12T21:53:28.001Z","avatar_url":"https://github.com/geraintluff.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MyJSON - MySQL as JSON\n\nThis package provides an interface to a MySQL database, accessing data as JSON documents.\n\nQueries are performed using JSON Schema to provide powerful search constraints on the data.\n\n## Constructing a class\n\nYou generate a class using a config object.  This config file specifies:\n\n* which table to use\n* which column(s) to use for keys\n* what data types to supply to / expect from different columns\n* where in the resulting JSON document those values should go\n\n```javascript\nvar myJson = require('my-json');\n\nvar TestClass = myJson({\n\ttable: 'TestTable',\n\tkeyColumn: 'integer/id',\n\tcolumns: {\n\t\t'integer/id': 'id',\n\t\t'string/name': 'name',\n\t\t'json': 'json_remainder'\n\t}\n});\n```\n\nThis will work with a table structure like:\n\n```\n+----------+----------+----------------+\n|    id    |   name   | json_remainder |\n+----------+----------+----------------+\n|    5     |   blah   | {\"extra\": 20}  |\n+----------+----------+----------------+\n```\n\nColumns of the `json` type contain a JSON representation of any properties that are *not* accounted-for by any of the other columns.  This table row therefore corresponds to a document like:\n\n```json\n{\n    \"id\": 5,\n    \"name\": \"blah\",\n    \"extra\": 20\n}\n```\n\nCurrently it only supports plain objects (taken from a single row), but support for arrays (as table joins) is planned - see the PHP equivalent [JSON Store](https://github.com/geraintluff/json-store) for what's planned.\n\n## Binding to a MySQL connection\n\nFor all the operations, you can either supply a MySQL connection each time, or you can bind a connection.\n\nBinding also creates a cache - this means that if the same document is returned by two different queries, then they will be represented by the same instance.  These caches are expected to be temporary (perhaps once per request for a web-server).\n\n```javascript\nvar mysql = require('mysql');\nvar connection = mysql.createConnection({...});\n\nvar BoundTestClass = TestClass.cacheWith(connection);\n```\n\n## Loading, saving, editing\n\n### Open:\n\n```javascript\nTestClass.open(connection, 5, function (err, result) {...});\nBoundTestClass.open(5, function (err, result) {...});\n```\n\nThe arguments given to `open()` should match (length and order) the columns specified in the config's `\"keyColumn\"` (or `\"keyColumns\"`) property.\n\nIf found, `result` will be an instance of the appropriate class - otherwise, it will be `undefined`.\n\n### Search (with JSON Schema):\n\n```javascript\nvar schema = {\n\ttype: \"object\",\n\tproperties: {\n\t\t\"id\": {\"enum\": [5]}\n\t}\n};\n\nTestClass.search(connection, schema, function (err, results) {...});\nBoundTestClass.search(schema, function (err, results) {...});\n```\n\nCurrently, the only schema keywords supported are `properties` and `enum`, but support for all validation keywords is planned.\n\n### Save:\n\n```javascript\nTestClass.save(connection, testObj, function (err, results) {...});\nBoundTestClass.save(testObj, function (err, results) {...});\n```\n\n### Create:\n\nCreation is performed by saving an object that is missing a key column:\n\n```javascript\nvar newObj = new TestClass();\nnewObj.name = 'test';\n\nTestClass.save(connection, testObj, function (err, results) {...});\n// or:\nBoundTestClass.save(testObj, function (err, results) {...});\n\nnewObj.id; // populated using the auto-increment, if there is one\n```\n\n### Remove/delete:\n\n```javascript\nTestClass.remove(connection, testObj, function (err, results) {...});\nBoundTestClass.remove(testObj, function (err, results) {...});\n```\n\n### Promises\n\nFor the above methods/functions that take a callback as a final argument, the callback can be omitted.\n\nIf the callback is omitted, then a Promise object is returned instead (from [this module](https://npmjs.org/package/promise)).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeraintluff%2Fmy-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgeraintluff%2Fmy-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgeraintluff%2Fmy-json/lists"}