Guest User

Untitled

a guest
Oct 27th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 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. sequelize = new Sequelize(process.env.DATABASE_URL, {
  29. "dialect": "postgres",
  30.  
  31. });
  32. } else {
  33. var sequelize = new Sequelize(undefined, undefined, undefined, {
  34. 'dialect': 'sqlite',
  35. 'storage': __dirname + '/data/dev-todo-api.sqlite' // location where you create a new sqlite database
  36. });
  37. }
  38.  
  39. var db = {};
  40.  
  41. db.todo = sequelize.import(__dirname + "/models/todo.js");
  42. db.user = sequelize.import(__dirname + "/models/user.js");
  43. db.sequelize = sequelize; //contain a settings of database
  44. db.Sequelize = Sequelize;
  45.  
  46. module.exports = db;
  47.  
  48. Sequelize.prototype.import = function(path) {
  49. // is it a relative path?
  50. if(Path.normalize(path) !== Path.resolve(path)){
  51. // make path relative to the caller
  52. var callerFilename = Utils.stack()[1].getFileName()
  53. , callerPath = Path.dirname(callerFilename);
  54.  
  55. path = Path.resolve(callerPath, path);
  56. }
  57.  
  58. if (!this.importCache[path]) {
  59. var defineCall = (arguments.length > 1 ? arguments[1] : require(path));
  60. if (typeof defineCall === 'object' && defineCall.__esModule) {
  61. // Babel/ES6 module compatability
  62. defineCall = defineCall['default'];
  63. }
  64. this.importCache[path] = defineCall(this, DataTypes);
  65. }
  66.  
  67. return this.importCache[path];
  68. };
  69.  
  70. /**
  71. * Imports a model defined in another file
  72. *
  73. * Imported models are cached, so multiple calls to import with the same path will not load the file multiple times
  74. *
  75. * 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
  76. * @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
  77. * @return {Model}
  78. */
  79. import(path) {
  80. // is it a relative path?
  81. if (Path.normalize(path) !== Path.resolve(path)) {
  82. // make path relative to the caller
  83. const callerFilename = Utils.stack()[1].getFileName();
  84. const callerPath = Path.dirname(callerFilename);
  85.  
  86. path = Path.resolve(callerPath, path);
  87. }
  88.  
  89. if (!this.importCache[path]) {
  90. let defineCall = arguments.length > 1 ? arguments[1] : require(path);
  91. if (typeof defineCall === 'object') {
  92. // ES6 module compatibility
  93. defineCall = defineCall.default;
  94. }
  95. this.importCache[path] = defineCall(this, DataTypes);
  96. }
  97.  
  98. return this.importCache[path];
  99. }
Add Comment
Please, Sign In to add comment