From eb36f33f8c224b3c87b87936fce72c428c27f52c Mon Sep 17 00:00:00 2001 From: Reinaldy Rafli Date: Tue, 13 Jul 2021 19:34:34 +0700 Subject: [PATCH] chore: mysql and postgres examples --- examples/.gitignore | 3 +++ examples/mysql/index.js | 19 +++++++++++++++++++ examples/mysql/package.json | 11 +++++++++++ examples/postgres/index.js | 17 +++++++++++++++++ examples/postgres/package.json | 11 +++++++++++ package.json | 3 ++- 6 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 examples/.gitignore create mode 100644 examples/mysql/index.js create mode 100644 examples/mysql/package.json create mode 100644 examples/postgres/index.js create mode 100644 examples/postgres/package.json diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..3f90a09 --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1,3 @@ +yarn.lock +pnpm-lock.yaml +package-lock.json \ No newline at end of file diff --git a/examples/mysql/index.js b/examples/mysql/index.js new file mode 100644 index 0000000..129ff85 --- /dev/null +++ b/examples/mysql/index.js @@ -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(); diff --git a/examples/mysql/package.json b/examples/mysql/package.json new file mode 100644 index 0000000..a5b2361 --- /dev/null +++ b/examples/mysql/package.json @@ -0,0 +1,11 @@ +{ + "name": "mysql", + "version": "1.0.0", + "main": "index.js", + "author": "Reinaldy Rafli ", + "license": "MIT", + "dependencies": { + "mysql": "^2.18.1", + "sql-dsl": "^0.0.1" + } +} diff --git a/examples/postgres/index.js b/examples/postgres/index.js new file mode 100644 index 0000000..8259d93 --- /dev/null +++ b/examples/postgres/index.js @@ -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(); diff --git a/examples/postgres/package.json b/examples/postgres/package.json new file mode 100644 index 0000000..a5a48fe --- /dev/null +++ b/examples/postgres/package.json @@ -0,0 +1,11 @@ +{ + "name": "postgres", + "version": "1.0.0", + "main": "index.js", + "author": "Reinaldy Rafli ", + "license": "MIT", + "dependencies": { + "pg": "^8.6.0", + "sql-dsl": "^0.0.1" + } +} diff --git a/package.json b/package.json index d629223..7ab0f31 100644 --- a/package.json +++ b/package.json @@ -30,7 +30,8 @@ ], "directories": { "lib": "./src", - "test": "./test" + "test": "./test", + "example": "./examples" }, "scripts": { "build": "rollup -c",