2.1.8 • Published 3 months ago

@team-decorate/alcts v2.1.8

Weekly downloads
1
License
MIT
Repository
github
Last release
3 months ago

alcts

Installation

With yarn:

yarn add @team-decorate/alcts

Usage

Model create

import {Model, ArrayMappable, Relation} from '@team-decorate/alcts'
import Post from './models/Post'
import Comment from './models/Comment'
import ParentGroup from './models/ParentGroup'

/*
 * Only those added to fillable will be sent
 */
const FILLABLE = ['id', 'name', 'email', 'password', 'type']

class User extends Model {
  id: number = 0
  name: string = ''
  email: string = ''
  password: string = ''
  type: number = 0
  posts: Array<Post> = []
  userComments: Array<Comment> = []
  parentGroup = new Relation(ParentGroup)

  constructor(data: object = {}) {
    super()
    this.fillable = FILLABLE
    //presents is send even if the field is empty
    this.presents = ['type']

    this.arrayMap(
      new ArrayMappable(Post),
      new ArrayMappable(Comment).bind('userComments')
    )

    this.data = data
  }
}

class ParentGroup extends Model {
  user = new Relation(User)
}

How to use

#user api json response
{
  "id": 1,
  "name": "test-user",
  "email": "test@mail.com",
  "type": 1,
  "posts": [
    {"id": 1, "text": "test post 1"},
    {"id": 2, "text": "test post 2"}
  ],
  "user_comments": [
    {"id": 1, "text": "test comment 1"},
    {"id": 2, "text": "test comment 2"}
  ],
  "parent_group": {
    "id": 1,
    "name": "Test Group",
    "user": {
      "id": 2,
      "name": "test2-user"
    }
  }
}
export default {
  methods: {
    async get() {
      // 方法1: コンストラクタでデータを渡す
      const {data} = await axios.get('/api/user')
      const user = new User(data)

      // 方法2: 後からデータを設定
      const user2 = new User()
      user2.data = data

      // リレーションの取得
      const parentGroup = user.parentGroup.get()
      console.log(parentGroup instanceof ParentGroup) // true
      console.log(parentGroup.id) // 1
      console.log(parentGroup.name) // "Test Group"

      // 深い循環参照のパターン
      const parentGroupUser = parentGroup.user.get()
      console.log(parentGroupUser instanceof User) // true
      console.log(parentGroupUser.id) // 2
      console.log(parentGroupUser.name) // "test2-user"

      // リレーションの設定
      const newParentGroup = new ParentGroup({id: 2, name: 'New Group'})
      user.parentGroup.set(newParentGroup)

      // 配列の処理
      user.posts.forEach(post => {
        console.log(post instanceof Post) // true
        console.log(post.text)
      })

      user.userComments.forEach(comment => {
        console.log(comment instanceof Comment) // true
      })
    },

    async post() {
      /*
       * What is added to fillable and contains value is sent
       */
      const {data} = await this.user.post('/api/user')
      this.user.update(data)
    },
  },
}

Relation Class

Relationクラスは、モデル間の関連を管理するためのユーティリティクラスです。

メソッド

メソッド引数戻り値説明
get-T関連するモデルのインスタンスを取得。初回アクセス時に初期化されます。
setT | anyvoid関連するモデルのインスタンスを設定します。

特徴

  • 遅延初期化による循環参照の防止
  • 型安全性の確保
  • シンプルで直感的な API
  • 深い循環参照のパターンにも対応
  • コンストラクタでのデータ渡しに対応

Overridable Property

methodsvaluedescription
beforePostablenullCalled before sending api
afterPostableresCalled after sending api

Model Methods

methodsargsoutput
getPostablenullObject
updateObjectnull
2.1.2

3 months ago

2.0.3

4 months ago

2.1.1

3 months ago

2.1.4

3 months ago

2.1.3

3 months ago

2.1.6

3 months ago

2.1.5

3 months ago

2.1.8

3 months ago

2.1.7

3 months ago

2.1.0

3 months ago

2.0.2

4 months ago

2.0.1

4 months ago

2.0.0

4 months ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.2

2 years ago

1.5.1

2 years ago

1.5.0

2 years ago

1.4.1

3 years ago

1.4.0

3 years ago

1.3.7

3 years ago

1.3.6

3 years ago

1.3.5

3 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.1

5 years ago

1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.10

5 years ago

1.0.0

5 years ago

0.0.3

5 years ago

0.0.4

5 years ago

0.0.2

5 years ago