Comments (1)
Hi,
json-sql-builder2
is a transpiler so that you can write your SQL-Queries using the JSON instead of writing your queries as plain text and manage the parameters by your own.
Here I will show you a really basic example using a PostgreSQL database and the node-pg driver.
// require the real database driver
// in this example I am using PostgreSQL
// npm install --save pg
const { Client } = require('pg')
const SQLBuilder = require('json-sql-builder2');
// create a new instance of the SQLBuilder and load the language extension for PostgreSQL
var sql = new SQLBuilder('PostgreSQL');
// initialize you connection to your database
const client = new Client({
host: 'localhost',
port: 5433,
database: 'pureworkx',
user: 'postgres',
password: 'testtest',
});
// connect to your pg database
client.connect((err) => {
if (err) {
console.error('connection error', err.stack)
} else {
// okay, now we are connected to the database
// let's and we can query the database
console.log('connected');
// write your SQL-query using json-sql-builder2
var peoples = sql.$select({
$from: 'people',
$where: {
first_name: 'Koo'
}
});
// now query the database with the transpiled results from json-sql-builder2
client.query(peoples.sql, peoples.values, (err, result) => {
if (err) {
console.log(err);
return;
}
result.rows.forEach(people => {
console.log(people.id, people.first_name, people.last_name);
})
});
// without json-sql-builder2 you will write your
// query using plaintext and support your values
// manually like this:
var sqlQuery = 'SELECT * FROM people WHERE first_name = $1';
var queryValues = ['Koo'];
// query the database using your plain defined SQL and values
client.query(sqlQuery, queryValues, (err, result) => {
// output error and results
if (err) {
console.log(err);
return;
}
result.rows.forEach(people => {
console.log(people.id, people.first_name, people.last_name);
});
});
}
});
So you can write your own small database-wrapper using json-sql-builder2
with the database-driver of your choice or use it like the examle above.
I have written a wrapper for PostgreSQL with the package pure-live-pg
that uses the json-sql-builder2 and supports reactive queries. Have a look at this repository - but it's not documented at this time. https://github.com/planetarydev/pure-live-pg
I hope this helps you to get started with json-sql-builder2. Feel free to open further issues if you've got more questions.
from json-sql-builder2.
Related Issues (20)
- $ine broken for $createView HOT 2
- $concat broken for SQLite
- Select all, with further columns? HOT 1
- SyntaxError: Unexpected token ...
- Division arithmetic helper does not exists HOT 6
- Using cast function in WHERE clause HOT 3
- Support for UNSIGNED values in $createTable? HOT 2
- isValidIdent method question HOT 2
- order by function(value)
- Window functions support
- how can we add 'and' 'or' and 'not' condition in where clause? HOT 2
- MySQL | possible to search JSON values HOT 1
- complex sql handling : case statements, string functions, analytic functions HOT 2
- not able to use trim(), upper(), lower(), etc HOT 1
- Feature request: add function LAST_INSERT_ID() HOT 1
- Compatibility with MongoDB HOT 2
- Can not order by DESC?? HOT 1
- Alter Table
- syntax error in readme.md
- Does it support nested condition in where ?
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-sql-builder2.