Sequelize - 自定义创建方法

时间:2017-02-12 00:39:02

标签: node.js express sequelize.js

是否可以在create中创建自定义Sequelize方法。我希望它可以传入一个URL来下载缩略图照片,然后调用该数据下载照片的方法,将其上传到S3,并将该S3 URL保存为thumbnailPhotoURL。

以下是我尝试执行的语法示例:

var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password');

var User = sequelize.define('user', {
  username: Sequelize.STRING,
  birthday: Sequelize.DATE,
  thumbnailPhotoURL: Sequelize.STRING
});

sequelize.sync().then(function() {
  return User.create({
    username: 'janedoe',
    birthday: new Date(1980, 6, 20),
    // this will be used to download and upload the thumbnailPhoto to S3
    urlToDownloadThumbnailPhotoFrom: 'http://example.com/test.png'
  });
}).then(function(jane) {
  console.log(jane.get({
    plain: true
  }));
});

请注意我如何使用User.create参数调用urlToDownloadThumbnailPhotoFrom,而不是thumbnailPhotoURL参数

1 个答案:

答案 0 :(得分:2)

您可以在create hook之前使用,无需定义自定义创建函数

<!doctype html>
<html>
<head>
<link href="css/Layout.css" rel="stylesheet" type="text/css" />
<link href="css/Menu.css" rel="stylesheet" type="text/css" />
<meta charset="utf-8">
<title>Student Welcome</title>

<link rel="stylesheet" href="themes/bar/bar.css" type="text/css" media= "screen" /> 
    <link rel="stylesheet" href="nivo-slider.css" type= "text/css" media="screen" />
    <script src="jquery.min.js" ></script> 
    <script type="text/javascript" src="jquery.nivo.slider.js" ></script>
</head>

<body>
<div id="Holder">
    <div id="Header">
        <a href="Home.php"><img src="assets/Logo.png"></a>
    </div>
    <div id="NavBar">
        <nav>
        <ul>
            <li><a href="Home.php">Home</a></li>
            <li><a href="Login.php">Student Portal</a>
            <ul>
                <li><a href="Login.php">Login</a></li>
                <li><a href="Candidate Profile.php">Candidates Profile</a></li>
            </ul>
            </li>
            <li><a href="Admin.php">Admin</a></li>
            <li><a href="About Site.php">About Us</a>
            <ul>
                <li><a href="About Site.php">About Site</a></li>
                <li><a href="Contact Us.php">Contact Us</a></li>
            </ul>
            </li>
        </ul>
    </nav>
    </div>
    <div id="Content">
        <div id="PageHeading">
      <h1>&nbsp;</h1>
      <h1>&nbsp;</h1>
      <h1>&nbsp;</h1>
      <h1>Welcome, <?php echo $row_Voters_Account_Form['Fname']; ?> <?php echo $row_Voters_Account_Form['Mname']; ?> <?php echo $row_Voters_Account_Form['Lname']; ?>!</h1>
      </div>
    <div id="ContentLeft">
      <h2>Your Message Here</h2></b>
      <h6>This site is to improve the style of voting in West End University College and other school at large, if it is implementted. </h6>
      <h6>&nbsp;</h6>
      <h6>Note: Please be advice that once you have placed your  vote, you won't be allowed to login again. so there for you can only view the result from of the election from the Hoe page.</h6>
    </div>
    <div id="ContentRight">
      <form id="form1" name="form1" method="post">
      </form>
      <table width="400" border="0" align="center">
        <tbody>
          <tr>
            <td>
                <div id="slider" class="nivoSlider">
            <img src="slider/IMG-20151117-WA00011234.jpg" name="imageField" data-thumb= "slider/IMG-20151117-WA00011234.jpg" alt="" title="These are the candidates for the election"/>
            <img src="slider/FB_IMG_1484460719710123.jpg" data-thumb="slider/FB_IMG_1484460719710123.jpg" alt ="" title="These are the candidates for the election" />
            <img src="slider/FB_IMG_14845217848411234567.jpg" data-thumb= "slider/FB_IMG_14845217848411234567.jpg" alt="" title="These are the candidates for the election"/>
            <img src="slider/IMAG002012345678.jpg" data-thumb= "slider/IMAG002012345678.jpg" alt="" title="These are the candidates for the election"/>
            <img src="slider/FB_IMG_1484461298331123456.jpg" data-thumb= "slider/FB_IMG_1484461298331123456.jpg" alt="" data-transition="slideInRight" title="These are the candidates for the election"/>
            <img src="slider/FB_IMG_148446110711012345.jpg" data-thumb="slider/FB_IMG_148446110711012345.jpg" alt="" title="#htmlcaption" />
        </div>

        <script type="text/javascript">
     $(window).load(function() { 
     $('#slider').nivoSlider({ 
     effect:'random', 
     slices: 15, 
     boxCols: 8, 
     boxRows: 4, 
     animSpeed: 500, 
     pauseTime: 3000,
     startSlide: 0, 
     directionNav:true, 
     controlNav:true, 
     controlNavThumbs:false, 
     pauseOnHover:true, 
     manualAdvance:false, 
     prevText:'Prev', 
     nextText:'Next', 
     randomStart:false, 
     beforeChange:function(){}, 
     afterChange:function(){}, 
     slideshowEnd:function(){}, 
     lastSlide:function(){}, 
     afterLoad:function(){} 
     }); 
     }); 
    </script>
            </td>
          </tr>
        <table width="0" border="0" align="center">
          <tr>
            <td>
                <input type="button" name="button" id="button" value="Click here to cast your vote" onClick="window.location.href='Voting.php' ">
            </td>
          </tr>
        </table>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
          <tr>
            <td>&nbsp;</td>
          </tr>
        </tbody>
      </table>
    </div>
    </div>
    <div id="Footer"></div>
</div>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Voters_Account_Form);
?>