{"componentChunkName":"component---node-modules-nehalist-gatsby-theme-nehalem-src-templates-posts-tsx","path":"/","result":{"pageContext":{"posts":[{"id":"ed1cb76d-3db6-53e6-adc4-ff5bed1bdcd6","headings":[{"depth":2},{"depth":2},{"depth":2},{"depth":3},{"depth":3},{"depth":3},{"depth":2}],"frontmatter":{"title":"Going Code First: Sharing Types with TypeGraphQL","path":"/going-code-first-with-typegraphql","tags":["GraphQL","Typescript"],"excerpt":"How using a code first GraphQL approach will save you from maintaining separate GraphQL & TypeScript types. First part of the Going Code First series which dives deep into the power of GraphQL.","created":"2021-08-16T00:00:00.000Z","createdPretty":"16 August, 2021","updated":"2021-08-16T00:00:00.000Z","updatedPretty":"16 August, 2021","featuredImage":{"childImageSharp":{"sizes":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFgABAQEAAAAAAAAAAAAAAAAAAAQD/8QAFAEBAAAAAAAAAAAAAAAAAAAAA//aAAwDAQACEAMQAAABrox0JLgp/wD/xAAaEAACAgMAAAAAAAAAAAAAAAAAAwEEAhIz/9oACAEBAAEFArkSK2yUWxHI/8QAFhEAAwAAAAAAAAAAAAAAAAAAARAy/9oACAEDAQE/ATC//8QAFxEAAwEAAAAAAAAAAAAAAAAAAQIQMv/aAAgBAgEBPwFdmf/EABoQAAICAwAAAAAAAAAAAAAAAAACEBEBIVH/2gAIAQEABj8CWhb1C46JH//EAB0QAAIBBAMAAAAAAAAAAAAAAAERABAhMUFRYfD/2gAIAQEAAT8hNcSU3MzEeD3Q762cuH7NP//aAAwDAQACAAMAAAAQp8//xAAYEQACAwAAAAAAAAAAAAAAAAABECFR8P/aAAgBAwEBPxAY61//xAAXEQADAQAAAAAAAAAAAAAAAAABEHHw/9oACAECAQE/EC1F/8QAHhAAAgEDBQAAAAAAAAAAAAAAASERABBRMUFhkbH/2gAIAQEAAT8QxRAMJVMN2IKL2xDwIzpTASQZHRRb/9k=","aspectRatio":1.6025641025641026,"src":"/static/e8ca36083c2d703b73b69f825d3da6e7/ba9ae/cover.jpg","srcSet":"/static/e8ca36083c2d703b73b69f825d3da6e7/86d0f/cover.jpg 125w,\n/static/e8ca36083c2d703b73b69f825d3da6e7/520da/cover.jpg 250w,\n/static/e8ca36083c2d703b73b69f825d3da6e7/ba9ae/cover.jpg 500w,\n/static/e8ca36083c2d703b73b69f825d3da6e7/72227/cover.jpg 750w,\n/static/e8ca36083c2d703b73b69f825d3da6e7/5c39c/cover.jpg 1000w,\n/static/e8ca36083c2d703b73b69f825d3da6e7/ced74/cover.jpg 1919w","sizes":"(max-width: 500px) 100vw, 500px"}}}},"html":"<h2 id=\"introduction\" style=\"position:relative;\"><a href=\"#introduction\" aria-label=\"introduction permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Introduction</h2>\n<p>The biggest pain point of adopting GraphQL for any team (big or small) is creating your initial schema and maintaining it afterwards, period, end of story. Lets just face it, every GraphQL enthusiast has faced the same problem when explaining how amazing GraphQL is to their team for the first time and having it go like this:</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 500px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/fdd681c625fb1a88beae6ff2caccd7aa/41099/gql-intro-meme.jpg\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 136%; position: relative; bottom: 0; left: 0; background-image: url('data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAbABQDASIAAhEBAxEB/8QAGQABAAMBAQAAAAAAAAAAAAAAAAIDBAEF/8QAFgEBAQEAAAAAAAAAAAAAAAAAAQAC/9oADAMBAAIQAxAAAAH06pV4N41ZIbuCWG//xAAbEAACAgMBAAAAAAAAAAAAAAABAgASAxEyIv/aAAgBAQABBQJ3rLkzGdq59AqTj5frcx80UyiwDQ//xAAUEQEAAAAAAAAAAAAAAAAAAAAg/9oACAEDAQE/AR//xAAXEQEBAQEAAAAAAAAAAAAAAAABABAh/9oACAECAQE/AexJn//EABkQAAMBAQEAAAAAAAAAAAAAAAABMRARIf/aAAgBAQAGPwJlxo5zIRZCHh//xAAcEAEBAQEAAgMAAAAAAAAAAAABEQAhMVEQYbH/2gAIAQEAAT8hkSsPWRiTT3kFe6bClzsjn1v01Wylx3wHX5+bnoafjlAQ3//aAAwDAQACAAMAAAAQ+DGy/8QAFhEBAQEAAAAAAAAAAAAAAAAAAQAQ/9oACAEDAQE/EEic/8QAGBEBAQEBAQAAAAAAAAAAAAAAAQARMSH/2gAIAQIBAT8QUS02R4xz2//EAB0QAAMBAQACAwAAAAAAAAAAAAABESExUXFhgZH/2gAIAQEAAT8QjVY1rfgme0W0MZrynaQB2b7GTE39aQspJVwlU33Fvmep2oZsUqWDVrseusfKJ7Z20OH/2Q=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Explaining GraphQL Meme\"\n        title=\"Explaining GraphQL Meme\"\n        src=\"/static/fdd681c625fb1a88beae6ff2caccd7aa/41099/gql-intro-meme.jpg\"\n        srcset=\"/static/fdd681c625fb1a88beae6ff2caccd7aa/f93b5/gql-intro-meme.jpg 300w,\n/static/fdd681c625fb1a88beae6ff2caccd7aa/41099/gql-intro-meme.jpg 500w\"\n        sizes=\"(max-width: 500px) 100vw, 500px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"code-first-vs-schema-first\" style=\"position:relative;\"><a href=\"#code-first-vs-schema-first\" aria-label=\"code first vs schema first permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Code First vs Schema First</h2>\n<p>The biggest difference between a RESTful API &#x26; GraphQL is that GQL requires a Schema to work, much like a SQL database. Just that fact alone can be cause GraphQL to appear fairly daunting to setup &#x26; maintain. And with a schema first approach I would agree, especially when you are looking to migrate a fairly developed project, its going to take days, if not longer, to create a schema that matches your current REST system, but if you are already using Typescript (which I hope you are 😉) you might have the thought 🤔</p>\n<p><strong>\"Can I use my Typescript types as my GQL Schema?\".</strong> </p>\n<p>Unfortunately, the answer is \"No\" or at least not directly (foreshadowing). There are methods for using codegen to generate Typescript types from a GQL Schema, but that still means you have to write &#x26; maintain the Schema first. However, there is another approach which is to write code to generate a schema for you programmatically.</p>\n<h2 id=\"a-better-way---code-first\" style=\"position:relative;\"><a href=\"#a-better-way---code-first\" aria-label=\"a better way   code first permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>A better way - Code First</h2>\n<p>Writing out a whole schema often feels like a waste of time, because you often already have Typescript types or entities/models in your codebase that look similar to your Schema. </p>\n<h3 id=\"example-schema\" style=\"position:relative;\"><a href=\"#example-schema\" aria-label=\"example schema permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Example Schema</h3>\n<p>Lets say your app is a todo app and you will need a final schema that looks like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"graphql\"><pre class=\"language-graphql\"><code class=\"language-graphql\"><span class=\"token comment\">## src/schema/typedefs.gql</span>\n<span class=\"token keyword\">type</span> <span class=\"token class-name\">User</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token attr-name\">email</span><span class=\"token punctuation\">:</span> String<span class=\"token operator\">!</span>\n    <span class=\"token attr-name\">todos</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">[</span>Todo<span class=\"token operator\">!</span><span class=\"token punctuation\">]</span><span class=\"token operator\">!</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token keyword\">type</span> <span class=\"token class-name\">Todo</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token attr-name\">text</span><span class=\"token punctuation\">:</span> String<span class=\"token operator\">!</span>\n    <span class=\"token attr-name\">isDone</span><span class=\"token punctuation\">:</span> Boolean<span class=\"token operator\">!</span>\n    <span class=\"token attr-name\">owner</span><span class=\"token punctuation\">:</span> User<span class=\"token operator\">!</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<h3 id=\"entitymodels\" style=\"position:relative;\"><a href=\"#entitymodels\" aria-label=\"entitymodels permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Entity/Models</h3>\n<p>In Code first approach the goal is to generate the above example schema 👆 from the model/entity files you already probably have in your codebase that probably look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// src/entity/User.entity.ts</span>\n@<span class=\"token function\">Entity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">User</span> <span class=\"token punctuation\">{</span>\n    @<span class=\"token function\">Column</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    email<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n\n    @<span class=\"token function\">OneToMany</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    todos<span class=\"token operator\">:</span> Todo<span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// src/entity/Todo.entity.ts</span>\n@<span class=\"token function\">Entity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">Todo</span> <span class=\"token punctuation\">{</span>\n    @<span class=\"token function\">Column</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    text<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n\n    @<span class=\"token function\">Column</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    isDone<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n\n    @<span class=\"token function\">ManyToOne</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    owner<span class=\"token operator\">:</span> User\n<span class=\"token punctuation\">}</span></code></pre></div>\n<ul>\n<li>The <code class=\"language-text\">@Column()</code> decorator creates a column in the database table</li>\n<li>The <code class=\"language-text\">@ManyToOne()</code> &#x26; <code class=\"language-text\">@OneToMany()</code> decorators create relationships between the tables</li>\n</ul>\n<p><em>These decorators are based off of <a href=\"https://typeorm.io/#/\">TypeORM</a></em></p>\n<h3 id=\"solution-typegraphql-\" style=\"position:relative;\"><a href=\"#solution-typegraphql-\" aria-label=\"solution typegraphql  permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Solution: TypeGraphQL 🚀</h3>\n<p>For you to generate your schema from your model/entity files your going to need <a href=\"https://typegraphql.com\">TypeGraphQL</a>. TypeGraphQL has quite a few tools which are useful and worth looking into, but the main we are going to focus on is that it gives you access to a couple new decorators to add to your models that will allow you to generate your schema from your models 👀. To continue with our example above our new entity files are going to look like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"typescript\"><pre class=\"language-typescript\"><code class=\"language-typescript\"><span class=\"token comment\">// src/entity/User.entity.ts</span>\n@<span class=\"token function\">Entity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n@<span class=\"token function\">ObjectType</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">User</span> <span class=\"token punctuation\">{</span>\n    @<span class=\"token function\">Column</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    @<span class=\"token function\">Field</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    email<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n\n    @<span class=\"token function\">OneToMany</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    @<span class=\"token function\">Field</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> Todo<span class=\"token punctuation\">)</span>\n    todos<span class=\"token operator\">:</span> Todo<span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span>\n<span class=\"token punctuation\">}</span>\n\n<span class=\"token comment\">// src/entity/Todo.entity.ts</span>\n@<span class=\"token function\">Entity</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n@<span class=\"token function\">ObjectType</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n<span class=\"token keyword\">export</span> <span class=\"token keyword\">class</span> <span class=\"token class-name\">Todo</span> <span class=\"token punctuation\">{</span>\n    @<span class=\"token function\">Column</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    @<span class=\"token function\">Field</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    text<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n\n    @<span class=\"token function\">Column</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    @<span class=\"token function\">Field</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    isDone<span class=\"token operator\">:</span> <span class=\"token builtin\">string</span>\n\n    @<span class=\"token function\">ManyToOne</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span>\n    @<span class=\"token function\">Field</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">[</span>User<span class=\"token punctuation\">]</span><span class=\"token punctuation\">)</span>\n    owner<span class=\"token operator\">:</span> User\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>As you can see we added <code class=\"language-text\">@Field()</code> &#x26; <code class=\"language-text\">@ObjectType()</code> decorators to our models, the <code class=\"language-text\">@ObjectType()</code> decorators tells TypeGraphQL that the class should be a <code class=\"language-text\">type</code> in your schema, and the <code class=\"language-text\">@Field</code> is for making properties of the class turn into properties of the type inside your GQL Schema. All and all adding a couple decorators was all it took to stop you and your team from maintaining separate Typescript &#x26; GraphQL types. However, the real power of TypeGraphQL is that now your GQL types are available in code at compile time, which leads to all sorts of cool codegen possibilities.</p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>This was a very brief intro into Code First GraphQL using <a href=\"https://typegraphql.com\">TypeGraphQL</a>, but its just the tip of the iceberg, and in future blog posts I'm going to dive deeper into how you can utilize this to generate not only your model Schema, but also your CRUD resolvers and their Schemas using TypeGraphQL. Until then, let me know what you think in the comments and also feel free to ask me any clarifying questions.</p>"},{"id":"c1a9e7cf-9fd6-5e1a-8571-f13c8e00bedc","headings":[{"depth":2},{"depth":2},{"depth":2},{"depth":3},{"depth":3},{"depth":3},{"depth":3},{"depth":3},{"depth":3},{"depth":2}],"frontmatter":{"title":"Github Actions: Getting Started Guide","path":"/getting-started-with-github-actions","tags":["DevOps"],"excerpt":"How to get up and running with Github Actions, in only a couple minutes.","created":"2020-11-23T00:00:00.000Z","createdPretty":"23 November, 2020","updated":"2020-11-24T00:00:00.000Z","updatedPretty":"23 November, 2020","featuredImage":{"childImageSharp":{"sizes":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAOABQDASIAAhEBAxEB/8QAGAAAAwEBAAAAAAAAAAAAAAAAAAIFAQT/xAAVAQEBAAAAAAAAAAAAAAAAAAAAAf/aAAwDAQACEAMQAAAB1u17JxTD/8QAGRABAQADAQAAAAAAAAAAAAAAAQACERIT/9oACAEBAAEFAuS8y4nAYxC1f//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQMBAT8BP//EABQRAQAAAAAAAAAAAAAAAAAAABD/2gAIAQIBAT8BP//EABYQAAMAAAAAAAAAAAAAAAAAAAEQMf/aAAgBAQAGPwKhwP8A/8QAGxABAQEAAgMAAAAAAAAAAAAAAREAIVFxgaH/2gAIAQEAAT8h54h71CiM6wUsyl+OAgB40db/2gAMAwEAAgADAAAAEOAP/8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPxA//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAgEBPxA//8QAGRABAQEBAQEAAAAAAAAAAAAAAREAITFh/9oACAEBAAE/EOxIPRF1kb6q4mVHpSOe9T6pV3ZGaxO74t//2Q==","aspectRatio":1.4204545454545454,"src":"/static/eb51d7ccf12dc52a402253785972df62/ba9ae/cover.jpg","srcSet":"/static/eb51d7ccf12dc52a402253785972df62/86d0f/cover.jpg 125w,\n/static/eb51d7ccf12dc52a402253785972df62/520da/cover.jpg 250w,\n/static/eb51d7ccf12dc52a402253785972df62/ba9ae/cover.jpg 500w,\n/static/eb51d7ccf12dc52a402253785972df62/72227/cover.jpg 750w,\n/static/eb51d7ccf12dc52a402253785972df62/5c39c/cover.jpg 1000w,\n/static/eb51d7ccf12dc52a402253785972df62/b1e91/cover.jpg 1920w","sizes":"(max-width: 500px) 100vw, 500px"}}}},"html":"<h2 id=\"overview\" style=\"position:relative;\"><a href=\"#overview\" aria-label=\"overview permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Overview</h2>\n<p>Hello again! Today I'm going to get you started using Github Actions. Github Actions are an absolutely amazing resource for helping developers perform mundane tasks automatically. If you have never setup CI/CD systems before and want to get started, there is no better way than with Github Actions. So, let's get started!</p>\n<h2 id=\"github-actions-pricing\" style=\"position:relative;\"><a href=\"#github-actions-pricing\" aria-label=\"github actions pricing permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Github Actions Pricing</h2>\n<p><strong>TLDR; It's free for public repos and really reasonable for private repos with a very generous free tier! (2000 minutes free)</strong> </p>\n<p>So, let's talk pricing first since you don't want to read a tutorial about some great product that you can't afford. Luckily for you Github Actions Pricing is very affordable and by affordable I mean basically free for most users (I personally haven't payed a dime) and still pretty cost-effective for larger teams and organizations. But you don't have to take my word for it. Their <a href=\"https://github.com/pricing\">pricing page</a> is super straightforward and gives a really solid outline of how the tiered system works.</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1200px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/c10409d55186f57a0d7d689007fb3107/a2b91/GithubPricing.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 50.66666666666667%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAABYlAAAWJQFJUiTwAAACQUlEQVQoz3VSO4sUQRBeMBN8BgYXiok/wEjBU1FPTRTkDAwEUU89DwM5QVA0EfG8wFMxMPHwXD1xzU48A0Ew8BEJGon32MfMzkz39EzP7M6rZ/ezenYXQbCg6P66qr/6uqtKIOt2u/jXVJaCMwfS9yFcF7ZtgXMGT7j4n6VpitKALM1yiCAG9yO4MoLFJVbqFmqmg3qTYbVhYalq0mrDky14HhUSHjw/gMo7BUee5ygp1QMPK1+xZvctbD/5GFtHH2DbiUcYOnofGw7exeZD97BxZAqbDk9j/YE7WDsygy2nFjB0+h3WHX+JxS+/e69SmrDP/urDT+w48wSjN17j2LV5HJksY9/lZ9gzMVv48KVZwnMYHn+KneNl7L3+EftvfsKuKwv4/KPWI9QKs0zRczPknQ70nnFBWCGjakHYgi9lkZhRjmGYBaZfh/RcGPUqkqhVKOvdUX3CNCtAnCTgrij+Qp8FQViQ6n0UxbAdDklnukCrHcNhLt1JMeDQwkqaSFcIojY1wcBq3YTwQzBfYrnaQKPpwA/bMBnHSs0oYrIdFXmGxSgWIekL0qSFQk2YUMst26bxsBHHMSkLCVvwaWz0OAhPwDRNhHQeJzF1WVBeRK9RfwkHCnPydqJQ4zGWmxIs7MCWGZZMiSprwwkoxiL8anhouAnFFAy/gxWeoy4UIrqrv6kYmyRVeoLwvRbjwguOi2UHV99wTL8XODfnkNu4/dbFZIVjjPBZ8qlFgYl5hrHnDOfLNPR+Cr+VovJN4A+qjMVb1aIzOgAAAABJRU5ErkJggg=='); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Github Pricing Plan Page Screenshot\"\n        title=\"Github Pricing Plan Page Screenshot\"\n        src=\"/static/c10409d55186f57a0d7d689007fb3107/c1b63/GithubPricing.png\"\n        srcset=\"/static/c10409d55186f57a0d7d689007fb3107/5a46d/GithubPricing.png 300w,\n/static/c10409d55186f57a0d7d689007fb3107/0a47e/GithubPricing.png 600w,\n/static/c10409d55186f57a0d7d689007fb3107/c1b63/GithubPricing.png 1200w,\n/static/c10409d55186f57a0d7d689007fb3107/d61c2/GithubPricing.png 1800w,\n/static/c10409d55186f57a0d7d689007fb3107/97a96/GithubPricing.png 2400w,\n/static/c10409d55186f57a0d7d689007fb3107/a2b91/GithubPricing.png 3360w\"\n        sizes=\"(max-width: 1200px) 100vw, 1200px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"getting-started\" style=\"position:relative;\"><a href=\"#getting-started\" aria-label=\"getting started permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting Started</h2>\n<p>For starters, I'm going to assume you have a repo, preferably a Javascript repo, that you'll want to follow along with in this Github Actions Tutorial. In this run through I'm going to go over how to setup an automatic eslint checker for your code! Exciting stuff I know, so let's get started!</p>\n<h3 id=\"create-github-workflows-folder\" style=\"position:relative;\"><a href=\"#create-github-workflows-folder\" aria-label=\"create github workflows folder permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create Github Workflows Folder</h3>\n<p>First things first we need to create the <code class=\"language-text\">.github/workflows</code> folders in the root of your repo. You can do this in your VS Code sidebar or by using the terminal with the following code:</p>\n<p> <code class=\"language-text\">mkdir .github/workflows</code></p>\n<p>This is where all your Github Actions live in your repo. By adding this folder we are letting Github know that there are actions inside this repo that it needs to be executed the next time you push up your code.</p>\n<h3 id=\"create-our-first-github-action\" style=\"position:relative;\"><a href=\"#create-our-first-github-action\" aria-label=\"create our first github action permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Create Our First Github Action</h3>\n<p>Next, let's go ahead and create our file. Github Actions use YAML files for configuration so our file will end with <code class=\"language-text\">.yml</code>. In this case let's call just call it <code class=\"language-text\">eslint.yml</code>, again you can create it in VS Code or using the following command:</p>\n<p><code class=\"language-text\">touch .github/workflows/eslint.yml</code></p>\n<p>Go ahead and open your new file and let's start coding!</p>\n<h3 id=\"name-our-workflow\" style=\"position:relative;\"><a href=\"#name-our-workflow\" aria-label=\"name our workflow permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Name Our Workflow</h3>\n<p>The first thing we need to add is name of our Github Action Workflow. Like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token comment\"># eslint.yml</span>\n\n<span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Our First Eslint Checker 👶</code></pre></div>\n<h3 id=\"specify-when-our-workflow-should-trigger\" style=\"position:relative;\"><a href=\"#specify-when-our-workflow-should-trigger\" aria-label=\"specify when our workflow should trigger permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Specify When Our Workflow Should Trigger</h3>\n<p>Next, we are going to define when this action should trigger. There are a couple options here, but we're going to go with whenever a <strong>Pull Request</strong> is <strong>created</strong> or <strong>updated</strong>, which we specify in the file using the <code class=\"language-text\">on</code> keyword:</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token comment\"># eslint.yml</span>\n\n<span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Our First Eslint Checker 👶\n\n<span class=\"token key atrule\">on</span><span class=\"token punctuation\">:</span>\n  <span class=\"token key atrule\">pull_request</span><span class=\"token punctuation\">:</span>\n    <span class=\"token key atrule\">branches</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">[</span> main <span class=\"token punctuation\">]</span> <span class=\"token comment\"># PRs to main only</span></code></pre></div>\n<h3 id=\"setup-our-workflow-environment\" style=\"position:relative;\"><a href=\"#setup-our-workflow-environment\" aria-label=\"setup our workflow environment permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setup Our Workflow Environment</h3>\n<p>Ok, the last thing we have to do before we get to the meat of it is specify the environment or virtual machine we want our CI to run in. For most cases using ubuntu-latest is going to be our best bet. So, let's do that:</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token comment\"># eslint.yml</span>\n\n<span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Our First Eslint Checker 👶\n\n<span class=\"token key atrule\">on</span><span class=\"token punctuation\">:</span>\n  <span class=\"token key atrule\">pull_request</span><span class=\"token punctuation\">:</span>\n    <span class=\"token key atrule\">branches</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">[</span> main <span class=\"token punctuation\">]</span> <span class=\"token comment\"># PRs to main only</span>\n\n<span class=\"token key atrule\">jobs</span><span class=\"token punctuation\">:</span> <span class=\"token comment\"># Jobs to be run</span>\n  <span class=\"token key atrule\">run-linters</span><span class=\"token punctuation\">:</span> <span class=\"token comment\"># Name of job (pretty arbitrary can be called basically anything)</span>\n    <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Lint Frontend <span class=\"token comment\"># (!Important) This name will show up in the Github UI so its important</span>\n    <span class=\"token key atrule\">runs-on</span><span class=\"token punctuation\">:</span> ubuntu<span class=\"token punctuation\">-</span>latest <span class=\"token comment\"># Virtual env to use</span></code></pre></div>\n<h3 id=\"define-our-workflow-steps\" style=\"position:relative;\"><a href=\"#define-our-workflow-steps\" aria-label=\"define our workflow steps permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Define Our Workflow Steps</h3>\n<p>Ok last, but not least, we get to define the steps that our action should take to perform our linting task. These steps operate very similarly to pseudo code, because each step has a name (which will be visible in the Github UI for debugging) and an action that it executes. The actions can either be a command that the terminal will execute or better yet, you can it can execute an entire plugin from the Github Marketplace. So let's go ahead and layout our actions in a step-by-step manner:</p>\n<div class=\"gatsby-highlight\" data-language=\"yaml\"><pre class=\"language-yaml\"><code class=\"language-yaml\"><span class=\"token comment\"># eslint.yml</span>\n\n<span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Our First Eslint Checker 👶\n\n<span class=\"token key atrule\">on</span><span class=\"token punctuation\">:</span>\n  <span class=\"token key atrule\">pull_request</span><span class=\"token punctuation\">:</span>\n    <span class=\"token key atrule\">branches</span><span class=\"token punctuation\">:</span> <span class=\"token punctuation\">[</span> main <span class=\"token punctuation\">]</span> <span class=\"token comment\"># PRs to main only</span>\n\n<span class=\"token key atrule\">jobs</span><span class=\"token punctuation\">:</span> <span class=\"token comment\"># Jobs to be run</span>\n  <span class=\"token key atrule\">run-linters</span><span class=\"token punctuation\">:</span> <span class=\"token comment\"># Name of job (pretty arbitrary can be called basically anything)</span>\n    <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Lint Code <span class=\"token comment\"># (!Important) This name will show up in the Github UI so its important</span>\n    <span class=\"token key atrule\">runs-on</span><span class=\"token punctuation\">:</span> ubuntu<span class=\"token punctuation\">-</span>latest <span class=\"token comment\"># Virtual env to use</span>\n\n    <span class=\"token key atrule\">steps</span><span class=\"token punctuation\">:</span>\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Check out Git repository <span class=\"token comment\"># The first step of most actions is going to </span>\n        <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/checkout@v2      <span class=\"token comment\"># use this plugin to clone the repo to the virtual machine</span>\n\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Set up Node.js <span class=\"token comment\"># Secondarily because this is a javascript ecosystem we need to install node</span>\n        <span class=\"token key atrule\">uses</span><span class=\"token punctuation\">:</span> actions/setup<span class=\"token punctuation\">-</span>node@v1 <span class=\"token comment\"># This plugin takes care of that for us</span>\n        <span class=\"token key atrule\">with</span><span class=\"token punctuation\">:</span>\n          <span class=\"token key atrule\">node-version</span><span class=\"token punctuation\">:</span> <span class=\"token number\">12</span> <span class=\"token comment\"># Some plugins allow for configuration like this</span>\n\n      <span class=\"token comment\"># Install our packages now that we have node</span>\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Install Node.js dependencies\n        <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> npx yarn install <span class=\"token comment\"># I use npx here so I don't have to install yarn globally on this VM</span>\n\n      <span class=\"token comment\"># Lint the folder system</span>\n      <span class=\"token punctuation\">-</span> <span class=\"token key atrule\">name</span><span class=\"token punctuation\">:</span> Lint Code\n        <span class=\"token key atrule\">run</span><span class=\"token punctuation\">:</span> yarn eslint . <span class=\"token comment\"># You could customize this line to use whatever options you needed.</span></code></pre></div>\n<p>And there you have it. Go ahead and create a PR with these changes and you will see the Github Action takes no time in telling you about all your eslint errors!</p>\n<p><span\n      class=\"gatsby-resp-image-wrapper\"\n      style=\"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 1200px; \"\n    >\n      <a\n    class=\"gatsby-resp-image-link\"\n    href=\"/static/61c824df0e0bdd988d8445339382fa92/c9d77/ActionScreenshot.png\"\n    style=\"display: block\"\n    target=\"_blank\"\n    rel=\"noopener\"\n  >\n    <span\n    class=\"gatsby-resp-image-background-image\"\n    style=\"padding-bottom: 21.333333333333336%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAYAAACOXx+WAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAyUlEQVQY022P22oDMQxE9/9/rFAKeelrKQlNdiPfLV92nXinWkPzVMFhkDWMrKn3jvZ4gHNFGpQXzgc452Gtg7F2aIw8IFIgpaGEOxFqrdj3HdPH6RNv7yes5YLCM1JYhBk53sH+R940uhj/r33MYnkirR29S2CpK3KpqIlwu56hNclWgjELjJ7lFwQfIsrhWbfB4T/YtjYCa3tKaENrDdPfrpgyvr7PWJTFdVbjDGXs6G+LnKcdyDjYwHAxwYkGLtDWSy/qM5gZvyi/Mqw1hW+aAAAAAElFTkSuQmCC'); background-size: cover; display: block;\"\n  ></span>\n  <img\n        class=\"gatsby-resp-image-image\"\n        alt=\"Github Action in Action!\"\n        title=\"Github Action in Action!\"\n        src=\"/static/61c824df0e0bdd988d8445339382fa92/c1b63/ActionScreenshot.png\"\n        srcset=\"/static/61c824df0e0bdd988d8445339382fa92/5a46d/ActionScreenshot.png 300w,\n/static/61c824df0e0bdd988d8445339382fa92/0a47e/ActionScreenshot.png 600w,\n/static/61c824df0e0bdd988d8445339382fa92/c1b63/ActionScreenshot.png 1200w,\n/static/61c824df0e0bdd988d8445339382fa92/d61c2/ActionScreenshot.png 1800w,\n/static/61c824df0e0bdd988d8445339382fa92/c9d77/ActionScreenshot.png 1964w\"\n        sizes=\"(max-width: 1200px) 100vw, 1200px\"\n        style=\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;\"\n        loading=\"lazy\"\n      />\n  </a>\n    </span></p>\n<h2 id=\"conclusion\" style=\"position:relative;\"><a href=\"#conclusion\" aria-label=\"conclusion permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Conclusion</h2>\n<p>There is so much more you can do with Github Actions, and I will probably create more tutorials in the future taking about this in more depth, so stay turned for those upcoming posts. If you have any more questions feel free to reach out either via my contact page or better yet leave a comment below and I'd be happy to help you in anyway I can!</p>"},{"id":"745aa689-ebf0-5f72-8f96-3dec3cf511d2","headings":[{"depth":2},{"depth":2},{"depth":3},{"depth":3},{"depth":3},{"depth":2},{"depth":3},{"depth":4},{"depth":2},{"depth":3},{"depth":3}],"frontmatter":{"title":"Callback Functions: Simple Nodejs File IO","path":"/nodejs-file-io-using-callbacks","tags":["NodeJS"],"excerpt":"How to use Callback Functions to handle asynchronous tasks such as reading and writing files in Nodejs.","created":"2019-10-19T00:00:00.000Z","createdPretty":"19 October, 2019","updated":"2019-10-19T00:00:00.000Z","updatedPretty":"19 October, 2019","featuredImage":{"childImageSharp":{"sizes":{"base64":"data:image/jpeg;base64,/9j/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wgARCAAMABQDASIAAhEBAxEB/8QAFwAAAwEAAAAAAAAAAAAAAAAAAAIDBP/EABYBAQEBAAAAAAAAAAAAAAAAAAIAAf/aAAwDAQACEAMQAAABkq4XmsoK/8QAGRABAAMBAQAAAAAAAAAAAAAAAQACEhED/9oACAEBAAEFAt8j6WjdmBLHL4J//8QAFBEBAAAAAAAAAAAAAAAAAAAAEP/aAAgBAwEBPwE//8QAFREBAQAAAAAAAAAAAAAAAAAAEBL/2gAIAQIBAT8Bo//EABgQAAIDAAAAAAAAAAAAAAAAAAAQETFB/9oACAEBAAY/AilpC//EABsQAAMAAwEBAAAAAAAAAAAAAAABESFBcTFR/9oACAEBAAE/IcF+KnoWHSrYoVTjMdblLbZ//9oADAMBAAIAAwAAABCLz//EABURAQEAAAAAAAAAAAAAAAAAABAR/9oACAEDAQE/EIf/xAAYEQACAwAAAAAAAAAAAAAAAAAAASExUf/aAAgBAgEBPxBtaJ0//8QAGxABAAIDAQEAAAAAAAAAAAAAAQARITFhkeH/2gAIAQEAAT8QNYpoTECPS/M0yeUYipmL6ewnp4wsc5tek//Z","aspectRatio":1.6233766233766234,"src":"/static/8f2afe03460156f93ec3da8886e76b96/ba9ae/cover.jpg","srcSet":"/static/8f2afe03460156f93ec3da8886e76b96/86d0f/cover.jpg 125w,\n/static/8f2afe03460156f93ec3da8886e76b96/520da/cover.jpg 250w,\n/static/8f2afe03460156f93ec3da8886e76b96/ba9ae/cover.jpg 500w,\n/static/8f2afe03460156f93ec3da8886e76b96/72227/cover.jpg 750w,\n/static/8f2afe03460156f93ec3da8886e76b96/5c39c/cover.jpg 1000w,\n/static/8f2afe03460156f93ec3da8886e76b96/b1e91/cover.jpg 1920w","sizes":"(max-width: 500px) 100vw, 500px"}}}},"html":"<h2 id=\"overview\" style=\"position:relative;\"><a href=\"#overview\" aria-label=\"overview permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Overview</h2>\n<p>Hi guys! Today, I will be walking you through how to use Callback functions with Asynchronous tasks in Nodejs. The main goal of this tutorial will be to familiarize yourself with reading files in Nodejs while utilizing callback functions. I will be adding more posts in the future, on how to do the same thing with Promises and ES6 Async/Await. However, if you are just starting out callbacks is a great place to start! So, let's get started!</p>\n<h2 id=\"setup\" style=\"position:relative;\"><a href=\"#setup\" aria-label=\"setup permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Setup</h2>\n<h3 id=\"getting-the-files\" style=\"position:relative;\"><a href=\"#getting-the-files\" aria-label=\"getting the files permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Getting the files</h3>\n<p>A quick note: Before you can follow the steps of this tutorial you will need to have Nodejs installed on your system as well as Git.</p>\n<p>To get started, run this command to git clone the repo:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\"><span class=\"token comment\"># using https</span>\n<span class=\"token function\">git</span> clone --branch Starting-Point-v2 https://github.com/andrewgeorgemitchell/amwebdev-nodejs-file-io-tut.git\n \n<span class=\"token comment\"># using ssh</span>\n<span class=\"token function\">git</span> clone --branch Starting-Point-v2 git@github.com:andrewgeorgemitchell/amwebdev-nodejs-file-io-tut.git</code></pre></div>\n<p><em>If your are curious what the --branch Starting-Point-v2 does, it clones the repo at a tag I setup to allow you to follow along in the tutorial from the beginning. If you wish to just see the finished repo simply remove --branch Starting-Point-v2 from the command. Also, if you’ve already cloned the repo, run git checkout master and you will be checked out to the latest commit.</em></p>\n<p>From here, you can open the folder in your favorite code editor aka <strong>VS Code</strong>.</p>\n<h3 id=\"the-file-structure\" style=\"position:relative;\"><a href=\"#the-file-structure\" aria-label=\"the file structure permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>The File Structure</h3>\n<div class=\"gatsby-highlight\" data-language=\"jsx\"><pre class=\"language-jsx\"><code class=\"language-jsx\"><span class=\"token operator\">-</span> amwebdev<span class=\"token operator\">-</span>nodejs<span class=\"token operator\">-</span>file<span class=\"token operator\">-</span>io<span class=\"token operator\">-</span>tut<span class=\"token operator\">/</span>\n  <span class=\"token operator\">-</span> index<span class=\"token punctuation\">.</span>js <span class=\"token comment\">// our entry point file where we will right our program</span>\n  <span class=\"token operator\">-</span> data<span class=\"token operator\">/</span> <span class=\"token comment\">// folder where will read and write our data</span>\n    <span class=\"token operator\">-</span> users<span class=\"token punctuation\">.</span>csv <span class=\"token comment\">// a csv file I made to for data to read from</span>\n  <span class=\"token operator\">-</span> <span class=\"token keyword\">package</span><span class=\"token punctuation\">.</span>json <span class=\"token comment\">// npm package.json (not used in this tutorial)</span></code></pre></div>\n<h3 id=\"nodejs-modules\" style=\"position:relative;\"><a href=\"#nodejs-modules\" aria-label=\"nodejs modules permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Nodejs Modules</h3>\n<p>In Nodejs most of the core functionality has been segmented out into modules, for example in this tutorial we will be using the 'fs' module for reading and writing to the filesystem. Importing the fs module is simple just <strong>add the following to the top of index.js</strong>:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> fs <span class=\"token operator\">=</span> <span class=\"token function\">require</span><span class=\"token punctuation\">(</span><span class=\"token string\">'fs'</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p><em>Note: You don't have to run npm install fs because fs is included by default inside your installation of Nodejs. However for non-core Nodejs packages you will have to install them using npm.</em></p>\n<h2 id=\"how-to-readwrite-files-in-3-different-methods\" style=\"position:relative;\"><a href=\"#how-to-readwrite-files-in-3-different-methods\" aria-label=\"how to readwrite files in 3 different methods permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>How to Read/Write files in 3 Different Methods</h2>\n<p>When reading and writing from files in Nodejs, you are performing what is called an asynchronous action, meaning an action that has no scheduled or predictable end time. Nodejs has 3 different syntactical ways of dealing with asynchronous action and while some are definitely better than others (cough cough async/await) all of them are worth understanding, because the likelihood you come across all three is very high. So we will go over all of them in order of difficulty (for beginners) as well as the order that most people should learn them in.</p>\n<p><strong>The three methods are:</strong></p>\n<ul>\n<li><a href=\"#callback-method\">Callback Method</a></li>\n<li>Promise Method (Coming Soon)</li>\n<li>Async/Await Method (Coming Soon)</li>\n</ul>\n<h3 id=\"callback-method\" style=\"position:relative;\"><a href=\"#callback-method\" aria-label=\"callback method permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Callback Method</h3>\n<p>Simply put, callbacks are a function which you pass into an asynchronous function  that will be executed once the asynchronous function finishes. So in layman terms it works like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Asynchronous Function: Do some asynchronous action such as read from file</span>\n  <span class=\"token comment\">// Callback Function: Once your done do something with the file</span></code></pre></div>\n<p>The strange thing about this is that we don't know when the second line or callback function will execute. It could be in a minute, or in an hour. So in order to avoid errors, we have to make sure our callback function doesn't rely on anything but what is getting passed to it from the original asynchronous function.</p>\n<p>As a final note, dealing with errors in callbacks can be somewhat repetitive, but it needs to be done, so we will employ what is called error first callbacks which deal with the error case first, then deals with the success case.</p>\n<p>So let’s adjust the pseudo code to handle errors:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Asynchronous Function: Do some asynchronous action</span>\n  <span class=\"token comment\">// Callback Function: Once said action is done</span>\n    <span class=\"token comment\">// If there is an error deal with it</span>\n    <span class=\"token comment\">// If there isn't an error do what I wanted</span></code></pre></div>\n<h4 id=\"reading-from-file\" style=\"position:relative;\"><a href=\"#reading-from-file\" aria-label=\"reading from file permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Reading from file</h4>\n<p>When it comes to implementing a function to read a file from the filesystem we will take advantage of nodes built in fs (filesystem) module to do this. Specifically we will be using the <a href=\"https://nodejs.org/api/fs.html\">fs.readFile</a> method which takes two arguments:</p>\n<ul>\n<li>the path to the file you want to read</li>\n<li>the callback function to execute once it has the files data</li>\n</ul>\n<p>So let’s take the pseudo code from above and add some Nodejs skeleton to it.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Callback Function: Do something with the file once its read</span>\n<span class=\"token keyword\">const</span> <span class=\"token function-variable function\">readFileCallbackFunction</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> file</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// If there is an error deal with it</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">throw</span> error<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token comment\">// If there isn't an error do what I wanted with the file</span>\n<span class=\"token punctuation\">}</span>\n \n<span class=\"token comment\">// Asynchronous Function: Read file</span>\nfs<span class=\"token punctuation\">.</span><span class=\"token function\">readFile</span><span class=\"token punctuation\">(</span>filepath<span class=\"token punctuation\">,</span> readFileCallbackFunction<span class=\"token punctuation\">)</span></code></pre></div>\n<p>So above you can see we actually moved the callback function above the fs.readFile which seems counterintuitive but we are actually just declaring the callback function so it can be <strong>passed down into</strong> fs.readFile(). This method is actually somewhat unorthodox as normally what is very common for callback functions is not give it a name at all, but instead to just declare it as an anonymous function when you need it like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Asynchronous Function: Read file</span>\nfs<span class=\"token punctuation\">.</span><span class=\"token function\">readFile</span><span class=\"token punctuation\">(</span>filepath<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> file</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// If there is an error deal with it</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">throw</span> error<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token comment\">// If there isn't an error do what I wanted</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Great! Now we are using an anonymous function, which is definitely the more popular approach when using callbacks. That being said, this can definitely be more confusing for beginners, so if you wish to use the first method feel free, but I will be using the second as it is more of an industry standard.</p>\n<p>Now we actually have a working function that will read the file’s contents, but we need to do something with the raw file data that was passed into our callback by fs.readFile. Spoiler alert, fs.readFile gives the raw buffer data which isn't very useful to anyone, so let’s look into how to change into a string. So let’s use the very helpful .toString method available on any variable that is of type buffer.</p>\n<p>To do this we need only add one line, but let’s also add the code to actually read the file of our choosing because filepath is not defined yet: </p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> filepath <span class=\"token operator\">=</span> <span class=\"token template-string\"><span class=\"token template-punctuation string\">`</span><span class=\"token interpolation\"><span class=\"token interpolation-punctuation punctuation\">${</span>__dirname<span class=\"token interpolation-punctuation punctuation\">}</span></span><span class=\"token string\">/data/users.csv</span><span class=\"token template-punctuation string\">`</span></span><span class=\"token punctuation\">;</span>\n \n<span class=\"token comment\">// Asynchronous Function: Read file</span>\nfs<span class=\"token punctuation\">.</span><span class=\"token function\">readFile</span><span class=\"token punctuation\">(</span>filepath<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> file</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// If there is an error deal with it</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">throw</span> error<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token comment\">// If there isn't an error do what I wanted</span>\n  <span class=\"token keyword\">const</span> fileStringData <span class=\"token operator\">=</span> file<span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  console<span class=\"token punctuation\">.</span><span class=\"token function\">log</span><span class=\"token punctuation\">(</span>fileStringData<span class=\"token punctuation\">)</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<p>Now if we run our program using this command:</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">node index.js</code></pre></div>\n<p>We will get this output to the console.</p>\n<div class=\"gatsby-highlight\" data-language=\"bash\"><pre class=\"language-bash\"><code class=\"language-bash\">name, age, favAnimal\nEli Miller, <span class=\"token number\">24</span>, Dog\nJohn Smith, <span class=\"token number\">35</span>, Tiger\nBetsy Blue, <span class=\"token number\">19</span>, Warthog\nJustin Long, <span class=\"token number\">15</span>, Cat</code></pre></div>\n<p>This isn't very useful though so let’s convert this CSV string into a javascript array of user objects where the first row is used as the keys and the rows after that are the values. To keep our code clean, let’s create a helper function to do this called convert csvToObject.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token keyword\">const</span> <span class=\"token function-variable function\">csvToObject</span> <span class=\"token operator\">=</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">csvString</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token keyword\">const</span> splitByLineEndingAndCommas <span class=\"token operator\">=</span> csvString\n    <span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">'\\n'</span><span class=\"token punctuation\">)</span> <span class=\"token comment\">// splits the string by new line so we end of with an array where each line is now on its own</span>\n    <span class=\"token punctuation\">.</span><span class=\"token function\">map</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">(</span><span class=\"token parameter\">lineString</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> lineString<span class=\"token punctuation\">.</span><span class=\"token function\">split</span><span class=\"token punctuation\">(</span><span class=\"token string\">','</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span> <span class=\"token comment\">// Split each line by , so that each value is on its own</span>\n \n  <span class=\"token comment\">// We end up with this:</span>\n  <span class=\"token comment\">// [</span>\n  <span class=\"token comment\">//   [ 'name', ' age', ' favAnimal' ],</span>\n  <span class=\"token comment\">//   [ 'Eli Miller', ' 24', ' Dog' ],</span>\n  <span class=\"token comment\">//   [ 'John Smith', ' 35', ' Tiger' ],</span>\n  <span class=\"token comment\">//   [ 'Betsy Blue', ' 19', ' Warthog' ],</span>\n  <span class=\"token comment\">//   [ 'Justin Long', ' 15', ' Cat' ]</span>\n  <span class=\"token comment\">// ]</span>\n \n  <span class=\"token comment\">// Create a header row for reference</span>\n  <span class=\"token keyword\">const</span> headerRow <span class=\"token operator\">=</span> splitByLineEndingAndCommas<span class=\"token punctuation\">[</span><span class=\"token number\">0</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n  <span class=\"token comment\">// Create our array of user objects</span>\n  <span class=\"token keyword\">const</span> users <span class=\"token operator\">=</span> <span class=\"token punctuation\">[</span><span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n  <span class=\"token comment\">// Loop over the lines skipping the first header row because it doesn't represent a user</span>\n  <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> i <span class=\"token operator\">=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">;</span> i <span class=\"token operator\">&lt;</span> splitByLineEndingAndCommas<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> i <span class=\"token operator\">+=</span> <span class=\"token number\">1</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token comment\">// Create the blank object for the current user</span>\n    <span class=\"token keyword\">const</span> newUser <span class=\"token operator\">=</span> <span class=\"token punctuation\">{</span><span class=\"token punctuation\">}</span><span class=\"token punctuation\">;</span>\n    <span class=\"token comment\">// Create a reference variable to the current line array</span>\n    <span class=\"token keyword\">const</span> line <span class=\"token operator\">=</span> splitByLineEndingAndCommas<span class=\"token punctuation\">[</span>i<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n    <span class=\"token comment\">// Loop over the headers row</span>\n    <span class=\"token keyword\">for</span> <span class=\"token punctuation\">(</span><span class=\"token keyword\">let</span> b <span class=\"token operator\">=</span> <span class=\"token number\">0</span><span class=\"token punctuation\">;</span> b <span class=\"token operator\">&lt;</span> headerRow<span class=\"token punctuation\">.</span>length<span class=\"token punctuation\">;</span> b<span class=\"token operator\">++</span><span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n      <span class=\"token keyword\">const</span> headerKey <span class=\"token operator\">=</span> headerRow<span class=\"token punctuation\">[</span>b<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n      <span class=\"token comment\">// Assign the key value pair for each header</span>\n      newUser<span class=\"token punctuation\">[</span>headerKey<span class=\"token punctuation\">]</span> <span class=\"token operator\">=</span> line<span class=\"token punctuation\">[</span>b<span class=\"token punctuation\">]</span><span class=\"token punctuation\">;</span>\n    <span class=\"token punctuation\">}</span>\n    <span class=\"token comment\">// Push the finished user to the users array</span>\n    users<span class=\"token punctuation\">.</span><span class=\"token function\">push</span><span class=\"token punctuation\">(</span>newUser<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n \n  <span class=\"token keyword\">return</span> users<span class=\"token punctuation\">;</span>\n<span class=\"token punctuation\">}</span></code></pre></div>\n<p>Which results in very clean data like this:</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\">users<span class=\"token operator\">:</span> <span class=\"token punctuation\">[</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token operator\">:</span> <span class=\"token string\">'Eli Miller'</span><span class=\"token punctuation\">,</span> age<span class=\"token operator\">:</span> <span class=\"token string\">'24'</span><span class=\"token punctuation\">,</span> favAnimal<span class=\"token operator\">:</span> <span class=\"token string\">'Dog'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token operator\">:</span> <span class=\"token string\">'John Smith'</span><span class=\"token punctuation\">,</span> age<span class=\"token operator\">:</span> <span class=\"token string\">'35'</span><span class=\"token punctuation\">,</span> favAnimal<span class=\"token operator\">:</span> <span class=\"token string\">'Tiger'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token operator\">:</span> <span class=\"token string\">'Betsy Blue'</span><span class=\"token punctuation\">,</span> age<span class=\"token operator\">:</span> <span class=\"token string\">'19'</span><span class=\"token punctuation\">,</span> favAnimal<span class=\"token operator\">:</span> <span class=\"token string\">'Warthog'</span> <span class=\"token punctuation\">}</span><span class=\"token punctuation\">,</span>\n  <span class=\"token punctuation\">{</span> name<span class=\"token operator\">:</span> <span class=\"token string\">'Justin Long'</span><span class=\"token punctuation\">,</span> age<span class=\"token operator\">:</span> <span class=\"token string\">'15'</span><span class=\"token punctuation\">,</span> favAnimal<span class=\"token operator\">:</span> <span class=\"token string\">'Cat'</span> <span class=\"token punctuation\">}</span>\n<span class=\"token punctuation\">]</span></code></pre></div>\n<p>Perfect! Now we have our array of users that we could do all types of things with.</p>\n<div class=\"gatsby-highlight\" data-language=\"javascript\"><pre class=\"language-javascript\"><code class=\"language-javascript\"><span class=\"token comment\">// Asynchronous Function: Read file</span>\nfs<span class=\"token punctuation\">.</span><span class=\"token function\">readFile</span><span class=\"token punctuation\">(</span>filepath<span class=\"token punctuation\">,</span> <span class=\"token punctuation\">(</span><span class=\"token parameter\">error<span class=\"token punctuation\">,</span> file</span><span class=\"token punctuation\">)</span> <span class=\"token operator\">=></span> <span class=\"token punctuation\">{</span>\n  <span class=\"token comment\">// If there is an error deal with it</span>\n  <span class=\"token keyword\">if</span> <span class=\"token punctuation\">(</span>error<span class=\"token punctuation\">)</span> <span class=\"token punctuation\">{</span>\n    <span class=\"token keyword\">throw</span> error<span class=\"token punctuation\">;</span>\n  <span class=\"token punctuation\">}</span>\n  <span class=\"token comment\">// If there isn't an error do what I wanted</span>\n  <span class=\"token keyword\">const</span> fileStringData <span class=\"token operator\">=</span> file<span class=\"token punctuation\">.</span><span class=\"token function\">toString</span><span class=\"token punctuation\">(</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token keyword\">const</span> users <span class=\"token operator\">=</span> <span class=\"token function\">csvToObject</span><span class=\"token punctuation\">(</span>fileStringData<span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span>\n  <span class=\"token comment\">// Do something with users</span>\n<span class=\"token punctuation\">}</span><span class=\"token punctuation\">)</span><span class=\"token punctuation\">;</span></code></pre></div>\n<h2 id=\"wrapping-up\" style=\"position:relative;\"><a href=\"#wrapping-up\" aria-label=\"wrapping up permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Wrapping Up</h2>\n<p>Callbacks and asynchronous functions can be complicated when you first start, but like most programming principles, if you keep practicing you will get the hang of them. In the future I will be adding guides for both Promises and Async/Await, so if callbacks and their messy looking coding style left you feeling icky then you should stay tuned. Callbacks are a thing of the past and Promises and Async/Await are the future.</p>\n<h3 id=\"part-2-simple-nodejs-file-io-with-promises-coming-soon\" style=\"position:relative;\"><a href=\"#part-2-simple-nodejs-file-io-with-promises-coming-soon\" aria-label=\"part 2 simple nodejs file io with promises coming soon permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Part 2: Simple Nodejs File IO With Promises (Coming Soon)</h3>\n<h3 id=\"part-3-simple-nodejs-file-io-with-asyncawait-coming-soon\" style=\"position:relative;\"><a href=\"#part-3-simple-nodejs-file-io-with-asyncawait-coming-soon\" aria-label=\"part 3 simple nodejs file io with asyncawait coming soon permalink\" class=\"anchor before\"><svg aria-hidden=\"true\" focusable=\"false\" height=\"16\" version=\"1.1\" viewBox=\"0 0 16 16\" width=\"16\"><path fill-rule=\"evenodd\" d=\"M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z\"></path></svg></a>Part 3: Simple Nodejs File IO With Async/Await (Coming Soon)</h3>"}],"postsPerPage":5}},"staticQueryHashes":["1116962356","2041457101","2275848304","2775657032","444477021","545095672"]}