chore: mysql and postgres examples

This commit is contained in:
Reinaldy Rafli 2021-07-13 19:34:34 +07:00
parent 8e25d3d6fc
commit eb36f33f8c
6 changed files with 63 additions and 1 deletions

3
examples/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
yarn.lock
pnpm-lock.yaml
package-lock.json

19
examples/mysql/index.js Normal file
View File

@ -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();

View File

@ -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"
}
}

View File

@ -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();

View File

@ -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"
}
}

View File

@ -30,7 +30,8 @@
], ],
"directories": { "directories": {
"lib": "./src", "lib": "./src",
"test": "./test" "test": "./test",
"example": "./examples"
}, },
"scripts": { "scripts": {
"build": "rollup -c", "build": "rollup -c",