Skip to content

Commit 612ed69

Browse files
authored
Merge pull request #12 from goby-lang/use-plugin
Use plugin for model instead of builtin DB library
2 parents 4607fe3 + 1e190c0 commit 612ed69

File tree

6 files changed

+103
-24
lines changed

6 files changed

+103
-24
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
FROM gobylang/goby:v0.1.2
1+
FROM gobylang/goby:latest
2+
3+
RUN go get github.com/jmoiron/sqlx
4+
RUN go get github.com/lib/pq
25

36
ADD . ./
47

docker-compose.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# docker-compose up
2+
3+
version: '3'
4+
services:
5+
web:
6+
environment:
7+
- POSTGRES_HOST=postgres
8+
image: 'gobylang/todo-sample:latest'
9+
links:
10+
- postgres
11+
ports:
12+
- 3000:3000
13+
postgres:
14+
environment:
15+
- POSTGRES_DB=goby_test
16+
- POSTGRES_USER=postgres
17+
image: 'postgres:latest'
18+
ports:
19+
- '5432'
20+

model.gb

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
1-
require "db"
1+
require_relative "./plugin_pg"
22

3-
db_user = ENV["POSTGRES_USER"] || "postgres"
4-
db_name = ENV["POSTGRES_DB"] || "goby_test"
5-
db_host = ENV["POSTGRES_HOST"] || "0.0.0.0"
6-
7-
PG = DB.open("postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
8-
9-
PG.run("create table if not exists list_items (
3+
PluginPG.run("create table if not exists list_items (
104
id serial primary key,
115
title varchar(40),
126
checked boolean
@@ -23,37 +17,38 @@ class ListItem
2317
end
2418

2519
def check
26-
ListItem.db.exec('UPDATE list_items SET checked = true WHERE id = $1', @id)
20+
self.class.plugin_db.exec('UPDATE list_items SET checked = true WHERE id = $1', @id)
2721
@checked = true
2822
end
2923

3024
def uncheck
31-
ListItem.db.exec('UPDATE list_items SET checked = false WHERE id = $1', @id)
25+
self.class.plugin_db.exec('UPDATE list_items SET checked = false WHERE id = $1', @id)
3226
@checked = false
3327
end
3428

3529
def update_title(title)
36-
ListItem.db.exec('UPDATE list_items SET title = $1 WHERE id = $2', title, @id)
30+
self.class.plugin_db.exec('UPDATE list_items SET title = $1 WHERE id = $2', title, @id)
3731
end
3832

3933
def destroy
40-
ListItem.db.exec('DELETE FROM list_items WHERE id = $1', @id)
34+
self.class.plugin_db.exec('DELETE FROM list_items WHERE id = $1', @id)
4135
end
4236

4337
def valid?
4438
@error.nil?
4539
end
4640

47-
def self.db
48-
PG
41+
42+
def self.plugin_db
43+
PluginPG
4944
end
5045

5146
def self.all
52-
self.db.query("SELECT * FROM list_items ORDER BY id DESC")
47+
plugin_db.query("SELECT * FROM list_items ORDER BY id DESC")
5348
end
5449

5550
def self.find(id)
56-
result = self.db.query("SELECT * FROM list_items WHERE id = $1", id).first
51+
result = plugin_db.query("SELECT * FROM list_items WHERE id = $1", id).first
5752
if result
5853
new({ id: result[:id], title: result[:title], checked: result[:checked] })
5954
end
@@ -64,7 +59,7 @@ class ListItem
6459
if result[:error].nil?
6560
title = params[:title]
6661
checked = params[:checked].to_i == 1
67-
resultID = self.db.exec("INSERT INTO list_items (title, checked) VALUES ($1, $2)", title, checked)
62+
resultID = self.plugin_db.exec("INSERT INTO list_items (title, checked) VALUES ($1, $2)", title, checked)
6863
new({ id: resultID, title: title, checked: checked })
6964
else
7065
new({ error: result[:error] })

plugin_pg.gb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
require "plugin"
2+
3+
# Create the plugin
4+
plugin = Plugin.generate("db") do |p|
5+
p.import_pkg("", "github.com/jmoiron/sqlx")
6+
p.import_pkg("_", "github.com/lib/pq")
7+
p.link_function("sqlx", "Open")
8+
end
9+
10+
# Some PG settings
11+
db_user = ENV["POSTGRES_USER"] || "postgres"
12+
db_name = ENV["POSTGRES_DB"] || "goby_test"
13+
db_host = ENV["POSTGRES_HOST"] || "0.0.0.0"
14+
15+
# Open PG connection using sqlx package's Open function
16+
conn, err = plugin.go_func("Open", "postgres", String.fmt("user=%s dbname=%s host=%s sslmode=disable", db_user, db_name, db_host))
17+
18+
if err
19+
puts(err.go_func("Error"))
20+
return nil
21+
end
22+
23+
class PluginDB
24+
def initialize(conn)
25+
@conn = conn
26+
end
27+
28+
def run(query, *args)
29+
r, err = @conn.go_func("Exec", query, *args)
30+
31+
if err != nil
32+
puts(err.go_func("Error"))
33+
end
34+
end
35+
36+
def exec(query, *args)
37+
id = 0
38+
err = @conn.go_func("QueryRow", String.fmt("%s RETURNING id", query), *args).go_func("Scan", id.ptr)
39+
40+
if err != nil
41+
puts(err.go_func("Error"))
42+
else
43+
id
44+
end
45+
end
46+
47+
def query(query, *args)
48+
rows, err = @conn.go_func("Queryx", query, *args)
49+
50+
results = []
51+
52+
while rows.go_func("Next") do
53+
row = GoMap.new
54+
rows.go_func("MapScan", row)
55+
results.push(row.to_hash)
56+
end
57+
58+
results
59+
end
60+
end
61+
62+
PluginPG = PluginDB.new(conn)

readme.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44

55
## Requirement
66

7-
- NodeJS & Yarn
8-
- Goby
9-
- PostgresDB
7+
- Docker
8+
- Docker compose
9+
1010
## Usage
1111

1212
Clone it!
@@ -15,10 +15,10 @@ Clone it!
1515
$ git clone [email protected]:goby-lang/sample-web-app.git
1616
```
1717

18-
Go into the project directory, then open another terminal and run the command:
18+
Go into the project directory and run the command:
1919

2020
```
21-
$ goby server.gb
21+
$ docker-compose up
2222
```
2323

2424
Open `http://localhost:3000` and enjoy!

server.gb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
require "net/simple_server"
2-
require "file"
32
require "json"
43
require_relative "model"
54
server = Net::SimpleServer.new("3000")

0 commit comments

Comments
 (0)