connector-manager.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. var sqlite3
  2. , Utils = require("../../utils")
  3. , Query = require("./query")
  4. module.exports = (function() {
  5. var ConnectorManager = function(sequelize, config) {
  6. this.sequelize = sequelize
  7. this.config = config
  8. if (config.dialectModulePath) {
  9. sqlite3 = require(config.dialectModulePath).verbose()
  10. } else {
  11. sqlite3 = require('sqlite3').verbose()
  12. }
  13. }
  14. Utils._.extend(ConnectorManager.prototype, require("../connector-manager").prototype)
  15. ConnectorManager.prototype.connect = function() {
  16. var emitter = new (require('events').EventEmitter)()
  17. , self = this
  18. , db
  19. this.database = db = new sqlite3.Database(self.sequelize.options.storage || ':memory:', function(err) {
  20. if (err) {
  21. if (err.code === "SQLITE_CANTOPEN") {
  22. emitter.emit('error', 'Failed to find SQL server. Please double check your settings.')
  23. }
  24. }
  25. if(!err && self.sequelize.options.foreignKeys !== false) {
  26. // Make it possible to define and use foreign key constraints unless
  27. // explicitly disallowed. It's still opt-in per relation
  28. db.run('PRAGMA FOREIGN_KEYS=ON')
  29. }
  30. })
  31. }
  32. ConnectorManager.prototype.query = function(sql, callee, options) {
  33. if (!this.database) {
  34. this.connect()
  35. }
  36. return new Query(this.database, this.sequelize, callee, options).run(sql)
  37. }
  38. return ConnectorManager
  39. })()