-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.ls
65 lines (47 loc) · 1.39 KB
/
db.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
* user: gisborne
* date: 9/13/14
* time: 16:11
*/
require! pg
require! hstore: 'node-postgres-hstore'
require! Q: q
require! path
require! _: 'prelude-ls'
pgTypes = pg.types
constring = "postgres://gisborne:@localhost/functional"
with_connection = (handler) ->
deferred = Q.defer()
pg.connect constring, (err, client, done) ->
handler err, client
done
deferred.resolve()
deferred.promise
export params_query = (q, params, handle) ->
with_connection (err, client) ->
client.query q, params, (e, r) ->
raise new Error e if e
handle r
export quick_query = (q, handle) ->
with_connection (err, client) ->
client.query q, (e, r) ->
raise new Error e if e
handle r
rowForInsert = (vals) ->
_.values(vals) * ',' + "\n"
export quick_insert = (name, vals, succeed) ->
with_connection (err, client) ->
raise new Error e if e
stream = client.copyFrom "COPY #name (#{ks * ', '} FROM STDIN WITH CSV"
stream.on 'error', ->
raise new Error e
_.each ((h) ->
stream.write rowForInsert(h)),
vals
stream.end
export get_relation = (name, handle) ->
params_query "SELECT fields FROM relations WHERE name = $1", [name], handle
/* required for hstore module */
quick_query "SELECT oid FROM pg_type WHERE typname = 'hstore'", (r) ->
export hstore_oid = r.rows[0].oid
pgTypes.setTypeParser hstore_oid, hstore.parse