bumip/json-schema-sql

Helper for converting json-schemas to sql create table statement

dev-master / 1.4.x-dev 2020-09-08 11:18 UTC

This package is not auto-updated.

Last update: 2024-04-18 04:59:13 UTC


README

This class converts a json-schema to a valid Sql table. Tested with MySql and Sqlite. A big shoutout to @WebMaestroFr for this helper.

Installation

Just run

composer require bumip/json-schema-sql dev-master

PHP

The php version is getting tested. Delete tests/database/dbtest.db to start testing

// Instanciate PDO
$pdo = new \PDO("mysql:dbname=example;host=localhost;port=3306", "user", "password");
// Instanciate JSON_Schema_MySQL
$sql_schema = new \Bumip\JsonSchema\JsonSchemaSql($pdo);

// Or, generate table from a single .json file
$sql_schema->createTableFromFile("path/to/schema.json");

## Shell
will be changed to a proper cli tool in the future.
`php -q /path/to/json-schema-mysql/json-schema-mysql.php "mysql:dbname=example;host=localhost;port=3306" "user" "password" "/path/to/json/schema/directory"`

CRUD Class UNTESTED

A CRUD class matching the DB architecture is available. NOT TESTED.

require_once "path/to/json-schema-mysql/json-schema-crud.php";
// Instanciate PDO
$pdo = new PDO("mysql:dbname=example;host=localhost;port=3306", "user", "password");
// Instanciate JSON_Schema_MySQL
$crud = new Schema_Model($pdo, "path/to/json/schema/directory");

// Get model by id
$model = $crud->my_schema($id);
// Create model
$model = $crud->my_schema->create([
    "column_1" => "Value one",
    "column_2" => "Value two"
]);
// Get model rows
$models = $crud->my_schema->read([
    "column_1" => "First filter",
    "column_2" => "Second filter"
], [
    "order_by" => "date",
    "order"    => "DESC",
    "limit"    => 24,
    "page"     => 0
]);
// Update model
$model = $crud->my_schema->update($model->id[
    "column_1" => "New value one",
    "column_2" => "New value two"
]);
// Delete model
$crud->my_schema->delete($model->id);

Feel free to whatever. Contributions are welcome.