Giter Club home page Giter Club logo

jsonhib's Introduction

jsonhib

A way to read/write JSON in a MySQL database. jsonhib is available for both Node.js and PHP.

Here's a simple jsonhib object (in Node.js):

// create a jsonhib object
var jh = new jsonhib({
   'link_identifier': conn,
   'sort_column': 'n', // array index column name
   'json_column': 'json' // freeform JSON column name
});

Or, in PHP:

// create a jsonhib object
$jh = new jsonhib(array(
  'sort_column'=>'n',
  'json_column'=>'json'
));

Here's an example of reading a table:

// assume 'mytable' is a MySQL table with these columns: id, name
jh.readRows('mytable', 'WHERE id > 0', function(s) {
  var str = s;
});
// str now contains [{"id": 1, "name": "bob"}, {"id": 2, "name": "fred"}]
jh.readDesc('mytable', function(s) {
  var str = s;
});
// str now contains {"id": 0, "name": ""}

In PHP:

// assume 'mytable' is a MySQL table with these columns: id, name
$str = $jh->readRows('mytable', 'WHERE id > 0');
// $str now contains [{"id": 1, "name": "bob"}, {"id": 2, "name": "fred"}]
$str = $jh->readDesc('mytable');
// $str now contains {"id": 0, "name": ""}

Here's an example of writing a table:

// insert a row
jh.insertRow('mytable', '', -1, '{"id": 1, "name": "bob"}', function() {
});
// delete a row
jh.deleteRow('mytable', '', 0, function() {
});
// update a row
jh.updateRow('mytable', 'WHERE id=1', 0, '{"id": 1, "name": "eric"}', function() {
});
// move a row (huh?)
jh.moveRow('mytable', 'WHERE id=1', 0, 1, function() {
});

In PHP:

// insert a row
$jh->insertRow('mytable', '', -1, '{"id": 1, "name": "bob"}');
// delete a row
$jh->deleteRow('mytable', '', 0);
// update a row
$jh->updateRow('mytable', 'WHERE id=1', 0, '{"id": 1, "name": "eric"}');
// move a row (huh?)
$jh->moveRow('mytable', 'WHERE id=1', 0, 1);

If you can add or alter mytable to have a json_column (e.g. json), then any JSON data that doesn't fit into a MySQL column will be stored in that column. So, jsonhib will store the age in the json_column since mytable does not have a MySQL age column:

jh.insertRow('mytable', '', -1, '{"id": 1, "name": "bob", "age": 35}', function() {
});

In PHP:

$jh->insertRow('mytable', '', -1, '{"id": 1, "name": "bob", "age": 35}');

If you can add or alter mytable to have a sort_column (e.g. n), then reading and writing JSON array values won't be in random order; it will use that column to save indices for your JSON array. jsonhib will quietly use this column to save values from 0 ... n-1 to keep array indices for your JSON array. With that, readRows(), insertRow(), deleteRow(), updateRow() and moveRow() will work exactly as expected.

jsonhib's People

Contributors

ajaximrpg avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.