Guest User

Untitled

a guest
Jun 21st, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. //user.js file
  2. module.exports = function (sequelize, DateTypes) {
  3.  
  4. return sequelize.define("user", {
  5. email: {
  6. type: DateTypes.STRING,
  7. allowNull: false,
  8. unique: true,
  9. validate: {
  10. isEmail: true
  11. }
  12. },
  13. password: {
  14. type: DateTypes.STRING,
  15. allowNull: false,
  16. validate: {
  17. len: [7, 100]
  18. }
  19. }
  20. });
  21. };
  22.  
  23. var Sequelize = require('sequelize');
  24. var env = process.env.NODE_ENV || "development"; // established if you work in production or in development mode
  25. var sequelize;
  26.  
  27. if (env == "production") {
  28.  
  29. sequelize = new Sequelize(process.env.DATABASE_URL, {
  30. "dialect": "postgres",
  31.  
  32. });
  33. } else {
  34. var sequelize = new Sequelize(undefined, undefined, undefined, {
  35. 'dialect': 'sqlite',
  36. 'storage': __dirname + '/data/dev-todo-api.sqlite' // location where you create a new sqlite database
  37. });
  38. }
  39.  
  40. var db = {};
  41.  
  42. db.todo = sequelize.import(__dirname + "/models/todo.js");
  43. db.user = sequelize.import(__dirname + "/models/user.js");
  44. db.sequelize = sequelize; //contain a settings of database
  45. db.Sequelize = Sequelize;
  46.  
  47. module.exports = db;
  48.  
  49. Sequelize.prototype.import = function(path) {
  50. // is it a relative path?
  51. if(Path.normalize(path) !== Path.resolve(path)){
  52. // make path relative to the caller
  53. var callerFilename = Utils.stack()[1].getFileName()
  54. , callerPath = Path.dirname(callerFilename);
  55.  
  56. path = Path.resolve(callerPath, path);
  57. }
  58.  
  59. if (!this.importCache[path]) {
  60. var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
  61. if (typeof defineCall === 'object' && defineCall.__esModule) {
  62. // Babel/ES6 module compatability
  63. defineCall = defineCall['default'];
  64. }
  65. this.importCache[path] = defineCall(this, DataTypes);
  66. }
  67.  
  68. return this.importCache[path];
  69. };
  70.  
  71. /**
  72. * Imports a model defined in another file
  73. *
  74. * Imported models are cached, so multiple calls to import with the same path will not load the file multiple times
  75. *
  76. * See https://github.com/sequelize/express-example for a short example of how to define your models in separate files so that they can be imported by sequelize.import
  77. * @param {String} path The path to the file that holds the model you want to import. If the part is relative, it will be resolved relatively to the calling file
  78. * @return {Model}
  79. */
  80. import(path) {
  81. // is it a relative path?
  82. if (Path.normalize(path) !== Path.resolve(path)) {
  83. // make path relative to the caller
  84. const callerFilename = Utils.stack()[1].getFileName();
  85. const callerPath = Path.dirname(callerFilename);
  86.  
  87. path = Path.resolve(callerPath, path);
  88. }
  89.  
  90. if (!this.importCache[path]) {
  91. let defineCall = arguments.length > 1 ? arguments[1] : require(path);
  92. if (typeof defineCall === 'object') {
  93. // ES6 module compatibility
  94. defineCall = defineCall.default;
  95. }
  96. this.importCache[path] = defineCall(this, DataTypes);
  97. }
  98.  
  99. return this.importCache[path];
  100. }
Add Comment
Please, Sign In to add comment