Sumor Cloud ツール。
さらに詳しいドキュメント
MySQLなどのデータベースコネクタです。エンティティベース。
npm i @sumor/database --save
Node.JS バージョン 16.x 以上が必要です。
このパッケージは ES モジュールで記述されているため、package.jsonファイル内の次のコードを変更してください。
{
  "type": "module"
}
import database from '@sumor/database'
const config = {
  host: 'localhost',
  user: 'root',
  password: 'password',
  database: 'database',
  port: 3306
}
await database.install(config, {
  entity: {
    Car: {
      property: {
        brand: {
          type: 'string',
          length: 100
        },
        model: {
          type: 'string',
          length: 100
        }
      }
    }
  },
  view: {}
})
// コネクションプール付きのクライアントを取得
const client = await database.client(config)
// コネクションを取得
const db = await client.connect()
// 操作ユーザーを設定
db.setUser('tester')
// レコードを作成
const car1Id = await db.insert('Car', {
  brand: 'BMW',
  model: 'X5'
})
const car2Id = await db.insert('Car', {
  brand: 'BMW',
  model: 'X6'
})
// レコードを取得
const car = await db.single('Car', { id: carId })
// car = {id: car1Id, brand: 'BMW', model: 'X5'}
// レコードをクエリ
const cars = await db.query('Car', {
  brand: 'BMW'
})
// cars = [{id: car1Id, brand: 'BMW', model: 'X5'}, {id: car2Id, brand: 'BMW', model: 'X6'}]
// レコードを数える
const count = await db.count('Car', {
  brand: 'BMW'
})
// count = 2
// レコードを更新
await db.update(
  'Car',
  { id: car1Id },
  {
    brand: 'BMW',
    model: 'X5M'
  }
)
// レコードを確保
await db.ensure('Car', ['brand'], {
  brand: 'BMW',
  model: 'X5C'
})
// brand が 'BMW' で既に存在する場合はレコードを挿入しません
// レコードを変更
await db.modify('Car', ['brand'], {
  brand: 'BMW',
  model: 'X5C'
})
// brand が 'BMW' で既に存在する場合はレコードの model を更新します
// レコードを削除
await db.delete('Car', { id: car1Id })
// コネクションを終了
await db.commit()
// ロールバック
await db.rollback()
// コネクションを終了
await db.release()
// サーバーがシャットダウンする場合はクライアントを破棄
await client.destroy()
// オプションを指定してレコードをクエリ
const cars = await db.select(
  'Car',
  {
    brand: 'BMW'
  },
  {
    term: 'X5',
    termRange: ['model'],
    top: 10,
    skip: 0
  }
)