UNPKG

3.72 kBJavaScriptView Raw
1// Generated by CoffeeScript 1.4.0
2var Engine, anyDB, dialects, fakeEngine, queries, url;
3
4url = require('url');
5
6anyDB = require('any-db');
7
8queries = require('./queries');
9
10dialects = require('./dialects');
11
12module.exports = function() {
13 return Engine.create.apply(Engine, arguments);
14};
15
16Engine = (function() {
17 /*
18 ``Engine`` is gesundheits interface to an actual database.
19
20 Engines have all of the :ref:`query factory functions <query-factories>`
21 attached to them as instance methods that automatically bind created queries
22 to the engine. They also have these additionaly methods
23 */
24
25 Engine.create = function(dbUrl, poolOptions) {
26 /*
27 Create an :class:`engine::Engine` instance from an Any-DB_ connect string
28 and extra connection pool options, this is exported by gesundheit as
29 ``gesundheit.engine(...)``.
30
31 :ref:`This example <engine-usage-example>` shows the most common way to set up
32 a single default database engine for an application.
33
34 .. _Any-DB: https://github.com/grncdr/node-any-db
35 .. _Any-DB ConnectionPool: https://github.com/grncdr/node-any-db/blob/master/DESIGN.md#connectionpool
36 */
37
38 var ctor, dialect, driverName, pool;
39 driverName = url.parse(dbUrl).protocol.replace(':', '').split('+').shift();
40 if (driverName === 'fake') {
41 return fakeEngine();
42 }
43 ctor = dialects[driverName];
44 if (!(ctor != null)) {
45 console.error('no such driver', driverName);
46 }
47 dialect = new ctor;
48 pool = anyDB.createPool(dbUrl, poolOptions);
49 return new Engine(driverName, dbUrl, pool, dialect);
50 };
51
52 function Engine(driver, url, pool, dialect) {
53 this.driver = driver;
54 this.url = url;
55 this.pool = pool;
56 this.dialect = dialect;
57 queries.mixinFactoryMethods(this);
58 }
59
60 Engine.prototype.query = function(statement, params, callback) {
61 /*
62 Passes arguments directly to the query method of the underlying `Any-DB
63 ConnectionPool`_
64 */
65
66 var _ref;
67 return (_ref = this.pool).query.apply(_ref, arguments);
68 };
69
70 Engine.prototype.begin = function(callback) {
71 /*
72 Start a new transaction and return it.
73
74 The returned object behaves exactly like a new engine, but has ``commit``
75 and ``rollback`` methods instead of ``close``. (In fact it's an `Any-DB
76 Transaction`_ that has had the query factory functions mixed in to it).
77
78 .. _Any-DB Transaction: https://github.com/grncdr/node-any-db#transaction
79 */
80
81 var tx;
82 tx = queries.mixinFactoryMethods(this.pool.begin(callback));
83 tx.engine = this;
84 tx.render = this.dialect.render.bind(this.dialect);
85 return tx;
86 };
87
88 Engine.prototype.render = function(root) {
89 /*
90 Render an AST to a SQL string
91 */
92 return this.dialect.render(root);
93 };
94
95 Engine.prototype.close = function() {
96 /*
97 Closes the internal connection pool.
98 */
99 return this.pool.close();
100 };
101
102 return Engine;
103
104})();
105
106fakeEngine = function() {
107 /*
108 Create a no-op engine that simply returns the compiled SQL and parameter
109 array to the result callback. This will be the default until you over-ride
110 with ``gesundheit.defaultEngine = myAppEngine``.
111 */
112
113 var dialect, engine;
114 dialect = new dialects.base;
115 engine = {
116 render: dialect.render.bind(dialect),
117 begin: function(cb) {
118 if (cb) {
119 process.nextTick(cb.bind(null, engine));
120 }
121 return engine;
122 },
123 query: function(sql, params, cb) {
124 throw new Error("Cannot query with fakeEngine. Do `gesundheit.defaultEngine = gesundheit.engine(url)` before querying");
125 },
126 close: function() {}
127 };
128 return engine;
129};