<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abdul</title>
    <description>The latest articles on DEV Community by Abdul (@abdddd).</description>
    <link>https://dev.to/abdddd</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1495974%2F9d8c2dd5-719f-446c-87f7-9ad69ed2fc6e.jpg</url>
      <title>DEV Community: Abdul</title>
      <link>https://dev.to/abdddd</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abdddd"/>
    <language>en</language>
    <item>
      <title>How to build a Web IDE like codesandbox?</title>
      <dc:creator>Abdul</dc:creator>
      <pubDate>Sun, 16 Jun 2024 08:24:06 +0000</pubDate>
      <link>https://dev.to/abdddd/how-to-build-a-web-ide-like-codesandbox-38e6</link>
      <guid>https://dev.to/abdddd/how-to-build-a-web-ide-like-codesandbox-38e6</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;I first came across Web IDE’s during my college days through online competitive coding platforms like leetcode and hackerrank, and since then the concept of web based IDE’s or Editors has always intruded me and this being relatively very niche topic I could not find much resources on its workings. However by the end of my final year I came up with a prototype web IDE that lets you build react applications in the browser, the prototype had a file explorer, working terminal, code editor and a live preview window. Back then i was too embarrassed about my code quality and architecture that i did not post or open-source the project. Fast forward to today, I revisited the project again, laughed at my mistakes and ended up improving the IDE to my best knowledge and now i have decided to open-source it. If you find my content interesting, leave a 💖 and follow me for more.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Features:&lt;/strong&gt;&lt;br&gt;
Code editor (Monaco)&lt;br&gt;
File explorer&lt;br&gt;
Xterm.js terminal&lt;br&gt;
LSP (Language server protocol) Support&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tech stack:&lt;/strong&gt;&lt;br&gt;
Node.js&lt;br&gt;
Docker&lt;br&gt;
React&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Available Implementations&lt;/strong&gt;&lt;br&gt;
Before I explain my implementation of online code editor, I would like to mention few other available approaches/websites such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;VS Code.dev&lt;/strong&gt;: Core VS Code is open-source (other than extensions marketplace) and since it is built with web technologies like HTML, CSS and JS through electron framework, all most all of the editor front end is compatible to run inside a web browser, but for editor backend it makes use of remote servers or docker containers.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2syy9kgvi4kwzmsb3xm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg2syy9kgvi4kwzmsb3xm.png" alt="vscode.dev" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;StackBlitz&lt;/strong&gt;: Uses WASM and WebContainers to power their code playgrounds that run entirely inside a web browser.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fun9flrfdm2l8swi56wjw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fun9flrfdm2l8swi56wjw.png" alt="stack blitz webContainers" width="800" height="465"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Build:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since I wanted to build (or at least assemble) my own editor VS Code wasn’t the approach that i was interested in. The StackBlitz is cutting edge and a great piece of tech but there is limitation to amount of stuff that can be done through WASM.&lt;/p&gt;

&lt;p&gt;So my approach was to take all the available open-source tools and stitched them together to run as a web editor.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;FrontEnd&lt;/strong&gt;: I used monaco and xterm.js as my editor and terminal respectively (VS Code also uses these). I wrote a small recursive file explorer component for viewing, creating and deleting files/folders. The entire editor is responsive thanks to split.js.&lt;/p&gt;

&lt;p&gt;The frontend is build with react and deployed as a static site inside my S3 bucket.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/iam-abdul/react_monaco_xtermjs_web_ide_with_LSP"&gt;The link to frontend repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Backend&lt;/strong&gt;: The backend is written in node.js and it runs inside a docker container. The backend is responsible for serving project files and writing content to the files and also spawning a terminal instance for the user (with non root privileges).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/iam-abdul/python_golang_monaco_language_client_xterm_backend"&gt;The link to backend repo&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqlgqkk0z0ycfu4i5fpf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Feqlgqkk0z0ycfu4i5fpf.png" alt="high level system design of an online code editor" width="800" height="473"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Enough talk show me code…!
&lt;/h2&gt;

&lt;p&gt;For Editor there are many react monaco wrappers out there, but after trying out a few of them the best one I found is from typefox called monaco-language client. The great thing about this is, it seamlessly integrates with almost all vs-code syntax highlighting extensions and also it provides a way to integrate Language servers over web sockets (this is the coolest part, more on this in later section).&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The coolest part of this repo is that it strips down and makes use of existing VS Code extensions for providing language specific syntax highlighting (check line no 1). Checkout &lt;a href="https://microsoft.github.io/monaco-editor/monarch.html"&gt;Monarch&lt;/a&gt; if you want to build syntax highlighting by yourself.&lt;/p&gt;

&lt;p&gt;Also here you can see the golang lsp configuration, but what is &lt;strong&gt;LSP&lt;/strong&gt; ?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;LSP&lt;/strong&gt; stands for &lt;strong&gt;Language Server Protocol&lt;/strong&gt;. It’s basically a special language that lets code editors and development environments (like Visual Studio Code) to talk to language servers.&lt;/p&gt;

&lt;p&gt;These language servers are like mini programs that understand the ins and outs of a specific programming language. The editor can ask the server questions about your code, and the server can provide cool features like auto-completion, finding errors, auto importing packages, or jumping to where a function is defined.&lt;/p&gt;

&lt;p&gt;Before Microsoft introduced the LSP protocol the language developers used to write language support plugins for each and every editor out there, but now thanks to this protocol the language server is implemented once and used everywhere.&lt;/p&gt;

&lt;p&gt;When a language support extension is installed in VS Code, it basically gets the syntax highlighting and sets up the language server. The VS Code runs the installed language server as a process and communicates through Json-RPC calls. But for our web editor we cannot run these language servers in browser, so we run the language server inside the docker container and transfer all the JSON-RPC calls made by the editor to the language server via a web socket connection. You can think of this as a way of piping | the RPC calls over sockets.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    serverName: "GOPLS",
    pathName: "/gopls",
    serverPort: 80,
    runCommand: "gopls",
    runCommandArgs: ["serve"],
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Checkout the &lt;a href="https://github.com/iam-abdul/python_golang_monaco_language_client_xterm_backend/blob/9e6b6cbea7bab083a2239f8dd5e09e904450dc4c/python-lsp.ts#L53C3-L64C4"&gt;backend&lt;/a&gt; repo for complete code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let’s talk about the terminal&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;VS Code uses &lt;a href="https://xtermjs.org/"&gt;Xterm.js&lt;/a&gt; for its integrated terminal. Xterm.js is a terminal emulator with rich plugin/add-on ecosystem. Since our entire user workspace is inside a docker container and only the front end is being served through web, we need a way to attach our Xterm.js terminal emulator to a process that in turn interacts with pty on docker.&lt;/p&gt;

&lt;p&gt;To achieve this I made use of node-pty, xterm.js attach add on. The node-pty spawns a child processes that can interact with the operating system like you would on a real terminal, but we need a way to transfer the commands and responses from web to backend running inside the docker container, for this I made use of attach add-on for xterm.js. This works similar to the LSP setup i.e, each keystroke in terminal (xterm.js) is transferred to backend via socket connection and then its fed into the node-pty process and response generated in turn is brought back.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;Backend code for terminal:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;This concludes the terminal section of the web IDE.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional features:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have a file explorer and a terminal, since files or directories could also be modified via terminal we need a way to communicate those changes to frontend. For this I made use of &lt;a href="https://www.npmjs.com/package/chokidar"&gt;Chokidar&lt;/a&gt;, this package lets us setup file system watcher (nodemon also makes use of chokidar) and through following callbacks we can communicate file system changes.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;The above piece of code listens for new file additions and sends a socket event to frontend so that UI stays in sync even if the files are being created from the terminal or any other process.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security and deployment:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Through a web IDE we are basically giving people on the internet access to free cloud compute and chances of malicious actors taking advantage of such system is high. Since the backend is in docker, we could easily limit the CPU and Memory usage of each container and instead of giving root access to the user we can create a low privileged user and spawn the terminal through this user.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Dockerfile&lt;/strong&gt;:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;Run the docker container with command &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;docker run -p 80:80 -d playground&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is just a fun little project, its far from being a perfect and secure code editor, if you find any issues please report them on github.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, leave a 💖 here or a ⭐ on &lt;a href="https://github.com/iam-abdul"&gt;github&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Follow me for interesting content&lt;/p&gt;

&lt;p&gt;Socials:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/abdul-mohammad-a567b2183/"&gt;https://www.linkedin.com/in/abdul-mohammad-a567b2183/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/iam-abdul"&gt;https://github.com/iam-abdul&lt;/a&gt;&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>node</category>
      <category>react</category>
      <category>vscode</category>
    </item>
    <item>
      <title>Simplified OpenAPI Docs Using Serverless Framework for Node.js Developers</title>
      <dc:creator>Abdul</dc:creator>
      <pubDate>Sun, 19 May 2024 18:58:10 +0000</pubDate>
      <link>https://dev.to/abdddd/openapi-swagger-docs-integration-with-serverless-framework-nodejs-expressjs-typescript-30l</link>
      <guid>https://dev.to/abdddd/openapi-swagger-docs-integration-with-serverless-framework-nodejs-expressjs-typescript-30l</guid>
      <description>&lt;h2&gt;
  
  
  Introduction:
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;OpenAPI&lt;/strong&gt; formerly known as swagger docs is a specification. It's a standardised way to describe web APIs using a machine-readable format, typically YAML or JSON. OpenAPI docs could also be written in line as comments in javascript/typescript files. This format outlines the different endpoints, what kind of data they expect (request body), what kind of data they return (response), and other details that are crucial for developers and tools (eg: postman) to understand and interact with your API effectively. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Serverless&lt;/strong&gt; framework lets you build and deploy applications without managing servers. You write code for specific functions that execute in response to events (like an API request). The cloud provider handles server provisioning, scaling, and maintenance, allowing you to focus on core functionality and benefit from pay-per-use pricing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setting up swagger/OpenAPI with serverless:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Install the following dependencies&lt;br&gt;
&lt;code&gt;npm i swagger-jsdoc&lt;/code&gt; with this dependency you will be able to extract swagger doc from comments in javascript files. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm i swagger-ui-express&lt;/code&gt; this serves interactive swagger ui.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2yqyuusigxn56a3dhiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ft2yqyuusigxn56a3dhiw.png" alt="sample swagger ui"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Following is a utility function that sets up the swagger configuration, the function accepts express app as the input&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8q5e3khd79abmoebemgs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8q5e3khd79abmoebemgs.png" alt="sawgger setup with serverless framework"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;swagger docs extracts the api definitions from the .js files mentioned in the apis array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sinwd36hsns2z41qq2l.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6sinwd36hsns2z41qq2l.png" alt="swagger and serverless x-forwarded-prefix"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Swagger needs the original URL to correctly generate the api documentation, but the api gateway (proxy) modifies the request URL and store a copy of original URL on the "x-forwarded-prefix" header and with this middleware the original URL gets assigned to &lt;code&gt;req.originalUrl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qw2vy4qozp27nk2kyiq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9qw2vy4qozp27nk2kyiq.png" alt="setting up swagger (openAPI) with serverless framework"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A sample swagger doc&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6g0b16vua39vwqu01t11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6g0b16vua39vwqu01t11.png" alt="Sample swagger doc"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4wui5cqfj5wlwls8g4cw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4wui5cqfj5wlwls8g4cw.png" alt="final result swagger doc integration with serverless framework"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I had trouble setting up swagger docs with AWS and i hope this article helps you out.&lt;br&gt;
Thank you for reading :)&lt;br&gt;
Abd038&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Socials:&lt;br&gt;
&lt;a href="https://www.linkedin.com/in/abdul-mohammad-a567b2183/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/abdul-mohammad-a567b2183/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/iam_abd038" rel="noopener noreferrer"&gt;https://twitter.com/iam_abd038&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/iam-abdul" rel="noopener noreferrer"&gt;https://github.com/iam-abdul&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>swagger</category>
      <category>serverless</category>
      <category>node</category>
      <category>typescript</category>
    </item>
    <item>
      <title>Vitest setup with serverless framework: Node.js Express.js</title>
      <dc:creator>Abdul</dc:creator>
      <pubDate>Thu, 16 May 2024 04:26:25 +0000</pubDate>
      <link>https://dev.to/abdddd/vitest-setup-with-serverless-framework-nodejs-expressjs-550c</link>
      <guid>https://dev.to/abdddd/vitest-setup-with-serverless-framework-nodejs-expressjs-550c</guid>
      <description>&lt;p&gt;While established testing frameworks like Jest and Mocha have long served the Node.js community, Vitest emerges as a compelling alternative, especially for projects that leverage TypeScript and modern tooling.&lt;/p&gt;

&lt;p&gt;Vitest boasts a rich testing ecosystem out of the box, including features like test coverage and optional visual test explorers for enhanced development experience.&lt;/p&gt;

&lt;p&gt;In this article i will walk over the setup for vitest with serverless framework for node and express applications.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hs563dqgp89z6a4c7fd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6hs563dqgp89z6a4c7fd.png" alt="serverless.yml file configuration for vitest"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The serverless.yml file remains almost unchanged, just add an exclude section under package to discard the test files when serverless build is getting created and deployed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hahxnxd6nxigo8ztu27.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1hahxnxd6nxigo8ztu27.png" alt="vitest.config.ts file for serverless framework"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Vitest.config.json&lt;br&gt;
Since the serverless framework encourages the use of config.stage.json files and environment files, i had slight trouble getting environment files to work with vite, so i came up with the above function that reads the &lt;code&gt;.json&lt;/code&gt; as well as &lt;code&gt;.env&lt;/code&gt; files and loads the environment variables.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzin3l1zej4tch5tc7zor.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzin3l1zej4tch5tc7zor.png" alt="package.json file for vitest and serverless framework"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The variable &lt;code&gt;CONFIG_FILE&lt;/code&gt; is passed during the run time.&lt;/p&gt;

&lt;p&gt;I hope you like this short article, thanks for reading :)&lt;/p&gt;

&lt;p&gt;Abd038&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Socials&lt;/strong&gt;:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.linkedin.com/in/abdul-mohammad-a567b2183/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/abdul-mohammad-a567b2183/&lt;/a&gt;&lt;br&gt;
&lt;a href="https://twitter.com/iam_abd038" rel="noopener noreferrer"&gt;https://twitter.com/iam_abd038&lt;/a&gt;&lt;br&gt;
&lt;a href="https://github.com/iam-abdul" rel="noopener noreferrer"&gt;https://github.com/iam-abdul&lt;/a&gt;&lt;/p&gt;

</description>
      <category>node</category>
      <category>webdev</category>
      <category>vitest</category>
      <category>serverless</category>
    </item>
    <item>
      <title>I built an open-source schema visualisation tool for mongoose</title>
      <dc:creator>Abdul</dc:creator>
      <pubDate>Wed, 15 May 2024 04:20:46 +0000</pubDate>
      <link>https://dev.to/abdddd/i-built-an-open-source-schema-visualisation-tool-for-mongoose-44m9</link>
      <guid>https://dev.to/abdddd/i-built-an-open-source-schema-visualisation-tool-for-mongoose-44m9</guid>
      <description>&lt;p&gt;Working with Mongoose models can sometimes feel like keeping track of a complex web. Unlike many SQL databases, finding free and easy tools to visualize your Mongoose schemas can be tough. This can make it difficult to understand how your data is connected and collaborate with other developers.&lt;/p&gt;

&lt;p&gt;To solve this problem, I built an open-source tool that visualizes your Mongoose schemas. Now you can easily see how your data fits together and keep your projects organised!&lt;/p&gt;

&lt;p&gt;URL: &lt;a href="http://mongoose-schema-visualize.s3-website.ap-south-1.amazonaws.com/"&gt;http://mongoose-schema-visualize.s3-website.ap-south-1.amazonaws.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Repo: &lt;a href="https://github.com/iam-abdul/Mongoose-Schema-Visualization-Tool"&gt;https://github.com/iam-abdul/Mongoose-Schema-Visualization-Tool&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NPM Package: &lt;a href="https://www.npmjs.com/package/mongoose-parser"&gt;https://www.npmjs.com/package/mongoose-parser&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach
&lt;/h2&gt;

&lt;p&gt;While it might be tempting to directly query the database using the connection URI to extract model relationships, this approach has a few drawbacks:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security Concerns&lt;/strong&gt;: Sharing connection URIs can be a security risk, as it exposes sensitive database credentials.&lt;br&gt;
Unnecessary Complexity: For a simple task like schema visualization, querying the database adds unnecessary complexity to the tool.&lt;br&gt;
To address these concerns, I opted to obtain the schema by parsing the JavaScript files. This approach has some key advantages:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Security&lt;/strong&gt;: By parsing the code, there’s no need to handle connection URIs, keeping those credentials safe.&lt;br&gt;
Efficiency: Parsing JavaScript files is generally faster and lighter on resources compared to querying the database.&lt;br&gt;
Parsing the JavaScript files does require some additional logic, but the benefits in terms of security and efficiency make it a worthwhile trade-off. It’s important to note that this entire operation happens within your browser. Since it’s a static site, there are no API calls involved. You simply upload your JavaScript files, and the tool does its magic to visualize your Mongoose schema relationships, all without ever touching your database directly.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parsing the Schema Files&lt;/strong&gt;&lt;br&gt;
Extracting model relationships from JavaScript files can be done using regular expressions. However, this approach has some limitations:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Complexity&lt;/strong&gt;: Mongoose schema definitions can involve complex nesting and property structures. Regular expressions can quickly become cumbersome and error-prone when dealing with such complexity.&lt;br&gt;
Maintainability: As your schemas evolve, regex patterns might need frequent updates to keep up, making the code harder to maintain in the long run.&lt;br&gt;
To address these limitations, I opted for a more robust approach: parsing the Abstract Syntax Tree of the JavaScript files.&lt;/p&gt;

&lt;h2&gt;
  
  
  Abstract syntax tree
&lt;/h2&gt;

&lt;p&gt;What’s an &lt;strong&gt;AST&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;An AST, or Abstract Syntax Tree, is a tree-like representation of the code’s structure. Imagine it like a family tree, but for your code! It breaks down the code into its core elements, like variables, functions, and expressions, making it easier to understand the relationships between different parts of the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where are ASTs Used?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;ASTs&lt;/strong&gt; are a powerful tool used in various programming contexts beyond this schema visualisation tool. Here are a few examples:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compilers&lt;/strong&gt;: Compilers, which translate code from one language to another, often use ASTs as an intermediate representation to analyse and optimise the code before generating the final output.&lt;br&gt;
Linters and Static Code Analysers: These tools use ASTs to identify potential errors, stylistic inconsistencies, or inefficiencies in the code without actually running it.&lt;/p&gt;

&lt;p&gt;Benefits of Using an AST for Schema Visualisation&lt;br&gt;
By converting the JavaScript files to ASTs, tool gains several advantages:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accuracy&lt;/strong&gt;: ASTs provide a more accurate and reliable way to identify schema definitions within your code.&lt;br&gt;
&lt;strong&gt;Flexibility&lt;/strong&gt;: ASTs can handle complex code structures more effectively than regular expressions, making the tool adaptable to different schema definition styles.&lt;br&gt;
Maintainability: Using ASTs makes the parsing logic cleaner and easier to maintain as your schemas evolve.&lt;br&gt;
While parsing with an AST requires a bit more initial setup compared to regular expressions, the benefits in terms of accuracy, flexibility, and maintainability make it a worthwhile investment for this tool. It allows for robust and reliable extraction of model relationships from your JavaScript schema files.&lt;/p&gt;

&lt;p&gt;Since mongoose schemas are javascript files, I made use of acorn (&lt;a href="https://www.npmjs.com/package/acorn"&gt;https://www.npmjs.com/package/acorn&lt;/a&gt;) a javascript parser written in javascript 🙃 to build the AST.&lt;/p&gt;

&lt;p&gt;Acron creates the AST for the given javascript files and from that i look for mongoose methods and extract the associated model definitions. I converted these set of functions into an &lt;br&gt;
NPM package of its own (&lt;a href="https://www.npmjs.com/package/mongoose-parser"&gt;https://www.npmjs.com/package/mongoose-parser&lt;/a&gt;). This package is used by the tool for extracting the schemas.&lt;/p&gt;

&lt;p&gt;Once the schema is extracted the frontend react app converts this schema into set of nodes and edges and creates the visual representation with the help of react-flow (&lt;a href="https://www.npmjs.com/package/reactflow"&gt;https://www.npmjs.com/package/reactflow&lt;/a&gt;).&lt;/p&gt;

&lt;h2&gt;
  
  
  Usage
&lt;/h2&gt;

&lt;p&gt;Just drag and drop the JavaScript files containing your Mongoose schema definitions here, and watch your schemas come to life! &lt;a href="http://mongoose-schema-visualize.s3-website.ap-south-1.amazonaws.com/"&gt;http://mongoose-schema-visualize.s3-website.ap-south-1.amazonaws.com/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading :)&lt;/p&gt;

&lt;h2&gt;
  
  
  Socials
&lt;/h2&gt;

&lt;p&gt;Linkedin: &lt;a href="https://www.linkedin.com/in/abdul-mohammad-a567b2183/"&gt;https://www.linkedin.com/in/abdul-mohammad-a567b2183/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Github: &lt;a href="https://github.com/iam-abdul"&gt;https://github.com/iam-abdul&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mongodb</category>
      <category>webdev</category>
      <category>node</category>
      <category>mongoose</category>
    </item>
  </channel>
</rss>
