diff --git a/examples/complex_types/src/index.did b/examples/complex_types/src/index.did index bee0f6b3f1..d75ebcf4b6 100644 --- a/examples/complex_types/src/index.did +++ b/examples/complex_types/src/index.did @@ -47,12 +47,12 @@ type rec_617 = record {id:text; text:text; author:rec_616; thread:record {id:tex type rec_618 = record {id:text; text:text; author:rec_616; thread:record {id:text; title:text; author:rec_616; posts:vec rec_618}; reactions:vec record {id:text; post:rec_618; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; type rec_616 = record {id:text; username:text; threads:vec record {id:text; title:text; author:rec_616; posts:vec rec_619}; posts:vec rec_617; reactions:vec record {id:text; post:rec_618; author:rec_616; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}}; service: () -> { - createPost: (text, text, text, nat32) -> (rec_440) ; - createReaction: (text, text, variant {Fire; ThumbsDown; ThumbsUp}, nat32) -> (record {id:text; post:rec_484; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) ; - createThread: (text, text, nat32) -> (record {id:text; title:text; author:rec_544; posts:vec rec_548}) ; - createUser: (text, nat32) -> (rec_600) ; - getAllPosts: (nat32) -> (vec rec_456) query; - getAllReactions: (nat32) -> (vec record {id:text; post:rec_516; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) query; - getAllThreads: (nat32) -> (vec record {id:text; title:text; author:rec_576; posts:vec rec_580}) query; - getAllUsers: (nat32) -> (vec rec_616) query; -} \ No newline at end of file + createPost: (text, text, text, nat32) -> (rec_440); + createReaction: (text, text, variant {Fire; ThumbsDown; ThumbsUp}, nat32) -> (record {id:text; post:rec_484; author:rec_480; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}); + createThread: (text, text, nat32) -> (record {id:text; title:text; author:rec_544; posts:vec rec_548}); + createUser: (text, nat32) -> (rec_600); + getAllPosts: (nat32) -> (vec rec_456) query; + getAllReactions: (nat32) -> (vec record {id:text; post:rec_516; author:rec_512; reactionType:variant {Fire; ThumbsDown; ThumbsUp}}) query; + getAllThreads: (nat32) -> (vec record {id:text; title:text; author:rec_576; posts:vec rec_580}) query; + getAllUsers: (nat32) -> (vec rec_616) query; +} diff --git a/examples/complex_types/src/index.ts b/examples/complex_types/src/index.ts index 7a23d4cd26..e6fcf675f6 100644 --- a/examples/complex_types/src/index.ts +++ b/examples/complex_types/src/index.ts @@ -1,5 +1,4 @@ -import { nat32, query, Canister, text, update, Vec } from 'azle'; -import { Post, Reaction, ReactionType, Thread, User } from './candid_types'; +import { Canister } from 'azle'; import { createPost, getAllPosts } from './posts'; import { createReaction, getAllReactions } from './reactions'; import { createThread, getAllThreads } from './threads'; @@ -7,40 +6,11 @@ import { createUser, getAllUsers } from './users'; export default Canister({ createPost, - - getAllPosts: query([nat32], Vec(Post), (joinDepth) => { - return getAllPosts(joinDepth); - }), - - createReaction: update( - [text, text, ReactionType, nat32], - Reaction, - (authorId, postId, reactionType, joinDepth) => { - return createReaction(authorId, postId, reactionType, joinDepth); - } - ), - - getAllReactions: query([nat32], Vec(Reaction), (joinDepth) => { - return getAllReactions(joinDepth); - }), - - createThread: update( - [text, text, nat32], - Thread, - (title, authorId, joinDepth) => { - return createThread(title, authorId, joinDepth); - } - ), - - getAllThreads: query([nat32], Vec(Thread), (joinDepth) => { - return getAllThreads(joinDepth); - }), - - createUser: update([text, nat32], User, (username, joinDepth) => { - return createUser(username, joinDepth); - }), - - getAllUsers: query([nat32], Vec(User), (joinDepth) => { - return getAllUsers(joinDepth); - }) + getAllPosts, + createReaction, + getAllReactions, + createThread, + getAllThreads, + createUser, + getAllUsers }); diff --git a/examples/complex_types/src/posts.ts b/examples/complex_types/src/posts.ts index 850e82b764..1aae042d59 100644 --- a/examples/complex_types/src/posts.ts +++ b/examples/complex_types/src/posts.ts @@ -1,4 +1,4 @@ -import { nat32, text, update, Vec } from 'azle'; +import { nat32, query, text, update, Vec } from 'azle'; import { Post } from './candid_types'; import { getReactionFromStateReaction } from './reactions'; import { state, StatePost, StateThread, StateUser } from './state'; @@ -37,11 +37,11 @@ export const createPost = update( } ); -export function getAllPosts(joinDepth: nat32): (typeof Post)[] { +export const getAllPosts = query([nat32], Vec(Post), (joinDepth) => { return Object.values(state.posts).map((statePost) => getPostFromStatePost(statePost, joinDepth) ); -} +}); export function getPostFromStatePost( statePost: StatePost, diff --git a/examples/complex_types/src/reactions.ts b/examples/complex_types/src/reactions.ts index fb5aa95948..5820d501e1 100644 --- a/examples/complex_types/src/reactions.ts +++ b/examples/complex_types/src/reactions.ts @@ -1,43 +1,42 @@ -import { nat32, Vec } from 'azle'; +import { nat32, query, text, update, Vec } from 'azle'; import { Reaction, ReactionType } from './candid_types'; import { getPostFromStatePost } from './posts'; import { state, StatePost, StateReaction, StateUser } from './state'; import { getUserFromStateUser } from './users'; -export function createReaction( - authorId: string, - postId: string, - reactionType: typeof ReactionType, - joinDepth: nat32 -): typeof Reaction { - const id = Object.keys(state.reactions).length.toString(); +export const createReaction = update( + [text, text, ReactionType, nat32], + Reaction, + (authorId, postId, reactionType, joinDepth) => { + const id = Object.keys(state.reactions).length.toString(); - const stateReaction: StateReaction = { - id, - authorId, - postId, - reactionType - }; - const updatedStateAuthor = getUpdatedStateAuthor( - authorId, - stateReaction.id - ); - const updatedStatePost = getUpdatedStatePost(postId, stateReaction.id); + const stateReaction: StateReaction = { + id, + authorId, + postId, + reactionType + }; + const updatedStateAuthor = getUpdatedStateAuthor( + authorId, + stateReaction.id + ); + const updatedStatePost = getUpdatedStatePost(postId, stateReaction.id); - state.reactions[id] = stateReaction; - state.users[authorId] = updatedStateAuthor; - state.posts[postId] = updatedStatePost; + state.reactions[id] = stateReaction; + state.users[authorId] = updatedStateAuthor; + state.posts[postId] = updatedStatePost; - const reaction = getReactionFromStateReaction(stateReaction, joinDepth); + const reaction = getReactionFromStateReaction(stateReaction, joinDepth); - return reaction; -} + return reaction; + } +); -export function getAllReactions(joinDepth: nat32): (typeof Reaction)[] { +export const getAllReactions = query([nat32], Vec(Reaction), (joinDepth) => { return Object.values(state.reactions).map((stateReaction) => getReactionFromStateReaction(stateReaction, joinDepth) ); -} +}); export function getReactionFromStateReaction( stateReaction: StateReaction, diff --git a/examples/complex_types/src/threads.ts b/examples/complex_types/src/threads.ts index 6bea9fd903..e4a358bed5 100644 --- a/examples/complex_types/src/threads.ts +++ b/examples/complex_types/src/threads.ts @@ -1,37 +1,40 @@ -import { nat32, Vec } from 'azle'; +import { nat32, query, text, update, Vec } from 'azle'; import { Thread } from './candid_types'; import { getPostFromStatePost } from './posts'; import { state, StateThread, StateUser } from './state'; import { getUserFromStateUser } from './users'; -export function createThread( - title: string, - authorId: string, - joinDepth: nat32 -): typeof Thread { - const id = Object.keys(state.threads).length.toString(); +export const createThread = update( + [text, text, nat32], + Thread, + (title, authorId, joinDepth) => { + const id = Object.keys(state.threads).length.toString(); - const stateThread: StateThread = { - id, - authorId, - postIds: [], - title - }; - const updatedStateAuthor = getUpdatedStateAuthor(authorId, stateThread.id); + const stateThread: StateThread = { + id, + authorId, + postIds: [], + title + }; + const updatedStateAuthor = getUpdatedStateAuthor( + authorId, + stateThread.id + ); - state.threads[id] = stateThread; - state.users[authorId] = updatedStateAuthor; + state.threads[id] = stateThread; + state.users[authorId] = updatedStateAuthor; - const thread = getThreadFromStateThread(stateThread, joinDepth); + const thread = getThreadFromStateThread(stateThread, joinDepth); - return thread; -} + return thread; + } +); -export function getAllThreads(joinDepth: nat32): (typeof Thread)[] { +export const getAllThreads = query([nat32], Vec(Thread), (joinDepth) => { return Object.values(state.threads).map((stateThread) => getThreadFromStateThread(stateThread, joinDepth) ); -} +}); export function getThreadFromStateThread( stateThread: StateThread, diff --git a/examples/complex_types/src/users.ts b/examples/complex_types/src/users.ts index 9c98ed25f2..4b9096c9e3 100644 --- a/examples/complex_types/src/users.ts +++ b/examples/complex_types/src/users.ts @@ -1,11 +1,11 @@ -import { nat32, Vec } from 'azle'; +import { nat32, query, text, update, Vec } from 'azle'; import { User } from './candid_types'; import { getPostFromStatePost } from './posts'; import { getReactionFromStateReaction } from './reactions'; import { state, StateUser } from './state'; import { getThreadFromStateThread } from './threads'; -export function createUser(username: string, joinDepth: nat32): typeof User { +export const createUser = update([text, nat32], User, (username, joinDepth) => { const id = Object.keys(state.users).length.toString(); const stateUser: StateUser = { @@ -21,13 +21,13 @@ export function createUser(username: string, joinDepth: nat32): typeof User { const user = getUserFromStateUser(stateUser, joinDepth); return user; -} +}); -export function getAllUsers(joinDepth: nat32): (typeof User)[] { +export const getAllUsers = query([nat32], Vec(User), (joinDepth) => { return Object.values(state.users).map((stateUser) => getUserFromStateUser(stateUser, joinDepth) ); -} +}); export function getUserFromStateUser( stateUser: StateUser,