2
0

More than 3 years have passed since last update.

Node.js BookshelfでCRUD処理

Last updated at Posted at 2019-09-21

はじめに

UpdateとDeleteに関して情報が少ないように感じたので、
自分が使った基本処理の記述をまとめておく。

Create

example.js
const rec = {
//カラム名:保存するデータ
    colmn1: req.body.data1,
    colmn2: req.body.data2
}
new Example(rec).save().then((result) => {
//保存後の処理を記述
});

Read

example.js
new Example().where('検索対象のカラム','演算子','検索値')
  .fetch()
  .then((result) => {
  //保存後の処理を記述
  });

Update

{patch:true}は設定したカラムの更新を許可する設定。

example.js
new Example().where('検索対象のカラム','演算子','検索値')
  .save({更新するカラム: },{patch:true})
  .then((result) => {
     //更新後の処理を記述
  });

Delete

取得したレコードのメゾットを使用する。

example.js
new Example().where('検索対象のカラム','演算子','検索値')
  .fetch()
  .then((record)=>{
     record.destroy();
  })
  .then((result) => {
     //削除後の処理を記述
  });

参考

https://github.com/bookshelf/bookshelf
https://arjunphp.com/bookshelf-js-deleting-row-related-rows-many-many-relationship/

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0