http://liu-xin.me/2017/03/24/%E8%AE%A9%E5%86%99%E5%85%A5%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A%84%E6%95%B0%E6%8D%AE%E8%87%AA%E5%8A%A8%E5%86%99%E5%85%A5%E7%BC%93%E5%AD%98/```
const to = require(‘await-to-js’).default;
const Redis = require(‘ioredis’);
const redis = new Redis(ooxxooxx);
function Cache() {}
Cache.get = async function(key) {
const [err, res] = await to(redis.get(key));
if (err) {console.error(‘REDIS::Cache::get]’, err); return null;}
return JSON.parse(res);
};
Cache.set = async function(key, value) {
value = JSON.stringify(value);
const [err] = await to(redis.set(key, value));
if (err) {console.error(‘REDIS::Cache::set]’, err); }
};
//
// sequelize.query
//
async function CacheSequelizeQuery(sequelize, sql, params, redisKey) {
const cacheData = await Cache.
const arr = {}; await Promise.all( UsersQuery.map(async function (data) { const city = await db.sequelize.query(` select * from city `, type: db.sequelize.QueryTypes.SELECT }); arr[data.user_id] = city[0].name; }) } UsersQuery.forEach(async function (data, index) { this[index].name = arr[data.user_id]; }, UsersQuery);
# Sequelize 基本認識
## 1. Timestamps
https://sequelize.org/v5/manual/models-definition.html#timestamps
## 2. Database synchronization
https://sequelize.org/v5/manual/models-definition.html#database-synchronization
建議不要直接使用於正式環境,應該在測試建立後,取得對應 sql 碼後,在正式上線時,手動更新正式 DB 資料結構
**2.1** 使用 sync 建立的 table name 會加上 s
**2.2** 正常情況下,對 table 操作盡可能還是已手動為主,雖然 Sequelize 有提供一些操作,但減少使用比較安全,當手動操作完畢後,應該把 raw sql 匯出備份,正式上線時,再手動更新
## 3. Modeling a table 建立
https://sequelize.org/v5/manual/getting-started.html
```
const Model = Sequelize.Model;
class User extends Model {}
User.init({
```
建議使用
```
sequelize.define:‘user’, {
// attributes
firstName: {
```
原因,看起來簡單多了
3.1 Model 操作
https://sequelize.org/v5/manual/models-usage.html
## 4. Raw queries