前回
Photoの更新
データベースからPhotoを取得して、更新して保存してみます。
src/index.ts
import { AppDataSource } from "./data-source"; import { Photo } from "./entity/Photo"; AppDataSource.initialize() .then(async () => { console.log("start..."); const photoRepository = AppDataSource.getRepository(Photo); const photo = await photoRepository.findOneBy({ id: 1, }); console.log("before photo: ", photo); photo.name = "updated name"; await photoRepository.save(photo); console.log("after photo: ", photo); }) .catch((error) => { console.log(error); });
実行すると、以下のような出力が出ます。
query: COMMIT start... query: SELECT `Photo`.`id` AS `Photo_id`, `Photo`.`name` AS `Photo_name`, `Photo`.`description` AS `Photo_description`, `Photo`.`filename` AS `Photo_filename`, `Photo`.`views` AS `Photo_views`, `Photo`.`isPublished` AS `Photo_isPublished` FROM `photo` `Photo` WHERE (`Photo`.`id` = ?) LIMIT 1 -- PARAMETERS: [1] before photo: Photo { id: 1, name: 'my photo', description: 'my description', filename: 'photo.jpg', views: 1, isPublished: true } query: SELECT `Photo`.`id` AS `Photo_id`, `Photo`.`name` AS `Photo_name`, `Photo`.`description` AS `Photo_description`, `Photo`.`filename` AS `Photo_filename`, `Photo`.`views` AS `Photo_views`, `Photo`.`isPublished` AS `Photo_isPublished` FROM `photo` `Photo` WHERE `Photo`.`id` IN (?) -- PARAMETERS: [1] query: START TRANSACTION query: UPDATE `photo` SET `name` = ? WHERE `id` IN (?) -- PARAMETERS: ["updated name",1] query: COMMIT after photo: Photo { id: 1, name: 'updated name', description: 'my description', filename: 'photo.jpg', views: 1, isPublished: true }
nameが、updated name に変化しています。
Photoの削除
次に、photoを削除してみましょう。
src/index.ts
import { AppDataSource } from "./data-source"; import { Photo } from "./entity/Photo"; AppDataSource.initialize() .then(async () => { console.log("start..."); const photoRepository = AppDataSource.getRepository(Photo); const photo = await photoRepository.findOneBy({ id: 1, }); console.log("before photo: ", photo); await photoRepository.remove(photo); console.log("after photo: ", photo); }) .catch((error) => { console.log(error); });
実行すると、以下のように出力されます。
start... query: SELECT `Photo`.`id` AS `Photo_id`, `Photo`.`name` AS `Photo_name`, `Photo`.`description` AS `Photo_description`, `Photo`.`filename` AS `Photo_filename`, `Photo`.`views` AS `Photo_views`, `Photo`.`isPublished` AS `Photo_isPublished` FROM `photo` `Photo` WHERE (`Photo`.`id` = ?) LIMIT 1 -- PARAMETERS: [1] before photo: Photo { id: 1, name: 'updated name', description: 'my description', filename: 'photo.jpg', views: 1, isPublished: true } query: SELECT `Photo`.`id` AS `Photo_id`, `Photo`.`name` AS `Photo_name`, `Photo`.`description` AS `Photo_description`, `Photo`.`filename` AS `Photo_filename`, `Photo`.`views` AS `Photo_views`, `Photo`.`isPublished` AS `Photo_isPublished` FROM `photo` `Photo` WHERE `Photo`.`id` IN (?) -- PARAMETERS: [1] query: START TRANSACTION query: DELETE FROM `photo` WHERE `id` = ? -- PARAMETERS: [1] query: COMMIT after photo: Photo { id: undefined, name: 'updated name', description: 'my description', filename: 'photo.jpg', views: 1, isPublished: true }
photoが削除されていることがわかります。
まとめ
ここまでで、
- Photoの更新について
- Photoの削除について
を学びました。
次回はリレーションについてを掘り下げようと思います。
コード
今回のコードは、以下に格納しました。