# TS SDK v6.0.0 Migration Guide

## Installation

#### Using NPM

`npm i @amityco/ts-sdk@^6.0.0`

#### Using Yarn

`yarn add @amityco/ts-sdk@^6.0.0`

### Deprecation of RunQuery / CreateQuery

Starting from `6.0.0` we recommend using API's `async`hronously. As it will soon be deprecated. Example:

```typescript
// before
import { updatePost, runQuery, createQuery } from '@amityco/ts-sdk';

const query = createQuery(updatePost, 'postId', { ...updatedPost });

runQuery(query, callback);

// after v6.0.0
import { PostRepository } from '@amityco/ts-sdk'

const updatedPost = await PostRepository.updatePost('postId', { ...updatedPost });
```

### Real Time Events

The real time events are supported via MQTT, which has a crucial difference in how it works compared to web sockets. This is, events are not broadcast-ed to all users. So this means that you only get real time event updates to those topics that you subscribe to.

[Note](https://mqtt.org/): Some exceptions to this are message events and smart feed events. If you are unsure if subscription is required for a particular topic, check the code-snippet for that particular API for details.

There are also other limitations to getting RTE's, example: you cannot subscribe to events for a community that you are not a member of. Check documentation for full details.

You can read more about MQTT here: <https://mqtt.org/>

```typescript
/*
 * Whenever you notice similar code block in the code snippets it indicates that you
 * subscribing to a particular topic, which in turn let's the server know that you
 * would require real time events for this topic
*/

subscribeTopic(getUserTopic(user, SubscriptionLevels.POST), () => {
    // use callback to handle errors with event subscription
}),
```

### Live Object

Although live objects were introduced prior to v6. All getter methods for singular objects (example `getPost`) will now return a subscribe-able object.

This means that if an object gets updated and you have subscribed to real time events, the object will get updated automatically via real time events.

If for your use case you don't require any real time updates, you can unsubscribe immediately. For further information about Live Object, please visit [#live-object](https://docs.social.plus/social-plus-sdk/core-concepts/live-objects-collections#live-object "mention") page.

#### Getting Real Time Updates for an Object

```typescript
import { PostRepository, subscribeTopic, getPostTopic } from '@amityco/ts-sdk';
import { FC, useEffect, useState } from 'react';

const disposer: Amity.Unsubscriber[] = [];

const GetPost: FC<{ postId: string }> = ({ postId }) => {
  const [post, setPost] = useState<Amity.Post>();

  useEffect(() => {

    const unsubscribePost = PostRepository.getPost(postId, ({ data }) => {
     const { post, loading, error } = data

     if (post) {
       /*
       * This step is important if you wish to recieve real time updates
       * Here, you are letting the server know that you wish to recieve real time
       * updates regarding this post
       */
       disposers.push(subscribeTopic(getPostTopic(post)))

       setPost(post)
     }
    });

    disposers.push(unsubscribePost);
  }, [postId]);

  return null;
};
```

#### Getting the object only once

```typescript
import { PostRepository } from '@amityco/ts-sdk';
import { FC, useEffect, useState } from 'react';

const GetPostOnce: FC<{ postId: string }> = ({ postId }) => {
  const [post, setPost] = useState<Amity.Post>();

  useEffect(() => {
    const unsubscribePost = PostRepository.getPost(postId, ({ data }) => {
     const { post, loading, error } = data

     if (post) {
       setPost(post)
     }
    });

    unsubscribePost()
  }, [postId]);

  return null;
};
```

### Live Collection

Although live collections were introduced prior to v6. All query methods for collection of objects (example `getPosts`) will now return a subscribe-able collection.

This means that if an object in the collection gets updated and you have subscribed to real time events, the collection will get updated automatically via real time events.

If for your use case you don't require any real time updates, you can unsubscribe immediately. Similar to live objects above. For further information about Live Object, please visit [#live-collection](https://docs.social.plus/social-plus-sdk/core-concepts/live-objects-collections#live-collection "mention") page.

#### Getting Real Time updates for a collection

```typescript
import {
  PostRepository,
  subscribeTopic,
  getUserTopic,
  getCommunityTopic,
  SubscriptionLevels,
} from '@amityco/ts-sdk';
import { FC, useEffect, useState } from 'react';

const disposers: Amity.Unsubscriber[] = [];

const GetPosts: FC<{ targetId: string; targetType: string }> =
({ targetId, targetType }) => {
  const [posts, setPosts] = useState<Amity.Post[]>();

  useEffect(() => {
    const unsubscribe = PostRepository.getPosts(
      { targetId, targetType },
      ({ data: posts, onNextPage, hasNextPage, loading, error }) => {
        setPosts(posts);
        /*
         * this is only required if you want real time updates for each
         * post in the collection
         */
        subscribePostTopic(targetType, targetId);
      },
    );

    disposers.push(unsubscribe);

    return () => {
      disposers.forEach(fn => fn());
    };
  });

  return null;
};

const subscribePostTopic = (targetType: string, targetId: string) => {
  if (isSubscribed) return;

  if (targetType === 'user') {
    const user = {} as Amity.User; // use getUser to get user by targetId
    disposers.push(
      subscribeTopic(getUserTopic(user, SubscriptionLevels.POST), () => {
        // use callback to handle errors with event subscription
      }),
    );
    return;
  }

  if (targetType === 'community') {
    // use getCommunity to get community by targetId
    const community = {} as Amity.Community;
    disposers.push(
      subscribeTopic(getCommunityTopic(community, SubscriptionLevels.POST), () => {
        // use callback to handle errors with event subscription
      }),
    );
  }
};

```

#### Getting paginated collection without and real time updates

```typescript
import {
  PostRepository,
  subscribeTopic,
  getUserTopic,
  getCommunityTopic,
  SubscriptionLevels,
} from '@amityco/ts-sdk';
import { FC, useEffect, useState } from 'react';

const GetPosts: FC<{ targetId: string; targetType: string }> =
({ targetId, targetType }) => {
  const [posts, setPosts] = useState<Amity.Post[]>();

  useEffect(() => {
    const unsubscribe = PostRepository.getPosts(
      { targetId, targetType },
      ({ data: posts, onNextPage, hasNextPage, loading, error }) => {
        if (posts)
          setPosts(posts);

        // to get next page use onNextPage()
      },
    );

    unsubscribe()
  });

  return null;
};
```

## Introducing Repositories

Starting from `6.0.0`, you will no longer be able to import individual SDK API's. You'll need to import the required API from it's respective Repositories.

**Client**

**`import { Client } from '@amityco/ts-sdk`**

| Method           | Changed To            |
| ---------------- | --------------------- |
| createClient     | Client.createClient   |
| connectClient    | Client.login          |
| disconnectClient | Client.logout         |
| subscribeTopic   | Client.SubscribeTopic |

#### UserRepository

**`import { UserRepository } from '@amityco/ts-sdk`**

| Method          | Changed To                                  | Type            |
| --------------- | ------------------------------------------- | --------------- |
| liveUser        | UserRepository.getUser                      | Live Object     |
| liveUsers       | UserRepository.getUsers                     | Live Collection |
| updateUser      | UserRepository.updateUser                   |                 |
| createReport    | UserRepository.flagUser                     |                 |
| deleteReport    | UserRepository.unFlagUser                   |                 |
| liveFollowers   | UserRepository.Relationship.getFollowers    | Live Collection |
| liveFollowings  | UserRepository.Relationship.getFollowings   | Live Collection |
| liveFollowInfo  | UserRepository.Relationship.getFollowInfo   | Live Object     |
| acceptFollower  | UserRepository.Relationship.acceptFollower  |                 |
| declineFollower | UserRepository.Relationship.declineFollower |                 |
| follow          | UserRepository.Relationship.follow          |                 |
| unfollow        | UserRepository.Relationship.unfollow        |                 |

#### FileRepository

**`import { FileRepository } from '@amityco/ts-sdk`**

<table><thead><tr><th width="247">Method</th><th width="278.3333333333333" align="center">Changed To</th></tr></thead><tbody><tr><td>getFile</td><td align="center">FileRepository.getFile</td></tr><tr><td>deleteFile</td><td align="center">FileRepository.deleteFile</td></tr></tbody></table>

#### ReactionRepository

**`import { ReactionRepository } from '@amityco/ts-sdk`**

<table><thead><tr><th>Method</th><th width="306.3333333333333">Changed To</th><th>Type</th></tr></thead><tbody><tr><td>queryReactions</td><td>ReactionRepository.getReactions</td><td>Live Collection</td></tr><tr><td>addReaction</td><td>ReactionRepository.addReaction</td><td></td></tr><tr><td>removeReaction</td><td>ReactionRepository.removeReaction</td><td></td></tr></tbody></table>

#### CommunityRepository

**`import { CommunityRepository } from '@amityco/ts-sdk`**

<table><thead><tr><th width="300">Method</th><th width="311.3333333333333">Changed To</th><th>Type</th></tr></thead><tbody><tr><td>createCommunity</td><td>CommunityRepository.createCommunity</td><td></td></tr><tr><td>joinCommunity</td><td>CommunityRepository.joinCommunity</td><td></td></tr><tr><td>leaveCommunity</td><td>CommunityRepository.leaveCommunity</td><td></td></tr><tr><td>liveCommunity</td><td>CommunityRepository.getCommunity</td><td>Live Object</td></tr><tr><td>liveCommunities</td><td>CommunityRepository.getCommunities</td><td>Live Collection</td></tr><tr><td>updateCommunity</td><td>CommunityRepository.updateCommunity</td><td></td></tr><tr><td>liveCommunityMembers</td><td>CommunityRepository.Membership.getMembers</td><td>Live Collection</td></tr><tr><td>addCommunityMembers</td><td>CommunityRepository.Membership.addMembers</td><td></td></tr><tr><td>removeCommunityMembers</td><td>CommunityRepository.Membership.removeMembers</td><td></td></tr><tr><td>addCommunityMemberRoles</td><td>CommunityRepository.Membership.addRoles</td><td></td></tr><tr><td>removeCommunityMemberRoles</td><td>CommunityRepository.Membership.removeRoles</td><td></td></tr><tr><td>liveCategories</td><td>CommunityRepository.getCategories</td><td>Live Collection</td></tr><tr><td>deleteCommunity</td><td>CommunityRepository.deleteCommunity</td><td></td></tr><tr><td>getRecommendedCommunities</td><td>CommunityRepository.getRecommendedCommunities</td><td></td></tr><tr><td>getTopTrendingCommunities</td><td>CommunityRepository.getTopTrendingCommunities</td><td></td></tr></tbody></table>

#### StreamRepository

**`import { StreamRepository } from '@amityco/ts-sdk`**

<table><thead><tr><th>Method</th><th width="286.3333333333333">Changed To</th></tr></thead><tbody><tr><td>getStream</td><td>StreamRepository.getStream</td></tr><tr><td>queryStreams</td><td>StreamRepository.getStreams</td></tr><tr><td>createStream</td><td>StreamRepository.createStream</td></tr></tbody></table>

#### PostRepository

**`import { PostRepository } from '@amityco/ts-sdk`**

<table><thead><tr><th>Method</th><th width="273.3333333333333">Changed To</th><th>Type</th></tr></thead><tbody><tr><td><code>createPost</code></td><td>PostRepository.createPost</td><td></td></tr><tr><td><code>updatePost</code></td><td>PostRepository.updatePost</td><td></td></tr><tr><td>deletePost</td><td>PostRepository.deletePost</td><td></td></tr><tr><td>livePost</td><td>PostRepository.getPost</td><td>Live Object</td></tr><tr><td>livePosts</td><td>PostRepository.getPosts</td><td>Live Collection</td></tr><tr><td>approvePost</td><td>PostRepository.approvePost</td><td></td></tr><tr><td>declinePost</td><td>PostRepository.declinePost</td><td></td></tr></tbody></table>

#### CommentRepository

**`import { CommentRepository } from '@amityco/ts-sdk`**

| Method        | Changed To                      | Type            |
| ------------- | ------------------------------- | --------------- |
| createComment | CommentRepository.createComment |                 |
| updateComment | CommentRepository.updateComment |                 |
| deleteComment | CommentRepository.deleteComment |                 |
| liveComment   | CommentRepository.getComment    | Live Object     |
| liveComments  | CommentRepository.getComments   | Live Collection |

#### PollRepository

**`import { PollRepository } from '@amityco/ts-sdk`**

| Method     | Changed To                |
| ---------- | ------------------------- |
| createPoll | PollRepository.createPoll |
| closePoll  | PollRepository.closePoll  |
| getPoll    | PollRepository.getPoll    |
| votePoll   | PollRepository.votePoll   |
| deletePoll | PollRepository.deletePoll |

#### ChannelRepository

**`import { ChannelRepository } from '@amityco/ts-sdk`**

<table><thead><tr><th width="197">Method</th><th width="311.3333333333333">ChangedTo</th><th>Type</th></tr></thead><tbody><tr><td>createChannel</td><td>ChannelRepository.createChannel</td><td></td></tr><tr><td>liveChannel</td><td>ChannelRepository.getChannel</td><td>Live Object</td></tr><tr><td>liveChannels</td><td>ChannelRepository.getChannels</td><td>Live Collection</td></tr><tr><td>joinChannel</td><td>ChannelRepository.joinChannel</td><td></td></tr><tr><td>leaveChannel</td><td>ChannelRepository.leaveChannel</td><td></td></tr><tr><td>addChannelMembers</td><td>ChannelRepository.Membership.addMembers</td><td></td></tr><tr><td>removeChannelMembers</td><td>ChannelRepository.Membership.removeMembers</td><td></td></tr><tr><td>getChannelMembers</td><td>ChannelRepository.Membership.getMembers</td><td>Live Collection</td></tr></tbody></table>

#### MessageRepository

**`import { MessageRepository } from '@amityco/ts-sdk`**

| Method        | Changed To                      | Type            |
| ------------- | ------------------------------- | --------------- |
| createMessage | MessageRepository.createMessage |                 |
| updateMessage | MessageRepository.updateMessage |                 |
| deleteMessage | MessageRepository.deleteMessage |                 |
| liveMessage   | MessageRepository.getMessage    | Live Object     |
| liveMessages  | MessageRepository.getMessages   | Live Collection |
