<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by aunn on Medium]]></title>
        <description><![CDATA[Stories by aunn on Medium]]></description>
        <link>https://medium.com/@aunn_it08?source=rss-3e5021a94f07------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*u1Q9BQ7kboIC_4i6AoHZ3Q.gif</url>
            <title>Stories by aunn on Medium</title>
            <link>https://medium.com/@aunn_it08?source=rss-3e5021a94f07------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 09:49:43 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@aunn_it08/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[รู้จัก Caching patterns กันเถอะ]]></title>
            <link>https://medium.com/@aunn_it08/%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81-caching-patterns-%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B9%80%E0%B8%96%E0%B8%AD%E0%B8%B0-4374e21f4ed?source=rss-3e5021a94f07------2</link>
            <guid isPermaLink="false">https://medium.com/p/4374e21f4ed</guid>
            <category><![CDATA[software-engineer]]></category>
            <category><![CDATA[cache]]></category>
            <category><![CDATA[patterns]]></category>
            <dc:creator><![CDATA[aunn]]></dc:creator>
            <pubDate>Tue, 26 Apr 2022 18:45:59 GMT</pubDate>
            <atom:updated>2022-04-26T18:45:59.187Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8zM5EAIBGeOv9dVyjJEyYQ.jpeg" /></figure><p>ปกติแล้วเวลา Application ของเรามีคนเริ่มใช้เยอะ ๆ มันก็จะเริ่มช้าขึ้นเรื่อย ๆ ซึ่งโดยส่วนมากแล้วการที่มันช้าเนี่ย มันมาจาก DB หรืองานที่ต้อง Computed อะไรหนัก ๆ สักอย่าง การทำ Cache ก็ถือเป็นทางออกหนึ่งในการแก้ปัญหานี้ แต่ก่อนที่จะเริ่มใช้ Cache เรามาทำความรู้จัก Patterns ที่นิยมใช้กัน</p><h3>Cache aside</h3><p>ท่านี้น่าจะใช้กันหลากหลายที่สุดแล้วหละ คือตัว Application ของเรานี่แหละครับ จะเชคก่อนว่ามีของใน Cache ไหมถ้าไม่มีก็จะดึงจาก DB แล้วมาบันทึกลง Cache ครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/422/1*7nUTBDDkHkPtIHt_9dRb-A.png" /></figure><p>นิยมใช้สำหรับ Application ที่มีการอ่านเยอะ ๆ อ่านข้อมูลซ้ำ ๆ</p><h4>ข้อดี</h4><ul><li>ถ้า Cache ระเบิด Application ก็ยังทำงานได้ แต่ถ้าโหลดหนักมาก ๆ อาจจะลาก DB ระเบิดไปด้วย ไม่รู้ว่าข้อดีหรือข้อเสียแฮะ 55</li><li>implement ง่ายมาก สมมติใช้ springboot เขียน annotation นิดหน่อยจบเลย</li></ul><h4>ข้อเสีย</h4><ul><li>ถ้ามีการอัพเดทข้อมูล Cache มักจะไม่ถูกอัพเดท แต่แก้ได้แหละ ก็ต้องเพิ่มโค้ดให้อัพเดท Cache เวลา อัพเดทข้อมูลจากที่อื่น</li><li>โดยปกติแล้วจะไม่ค่อยกัน Cache Stempede สักเท่าไหร่ คร่าว ๆ มันคือ การที่ Request เข้ามาเยอะ ๆ แล้วไม่เจอ cache เลยทำให้ DB พัง อาจจะมี Entry สำหรับเรื่อง Cache Stempede ก็ฝากติดตามกันไว้ด้วยครับ</li></ul><h3>Read-Through</h3><p>อันนี้จะคล้าย ๆ กับ Cache aside แต่เราจะยกการดึงของจาก DB ไปไว้ที่ Cache provider แทนครับ จะเป็นในระดับของ code แทนละสำหรับตัวนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/552/1*izx9R3qaykwC2lhnmvc_uA.png" /></figure><p>นิยมใช้สำหรับ Application ที่มีการอ่านเยอะ ๆ อ่านข้อมูลซ้ำ ๆ ข้อดีข้อเสีย ก็จะเหมือนกันกับ Cache aside ครับ</p><h3>Write-Through</h3><p>อันนี้จะเป็น pattern การเขียนข้อมูลแทนละ จะไม่เหมือนสองอันบนเนอะ ก็คือจะเขียนลง Cache ก่อนแล้วค่อยเขียนลง DB</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/569/1*u_w1MBVwflcUrw685tNvKQ.png" /><figcaption>จะเป็นอันบนหรืออันล่างแล้วแต่ตอน implement เลยครับ</figcaption></figure><h4>ข้อดี</h4><ul><li>เอาไว้ใช้คู่กับ Read-Through ข้อมูลจะได้อัพเดทที่ cache ทันที</li></ul><h3>Write-Behind</h3><p>หรืออาจจะเรียกกันว่า Write-Back ก็จะทำการเขียนที่ Cache ก่อนแล้วตอบทันที แล้วค่อย Lazy Update ตัว DB ในภายหลังครับ</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/426/1*OpQby4GoEe_EnL3mjkbl5g.png" /></figure><p>นิยมใช้สำหรับ Application ที่มีการเขียนข้อมูลลง DB เยอะ ๆ แล้ว DB มันตอบช้าละ</p><h4>ข้อดี</h4><ul><li>ความไวดุจสายฟ้า 55</li></ul><h4>ข้อเสีย</h4><ul><li>การ rollback transaction จะทำได้ยาก ปกติ Cache จะไม่ค่อยทำตรงนี้ให้ อาจจะต้อง implement ส่วนนี้เพิ่มเติมเองครับ</li><li>ถ้า Cache ระเบิดอาจจะต้องไปใช้ DBแทน ซึ่งการไปใช้ DB แทนก็อาจจะทำให้ DB ระเบิดอีก</li></ul><h3>สรุป</h3><p>เราก็ได้รู้จัก Caching Patterns กันคร่าว ๆ แล้วนะครับ ทั้งส่วนของการอ่านและการเขียนข้อมูล จริง ๆ จะท่ามากกว่านี้ครับ ลองอ่านเพิ่มเติมได้จาก <a href="https://codeahoy.com/2017/08/11/caching-strategies-and-how-to-choose-the-right-one/">1</a>, <a href="https://www.educative.io/edpresso/caching-patterns">2</a> และ <a href="https://hazelcast.com/blog/a-hitchhikers-guide-to-caching-patterns/">3</a> มีใครใช้ท่าข้างบนหรือท่าอื่นนอกเหนือจากนี้คอมเม้นพูดคุยกันได้นะครับ</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4374e21f4ed" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[GraphQL server ด้วย gin, gqlgen part1]]></title>
            <link>https://medium.com/@aunn_it08/graphql-server-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-gin-gqlgen-part1-7d961850096c?source=rss-3e5021a94f07------2</link>
            <guid isPermaLink="false">https://medium.com/p/7d961850096c</guid>
            <category><![CDATA[graphql]]></category>
            <category><![CDATA[apollo]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[gin]]></category>
            <category><![CDATA[gqlgen]]></category>
            <dc:creator><![CDATA[aunn]]></dc:creator>
            <pubDate>Tue, 05 May 2020 12:43:50 GMT</pubDate>
            <atom:updated>2020-05-05T13:05:55.581Z</atom:updated>
            <content:encoded><![CDATA[<h3>GraphQL server ด้วย gin, gqlgen part 1</h3><p>วันนี้จะมาลอง Implement tutorial Apollo ฝั่ง server side ด้วย go นะครับ <a href="https://www.apollographql.com/docs/tutorial/introduction/">อันนี้ลิงค์ต้นทาง</a> โจทย์คร่าว ๆ คือ เราจะทำ API ในการจองยานอวกาศของ SpaceX นะครับ ซึ่งต้นทางเนี่ยมันจะเขียนด้วย NodeJS แต่เราจะมาลองเปลี่ยนให้มันเป็น go กัน บทความนี้จะไม่ได้เล่ารายละเอียดของ GraphQL นะครับ จะลง implement เลย เราจะใช้ gin กับ gqlgen</p><h4>ทำไมต้อง gqlgen</h4><p>ตัว gqlgen จะทำการ generate server กับตัว resolver ให้เลย ที่เราต้องทำ คือเชื่อม server เข้ากับ server ของ gin แล้ว implement resolver ครับ ข้อดีของ gqlgen ก็ประมาณนี้</p><ul><li>Schema first</li><li>รองรับ Federation</li><li>Document ดีงาม</li></ul><p>มาเร่ิม setup project กัน</p><pre>go mod init github.com/pangaunn/gqlgen-apollo-tutorial<br>go get github.com/99designs/gqlgen<br>go run github.com/99designs/gqlgen init</pre><p>พอรัน gqlgen init แล้วก็จะได้ไฟล์ gqlgen.yml ,server.go และ folder graph มาลบให้หมดเลย เราจะใช้แค่ gqlgen.yml ไฟล์เดียว</p><p>จากนั้นแก้ไฟล์ gqlgen.yml ให้เป็น</p><pre>schema:<br>  - app/graphql/schema/*.graphql</pre><pre>exec:<br>  filename: app/graphql/generated.go<br>  package: graphql</pre><pre>model:<br>  filename: app/graphql/model/generated.go<br>  package: model</pre><pre>resolver:<br>  layout: follow-schema<br>  dir: app/graphql/resolver<br>  package: resolver</pre><pre>autobind: []</pre><pre>models:<br>  ID:<br>    model:<br>      - github.com/99designs/gqlgen/graphql.ID<br>      - github.com/99designs/gqlgen/graphql.Int<br>      - github.com/99designs/gqlgen/graphql.Int64<br>      - github.com/99designs/gqlgen/graphql.Int32<br>  Int:<br>    model:<br>      - github.com/99designs/gqlgen/graphql.Int<br>      - github.com/99designs/gqlgen/graphql.Int64<br>      - github.com/99designs/gqlgen/graphql.Int32</pre><p>ที่เราแก้ไฟล์ไปจะเป็นการ setup folder/package ให้กับ ไฟล์ที่เรา generate มาครับ แล้วก็จะให้อ่านไฟล์ .graphql จากที่ไหน ต่อไปก็สร้าง schema กันครับ</p><pre>mkdir -p app/graphql/schema</pre><p>เราจะ copy ไฟล์ schema จากต้นทางกันก็คือไฟล์นี้ <a href="https://github.com/apollographql/fullstack-tutorial/blob/master/final/server/src/schema.js">https://github.com/apollographql/fullstack-tutorial/blob/master/final/server/src/schema.js</a> ผมจะก็อบมาเป็น 3 file นะครับ จะแยกเป็น mutaion.graphql query.graphql และ schema.graphql เหตุผลที่แยกไฟล์ก็เพราะเวลาเรารัน generate เราจะได้ไฟล์ resolver หลายไฟล์ครับ ทำให้ไฟล์ไม่ใหญ่เกินไป copy มาจะได้หน้าตาประมาณนี้ครับ อ่อลืมบอกผมขอตัด Mutation Upload ออกก่อนนะครับ</p><p><em>app/graphql/schema/mutation.graphql</em></p><pre>type Mutation {<br>  # if false, signup failed -- check errors<br>  bookTrips(launchIds: [ID]!): TripUpdateResponse!<br>  # if false, cancellation failed -- check errors<br>  cancelTrip(launchId: ID!): TripUpdateResponse!<br>  login(email: String): String # login token<br>}<br>type TripUpdateResponse {<br>  success: Boolean!<br>  message: String<br>  launches: [Launch]<br>}</pre><p><em>app/graphql/schema/query.graphql</em></p><pre>type Query {<br>  launches(<br>    &quot;&quot;&quot;<br>    The number of results to show. Must be &gt;= 1. Default = 20<br>    &quot;&quot;&quot;<br>    pageSize: Int = 20<br>    &quot;&quot;&quot;<br>    If you add a cursor here, it will only return results _after_ this cursor<br>    &quot;&quot;&quot;<br>    after: String<br>  ): LaunchConnection!<br>  launch(id: ID!): Launch<br>  me: User<br>}</pre><p><em>app/graphql/schema/schema.graphql</em></p><pre>type LaunchConnection {<br>  cursor: String!<br>  hasMore: Boolean!<br>  launches: [Launch]!<br>}<br>type Launch {<br>  id: ID!<br>  site: String<br>  mission: Mission<br>  rocket: Rocket<br>  isBooked: Boolean!<br>}<br>type Rocket {<br>  id: ID!<br>  name: String<br>  type: String<br>}<br>type User {<br>  id: ID!<br>  email: String!<br>  profileImage: String<br>  trips: [Launch]!<br>}<br>type Mission {<br>  name: String<br>  missionPatch(size: PatchSize): String<br>}<br>enum PatchSize {<br>  SMALL<br>  LARGE<br>}</pre><p>จากนั้นรัน generate อีกรอบครับ</p><pre>go run github.com/99designs/gqlgen generate</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*08qCGRedBLTFWcKVImgb2w.png" /><figcaption>พอรันเสร็จเจ้า gqlgen ก็จะ generate ไฟล์ให้ตามนี้ครับ</figcaption></figure><p>อันนี้แหละครับที่เขาเรียกกันว่า schema first คือเราจะทำการสร้าง หรืออัพเดทไฟล์ graphql กันก่อนแล้วค่อยมาลงมือเขียนโค้ดกัน</p><p>หลังจากนั้นเราก็จะมา setup ตัว gin ให้มี graphql playground กับ graphql handler กันต่อครับ เริ่มด้วยสร้างไฟล์</p><p><em>app/handler/graphql.go</em></p><pre>package handler</pre><pre>import (<br> &quot;github.com/99designs/gqlgen/graphql/handler&quot;<br> &quot;github.com/99designs/gqlgen/graphql/playground&quot;<br> &quot;github.com/gin-gonic/gin&quot;<br> &quot;github.com/pangaunn/gqlgen-apollo-tutorial/app/graphql&quot;<br>)</pre><pre>// GraphqlHandler *<br>func GraphqlHandler() gin.HandlerFunc {<br> h := handler.NewDefaultServer(graphql.NewExecutableSchema(graphql.Config{}))</pre><pre>return func(c *gin.Context) {<br>  h.ServeHTTP(c.Writer, c.Request)<br> }<br>}</pre><pre>// PlaygroundHandler Defines the Playground handler to expose our playground<br>func PlaygroundHandler(path string) gin.HandlerFunc {<br> h := playground.Handler(&quot;Go GraphQL Server&quot;, path)<br> return func(c *gin.Context) {<br>  h.ServeHTTP(c.Writer, c.Request)<br> }<br>}</pre><p>สร้างไฟล์ main.go ไว้รัน server กันครับ</p><p><em>main.go</em></p><pre>package main</pre><pre>import (<br> &quot;log&quot;</pre><pre>&quot;github.com/gin-gonic/gin&quot;<br> &quot;github.com/pangaunn/gqlgen-apollo-tutorial/app/handler&quot;<br>)</pre><pre>func main() {<br> port := &quot;7777&quot;<br> g := gin.Default()</pre><pre> g.GET(&quot;/pg&quot;, handler.PlaygroundHandler(&quot;/graph&quot;))<br> g.POST(&quot;/graph&quot;, handler.GraphqlHandler())<br> log.Fatalln(g.Run(&quot;:&quot; + port))<br>}</pre><p>ลองรันด้วย</p><pre>go run main.go</pre><p>เปิด browser แล้วลองไปที่ localhost:7777/pg</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FbCuIyVOTscFpswj3sKi2Q.png" /><figcaption>ก็จะได้ตัว playground ครับ มันจะคล้าย ๆ POSTMAN แต่เราจะเห็น type ได้เลยว่า graphql นี้มี type, query, mutaion อะไรให้ใช้บ้าง</figcaption></figure><p>ก็ขอจบ part1 เท่านี้ก่อนนะครับ เดี๋ยว part2 จะเพิ่ม spaceX API ครับ<br>อันนี้ github สำหรับ part1 ครับ</p><p><a href="https://github.com/pangaunn/gqlgen-apollo-tutorial">pangaunn/gqlgen-apollo-tutorial</a></p><p>Reference</p><ul><li><a href="https://dev.to/cmelgarejo/creating-an-opinionated-graphql-server-with-go-part-1-3g3l">https://dev.to/cmelgarejo/creating-an-opinionated-graphql-server-with-go-part-1-3g3l</a></li><li><a href="https://www.apollographql.com/docs/tutorial/introduction/">https://www.apollographql.com/docs/tutorial/introduction/</a></li><li><a href="https://github.com/99designs/gqlgen">https://github.com/99designs/gqlgen</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7d961850096c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[mapState กับ mapGetter ต่างกันยังไง]]></title>
            <link>https://medium.com/secrets-of-sellsuki/mapstate-%E0%B8%81%E0%B8%B1%E0%B8%9A-mapgetter-%E0%B8%95%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B8%A2%E0%B8%B1%E0%B8%87%E0%B9%84%E0%B8%87-ddf98c51a41c?source=rss-3e5021a94f07------2</link>
            <guid isPermaLink="false">https://medium.com/p/ddf98c51a41c</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[vuex]]></category>
            <category><![CDATA[dev]]></category>
            <dc:creator><![CDATA[aunn]]></dc:creator>
            <pubDate>Sat, 19 May 2018 17:59:40 GMT</pubDate>
            <atom:updated>2018-05-19T18:05:43.907Z</atom:updated>
            <content:encoded><![CDATA[<p>วันนี้คุยกันเบา ๆ เกี่ยวกับ vuex ประเด็นนี้เกิดขึ้นหลังจากที่ช่วยสอน vue ที่ทาง Sellsuki จัดขึ้น ซึ่งในหัวข้อที่ผมต้องสอนก็คือ vuex ผมเลยต้องไปหาดู course ที่สอนเรื่องนี้ ดูไปดูมาก็ได้ไปเจอ <a href="https://frontendmasters.com/courses/vue/">course</a> นี้ พอดูจบก็ได้เห็นว่าเอ้ยมันมี mapState ด้วย</p><h3>mapState คืออะไร?</h3><p>เอาเป็นว่ามันเป็น helper function ของ vuex ที่เอาไว้ดึง state ที่เราอยากใช้มาแสดงค่าเลย อืมม อธิบายคงลำบากเอาเป็นตัวอย่างละกัน สมมติเรามี user store เก็บค่าเป็น</p><pre>const state = {<br>  firstName: &#39;&#39;,<br>  lastName: &#39;&#39;,<br>}</pre><p>ทีนี้ถ้า component ของเราอยากใช้ firstName หรือ lastName เราก็ใช้ผ่าน mapState ได้เลย</p><pre>&lt;template&gt;<br>  &lt;div&gt;{{ firstName }} {{ lastName }}&lt;/div&gt;<br>&lt;/template&gt;<br>&lt;script&gt;<br>import { mapState } from &#39;vuex&#39;<br>export default {<br>  computed: mapState(&#39;user&#39;, [&#39;firstName&#39;, &#39;lastName])<br>}<br>&lt;/script&gt;</pre><h3>mapGetter คืออะไร?</h3><p>ตัวนี้ก็เป็น helper function เหมือนกันแต่จุดประสงค์จะต่างจาก mapState อยู่นิดหน่อย คือ mapGetter จะเป็นการเปลี่ยนแปลง state แล้วเราจะเอาค่านั้นไปใช้ได้เลย มาดูตัวอย่างกัน เช่นจากตัวอย่างข้างบนถ้าเราอยากใช้เป็น fullName เราจะเขียน user store เป็น</p><pre>const state = {<br>  firstName: &#39;&#39;,<br>  lastName: &#39;&#39;,<br>}</pre><pre>const getters = {<br>  fullName: ({ firstName, lastName }) =&gt; `${firstName} ${lastName}`<br>}</pre><p>ทีนี้พอเราจะเอา fullName ไปใช้ใน component ก็</p><pre>&lt;template&gt;<br>  &lt;div&gt;{{ fullName }}&lt;/div&gt;<br>&lt;/template&gt;<br>&lt;script&gt;<br>import { mapGetters } from &#39;vuex&#39;<br>export default {<br>  computed: mapGetters({ fullName: &#39;user/fullName&#39; })<br>}<br>&lt;/script&gt;</pre><p>ถ้าจะยกตัวอย่างเพิ่มเติมการใช้ getter ก็เช่น</p><ul><li>array operation ต่าง ๆ map, reduce</li><li>concat string</li><li>คำนวณเช่น db ส่งปีเกิดมาแต่เราอยากให้แสดงอายุ</li></ul><h3>แล้วทำไมเราต้องสนใจสองอันนี้ด้วยหละ</h3><p>อันนี้เป็นเชิงสารภาพบาป คือผมก็เพิ่งรู้นี้แหละว่ามันมี mapState ด้วยเหมือนอ่านมันผ่าน ๆ ไปเลยตอนที่น้อง ๆ ในทีมสอนเขียนก็ให้ใช้ getter อย่างเดียว จนมาเตรียมเนื้อหาการสอนนี่แหละ หลังจากสอนผมก็หาข้อมูลใน community ก็จะแบ่งเป็นสองฝั่ง ฝั่งแรกคือ mapGetter ทุกอย่างเลยกับอีกฝั่งคือ mapState + mapGetter</p><h3>สรุปใช้อันไหนดี</h3><p>จะเห็นได้ว่าเราสามารถใช้ mapGetter ให้กับทุก state ได้ หรือจะใช้เป็น mapState บวกกับ mapGetter ก็ได้ มาดูข้อเสียของการเขียนสองแบบนี้กัน</p><h4>mapGetter ทุกอย่างเลย</h4><p>ข้อดี ไม่ต้องเสียเวลาอธิบายความแตกต่างระหว่าง mapState กับ mapGetter</p><p>ข้อเสีย เราจะไม่รู้ว่า getter อันไหนที่มันเป็น getter จริง ๆ</p><h4>mapState + mapGetter</h4><p>ข้อดี เราจะเห็นที่ component เลยว่าอันไหนคือ state อันไหนคือ getter และเราไม่ต้องเขียน getter กับ state ที่ไม่ได้เป็น getter จริง ๆ</p><p>ข้อเสีย คนที่เข้าทีมมาใหม่จะงงความแตกต่าง อาจจะต้องเสียเวลาในการอธิบายหน่อย</p><p>คือเอาจริง ๆ มันเป็น context ของแต่ละทีมแหละครับว่าชอบการเขียนแบบไหน ส่วนทีมผมตอนแรกใช้เป็น mapGetter ทุกอย่างเลย แต่หลังจากคุยกับทีมแล้ว หลังจากนี้คงใช้เป็น mapState + mapGetter ครับ</p><p>ฝากไว้นิดนึงครับ ก่อนที่เราจะใช้อะไรเราควรจะรู้ก่อนว่ามันมีอะไรให้ใช้บ้าง เราไม่ควรจะใช้สิ่งนั้นเพราะคนในทีมบอกอย่างเดียว เราควรจะรู้เหตุผลที่มาที่ไปของมันด้วยว่าเราใช้มันทำไม อย่าเป็นเหมือนผมเลยกว่าจะรู้ก็กดไปหลาย project ละ 555</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ddf98c51a41c" width="1" height="1" alt=""><hr><p><a href="https://medium.com/secrets-of-sellsuki/mapstate-%E0%B8%81%E0%B8%B1%E0%B8%9A-mapgetter-%E0%B8%95%E0%B9%88%E0%B8%B2%E0%B8%87%E0%B8%81%E0%B8%B1%E0%B8%99%E0%B8%A2%E0%B8%B1%E0%B8%87%E0%B9%84%E0%B8%87-ddf98c51a41c">mapState กับ mapGetter ต่างกันยังไง</a> was originally published in <a href="https://medium.com/secrets-of-sellsuki">Stories of Sellsuki</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Unit test firebase rules with targaryen]]></title>
            <link>https://medium.com/secrets-of-sellsuki/unit-test-firebase-rules-with-targaryen-174957135c99?source=rss-3e5021a94f07------2</link>
            <guid isPermaLink="false">https://medium.com/p/174957135c99</guid>
            <category><![CDATA[targaryen]]></category>
            <category><![CDATA[firebase]]></category>
            <category><![CDATA[rules]]></category>
            <category><![CDATA[unit-testing]]></category>
            <dc:creator><![CDATA[aunn]]></dc:creator>
            <pubDate>Sun, 10 Dec 2017 06:23:31 GMT</pubDate>
            <atom:updated>2017-12-13T08:04:29.133Z</atom:updated>
            <content:encoded><![CDATA[<h3>Firebase Rules คือ?</h3><p>มันคือการที่เรากำหนดสิทธิ์ ให้สามารถอ่านเขียนได้ตาม business logic ของเรา เช่น ถ้าเป็น app ที่เกี่ยวกับการ chat เราคงไม่อยากให้คนที่ไม่ได้อยู่ในห้องแชท มาส่งข้อความในห้องนั้นได้ หรือ ถ้าเป็น app จองห้องสมุด เราคงไม่อยากให้คนที่ไม่เคย login เข้าระบบเราทำการจองได้ สำหรับรายละเอียดเพิ่มเติมนั้นพี่ <a href="https://developers.ascendcorp.com/@jirawatee?source=post_header_lockup">Jirawatee</a> เขียนเรื่องนี้ไว้ดีมาก อ่านเพิ่มเติมได้ <a href="https://developers.ascendcorp.com/%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81-security-rules-%E0%B9%83%E0%B8%99-firebase-realtime-database-b1840fdc2703">ที่นี่</a></p><h3>ทำไมต้องใช้ targaryen</h3><p>ปกติการ test firebase rules จะทำได้ค่อนข้างยาก ไม่เขียน rule ด้วย <a href="https://github.com/firebase/bolt">bolt</a> ก็ต้องแก้ไฟล์ firebase.rules.json แล้วอัพโหลดไป simulate ที่ server ถ้าเราใช้ targaryen เราสามารถ unit test firebase rule ได้โดยที่ไม่ต้องเขียน bolt แล้วก็ไม่ต้องอัพโหลดไปที่ server ซึ่งสะดวกกว่ามากๆ</p><h3>Setup Project</h3><p>บทความนี้จะใช้ ava เป็น test runner นะ ซึ่ง targaryen ไม่มี plugin ให้ ava syntax ก็จะดูแปลก ๆ หน่อยนะครับ</p><p>เริ่มด้วยสร้างไฟล์ package.json ด้วย</p><pre>{<br>  &quot;scripts&quot;: {<br>    &quot;test&quot;: &quot;ava tests/*.test.js --verbose&quot;<br>  },<br>  &quot;devDependencies&quot;: {<br>    &quot;ava&quot;: &quot;^0.24.0&quot;,<br>    &quot;targaryen&quot;: &quot;^3.0.5&quot;<br>  }<br>}</pre><p>แล้วรัน</p><pre>npm install</pre><p>จากนั้นสร้างไฟล์ simple.test.js ใน folder tests ด้วย</p><pre>const test = require(&#39;ava&#39;)<br>const targaryen = require(&#39;targaryen&#39;)</pre><pre>const rules = {<br>  rules: {<br>    &#39;.read&#39;: &#39;auth !== null&#39;,<br>    &#39;.write&#39;: &#39;auth !== null&#39;<br>  }<br>};</pre><pre>const data = {<br>  message: {<br>    0: {<br>      text: &#39;hello&#39;<br>    }<br>  }<br>}</pre><pre>const database = targaryen.database(rules, data)<br>test(`Anonymous user shouldn&#39;t be able to read/write `, t =&gt; {<br>  t.is(database.read(&#39;/message&#39;).allowed, false)<br>  t.is(database.write(&#39;/message&#39;, {1: {text: &#39;hey&#39; }}).allowed, false)<br>})</pre><pre>test(&#39;Logged in user should be able to read/write&#39;, t =&gt; {<br>  t.is(database.as({uid: &#39;someuid&#39;}).read(&#39;/message&#39;).allowed, true)<br>  t.is(database.as({uid: &#39;someuid&#39;}).write(&#39;/message&#39;, {1: {text: &#39;hey&#39; }}).allowed, true)<br>})</pre><p>มาอธิบายโค้ดกัน</p><pre>const rules = {<br>  rules: {<br>    &#39;.read&#39;: &#39;auth !== null&#39;,<br>    &#39;.write&#39;: &#39;auth !== null&#39;<br>  }<br>};</pre><p>rules จะเปรียบเป็น firebase.rules.json ตามปกติแล้วถ้าเป็นโปรเจคที่ใช้ firebase เราควรจะต้อง require มา</p><pre>const database = targaryen.database(rules, data)</pre><p>บรรทัดนี้จะเป็นการ create database จาก rules และ data ที่เรา set ไว้เพื่อจะได้เอาไป test write, read ทีหลัง ดูรายละเอียดเพิ่มเติมได้จาก <a href="https://github.com/goldibex/targaryen#api">ที่นี่</a></p><pre>test(`Anonymous user shouldn&#39;t be able to read/write `, t =&gt; {<br>  t.is(database.read(&#39;/message&#39;).allowed, false)<br>  t.is(database.write(&#39;/message&#39;, {1: {text: &#39;hey&#39; }}).allowed, false)<br>})</pre><p>โค้ดชุดนี้จะเป็นการเริ่มเทส โดยผมคาดหวังว่าถ้าเป็น anonymous user จะไม่สามารถ อ่านหรือเขียนลง /message ได้</p><p>ถ้าอ่านตาม api ด้านบนจะเห็นว่า database.read(object) จะ return Result มา ซึ่งตัว Result เนี่ยเราจะสามารถใช้ property allowed ได้</p><pre>test(&#39;Logged in user should be able to read/write&#39;, t =&gt; {<br>  t.is(database.as({uid: &#39;someuid&#39;}).read(&#39;/message&#39;).allowed, true)<br>  t.is(database.as({uid: &#39;someuid&#39;}).write(&#39;/message&#39;, {1: {text: &#39;hey&#39; }}).allowed, true)<br>})</pre><p>ต่อไปจะเป็นคำสั่ง database.as(object) คำสั่งนี้จะทำการ login ด้วย object ที่เราส่งไปซึ่งจะ return เป็น database ทำให้เราสามารถใช้ .read ได้</p><p>มาลอง run test กันด้วย</p><pre>npm test</pre><p>จะได้ผลลัพธ์ประมาณนี้</p><pre>√ Anonymous user shouldn&#39;t be able to read/write<br>√ Logged in user should be able to read/write<br>2 tests passed</pre><p>เบื้องต้นในการ test firebase rules ด้วย targayen ก็มีประมาณนี้แหละครับ สำหรับ case ที่ต้องการ rules ที่ละเอียดกว่านี้ผมมีตัวอย่าง <a href="https://github.com/saturday-js/test-firebase-rules">ที่นี่</a></p><h3>References</h3><ul><li><a href="https://github.com/goldibex/targaryen">goldibex/targaryen</a></li><li><a href="https://developers.ascendcorp.com/%E0%B8%A3%E0%B8%B9%E0%B9%89%E0%B8%88%E0%B8%B1%E0%B8%81-security-rules-%E0%B9%83%E0%B8%99-firebase-realtime-database-b1840fdc2703">รู้จัก Security Rules ใน Firebase Realtime Database</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=174957135c99" width="1" height="1" alt=""><hr><p><a href="https://medium.com/secrets-of-sellsuki/unit-test-firebase-rules-with-targaryen-174957135c99">Unit test firebase rules with targaryen</a> was originally published in <a href="https://medium.com/secrets-of-sellsuki">Stories of Sellsuki</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[มาลดเวลาโหลด vue ด้วย lazy loading route กัน]]></title>
            <link>https://medium.com/secrets-of-sellsuki/%E0%B8%A1%E0%B8%B2%E0%B8%A5%E0%B8%94%E0%B9%80%E0%B8%A7%E0%B8%A5%E0%B8%B2%E0%B9%82%E0%B8%AB%E0%B8%A5%E0%B8%94-vue-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-lazy-loading-route-%E0%B8%81%E0%B8%B1%E0%B8%99-b6aa5ad76fa?source=rss-3e5021a94f07------2</link>
            <guid isPermaLink="false">https://medium.com/p/b6aa5ad76fa</guid>
            <category><![CDATA[vuejs]]></category>
            <category><![CDATA[dev]]></category>
            <dc:creator><![CDATA[aunn]]></dc:creator>
            <pubDate>Sun, 06 Aug 2017 15:31:25 GMT</pubDate>
            <atom:updated>2017-08-06T15:31:25.142Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*0R2YNW3xn6XPm4B2.jpg" /></figure><p>ด้วยตัวโปรเจคที่ผมทำอยู่ค่อนข้างใหญ่ เลยมีปัญหาเวลาโหลดครั้งแรกจะค่อนข้างช้า ก็เลยลองหาข้อมูลตรงนี้ดู ก็เลยมาเจอ <a href="https://router.vuejs.org/en/advanced/lazy-loading.html">อันนี้</a> กับคำแนะนำใน <a href="https://youtu.be/rn97hCNQsKI?t=948">วีดีโอนี้</a></p><p>มาลองเทสกันว่ามันได้ผลเป็นยังไง ก่อนอื่นมาสร้างโปรเจคใหม่กัน</p><pre>$ vue init webpack vue-test-lazy-route<br>$ cd vue-test-lazy-route<br>$ npm install</pre><p>ตอนรัน init อย่าลืมลง vue-router นะครับ</p><p>ลงเสร็จแล้วมาลองรันกันดูด้วย npm run dev ครับ จะได้ผลแบบนี้</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*94sa3ovk6drE-EzqrglDcQ.png" /></figure><p>มาสร้างหน้าใหม่เพิ่มอีกสักหน้าเอาเป็นชื่อ Other ด้วยโค้ดนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a92492e91d1dbaedd7f210073d0789f5/href">https://medium.com/media/a92492e91d1dbaedd7f210073d0789f5/href</a></iframe><p>จากนั้นมาอัพเดทไฟล์ router/index.js ให้เห็น Component Other ที่เราเพิ่มไปใหม่</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2573b8a99f26b115ae0600024e43125b/href">https://medium.com/media/2573b8a99f26b115ae0600024e43125b/href</a></iframe><p>อัพเดท template ไฟล์ Hello.vue จะได้กดไปหน้า /Other ได้ง่าย ๆ</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/138c8ffaaa16751e7fac52cbed71ff8e/href">https://medium.com/media/138c8ffaaa16751e7fac52cbed71ff8e/href</a></iframe><p>จากนั้นกด Ctrl + F5 ใน browser ที่เราเปิดไว้ เพื่อ request ไฟล์ใหม่</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*MSnyZJN3FtEGITp7aqwxhw.png" /></figure><p>จะเห็นว่าขนาด app.js เพิ่มขึ้นเป็น 1.3 MB เลยทีเดียว ตรงนี้เป็นเพราะไฟล์ Other.vue ของผมมีขนาดค่อนข้างใหญ่ เพื่อที่จะได้เห็นภาพกันว่าถ้ามีหลาย ๆ page มันจะโหลดครั้งแรกนานขนาดไหน</p><p>มาลองอัพเดทให้เป็น Lazy loading กันโดยแก้ไขไฟล์ router/index.js ให้เป็นแบบนี้</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e55fbd0e87489d361ec22dc09bc1a04b/href">https://medium.com/media/e55fbd0e87489d361ec22dc09bc1a04b/href</a></iframe><p>ลองกด Ctrl + F5 ใหม่อีกครั้ง</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*qb2TpxabEEalIAdbrXt6kQ.png" /></figure><p>จะเห็นว่าไฟล์ app.js มีขนาดเล็กลงแล้วก็มี 0.js โหลดเข้ามาใหม่ ที่เป็นแบบนี้เพราะเราเปลี่ยนโค้ดตอน import จาก</p><pre>import Hello from &#39;@/components/Hello&#39;</pre><pre>import Other from &#39;@/components/Other&#39;</pre><p>เปลี่ยนเป็น</p><pre>const Hello = () =&gt; import(&#39;@/components/Hello&#39;)</pre><pre>const Other = () =&gt; import(&#39;@/components/Other&#39;)</pre><p>มาลองคลิกไปหน้า Other ดู</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*d5UZo-RvMvDVoXPqQ74ujw.png" /></figure><p>จะเห็นว่ามีการโหลด 1.js เพิ่มอีกไฟล์ 1.js ก็เป็นไฟล์ Other.vue นั่นแหละครับ</p><p>ต้องขอบคุณ code-splitting ของ webpack ยังไงลอง clone ไปดูได้ครับที่ repo นี้</p><p><a href="https://github.com/pangaunn/vue-test-lazy-route">pangaunn/vue-test-lazy-route</a></p><p>ลองนึกภาพดูว่าถ้าเรามีหลาย ๆ page แต่ไม่ได้ทำ Lazy loading ไว้มันจะโหลดนานขนาดไหน ตรงนี้ผมค่อนข้างขัดใจกับ template webpack ของ vue-cli น่าจะทำตรงนี้มาให้ตั้งแต่แรกเลย เพราะมันค่อนข้างเป็นอะไรที่สมควรทำตาม <a href="https://youtu.be/rn97hCNQsKI?t=948">video</a> นี้ครับ</p><h3>TL;DR</h3><p>อย่าใช้ import ธรรดา ควรใช้</p><pre>const Hello = () =&gt; import(&#39;@/components/Hello&#39;)</pre><p>ท่านี้แทนเวลาโหลด route component ครับ</p><h3><strong>References</strong></h3><p><a href="https://router.vuejs.org/en/advanced/lazy-loading.html">Lazy Loading · vue-router</a></p><p><a href="https://www.youtube.com/watch?v=rn97hCNQsKI&amp;feature=youtu.be&amp;t=948">https://www.youtube.com/watch?v=rn97hCNQsKI</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b6aa5ad76fa" width="1" height="1" alt=""><hr><p><a href="https://medium.com/secrets-of-sellsuki/%E0%B8%A1%E0%B8%B2%E0%B8%A5%E0%B8%94%E0%B9%80%E0%B8%A7%E0%B8%A5%E0%B8%B2%E0%B9%82%E0%B8%AB%E0%B8%A5%E0%B8%94-vue-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-lazy-loading-route-%E0%B8%81%E0%B8%B1%E0%B8%99-b6aa5ad76fa">มาลดเวลาโหลด vue ด้วย lazy loading route กัน</a> was originally published in <a href="https://medium.com/secrets-of-sellsuki">Stories of Sellsuki</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Snapshot vue ด้วย jest กัน]]></title>
            <link>https://medium.com/secrets-of-sellsuki/snapshot-vue-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-jest-%E0%B8%81%E0%B8%B1%E0%B8%99-ad7b9b078559?source=rss-3e5021a94f07------2</link>
            <guid isPermaLink="false">https://medium.com/p/ad7b9b078559</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[snapshot]]></category>
            <category><![CDATA[dev]]></category>
            <category><![CDATA[jest]]></category>
            <category><![CDATA[vuejs]]></category>
            <dc:creator><![CDATA[aunn]]></dc:creator>
            <pubDate>Tue, 25 Jul 2017 12:34:21 GMT</pubDate>
            <atom:updated>2017-07-25T16:26:54.008Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/796/0*-ksKaqf-iJTFrwfN.png" /></figure><p>หลังจากดู <a href="https://www.youtube.com/watch?v=pqp0PsPBO_0&amp;t=1s">youtube </a>งาน Vueconf ก็เลยอยากลองดูบ้าง ครั้งนี้จะโฟกัสแค่เรื่อง snapshot</p><p>ก่อนอื่นสร้างโปรเจคก่อน</p><pre>$ vue init webpack snapshot-test<br>$ cd snapshot-test<br>$ npm install -D jest</pre><p>เลือกไม่เอา unit test นะครับเราจะมาสร้างตรงนั้นกันเอง</p><p>สร้างไฟล์ใหม่ใน tests/components/Hello.spec.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aa72b86383ef9cda9f4b5fb6ccdd4bf7/href">https://medium.com/media/aa72b86383ef9cda9f4b5fb6ccdd4bf7/href</a></iframe><p>มาลองรัน test กันด้วย node_modules\.bin\jest</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*6DE3k8NjpjmgeS1YVa5ZlQ.png" /></figure><p>ใจเย็น ๆ นะมันต้องไป setup ให้ jest อ่านไฟล์ vue ได้ก่อน</p><p>สร้างไฟล์ใหม่ใน jest/jest-vue.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e663f2590e74db26f7ef4fe26fb850e7/href">https://medium.com/media/e663f2590e74db26f7ef4fe26fb850e7/href</a></iframe><p>แล้วไปอัพเดทไฟล์ package.json คนับ</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45e711664044cc1447807e4f023c67ef/href">https://medium.com/media/45e711664044cc1447807e4f023c67ef/href</a></iframe><p>ลองรันอีกทีจะเห็นว่ามีไฟล์ snapshot แล้ว</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/788/1*RNsmofp0g6ik8EF0zYhDww.png" /></figure><p>ต่อไปมาลองเทส event โดยโค้ดจาก <a href="https://www.youtube.com/watch?v=pqp0PsPBO_0&amp;t=1s">youtube</a></p><p>สร้างไฟล์ src/components/ShoppingList.vue</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/adc2c5e41be02ef914ea9ab23856d222/href">https://medium.com/media/adc2c5e41be02ef914ea9ab23856d222/href</a></iframe><p>แล้วก็ตามด้วยไฟล์ test อีกไฟล์ tests/components/ShoppingList.spec.js</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b380933063d42de470bae667484bdfe/href">https://medium.com/media/8b380933063d42de470bae667484bdfe/href</a></iframe><p>จากโค้ดเราจะเทสว่าถ้าเราคลิก ปุ่มใน li อันที่ 3 มันจะต้อง snapshot html ที่คลิกแล้ว</p><p>มาลองรันเทสกันอีกทีครับ snapshot ก็จะมีhtml ชุดใหม่แล้ว</p><p>Reference</p><p><a href="https://hackernoon.com/jest-for-all-episode-1-vue-js-d616bccbe186">Jest for all: Episode 1 — Vue.js</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ad7b9b078559" width="1" height="1" alt=""><hr><p><a href="https://medium.com/secrets-of-sellsuki/snapshot-vue-%E0%B8%94%E0%B9%89%E0%B8%A7%E0%B8%A2-jest-%E0%B8%81%E0%B8%B1%E0%B8%99-ad7b9b078559">Snapshot vue ด้วย jest กัน</a> was originally published in <a href="https://medium.com/secrets-of-sellsuki">Stories of Sellsuki</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>