Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今日から始めるAmplify DataStore

sutetotanuki
June 23, 2020
1.1k

今日から始めるAmplify DataStore

sutetotanuki

June 23, 2020
Tweet

More Decks by sutetotanuki

Transcript

  1. (SBQI2-εΩʔϚ  enum TodoStatus { WAITING DONE } type Todo

    @model @auth(rules: [{allow: owner}]){ id: ID! owner: String title: String items: [TodoItem] @connection(name: "TodoItems") status: TodoStatus } type TodoItem @model { id: ID! owner: String content: String todo: Todo @connection(name: "TodoItems") status: TodoStatus }
  2. (SBQI2-εΩʔϚ  enum TodoStatus { WAITING DONE } type Todo

    @model @auth(rules: [{allow: owner}]){ id: ID! owner: String title: String items: [TodoItem] @connection(name: "TodoItems") status: TodoStatus } type TodoItem @model { id: ID! owner: String content: String todo: Todo @connection(name: "TodoItems") status: TodoStatus } ྻڍܕ 50%0ຊମ 50%0ΞΠςϜ
  3. (SBQI2-εΩʔϚݖݶ؅ཧ  type Todo @model @auth(rules: [{allow: owner}]){ owner: String

    BVUIτϥϯεϑΥʔϜΛ͚ͭΔ͜ͱͰɺ͜ͷϞσϧ ʹΞΫηεͰ͖ΔݖݶΛ͚ͭΔ͜ͱ͕Ͱ͖Δ ࠓճ͸Ϟσϧͷ࡞੒ऀͷΈ͕͜ͷϞσϧΞΫηε Ͱ͖ΔΑ͏ʹ͍ͯ͠Δ
  4. (SBQI2-εΩʔϚؔ࿈  type Todo @model @auth(rules: [{allow: owner}]){ items: [TodoItem]

    @connection(name: "TodoItems") type TodoItem @model { todo: Todo @connection(name: "TodoItems") DPOOFDUJPOτϥϯεϑΥʔϜͰϞσϧಉ࢜ͷ ؔ࿈Λઃఆ͢Δ͜ͱ͕Ͱ͖Δ
  5. Ϟσϧͷอଘ  await DataStore.save( new Todo({ title: this.todoTitle, status: TodoStatus.WAITING

    }) ); %BUB4UPSFTBWFʹϞσϧͷΠϯελϯεΛ౉͠ σʔλΛอଘ͠·͢
  6. ؔ࿈ͷ͋ΔϞσϧͷอଘ  async createTodoItem(todo) { await DataStore.save( new TodoItem({ todo:

    todo }) ); DPOOFDUJPOτϥϯεϨʔλʔΛઃఆͨ͠ ϑΟʔϧυʹؔ࿈͢ΔϞσϧͷΠϯελϯεΛ ઃఆͯ͠อଘ͢Δ
  7. ؔ࿈ͷ͋ΔϞσϧͷσʔλΛݕࡧ  GJMUFSͰؔ࿈Ϟσϧͷ*%͕ઃఆ͞Ε͍ͯΔσʔλ͚ͩ ΛߜΓࠐΉ async fetchTodoItems(todo) { this.todoItems = (

    await DataStore.query(TodoItem, c => c.status("eq", TodoStatus.WAITING)) ).filter(item => item.todo && item.todo.id === todo.id); },
  8. Ϟσϧͷߋ৽  await DataStore.save( TodoItem.copyOf(todoItem, updated => { updated.status =

    TodoStatus.DONE; }) ); ϞσϧͷΠϯελϯε͸ΠϛϡʔλϒϧͳͷͰɺ ίϐʔ͠Πϯελϯεͷ஋Λ্ॻ͖ͨ͠΋ͷΛ อଘ͢Δ͜ͱͰߋ৽͢Δ
  9. ϩʔΧϧσʔλͷΫϦΞ  async logout({ commit }) { await DataStore.clear(); }

    ϩάΞ΢τ࣌ʹϩʔΧϧσʔλΛΫϦΞ͢Δඞཁ ͕͋Δɻ ΫϦΞ͠ͳ͍ͱผͷϢʔβʔͰϩάΠϯͨ͠৔߹ ʹɺϩʔΧϧσʔλ͕ࠞࡏͯ͠͠·͏