ポイント操作について

完了

Microsoft.Azure.Cosmos ライブラリには、C# 言語のジェネリックに対するファースト クラスのサポートが含まれており、コンテナー内で何を操作しているかを表す方法を開発者が考えるのに不可欠です。

最も基本的なレベルでは、コンテナー内の項目を表す C# クラスを作成できます。このクラスには、少なくとも 2 つのメンバーが含まれます。

  • パブリック ゲッターとセッターを持つ id という名前の文字列プロパティ
  • パブリックなゲッターおよびセッターを持つパーティション キー パスと同じ名前の文字列プロパティ
public class item
{
    public string id { get; set; }

    public string partitionKey { get; set; }
}

他の型の他のメンバーの豊富なコレクションを含めることができます。 他のクラスなど、異なる複合型の他のメンバーを持つこともできます。

public decimal money { get; set;}

public bool boolean { get; set; }

public string[] set { get; set; }

public double numbers { get; set; }

public int moreNumbers  { get; set; }

public ComplexClass sophisticated { get; set;}

public List<ComplexType> oneToMany  { get; set; }

このモジュールの残りの部分に対して架空のシナリオを確立してみましょう。 一意の ID、製品の名前、一意のカテゴリ識別子、価格タグのコレクションの 5 つのメンバーを持つ Product クラスがあります。 カテゴリ識別子は、コンテナーのパーティション キー パスです。

public class Product
{
    public string id { get; set; }

    public string name { get; set; }

    public string categoryId { get; set; }

    public double price { get; set; }

    public string[] tags { get; set; }
}

この実装は、開発者がすぐに選択して使用できる非常に汎用性のある C# クラスです。 何らかの理由で、ビジネス ニーズに合わせて、プロパティの名前を変更する必要があるとします。 そのような場合は、プロパティ属性を使用して、C# コードで使用するプロパティの名前と、JSON および実質的に Azure Cosmos DB for NoSQL で使用するプロパティの名前の関連付けを解除することができます。 この例では、C# コードで InternalId という名前を使用し、JSON と Azure Cosmos DB for NoSQL の識別子 ID を 引き続き使用できます。

[JsonProperty(PropertyName = "id")]
public string InternalId { get; set; }

ヒント

変更できない C# メンバー名を含んだ既存アプリケーションをお持ちの場合、プロパティ属性は、既存コードに大幅な変更を加えるリスクや技術的負債を抱えるリスクなしで型の再利用を実現する手段になります。

azure-cosmos ライブラリは、Azure Cosmos DB コンテナー内の項目のモデリングと操作に対する堅牢なサポートを提供します。 Python 開発者は、データベースとアプリケーション ユース ケース要件に合わせて項目定義を慎重に構成することが重要です。

少なくとも、コンテナー内の各項目に以下 2 つの属性を含めることが必須です。

  • idという名前のこの文字列属性は、項目の一意識別子として機能します。
  • コンテナーの パーティション キー パスに対応する文字列属性。

最小限の項目構造を表す Python クラスの例を以下に示します。

class Item:
    def __init__(self, id: str, partition_key: str):
        self.id = id
        self.partition_key = partition_key

このクラスは、さまざまな型 (例: 文字列や数値などの基本型、入れ子になったオブジェクトやリストなどの複合型) の属性をさらに追加することで内容を拡充することもできます。

class Item:
    def __init__(
        self,
        id: str,
        partition_key: str,
        money: float,
        boolean: bool,
        tags: list[str],
        numbers: float,
        more_numbers: int,
        sophisticated: dict,
        one_to_many: list[dict]
    ):
        self.id = id
        self.partition_key = partition_key
        self.money = money
        self.boolean = boolean
        self.tags = tags
        self.numbers = numbers
        self.more_numbers = more_numbers
        self.sophisticated = sophisticated
        self.one_to_many = one_to_many

シナリオ例: 製品のモデリング

このモジュールの残りの部分は、架空のシナリオに基づいて進めることにします。 在庫内の製品を表す Product クラスを作成する必要があります。 各製品には以下のものが含まれます。

  • 一意の ID
  • 製品の 名前
  • パーティション キーとして「categoryId」。
  • 製品の 価格
  • 分類の タグ の一覧。

以下は、Python で実装したクラスの例です。

class Product:
    def __init__(self, id: str, name: str, category_id: str, price: float, tags: list[str]):
        self.id = id
        self.name = name
        self.category_id = category_id  # Partition key
        self.price = price
        self.tags = tags

この実装は柔軟性が高く、アプリケーション内のさまざまな操作に簡単に使用できます。 属性の名前を、何らかの理由でビジネス上の要件に合わせて調整する必要がある場合は、シリアル化手法を使用して、Python の属性名を JSON のプロパティ名にマップできます。

たとえば、JSON プロパティ名として id を引き続き使用しながら、Python コードで名前internal_idを使用するとします。

class Product:
    def __init__(self, internal_id: str, name: str, category_id: str, price: float, tags: list[str]):
        self.internal_id = internal_id
        self.name = name
        self.category_id = category_id  # Partition key
        self.price = price
        self.tags = tags

    def to_dict(self):
        return {
            "id": self.internal_id,  # Map internal_id to id
            "name": self.name,
            "categoryId": self.category_id,
            "price": self.price,
            "tags": self.tags,
        }

変更できない既存の Python クラスを使用している場合は、 to_dict() などのヘルパー メソッドを使用して、Azure Cosmos DB for NoSQL で想定される形式にオブジェクトを変換することを検討してください。 これにより、技術的負債を避けながら既存の型を再利用できます。

これらの基本的なモデリング手法により、Python アプリケーションで Azure Cosmos DB for NoSQL とのやり取りを効果的に行うことができます。

@azure/cosmos ライブラリは、Azure Cosmos DB コンテナー内の項目のモデリングと操作を堅牢にサポートします。 JavaScript 開発者は、データベースとアプリケーション ユース ケース要件に合わせて項目定義を慎重に構成することが重要です。

少なくとも、コンテナー内の各項目に以下 2 つのプロパティを含めることが必須です。

  • id という名前の文字列プロパティは、アイテムの一意識別子として機能します。
  • コンテナーの パーティション キー パスに対応する文字列プロパティ。

最小限の項目構造を表す JavaScript クラスの例を以下に示します。

class Item {
    constructor(id, partitionKey) {
        this.id = id;
        this.partitionKey = partitionKey;
    }
}

このクラスは、さまざまな型 (例: 文字列や数値などの基本型、入れ子になったオブジェクトや配列などの複合型) のプロパティをさらに追加することで内容を拡充することもできます。

class Item {
    constructor(id, partitionKey, money, boolean, tags, numbers, moreNumbers, sophisticated, oneToMany) {
        this.id = id;
        this.partitionKey = partitionKey;
        this.money = money;
        this.boolean = boolean;
        this.tags = tags;
        this.numbers = numbers;
        this.moreNumbers = moreNumbers;
        this.sophisticated = sophisticated;
        this.oneToMany = oneToMany;
    }
}

シナリオ例: 製品のモデリング

このモジュールの残りの部分は、架空のシナリオに基づいて進めることにします。 在庫内の製品を表す Product クラスを作成する必要があります。 各製品には以下のものが含まれます。

  • 一意の ID
  • 製品の 名前
  • パーティション キーとして「categoryId」。
  • 製品の 価格
  • 分類用の タグ の配列。

以下は、JavaScript で実装したクラスの例です。

class Product {
    constructor(id, name, categoryId, price, tags) {
        this.id = id;
        this.name = name;
        this.categoryId = categoryId; // Partition key
        this.price = price;
        this.tags = tags;
    }
}

この実装は柔軟性が高く、アプリケーション内のさまざまな操作に簡単に使用できます。 プロパティの名前を、何らかの理由でビジネス上の要件に合わせて調整する必要がある場合は、シリアル化手法を使用して、JavaScript のプロパティ名を JSON のプロパティ名にマップできます。

たとえば、JSON プロパティ名として ID を使用したまま、JavaScript コードで internalId という名前を使用するとします。

class Product {
    constructor(internalId, name, categoryId, price, tags) {
        this.internalId = internalId;
        this.name = name;
        this.categoryId = categoryId; // Partition key
        this.price = price;
        this.tags = tags;
    }

    toJSON() {
        return {
            id: this.internalId, // Map internalId to id
            name: this.name,
            categoryId: this.categoryId,
            price: this.price,
            tags: this.tags,
        };
    }
}

変更できない既存の JavaScript クラスを使用している場合は、 toJSON() などのメソッドを使用して、Azure Cosmos DB for NoSQL で想定される形式にオブジェクトを変換することを検討してください。 これにより、技術的負債を避けながら既存の型を再利用できます。

これらの基本的なモデリング手法により、JavaScript アプリケーションで Azure Cosmos DB for NoSQL とのやり取りを効果的に行うことができます。