0.18.1 • Published 10 months ago

@technote-space/vo-entity-ts v0.18.1

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

Vo Entity Ts

npm version CI Status codecov CodeFactor License: MIT

Table of Contents

Setup

yarn

  • yarn add @technote-space/vo-entity-ts

npm

  • npm i @technote-space/vo-entity-ts

Author

GitHub (Technote)
Blog

特徴

  • Value Object と Entity の基本クラスを提供
  • 型安全な実装
  • バリデーション機能
  • イミュータブルな値の取り扱い
  • コレクションのサポート

Value Object

Value Object は不変な値オブジェクトを表現するための基本クラスです。以下のような Value Object が提供されています:

  • DateObject: 日付を表現する Value Object
  • Flags: フラグを表現する Value Object
  • Float: 浮動小数点数を表現する Value Object
  • Int: 整数を表現する Value Object
  • ObjectValue: オブジェクトを表現する Value Object
  • Phone: 電話番号を表現する Value Object
  • StringId: 文字列IDを表現する Value Object
  • Text: テキストを表現する Value Object
  • Url: URLを表現する Value Object
  • Email: メールアドレスを表現する Value Object

使用例

import { Text, Email, ObjectValue } from 'vo-entity-ts';

class UserName extends Text {
  protected get symbol() {
    return Symbol();
  }

  protected override getValidationMinLength(): number | undefined {
    return 3;
  }
}

class UserEmail extends Email {
  protected get symbol() {
    return Symbol();
  }
}

// 使用例
const name1 = new UserName('John');
const name2 = new UserName('Jane');
const shortName = new UserName('Jo');

// 比較
name1.equals(name2); // false
name1.equals(new UserName('John')); // true

// 値の取得
name1.value; // 'John'

// バリデーション
name1.getErrors('name'); // undefined
shortName.getErrors('name'); // [{ name: 'name', error: '3文字より長く入力してください' }]

// イミュータブルな値
const name = name1.value;
name = 'New Name'; // エラー: Cannot assign to 'name' because it is a read-only property

その他の使用例

各 Value Object のより詳細な使用例や実装パターンについては、以下のテストファイルを参照してください:

これらのテストファイルには、実際の使用例、エラーハンドリング、エッジケースの処理方法などが含まれています。

Entity

Entity は識別子を持ち、ライフサイクルを通じて同一性を維持するオブジェクトを表現するための基本クラスです。

Entity を実装する際は、_create_reconstruct_update メソッドに対して、実装する Entity の型(例:User)をジェネリクスの型パラメータとして渡すことが重要です:

// 正しい使用法
public static create(...): User {
  return User._create<User>({ ... });
}

public static reconstruct(...): User {
  return User._reconstruct<User>({ ... });
}

public update(...): User {
  return User._update<User>(this, { ... });
}

使用例

import { Entity, Text } from 'vo-entity-ts';

class User extends Entity {
  protected constructor(props: {
    name: UserName;
    email: UserEmail;
    status?: UserStatus;
  }) {
    super(props);
  }

  public static create(name: UserName, email: UserEmail): User {
    return User._create<User>({ name, email });
  }

  public static reconstruct(
    name: UserName,
    email: UserEmail,
    status?: UserStatus,
  ): User {
    return User._reconstruct<User>({ name, email, status });
  }

  public update({ status }: { status?: UserStatus }): User {
    return User._update<User>(this, { status });
  }

  public equals(other: User): boolean {
    return this.get('email').equals(other.get('email'));
  }
}

// 使用例
// 新規作成
const name = new UserName('John Doe');
const email = new UserEmail('john@example.com');
const user = User.create(name, email);

// 再構築(バリデーションをスキップ)
const status = new UserStatus('active');
const reconstructedUser = User.reconstruct(name, email, status);

// 更新
const newStatus = new UserStatus('inactive');
const updatedUser = user.update({ status: newStatus });

// プロパティの取得
user.get('name').value; // 'John Doe'
user.get('email').value; // 'john@example.com'
user.get('status')?.value; // undefined

// 比較
user.equals(updatedUser); // true(email が同じため)
user.equals(User.create(new UserName('Jane Doe'), new UserEmail('jane@example.com'))); // false

// バリデーションエラー
try {
  User.create(
    new UserName('Jo'),
    new UserEmail('invalid-email')
  );
} catch (error) {
  if (error instanceof ValidationException) {
    console.log(error.errors);
    // {
    //   name: ['3文字より長く入力してください'],
    //   email: ['有効なメールアドレスを指定してください']
    // }
  }
}

コレクション

ValueObject のコレクションを扱うための Collection クラスが提供されています。

import { Collection } from 'vo-entity-ts';

class UserEmails extends Collection<UserEmail> {
  // カスタムのコレクション実装
}

ライセンス

MIT

開発

# 依存関係のインストール
npm install

# テストの実行
npm test

# ビルド
npm run build
0.8.5

12 months ago

0.9.3

10 months ago

0.8.4

12 months ago

0.18.1

10 months ago

0.11.0

10 months ago

0.12.0

10 months ago

0.13.0

10 months ago

0.14.0

10 months ago

0.15.0

10 months ago

0.16.0

10 months ago

0.15.1

10 months ago

0.17.0

10 months ago

0.15.2

10 months ago

0.18.0

10 months ago

0.17.1

10 months ago

0.10.0

10 months ago

0.9.0

12 months ago

0.9.2

12 months ago

0.9.1

12 months ago

0.8.3

1 year ago

0.8.1

1 year ago

0.8.0

1 year ago

0.8.2

1 year ago

0.7.0

1 year ago

0.6.7

3 years ago

0.6.6

3 years ago

0.6.5

3 years ago

0.6.4

3 years ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.5.2

4 years ago

0.6.0

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago

0.2.0

4 years ago

0.1.0

4 years ago