Git Product home page Git Product logo

sql-to-json's Introduction

SQL to JSON

SQL to JSON is a small PHP script allowing you to make GET and POST request to a database and retreive the result as a JSON object

GET

POST

+ Advantages and Features

- Drawbacks / Missing Features

  • No complex query (Join etc...)
  • No Put, Delete method at the moment (you must use the Post method)
  • For Subqueries, you must use my database model

##Configuration

1 - Setup your database informations (api.php)

All required database infos must be defined at the beginning of api.php

Here is an example :

// BEGIN DB CONFIGURATION \\

$PARAM_host='localhost';
$PARAM_port='3306';
$PARAM_db_name='yourdbname';
$PARAM_user='username';
$PARAM_pass='password';

// END DB CONFIGURATION \\

2 - Setup your database model (models.json)

When a "table" URL parameter is called : the script retrieves the table (in the database) to be called and the rows to display.

You probably don't want to display the hashed password of an user for example, that's why I advise to never use the "*" selector

An example of an expected output :

{
	"users":
	{
		"table" : "users",
		"rows" :
		[
			"id","nickname"
		]
	},
	"me":
	{
		"table" : "users",
		"rows" :
		[
			"id","nickname","last_login_date","signup_date","email","avatar_url","banner_url"
		]
	}
}

3 - .Htaccess (optionnal)

The default .htaccess is configured to pass parameters like this :

  /api/tablename/&parameter=value

Without it, it will be :

  /api.php?table=tablename&parameter=value

Do not forget to take a look at it!

GET

Simple Calls

Retreive users (default limit = 10)

/api/users

Filter (where)

Retreive all users who are in the group 1

/api/users/&where=group_id=1

Also, you can use multiple conditions with the separator :

/api/users/&where=group_id=1:nickname=CYRIAQU3

Limit

By default, the limit is set to 10, but you can specify a custom value

Retreive the two first users who are in the group 1

/api/users/&where=group_id=1&limit=2

Order

Retreive the two first users who are in the group 1, ordered by their name

/api/users/&where=group_id=1&limit=2&order=name

POST

When the script receive a POST query, it simply includes the file located in the sub-repository /post/{tableparam} You can do whatever you want with it

Post query :

/api/users

File called :

/api/post/users.php

##Others

The Subqueries

The script auto convert some values to object if they follow this following model :

  • The table name must be plural (ex : users, movies)
  • Every rows must have an id with the row name "id" (not user->user_id but user->id)
  • When you referenced another object in your rows, the table name must be singular following by _id ex : table_id

An example :

{
	"articles":
	{
		"table" : "articles",
		"rows" :
		[
			"id","user_id","name","etc"
		]
	}
}

Look at the user_id row. When you call an article, the script will check if the table users actually exist, in this case, it will transform user_id into user and you will get a result like it :

Call

  /api/articles/1

Article structure in Database

{
	"id": "1",
	"name": "Insert a clickbait article name here",
	"user_id" : "1"
}

Result

{
	"success" : true,
	"count" : 1,
	"articles": [
	{
		"id": "1",
		"name": "Insert a clickbait article name here",
		"user" : 
		{
			"id" : "1",
			"nickname" : "CYRIAQU3",
			"etc..." : "etc..."
		}
	}]
}

sql-to-json's People

Contributors

ciriak avatar theblackhole avatar

Watchers

 avatar  avatar

Forkers

theblackhole

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.