メインコンテンツへスキップ
データベースは Square Cloud が提供するマネージドサービスで、アプリケーションのデータを保存・管理できます。サポートされているデータベースには Redis、MongoDB、MySQL、PostgreSQL があります。

データベースの作成

client.create_databaseDatabase オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    # Create a MongoDB database with 1024MB of memory
    database = await client.create_database(
        name='my_database',
        memory=1024,  # Memory in MB
        type='mongo',  # "redis", "mongo", "mysql", or "postgres"
        version='8.0.11'  # Optional - version will be inferred if not provided
    )
    
    print(database.id)               # Database identifier
    print(database.name)             # Database name
    print(database.type)             # Database type
    print(database.cluster)          # Database cluster
    print(database.memory)           # Memory allocated (MB)
    print(database.cpu)              # CPU allocated
    print(database.password)         # Database password
    print(database.connection_url)   # Connection URL
    print(database.certificate)      # TLS certificate

データベース情報の取得

client.get_database_infoDatabaseInfo オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    database_info = await client.get_database_info(database_id='DATABASE_ID')
    
    print(database_info.id)            # Database identifier
    print(database_info.name)          # Database name
    print(database_info.type)          # Database type
    print(database_info.owner)         # Database owner user ID
    print(database_info.cluster)       # Database cluster
    print(database_info.ram)           # RAM usage in MB
    print(database_info.port)          # Database port
    print(database_info.created_at)    # Creation date (ISO 8601)
    print(database_info.created_at_datetime)  # Creation date as datetime object

データベースステータスの取得

client.get_database_statusStatusData オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    status = await client.get_database_status(database_id='DATABASE_ID')
    
    print(status.ram)      # RAM usage
    print(status.cpu)      # CPU usage percentage
    print(status.network)  # Network statistics
    print(status.running)  # Whether database is running
    print(status.storage)  # Storage usage

すべてのデータベースの一覧表示

client.all_databases_statusResumedStatus オブジェクトのリストを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    all_databases = await client.all_databases_status()
    
    for database in all_databases:
        print(f"Database: {database.id}")
        print(f"  CPU: {database.cpu}")
        print(f"  RAM: {database.ram}")
        print(f"  Running: {database.running}")

データベースの起動

client.start_databaseResponse オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.start_database(database_id='DATABASE_ID')
    print(f"Database started: {response.status}")

データベースの停止

client.stop_databaseResponse オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.stop_database(database_id='DATABASE_ID')
    print(f"Database stopped: {response.status}")

データベースの編集

client.edit_databaseResponse オブジェクトを返します。データベース名やメモリ割り当てを更新できます。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    # Update database name
    response = await client.edit_database(
        database_id='DATABASE_ID',
        name='new_database_name'
    )
    
    # Update memory allocation
    response = await client.edit_database(
        database_id='DATABASE_ID',
        memory=2048  # New memory in MB
    )
    
    # Update both name and memory
    response = await client.edit_database(
        database_id='DATABASE_ID',
        name='new_name',
        memory=2048
    )
    
    print(f"Database updated: {response.status}")

データベースの削除

client.delete_databaseResponse オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.delete_database(database_id='DATABASE_ID')
    print(f"Database deleted: {response.status}")

データベース認証情報の管理

データベース証明書の取得

client.get_database_certificateCertificate オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    certificate = await client.get_database_certificate(database_id='DATABASE_ID')
    
    # Save certificate to file
    certificate.save()  # Saves as 'certificate.pem' by default
    
    # Save certificate with custom filename
    certificate.save(filename='my_cert', export_to='cert')
    
    # Save certificate to specific directory
    certificate.save(dir='./certs', filename='my_cert', export_to='cert')

データベースパスワードのリセット

client.reset_database_password は新しいパスワード文字列を返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    new_password = await client.reset_database_password(database_id='DATABASE_ID')
    print(f"New password: {new_password}")

データベース証明書のリセット

client.reset_database_certificateResponse オブジェクトを返します。
import squarecloud as square

client = square.Client(api_key='API_KEY')

async def example():
    response = await client.reset_database_certificate(database_id='DATABASE_ID')
    print(f"Certificate reset: {response.status}")

データベースのデータ構造

Database

Database オブジェクトは、新しく作成されたデータベースの完全な詳細を表します。
プロパティ説明
idstrデータベースの一意な識別子
namestrデータベース名
typestrデータベースタイプ (redis, mongo, mysql, postgres)
clusterstrデータベースクラスタ情報
memoryint割り当てられたメモリ (MB)
cpuint割り当てられた CPU (vCPU)
passwordstrデータベースパスワード
certificateCertificateセキュア接続用の TLS 証明書
connection_urlstrデータベースの接続 URL

DatabaseInfo

DatabaseInfo オブジェクトは、既存のデータベースに関する情報を含みます。
プロパティ説明
idstrデータベースの一意な識別子
namestrデータベース名
typestrデータベースタイプ (redis, mongo, mysql, postgres)
clusterstrデータベースクラスタ情報
ownerstrデータベース所有者のユーザー ID
portintデータベースポート
ramint現在の RAM 使用量 (MB)
created_atstr作成タイムスタンプ (ISO 8601)

StatusData

StatusData オブジェクトは、データベースに関するリアルタイムのステータス情報を含みます。
プロパティ説明
ramstr現在の RAM 使用量
cpustr現在の CPU 使用率
networkdictネットワーク統計 (アップロード/ダウンロード)
runningboolデータベースが稼働中かどうか
storagestrストレージ使用量

サポートされているデータベースタイプ

Square Cloud は以下のデータベースをサポートしています。
タイプデフォルトバージョン説明
redis7.4.5キャッシュやリアルタイムアプリケーション向けのインメモリデータストア
mongo8.0.11NoSQL ドキュメントデータベース
mysql9.5リレーショナルデータベース
postgres17.6高度なリレーショナルデータベース