<?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: Vivek Chauhan</title>
    <description>The latest articles on DEV Community by Vivek Chauhan (@vivekagent47).</description>
    <link>https://dev.to/vivekagent47</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%2F492316%2F04e298a5-5230-4911-a975-e87a38806cf8.jpg</url>
      <title>DEV Community: Vivek Chauhan</title>
      <link>https://dev.to/vivekagent47</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vivekagent47"/>
    <language>en</language>
    <item>
      <title>Understanding MySQL Enums: Unveiling Surprising Behaviors</title>
      <dc:creator>Vivek Chauhan</dc:creator>
      <pubDate>Fri, 27 Oct 2023 10:20:05 +0000</pubDate>
      <link>https://dev.to/vivekagent47/understanding-mysql-enums-unveiling-surprising-behaviors-9lo</link>
      <guid>https://dev.to/vivekagent47/understanding-mysql-enums-unveiling-surprising-behaviors-9lo</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fbfgtgp4xthsc3lw6wf3n.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fbfgtgp4xthsc3lw6wf3n.jpg" alt="SQL" width="800" height="533"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever encountered MySQL enums in your database work and been left puzzled by their behavior? In this blog, we'll delve into MySQL enums, what they are, and explore some of the intriguing and, at times, counterintuitive aspects of how they work.&lt;/p&gt;

&lt;h3&gt;
  
  
  What Are MySQL Enums?
&lt;/h3&gt;

&lt;p&gt;Let's start with the basics. MySQL enums are a column type that allows you to define a set list of values. From a human perspective, you can view this column as a list of strings. However, beneath the surface, the database treats them as integers, resulting in compact storage on disk. This dual nature provides a hint of data validation, as you can only choose from the predefined list of values.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Right Down-the-Middle Use Case
&lt;/h3&gt;

&lt;p&gt;To illustrate a straightforward use case, imagine an "orders" table with a "size" column, which is defined as an enum. The allowed values for this column are: Extra Small, Small, Medium, Large, and Extra Large. These are the only options you can select, ensuring data integrity.&lt;/p&gt;

&lt;h3&gt;
  
  
  Unveiling the Integer-String Duality
&lt;/h3&gt;

&lt;p&gt;Now, here's where it gets interesting. MySQL enums offer both the readability of strings and the efficiency of integers. To prove this, you can perform a simple query: &lt;code&gt;SELECT size&lt;/code&gt; and &lt;code&gt;SELECT size + 0&lt;/code&gt;. The former will return the string label (e.g., "Small"), while the latter will coerce it into the underlying integer (e.g., 2 for "Small"). This clever design gives you the best of both worlds.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ordering by Enums
&lt;/h3&gt;

&lt;p&gt;One intriguing aspect of enums is their behavior when it comes to sorting or ordering. When you use &lt;code&gt;ORDER BY&lt;/code&gt; on an enum column, it sorts by the underlying integer values, not the string labels. This may initially seem odd, as you might expect strings to be ordered alphabetically. However, it's essential to remember that the database operates based on integers, not strings.&lt;/p&gt;

&lt;p&gt;For instance, if you order the "size" column, you'll notice that "Extra Small" (1), "Small" (2), "Large" (4), and "Extra Large" (5) are sorted based on their integer positions. This behavior might feel counterintuitive, but it's crucial to know when working with enum columns.&lt;/p&gt;

&lt;h3&gt;
  
  
  The Most Surprising Quirk: Max and Min Functions
&lt;/h3&gt;

&lt;p&gt;Here comes the real surprise. While ordering by an enum column follows the integer values, the &lt;code&gt;MAX&lt;/code&gt; and &lt;code&gt;MIN&lt;/code&gt; functions take an unexpected approach. Instead of considering the integer positions, these functions base their calculations on the string values.&lt;/p&gt;

&lt;p&gt;For example, if you were to run &lt;code&gt;SELECT MAX(size) FROM orders&lt;/code&gt;, you might expect it to return "Extra Large" since it's in position 5. However, the result will be "Extra Small" because, alphabetically, it's the largest string label.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping It Up
&lt;/h3&gt;

&lt;p&gt;In summary, MySQL enums can be incredibly useful for specific use cases where you need to maintain a specific order of values. However, it's crucial to declare the enum values in the order you want them sorted.&lt;/p&gt;

&lt;p&gt;The quirky behavior of enums, where ordering follows integers and max/min functions consider string values, may seem inconsistent, but it's an essential aspect to keep in mind when working with these data types. Understanding these nuances will help you make the most of MySQL enums in your database management endeavors.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Error Handling and Logging in NestJS: Best Practices</title>
      <dc:creator>Vivek Chauhan</dc:creator>
      <pubDate>Thu, 26 Oct 2023 04:50:22 +0000</pubDate>
      <link>https://dev.to/vivekagent47/error-handling-and-logging-in-nestjs-best-practices-5a5k</link>
      <guid>https://dev.to/vivekagent47/error-handling-and-logging-in-nestjs-best-practices-5a5k</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Fylxwslut8avgv8ehp5ih.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fylxwslut8avgv8ehp5ih.jpg" alt="Nestjs" width="800" height="336"&gt;&lt;/a&gt;&lt;br&gt;
When building applications with NestJS, handling errors and implementing robust logging are crucial aspects of ensuring your application’s reliability and maintainability. In this comprehensive guide, we’ll explore the best practices for error handling and logging in NestJS, specifically tailored for beginners. So, fasten your seatbelts and let’s embark on this journey to master error handling and logging in NestJS!&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding Error Handling in NestJS
&lt;/h2&gt;

&lt;p&gt;Error handling is the process of gracefully handling and managing unexpected situations that may arise during the execution of your application. NestJS offers a structured approach to handle errors effectively. The primary mechanism to achieve this is by using Exception Filters. Exception Filters allow you to intercept thrown exceptions and take appropriate actions, such as returning meaningful error responses to clients.&lt;/p&gt;

&lt;p&gt;Let’s take a look at how to implement an Exception Filter:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { Request, Response } from 'express';

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse&amp;lt;Response&amp;gt;();
    const request = ctx.getRequest&amp;lt;Request&amp;gt;();
    const status = exception.getStatus();

    response.status(status).json({
      statusCode: status,
      timestamp: new Date().toISOString(),
      path: request.url,
      message: exception.message,
    });
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we’ve created an HttpExceptionFilter, which catches any HttpException that might occur in our application. It then extracts relevant information from the exception and sends a structured JSON response to the client, including the status code, timestamp, request path, and error message.&lt;/p&gt;

&lt;p&gt;To apply this filter globally to your application, you can use the &lt;code&gt;app.useGlobalFilters()&lt;/code&gt; method in your &lt;code&gt;main.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { HttpExceptionFilter } from './http-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalFilters(new HttpExceptionFilter());
  await app.listen(3000);
}
bootstrap();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, all &lt;code&gt;HttpExceptions&lt;/code&gt; will be caught by our &lt;code&gt;HttpExceptionFilter&lt;/code&gt;, ensuring consistent error responses throughout the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handling Custom Exceptions
&lt;/h3&gt;

&lt;p&gt;While NestJS provides several built-in exceptions, you’ll often encounter scenarios where you need to define your custom exceptions. Creating custom exceptions is simple and allows you to provide specific details about the error to the client.&lt;/p&gt;

&lt;p&gt;Let’s say we want to create a custom &lt;code&gt;NotFoundException&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export class NotFoundException extends HttpException {
  constructor(message: string) {
    super(message, HttpStatus.NOT_FOUND);
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can then use this custom exception within our application’s services or controllers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, NotFoundException } from '@nestjs/common';
import { Task } from './interfaces/task.interface';

@Injectable()
export class TaskService {
  private tasks: Task[] = [];

  getTaskById(id: string): Task {
    const task = this.tasks.find((task) =&amp;gt; task.id === id);
    if (!task) {
      throw new NotFoundException(`Task with ID ${id} not found.`);
    }
    return task;
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, if the requested task with a specific id is not found, we throw the NotFoundException, which will be caught by our global HttpExceptionFilter. This allows us to send a user-friendly response back to the client without leaking sensitive implementation details.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logging in NestJS
&lt;/h2&gt;

&lt;p&gt;Effective logging is essential for monitoring the behavior of your application and diagnosing potential issues. NestJS provides a powerful logging mechanism out of the box, allowing you to choose from various logging levels, log formatters, and transports.&lt;/p&gt;

&lt;p&gt;By default, NestJS uses the Logger class to handle application logs. To use the default &lt;code&gt;logger&lt;/code&gt; in your services or controllers, you can inject it using the &lt;code&gt;@Logger()&lt;/code&gt; decorator:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Injectable, Logger } from '@nestjs/common';

@Injectable()
export class TaskService {
  private logger = new Logger(TaskService.name);

  // ...

  someMethod() {
    this.logger.log('This is a log message.');
    this.logger.debug('Debugging some process...');
    this.logger.warn('Warning: Something seems off!');
    this.logger.error('Oops! An error occurred.');
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Logger class provides methods for various log levels, such as log, debug, warn, and error. Each log level corresponds to a specific severity, and you can configure the logging behavior in your NestJS application.&lt;/p&gt;

&lt;p&gt;Customizing Logging Behavior&lt;/p&gt;

&lt;p&gt;If you need more control over your application’s logging behavior, NestJS allows you to create custom loggers by implementing the LoggerService interface.&lt;/p&gt;

&lt;p&gt;Let’s create a fun custom logger that writes logs to a file and also includes emojis!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { LoggerService } from '@nestjs/common';

export class EmojiLogger implements LoggerService {
  log(message: string) {
    this.writeToFile('📢 ' + message);
  }

  error(message: string, trace: string) {
    this.writeToFile('❌ ' + message);
    this.writeToFile('🔍 Stack Trace: ' + trace);
  }

  warn(message: string) {
    this.writeToFile('⚠️ ' + message);
  }

  debug(message: string) {
    this.writeToFile('🐞 ' + message);
  }

  private writeToFile(message: string) {
    // Implement the logic to write logs to a file here.
    console.log(message); // For demonstration purposes, we'll just log to the console.
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to use our EmojiLogger, let’s update the &lt;code&gt;main.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { EmojiLogger } from './emoji-logger';

async function bootstrap() {
  const app = await NestFactory.create(AppModule, { logger: new EmojiLogger() });
  await app.listen(3000);
}
bootstrap();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this setup, our application will use the fun EmojiLogger to log messages. Feel free to customize the writeToFile method to write logs to a file or any other preferred destination.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we’ve explored the best practices for error handling and logging in NestJS. We’ve learned how to use Exception Filters to gracefully handle errors and send structured error responses to clients. Additionally, we’ve seen how to create custom exceptions for more specific error reporting. Furthermore, we’ve delved into the world of logging and discovered how to use the default Logger class and create custom loggers, like our EmojiLogger.&lt;/p&gt;

&lt;p&gt;By mastering error handling and logging in NestJS, you’re well on your way to building robust and reliable applications. So, keep experimenting, stay curious, and be happy coding! 🚀🌟&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>beginners</category>
      <category>node</category>
      <category>nestjs</category>
    </item>
    <item>
      <title>How to save JSON array in MySQL with TypeORM</title>
      <dc:creator>Vivek Chauhan</dc:creator>
      <pubDate>Fri, 28 Jan 2022 16:54:30 +0000</pubDate>
      <link>https://dev.to/vivekagent47/how-to-save-json-array-in-mysql-with-typeorm-3jg7</link>
      <guid>https://dev.to/vivekagent47/how-to-save-json-array-in-mysql-with-typeorm-3jg7</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This article is &lt;code&gt;NestJS&lt;/code&gt; a record of the know-how I realized while developing through the framework. There may be a better way than the one I suggested. Suggestions are always welcome :)&lt;br&gt;
&lt;code&gt;NestJS&lt;/code&gt; - The framework &lt;code&gt;TypeORM&lt;/code&gt;manages the DB through the library. This article &lt;code&gt;TypeORM&lt;/code&gt; deals with how to store JSON arrays in MySQL DB.&lt;/p&gt;
&lt;h2&gt;
  
  
  When we save 'array' in DB column?
&lt;/h2&gt;

&lt;p&gt;Storing an array type in a DB column appears more often than you think.&lt;/p&gt;
&lt;h3&gt;
  
  
  Foreign Key Relation (OneToMany/ManyToOne)
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;Person Email Consider&lt;/code&gt; a case in which Entity and Entity exist for. At this time, since one person can have multiple emails, the relationship between the two is 'OneToMany/ManyToOne'. The MySQL table is stored as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+----+--------------------+--------------+
| id | person             | email_id(fk) |
+----+--------------------+--------------+
| 1  | Tom                | 1            |
| 2  | Evans              | 2, 3         |
+----+--------------------+--------------+

+----+------------------+----------------+
| id | email            | person_id(fk)  |
+----+------------------+----------------+
| 1  | tom@mail.com     | 1              |
| 2  | evans@mail.com   | 2              |
| 3  | evans@univ.ac.kr | 2              |
+----+------------------+----------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking at the two tables, each has a foreign key called &lt;code&gt;email_id&lt;/code&gt; and &lt;code&gt;person_id&lt;/code&gt;. &lt;code&gt;Person&lt;/code&gt; in the &lt;code&gt;Evans&lt;/code&gt; case of , it has two emails in particular, so it has an &lt;code&gt;email_id&lt;/code&gt; array of.&lt;br&gt;
That is, you can find &lt;code&gt;fk_id&lt;/code&gt; Array under the OneToMany/ManyToOne relationship.&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;simple-array&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Even if it is not a foreign key, you can store arrays in MySQL columns. &lt;code&gt;TypeORM&lt;/code&gt; helps &lt;code&gt;simple-array&lt;/code&gt; you to store simple arrays in MySQL through.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Column('simple-array')
num_arr: number[];

@Column('simple-array')
str_arr: string[];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However , if you store it in a &lt;code&gt;simple-array&lt;/code&gt; way &lt;code&gt;string[]&lt;/code&gt;, some problems arise. This is dealt with in more detail later.&lt;/p&gt;

&lt;h2&gt;
  
  
  How to save JSON array?
&lt;/h2&gt;

&lt;p&gt;So far, we have looked at the case where an array is stored in MySQL. Now let's see how we can store JSON array in MySQL.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;simple-array&lt;/code&gt; Let's try it!
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Column('simple-array')
json_arr: JSON[];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;@Column&lt;/code&gt;, &lt;code&gt;simple-array&lt;/code&gt; and declared in the form of an array as before. Can this be saved? NOPE! In this case, an error occurs. Let's look at the error message.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DataTypeNotSupportedError: Data type "Array" in "example.json_arr" is not supported by "mysql" database.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;?? Earlier we looked at the case where MySQL uses arrays. Array but now the error message says that the type is not supported by MySQL .&lt;br&gt;
The error message is correct. MySQL Array does not support types. It's just giving us the feeling of supporting arrays in some way!!&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;simple-array&lt;/code&gt; Let's try it! - &lt;code&gt;string[]&lt;/code&gt; version
&lt;/h3&gt;

&lt;p&gt;Let's see how to circumvent the current problem. Our purpose is still to store JSON arrays in MySQL. But the &lt;code&gt;JSON[]&lt;/code&gt; form is impossible, so &lt;code&gt;string[]&lt;/code&gt; let's bypass the method we looked at earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Column('simple-array')
json_arr: string[];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In javascript, it can be serialized in the form of through &lt;code&gt;JSON.stringify()&lt;/code&gt; a function called . Our plan is as follows- &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The API receives input as a JSON array.&lt;/li&gt;
&lt;li&gt;Iterates through the received JSON array and applies to all JSON &lt;code&gt;JSON.stringify()&lt;/code&gt; to serialize&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string[]&lt;/code&gt; Save as MySQL&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Get&lt;/code&gt; Whenever &lt;code&gt;string[]&lt;/code&gt; a request comes in, the string in the string array stored as &lt;code&gt;JSON.parse()&lt;/code&gt; is parsed and returned as&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If you do this, you can convert the JSON array to a string array and store it in the DB without an error message.&lt;/p&gt;

&lt;p&gt;But…&lt;/p&gt;

&lt;h3&gt;
  
  
  string[] version issue
&lt;/h3&gt;

&lt;p&gt;For those of us who store JSON, this method also has problems.&lt;br&gt;
Let's say you have a JSON array like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[{name: 'Baker', job: 'musician'}, {name: 'Euler', job: 'mathematician'}]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;JSON.stringify()&lt;/code&gt;If we change this to&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["{name: 'Baker', job: 'musician'}", "{name: 'Euler', job: 'mathematician'}"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes&lt;br&gt;
Saving to DB works fine. However Get, if you check what is stored in the DB with ...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;["{name: 'Baker'", 
 "job: 'musician'}",
 "{name: 'Euler'", 
 "job: 'mathematician'}"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It has the following bizarre form... !!&lt;br&gt;
The reason is that…&lt;/p&gt;
&lt;h3&gt;
  
  
  MySQL pseudo-Array
&lt;/h3&gt;

&lt;p&gt;We thought that MySQL supports number arrays or string arrays, even if JSON arrays cannot be stored. But… That was &lt;code&gt;TypeORM&lt;/code&gt; a trick of !!&lt;/p&gt;

&lt;p&gt;Actually MySQL &lt;code&gt;Array&lt;/code&gt; doesn't support types at all!! (Already confirmed with an error message.) So &lt;code&gt;simple-array&lt;/code&gt;, how did we save it using properties?&lt;br&gt;
&lt;code&gt;simple-array&lt;/code&gt; stores all arrays as string. If it is a number array, &lt;code&gt;"1, 2, 3"&lt;/code&gt;, if it is a string array, &lt;code&gt;"Bill, John, Baker"&lt;/code&gt;. So, if even a comma (,) is inserted in the middle of the string, it is stored in the DB like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"'In other words, please be true', 'In other words, I love you'"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, even if it is a string array, only one string is stored in the DB. And parses the information of the column declared as &lt;code&gt;TypeORM&lt;/code&gt;, based on the comma (,). &lt;code&gt;simple-array&lt;/code&gt; therefore, even if you originally saved 2 strings, you will get 4 strings if you read the DB value.&lt;/p&gt;

&lt;p&gt;Actually MySQL there is nothing wrong with it. &lt;code&gt;TypeORM&lt;/code&gt; it is possible that the &lt;code&gt;simple-array&lt;/code&gt; method of MySQL, especially in , is causing this problem. In other DBs, it may not be stored as a single string like this. 2 In MySQL, this is… ㅠㅠ&lt;/p&gt;

&lt;p&gt;In the case of JSON, commas (,) must appear to list objects, but in general strings, commas are rare. So &lt;code&gt;TypeORM&lt;/code&gt; the developer may not have noticed. However, even such a small error can become a big obstacle or cause an error in development. (I lost half a day because of this...)&lt;/p&gt;

&lt;h2&gt;
  
  
  [Solution] Just save it as a string!
&lt;/h2&gt;

&lt;p&gt;Through several trials and errors, &lt;code&gt;simple-array&lt;/code&gt; I confirmed how TypeORM stores arrays in MySQL...&lt;br&gt;
So let's use this to reorganize our code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Column()
json_arr: string;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Column is simply &lt;code&gt;string&lt;/code&gt; set to .&lt;/p&gt;

&lt;p&gt;Each JSON in the JSON array &lt;code&gt;JSON.stringify()&lt;/code&gt; is serialized as a function. Then, they &lt;code&gt;JSON.parse()&lt;/code&gt; are transformed into a possible form and stored.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// save JSON array into MySQL DB
saveJSONArray(json_arr: JSON[]) {
  let jsonArr_string = "[";
  for(let i=0; i &amp;lt; json_arr.lenght; i++){
    jsonArr_string += JSON.stringify(json_arr[i]);
    if(i &amp;lt; json_arr.lenght-1){
      jsonArr_string += ",";
    }
  }
  jsonArr_string += "]";
  this.yourRepository.save({ json_arr: jsonArr_string });
}

// load JSON array from MySQL DB
async findJSONArray(): Promise&amp;lt;YourEntry[]&amp;gt; {
  const entry_arr = await this.yourRepository.find();
  for(let i=0; i &amp;lt; entry_arr.lenght; i++){
    entry_arr[i].json_arr = JSON.parse(entry_arr[i].json_arr);
  }
  return entry_arr;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I think the best way is &lt;code&gt;json_arr: string&lt;/code&gt; to set it to , parse the DB value to , and return it as JSON. &lt;code&gt;JSON.parse()&lt;/code&gt; (Of course, someone may have found a better way than this and put it to good use!)&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typeorm</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Adding Audio to Your App with jQuery</title>
      <dc:creator>Vivek Chauhan</dc:creator>
      <pubDate>Fri, 11 Dec 2020 07:17:22 +0000</pubDate>
      <link>https://dev.to/vivekagent47/adding-audio-to-your-app-with-jquery-ae1</link>
      <guid>https://dev.to/vivekagent47/adding-audio-to-your-app-with-jquery-ae1</guid>
      <description>&lt;p&gt;Want to add some sound effects to your website or game? With HTML5, it’s easy! Here’s a quick tutorial. (If you have your soundbite already and want to get straight to the code, skip to step 4.)&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Find a soundbite
&lt;/h3&gt;

&lt;p&gt;You can Google around for the sound you want, but I would suggest signing up for a free sound effect library. I found freesound.org to be very helpful for finding those clicks, pops, and chimes. Alternatively, you can grab sounds from YouTube using an MP3 converter like this one. When you find the sound you want, download it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Trim the soundbite
&lt;/h3&gt;

&lt;p&gt;For this, you’ll want a free, lightweight audio editor. Audacity is my recommendation. Download it, install it and fire it up. Import your soundbite.&lt;br&gt;
Here’s the fun part. Zoom in really close, and trim off any dead space from the beginning of the sound. This subtle (or sometimes not-so-subtle) improvement will ultimately make your app feel more responsive and satisfying. Notice the straight line at the beginning of the example below. If you see that in your file, highlight it and press delete. Your soundbite is now ready for use.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Import soundbite into your project
&lt;/h3&gt;

&lt;p&gt;Simply use Finder or Windows Explorer to move the freshly trimmed audio file into your app directory. If you will have multiple audio files, it is helpful to make a dedicated audio folder to keep things organized.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: HTML
&lt;/h3&gt;

&lt;p&gt;Add an audio element in your HTML file. This will not show up on the page, so you can put it anywhere. It simply needs to exist so you can reference it with jQuery. In our example, we are using a file called pop.wav.&lt;br&gt;
&lt;code&gt;&amp;lt;audio id="pop"&amp;gt;&lt;br&gt;
  &amp;lt;source src="audio/pop.wav" type="audio/mpeg"&amp;gt;&lt;br&gt;
&amp;lt;/audio&amp;gt;&lt;/code&gt;&lt;br&gt;
(Note: If you want to add an audio player with play, pause, and volume controls, follow the below format. This will, of course, show up on the page.)&lt;br&gt;
&lt;code&gt;&amp;lt;audio controls&amp;gt;&lt;br&gt;
  &amp;lt;source src="horse.mp3" type="audio/mpeg"&amp;gt;&lt;br&gt;
&amp;lt;/audio&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 5: jQuery
&lt;/h3&gt;

&lt;p&gt;In your .js file, wherever you want your soundbite to play, use the following code:&lt;br&gt;
&lt;code&gt;$('audio#pop')[0].play()&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;$('audio#pop')&lt;/code&gt; uses jQuery to select all audio elements with an id of ‘pop’.&lt;/li&gt;
&lt;li&gt;This will return an array, so you need to specify the first element by adding [0].&lt;/li&gt;
&lt;li&gt;Finally, &lt;code&gt;.play()&lt;/code&gt; will play your soundbite.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note: if you have a long soundbite, you can pause it by calling &lt;code&gt;.pause()&lt;/code&gt;. There is no “stop” function, so if you’d like it to restart from the beginning, call &lt;code&gt;.currentTime = 0&lt;/code&gt; on another line.&lt;br&gt;
Altogether, that looks like this:&lt;br&gt;
&lt;code&gt;$('audio#pop')[0].pause()&lt;br&gt;
$('audio#pop')[0].currentTime = 0&lt;/code&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>7 Git tricks that changed my life</title>
      <dc:creator>Vivek Chauhan</dc:creator>
      <pubDate>Thu, 12 Nov 2020 18:07:23 +0000</pubDate>
      <link>https://dev.to/vivekagent47/7-git-tricks-that-changed-my-life-3aj3</link>
      <guid>https://dev.to/vivekagent47/7-git-tricks-that-changed-my-life-3aj3</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvyosi7vlujjxa9m7nfo7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvyosi7vlujjxa9m7nfo7.png" alt="Alt Text" width="519" height="292"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  These helpful tips will change the way you work with the popular version control system.
&lt;/h3&gt;

&lt;p&gt;Git is one of the most common version control systems available, and it’s used on private systems and publicly hosted websites for all kinds of development work. Regardless of how proficient with Git I become, it seems there are always features left to discover. Here are seven tricks that have changed the way I work with Git.&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Autocorrection in Git
&lt;/h4&gt;

&lt;p&gt;We all make typos sometimes, but if you have Git’s auto-correct feature enabled, you can let Git automatically fix a mistyped subcommand.&lt;br&gt;
Suppose you want to check the status with git status but you type git stats by accident. Under normal circumstances, Git tells you that 'stats' is not a valid command:&lt;br&gt;
&lt;code&gt;$ git stats&lt;br&gt;
git: ‘stats’ is not a git command. See ‘git --help’. &lt;br&gt;
The most similar command is status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;To avoid similar scenarios, enable Git autocorrection in your Git configuration:&lt;br&gt;
&lt;code&gt;$ git config --global help.autocorrect 1&lt;/code&gt;&lt;br&gt;
If you want this to apply only to your current repository, omit the --global option.&lt;br&gt;
This command enables the autocorrection feature. An in-depth tutorial is available at GitDocs, but trying the same errant command as above gives you a good idea of what this configuration does:&lt;br&gt;
&lt;code&gt;$ git stats&lt;br&gt;
git: ‘stats’ is not a git command. See ‘git --help’.&lt;br&gt;
On branch master&lt;br&gt;
Your branch is up to date with ‘origin/master’. nothing to commit, working tree clean&lt;/code&gt;&lt;br&gt;
Instead of suggesting an alternative subcommand, Git now just runs the top suggestion, which in this case was git status.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Count your commits
&lt;/h4&gt;

&lt;p&gt;There are many reasons you might need to count your commits. Many developers count the number of commits to judge when to increment the build number, for instance, or just to get a feel for how the project is progressing.&lt;br&gt;
To count your commits is really easy and straightforward; here is the Git command:&lt;br&gt;
&lt;code&gt;$ git rev-list --count&lt;/code&gt;&lt;br&gt;
In the above command, the branch-name should be a valid branch name in your current repository.&lt;br&gt;
&lt;code&gt;$ git rev-list –count master&lt;br&gt;
32&lt;br&gt;
$ git rev-list –count dev&lt;br&gt;
34&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Optimize your repo
&lt;/h4&gt;

&lt;p&gt;Your code repository is valuable not only for you but also for your organization. You can keep your repository clean and up to date with a few simple practices. One of the best practices is to use the .gitignore file. By using this file, you are telling Git not to store many unwanted files like binaries, temporary files, and so on.&lt;br&gt;
To optimize your repository further, you can use Git garbage collection.&lt;br&gt;
&lt;code&gt;$ git gc --prune=now --aggressive&lt;/code&gt;&lt;br&gt;
This command helps when you or your team heavily use pull or push commands.&lt;br&gt;
This command is an internal utility that cleans up unreachable or “orphaned” Git objects in your repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Take a backup of untracked files
&lt;/h4&gt;

&lt;p&gt;Most of the time, it’s safe to delete all the untracked files. But many times, there is a situation wherein you want to delete, but also to create a backup of your untracked files just in case you need them later.&lt;br&gt;
Git, along with some Bash command piping, makes it easy to create a zip archive for your untracked files.&lt;br&gt;
&lt;code&gt;$ git ls-files --others --exclude-standard -z |\&lt;br&gt;
xargs -0 tar rvf ~/backup-untracked.zip&lt;/code&gt;&lt;br&gt;
The above command makes an archive (and excludes files listed in .gitignore) with the name backup-untracked.zip&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Know your .git folder
&lt;/h4&gt;

&lt;p&gt;Every repository has a .git folder. It is a special hidden folder.&lt;br&gt;
&lt;code&gt;$ ls -a&lt;br&gt;
. … .git&lt;/code&gt;&lt;br&gt;
Git mainly works with two things:&lt;br&gt;
The working tree (the state of files in your current checkout)&lt;br&gt;
The path of your Git repository (specifically, the location of the .git folder, which contains the versioning information)&lt;br&gt;
This folder stores all references and other important details like configurations, repository data, the state of HEAD, logs, and much more.&lt;br&gt;
If you delete this folder, the current state of your source code is not deleted, but your remote information, such as your project history, is. Deleting this folder means your project (at least, the local copy) isn’t under version control anymore. It means you cannot track your changes; you cannot pull or push from a remote.&lt;br&gt;
Generally, there’s not much you need to do or should do, in your .git folder. It’s managed by Git and is considered mostly off-limits. However, there are some interesting artifacts in this directory, including the current state of HEAD:&lt;br&gt;
&lt;code&gt;$ cat .git/HEAD&lt;br&gt;
ref: refs/heads/master&lt;/code&gt;&lt;br&gt;
It also contains, potentially, a description of your repository:&lt;br&gt;
&lt;code&gt;$ cat .git/description&lt;br&gt;
This is an unnamed repository; edit this file ‘description’ to name the repository.&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  6. View a file of another branch
&lt;/h4&gt;

&lt;p&gt;Sometimes you want to view the content of the file from another branch. It’s possible with a simple Git command, and without actually switching your branch.&lt;br&gt;
Suppose you have a file called README.md, and it’s in the main branch. You’re working on a branch called dev.&lt;br&gt;
With the following Git command, you can do it from the terminal.&lt;br&gt;
&lt;code&gt;$ git show main:README.md&lt;/code&gt;&lt;br&gt;
Once you execute this command, you can view the content of the file in your terminal.&lt;/p&gt;

&lt;h4&gt;
  
  
  7. Search in Git
&lt;/h4&gt;

&lt;p&gt;You can search in Git like a pro with one simple command. Better still, you can search in Git even if you aren’t sure which commit — or even branch — you made your changes.&lt;br&gt;
&lt;code&gt;$ git rev-list --all | xargs git grep -F ‘’&lt;/code&gt;&lt;br&gt;
For example, suppose you want to search for the string “font-size: 52 px;” in your repository:&lt;br&gt;
&lt;code&gt;$ git rev-list –all | xargs git grep -F ‘font-size: 52 px;’&lt;br&gt;
F3022…9e12:HtmlTemplate/style.css: font-size: 52 px;&lt;br&gt;
E9211…8244:RR.Web/Content/style/style.css: font-size: 52 px;&lt;/code&gt;&lt;/p&gt;

</description>
      <category>github</category>
      <category>git</category>
      <category>gitbash</category>
    </item>
  </channel>
</rss>
