サンプルデータをmigrationしてみる
昨日作ったDBにサンプルデータをmigrationしてみました。(Node.jsにて)
昨日作ったDBとはこちらです → 昨日の記事
migration.js と models.js の内容
migration.js
import { sequelize, Diary } from "./models.js";
import * as data from "./sample-data.js";
await sequelize.sync({ force: true });
for (const { title, content } of data.diary) {
await Diary.create({ title, content });
}
models.js
import Sequelize from "sequelize";
const { DataTypes } = Sequelize;
const url =
process.env.DATABASE_URL ||
"postgres://postgres:xxxxxxxx@localhost:5432/mydb_iwashi";
export const sequelize = new Sequelize(url);
export const Diary = sequelize.define(
"diary",
{
title: {
type: DataTypes.STRING,
allowNull: false,
},
content: {
type: DataTypes.TEXT,
},
},
{ underscored: true }
);
xxxxxxxx の部分は、設定しているパスワードです。
sample-data.js の内容
sample-data.js
export const diary = [
{
id: 1,
title: "postgeSQLを使ってみます(1)",
content:'postgeSQLから表示されています。a',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 2,
title: "postgeSQLを使ってみます(2)",
content:'postgeSQLから表示されています。b',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 3,
title: "postgeSQLを使ってみます(3)",
content:'postgeSQLから表示されています。c',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 4,
title: "postgeSQLを使ってみます(4)",
content:'postgeSQLから表示されています。d',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 5,
title: "postgeSQLを使ってみます(5)",
content:'postgeSQLから表示されています。e',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 6,
title: "postgeSQLを使ってみます(6)",
content:'postgeSQLから表示されています。f',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 7,
title: "postgeSQLを使ってみます(7)",
content:'postgeSQLから表示されています。g',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 8,
title: "postgeSQLを使ってみます(8)",
content:'postgeSQLから表示されています。h',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 9,
title: "postgeSQLを使ってみます(9)",
content:'postgeSQLから表示されています。i',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
{
id: 10,
title: "postgeSQLを使ってみます(10)",
content:'postgeSQLから表示されています。j',
createdAt: "2021-01-31T00:00:00.000Z",
updatedAt: "2021-01-31T00:00:00.000Z",
},
];
node migration.js 実行
そして、node migration.js を実行しました。
$ node migration.js
index.js の内容
ちなみに、index.js は以下のように記述しています。
index.js
import express from "express";
import cors from "cors";
import { Diary } from "./models.js";
const app = express();
app.use(cors());
app.use(express.json());
app.get("/diaries", async (req, res) => {
const limit = +req.query.limit || 5;
const offset = +req.query.offset || 0;
const diaries = await Diary.findAndCountAll({
limit,
offset,
});
res.json({diaries})
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
})
ブラウザでの表示
ブラウザでの表示は下の図のようになりました。
