hyperion-sql 0.0.1.alpha2 → 0.0.1.alpha3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/hyperion/sql/middleware.rb +47 -0
- data/lib/hyperion/sql.rb +10 -1
- data/spec/hyperion/sql/middleware_spec.rb +46 -0
- metadata +22 -3
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'hyperion/sql'
|
2
|
+
|
3
|
+
module Hyperion
|
4
|
+
module Sql
|
5
|
+
|
6
|
+
# = Middleware
|
7
|
+
#
|
8
|
+
# This rack middleware will do the following
|
9
|
+
#
|
10
|
+
# 1. Wrap the request with a db connection using the :connection_url option
|
11
|
+
# 2. Wrap the request with a datastore using the :ds and :ds_opts options
|
12
|
+
# 3. Wrap the request with a transaction
|
13
|
+
#
|
14
|
+
# Examples
|
15
|
+
#
|
16
|
+
# 1. sqlite3
|
17
|
+
#
|
18
|
+
# use Hyperion::Sql::Middleware connection_url: 'sqlite3::memory:', db: :sqlite
|
19
|
+
#
|
20
|
+
# with Rails...
|
21
|
+
# config.middleware.use Hyperion::Sql::Middleware connection_url: 'sqlite3::memory:', ds: :sqlite
|
22
|
+
#
|
23
|
+
# 2. postgres
|
24
|
+
#
|
25
|
+
# use Hyperion::Sql::Middleware connection_url: 'postgres://localhost/hyperion_ruby', ds: :postgres
|
26
|
+
|
27
|
+
class Middleware
|
28
|
+
|
29
|
+
def initialize(app, opts={})
|
30
|
+
@app = app
|
31
|
+
@connection_url = opts[:connection_url]
|
32
|
+
@ds = opts[:ds]
|
33
|
+
@ds_opts = opts[:ds_opts]
|
34
|
+
end
|
35
|
+
|
36
|
+
def call(env)
|
37
|
+
Sql.with_connection_and_ds(@connection_url, @ds, @ds_opts) do
|
38
|
+
Sql.transaction do
|
39
|
+
@app.call(env)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
data/lib/hyperion/sql.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'hyperion/api'
|
1
2
|
require 'hyperion/sql/transaction'
|
2
3
|
|
3
4
|
module Hyperion
|
@@ -6,11 +7,19 @@ module Hyperion
|
|
6
7
|
def self.with_connection(url)
|
7
8
|
connection = DataObjects::Connection.new(url)
|
8
9
|
Thread.current[:connection] = connection
|
9
|
-
yield
|
10
|
+
yield
|
10
11
|
connection.close
|
11
12
|
Thread.current[:connection] = nil
|
12
13
|
end
|
13
14
|
|
15
|
+
def self.with_connection_and_ds(url, name, opts={})
|
16
|
+
with_connection(url) do
|
17
|
+
API.with_datastore(name, opts) do
|
18
|
+
yield
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
14
23
|
def self.connection
|
15
24
|
Thread.current[:connection] || raise('No Connection Established')
|
16
25
|
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'hyperion/sql/middleware'
|
2
|
+
require 'do_sqlite3'
|
3
|
+
|
4
|
+
describe Hyperion::Sql::Middleware do
|
5
|
+
|
6
|
+
def middleware(app)
|
7
|
+
midd = Hyperion::Sql::Middleware.new(app, {
|
8
|
+
connection_url: 'sqlite3::memory:',
|
9
|
+
ds: :memory,
|
10
|
+
ds_opts: {someopts: 1}
|
11
|
+
})
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'passes the env' do
|
15
|
+
mock_env = mock(:env)
|
16
|
+
midd = middleware lambda { |env|
|
17
|
+
env.should == mock_env
|
18
|
+
}
|
19
|
+
midd.call(mock_env)
|
20
|
+
end
|
21
|
+
|
22
|
+
it 'establishes a connection' do
|
23
|
+
midd = middleware lambda { |env|
|
24
|
+
expect {Hyperion::Sql.connection}.to_not raise_error
|
25
|
+
}
|
26
|
+
midd.call(nil)
|
27
|
+
expect {Hyperion::Sql.connection}.to raise_error
|
28
|
+
end
|
29
|
+
|
30
|
+
it 'assigns the datastore' do
|
31
|
+
midd = middleware lambda { |env|
|
32
|
+
Hyperion::API.datastore.class.should == Hyperion::Memory
|
33
|
+
}
|
34
|
+
midd.call(nil)
|
35
|
+
expect {Hyperion::API.datastore}.to raise_error
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'starts a transaction' do
|
39
|
+
midd = middleware lambda { |env|
|
40
|
+
Thread.current[:transaction].should_not be_nil
|
41
|
+
}
|
42
|
+
midd.call(nil)
|
43
|
+
Thread.current[:transaction].should be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hyperion-sql
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.1.
|
4
|
+
version: 0.0.1.alpha3
|
5
5
|
prerelease: 6
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -34,7 +34,7 @@ dependencies:
|
|
34
34
|
requirements:
|
35
35
|
- - '='
|
36
36
|
- !ruby/object:Gem::Version
|
37
|
-
version: 0.0.1.
|
37
|
+
version: 0.0.1.alpha3
|
38
38
|
type: :runtime
|
39
39
|
prerelease: false
|
40
40
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -42,7 +42,23 @@ dependencies:
|
|
42
42
|
requirements:
|
43
43
|
- - '='
|
44
44
|
- !ruby/object:Gem::Version
|
45
|
-
version: 0.0.1.
|
45
|
+
version: 0.0.1.alpha3
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: do_sqlite3
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - '='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: 0.10.8
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - '='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 0.10.8
|
46
62
|
description: Shared behavior for Sql databases
|
47
63
|
email:
|
48
64
|
- myles@8thlight.com
|
@@ -54,10 +70,12 @@ files:
|
|
54
70
|
- lib/hyperion/sql.rb
|
55
71
|
- lib/hyperion/sql/query_executor.rb
|
56
72
|
- lib/hyperion/sql/sql_query.rb
|
73
|
+
- lib/hyperion/sql/middleware.rb
|
57
74
|
- lib/hyperion/sql/transaction.rb
|
58
75
|
- lib/hyperion/sql/transaction_spec.rb
|
59
76
|
- lib/hyperion/sql/query_builder.rb
|
60
77
|
- lib/hyperion/sql/datastore.rb
|
78
|
+
- spec/hyperion/sql/middleware_spec.rb
|
61
79
|
- spec/hyperion/sql_spec.rb
|
62
80
|
homepage: https://github.com/mylesmegyesi/hyperion-ruby
|
63
81
|
licenses:
|
@@ -85,4 +103,5 @@ signing_key:
|
|
85
103
|
specification_version: 3
|
86
104
|
summary: Shared behavior for Sql databases
|
87
105
|
test_files:
|
106
|
+
- spec/hyperion/sql/middleware_spec.rb
|
88
107
|
- spec/hyperion/sql_spec.rb
|