mirror of https://github.com/aldy505/sql-dsl.git
chore: mysql and postgres examples
This commit is contained in:
parent
8e25d3d6fc
commit
eb36f33f8c
|
@ -0,0 +1,3 @@
|
|||
yarn.lock
|
||||
pnpm-lock.yaml
|
||||
package-lock.json
|
|
@ -0,0 +1,19 @@
|
|||
const mysql = require('mysql');
|
||||
const {sql} = require('sql-dsl');
|
||||
|
||||
async function main() {
|
||||
const connection = mysql.createConnection('mysql://root:password@localhost:3306/myapp');
|
||||
connection.connect();
|
||||
const query = sql`INSERT INTO users (name, email, password) VALUES (${'Foo Baar'}, ${'foo@bar.com'}, ${'super secure'})`.formatQuestion();
|
||||
connection.query(query.sql, query.values, (error, result, fields) => {
|
||||
if (error) {
|
||||
console.error(error);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
console.log(fields);
|
||||
});
|
||||
}
|
||||
|
||||
main();
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "mysql",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "Reinaldy Rafli <aldy505@tutanota.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"mysql": "^2.18.1",
|
||||
"sql-dsl": "^0.0.1"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
const {Client} = require('pg');
|
||||
const {sql} = require('sql-dsl');
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
const client = new Client({
|
||||
connectionString: 'postgresql://postgres:password@localhost:5432/myapp',
|
||||
});
|
||||
const query = sql`INSERT INTO users (name, email, password) VALUES (${'Foo Baar'}, ${'foo@bar.com'}, ${'super secure'})`;
|
||||
const insert = await client.query(query.sql, query.values);
|
||||
console.log(insert);
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
}
|
||||
}
|
||||
|
||||
main();
|
|
@ -0,0 +1,11 @@
|
|||
{
|
||||
"name": "postgres",
|
||||
"version": "1.0.0",
|
||||
"main": "index.js",
|
||||
"author": "Reinaldy Rafli <aldy505@tutanota.com>",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"pg": "^8.6.0",
|
||||
"sql-dsl": "^0.0.1"
|
||||
}
|
||||
}
|
|
@ -30,7 +30,8 @@
|
|||
],
|
||||
"directories": {
|
||||
"lib": "./src",
|
||||
"test": "./test"
|
||||
"test": "./test",
|
||||
"example": "./examples"
|
||||
},
|
||||
"scripts": {
|
||||
"build": "rollup -c",
|
||||
|
|
Loading…
Reference in New Issue