Skip to content

Commit

Permalink
feat: add support for mysql2 and pg-pool nodejs db clients
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Dec 22, 2022
1 parent 5b1366a commit 5350d7f
Show file tree
Hide file tree
Showing 20 changed files with 826 additions and 333 deletions.
5 changes: 1 addition & 4 deletions core/internal/psql/insert.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func (c *compilerContext) renderInsertStmt(m qcode.Mutate, embedded bool) {
c.renderValues(m, false)

if !embedded {
c.w.WriteString(` RETURNING `)
c.table(m.Ti.Schema, m.Ti.Name, false)
c.w.WriteString(`.*)`)

c.renderReturning(m)
}
}
23 changes: 11 additions & 12 deletions core/internal/psql/mutate.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,12 @@ func (c *compilerContext) renderUpsert() {

c.w.WriteString(` WHERE `)
c.renderExp(m.Ti, sel.Where.Exp, false)
c.w.WriteString(` RETURNING *) `)
c.renderReturning(m)
}

func (c *compilerContext) renderDelete() {
sel := c.qc.Selects[0]
m := c.qc.Mutates[0]

c.w.WriteString(`WITH `)
c.quoted(sel.Table)
Expand All @@ -332,9 +333,7 @@ func (c *compilerContext) renderDelete() {
c.w.WriteString(` WHERE `)
c.renderExp(sel.Ti, sel.Where.Exp, false)

c.w.WriteString(` RETURNING `)
c.table(sel.Ti.Schema, sel.Ti.Name, false)
c.w.WriteString(`.*) `)
c.renderReturning(m)
}

func (c *compilerContext) renderOneToManyConnectStmt(m qcode.Mutate) {
Expand Down Expand Up @@ -387,10 +386,7 @@ func (c *compilerContext) renderOneToOneConnectStmt(m qcode.Mutate) {

c.w.WriteString(` WHERE `)
c.renderExpPath(m.Ti, m.Where.Exp, false, m.Path)

c.w.WriteString(` RETURNING `)
c.table(m.Ti.Schema, m.Ti.Name, false)
c.w.WriteString(`.*)`)
c.renderReturning(m)
}

func (c *compilerContext) renderOneToManyDisconnectStmt(m qcode.Mutate) {
Expand Down Expand Up @@ -461,10 +457,7 @@ func (c *compilerContext) renderOneToOneDisconnectStmt(m qcode.Mutate) {
c.renderExpPath(m.Ti, m.Where.Exp, false, m.Path)
}
c.w.WriteString(`)`)

c.w.WriteString(` RETURNING `)
c.table(m.Ti.Schema, m.Ti.Name, false)
c.w.WriteString(`.*)`)
c.renderReturning(m)
}

func (c *compilerContext) renderOneToManyModifiers(m qcode.Mutate) {
Expand Down Expand Up @@ -553,6 +546,12 @@ func (c *compilerContext) renderMutateToRecordSet(m qcode.Mutate, n int) {
c.w.WriteString(`)`)
}

func (c *compilerContext) renderReturning(m qcode.Mutate) {
c.w.WriteString(` RETURNING `)
c.table(m.Ti.Schema, m.Ti.Name, false)
c.w.WriteString(`.*)`)
}

func (c *compilerContext) renderComma(i int) int {
if i != 0 {
c.w.WriteString(`, `)
Expand Down
5 changes: 1 addition & 4 deletions core/internal/psql/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,5 @@ func (c *compilerContext) renderUpdateStmt(m qcode.Mutate) {
}
c.w.WriteString(`)`)
}

c.w.WriteString(` RETURNING `)
c.table(m.Ti.Schema, m.Ti.Name, false)
c.w.WriteString(`.*)`)
c.renderReturning(m)
}
2 changes: 1 addition & 1 deletion core/internal/sdata/sql/mysql_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SELECT
p.ordinal_position as param_id,
COALESCE(p.parameter_name, '') as param_name,
p.data_type as param_type,
p.parameter_mode as param_kind
COALESCE(p.parameter_mode, '') as param_kind
FROM
information_schema.routines r
RIGHT JOIN
Expand Down
2 changes: 1 addition & 1 deletion core/internal/sdata/sql/postgres_functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ SELECT
p.ordinal_position as param_id,
COALESCE(p.parameter_name, '') as param_name,
p.data_type as param_type,
p.parameter_mode as param_kind
COALESCE(p.parameter_mode, '') as param_kind
FROM
information_schema.routines r
RIGHT JOIN
Expand Down
40 changes: 0 additions & 40 deletions docker-compose.yml

This file was deleted.

49 changes: 49 additions & 0 deletions examples/nodejs/mysql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import graphjin from "graphjin";
import express from "express";
import http from "http";
import mysql from "mysql2"

const pool = mysql.createPool({
host: 'localhost',
port: '/tmp/mysql.sock',
user: 'root',
database: 'db',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
});

const db = pool.promise();
const app = express();
const server = http.createServer(app);

// config can either be a filename (eg. `dev.yml`) or an object
const config = {
production: false,
db_type: "mysql",
disable_allow_list: true
};

const gj = await graphjin("./config", config, db);


const res1 = await gj.subscribe(
"subscription getUpdatedUser { users(id: $userID) { id email } }",
null,
{ userID: 2 })

res1.data(function(res) {
console.log(">", res.data())
})

app.get('/', async function(req, resp) {
const res2 = await gj.query(
"query getUser { users(id: $id) { id email } }",
{ id: 1 },
{ userID: 1 })

resp.send(res2.data());
});

server.listen(3000);
console.log('Express server started on port %s', server.address().port);
Loading

1 comment on commit 5350d7f

@vercel
Copy link

@vercel vercel bot commented on 5350d7f Dec 22, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.