๐Ÿ“œ ๋ชฉ์ฐจ

์•Œ๊ณ ๋ฆฌ์ฆ˜

์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

NestJS

Trigger / Procedure

Index

Redis

์•Œ๊ณ ๋ฆฌ์ฆ˜

๐Ÿ“Œ stringify( )

a = [1]
b = [1]

a === b // false
JSON.stringify(a) === JSON.stringify(b) // true ,"[1]" == "[1]" ๊ณผ ๋™์ผํ•œ ๊ฒฐ๊ณผ

๐Ÿ’ญ ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

// ์ƒˆ๋กœ ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์ด ์•„์ด๋”” ๊ทœ์น™์— ๋งž์ง€ ์•Š๋Š” ์•„์ด๋””๋ฅผ ์ž…๋ ฅํ–ˆ์„ ๋•Œ, 
// ์ž…๋ ฅ๋œ ์•„์ด๋””์™€ ์œ ์‚ฌํ•˜๋ฉด์„œ ๊ทœ์น™์— ๋งž๋Š” ์•„์ด๋””๋ฅผ ์ถ”์ฒœํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ฐœ๋ฐœํ•ด์•ผ ํ•œ๋‹ค.
// ๋‹ค์Œ์€ ์•„์ด๋””์˜ ๊ทœ์น™์ž…๋‹ˆ๋‹ค.

// ์•„์ด๋””์˜ ๊ธธ์ด๋Š” 3์ž ์ด์ƒ 15์ž ์ดํ•˜์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.
// ์•„์ด๋””๋Š” ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
// ๋‹จ, ๋งˆ์นจํ‘œ(.)๋Š” ์ฒ˜์Œ๊ณผ ๋์— ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ๋˜ํ•œ ์—ฐ์†์œผ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

/*
1๋‹จ๊ณ„ new_id์˜ ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ๋Œ€์‘๋˜๋Š” ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
2๋‹จ๊ณ„ new_id์—์„œ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ๋นผ๊ธฐ(-), ๋ฐ‘์ค„(_), ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
3๋‹จ๊ณ„ new_id์—์„œ ๋งˆ์นจํ‘œ(.)๊ฐ€ 2๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ ๋ถ€๋ถ„์„ ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ(.)๋กœ ์น˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
4๋‹จ๊ณ„ new_id์—์„œ ๋งˆ์นจํ‘œ(.)๊ฐ€ ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
5๋‹จ๊ณ„ new_id๊ฐ€ ๋นˆ ๋ฌธ์ž์—ด์ด๋ผ๋ฉด, new_id์— "a"๋ฅผ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
6๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด, new_id์˜ ์ฒซ 15๊ฐœ์˜ ๋ฌธ์ž๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋“ค์„ ๋ชจ๋‘ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
     ๋งŒ์•ฝ ์ œ๊ฑฐ ํ›„ ๋งˆ์นจํ‘œ(.)๊ฐ€ new_id์˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ๋์— ์œ„์น˜ํ•œ ๋งˆ์นจํ‘œ(.) ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค.
7๋‹จ๊ณ„ new_id์˜ ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ผ๋ฉด, new_id์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ new_id์˜ ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ๊นŒ์ง€ 
		 ๋ฐ˜๋ณตํ•ด์„œ ๋์— ๋ถ™์ž…๋‹ˆ๋‹ค.
*/

// ์‹ ๊ทœ ์œ ์ €๊ฐ€ ์ž…๋ ฅํ•œ ์•„์ด๋””๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” new_id๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, 
// ์„ค๊ณ„ํ•œ 7๋‹จ๊ณ„์˜ ์ฒ˜๋ฆฌ ๊ณผ์ •์„ ๊ฑฐ์นœ ํ›„์˜ ์ถ”์ฒœ ์•„์ด๋””๋ฅผ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด ์ฃผ์„ธ์š”.

// new_id๋Š” ๊ธธ์ด 1 ์ด์ƒ 1,000 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค.
// new_id๋Š” ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž, ์ˆซ์ž, ํŠน์ˆ˜๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
// new_id์— ๋‚˜ํƒ€๋‚  ์ˆ˜ ์žˆ๋Š” ํŠน์ˆ˜๋ฌธ์ž๋Š” -_.~!@#$%^&*()=+[{]}:?,<>/ ๋กœ ํ•œ์ •๋ฉ๋‹ˆ๋‹ค.

const filter = 'qwertyuiopasdfghjklzxcvbnm1234567890-_.';
function solution(new_id) {
    // 1. ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ 
    new_id = new_id.toLowerCase()
    // 2. ์•ŒํŒŒํŽซ ์†Œ๋ฌธ์ž, ์ˆซ์ž, -, _, . ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ
    let answer = ""
    for( let i = 0; i <new_id.length ; i++ ){
        if( filter.includes( new_id[i] ) ) {
            answer += new_id[i]
        }
    }
    
    // 3. ๋งˆ์นจํ‘œ(.)๊ฐ€ ๋‘ ๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ๋‹ค๋ฉด ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ๋กœ ์น˜ํ™˜
    while( answer.includes("..") ){
        answer = answer.replace("..",".")
    }
    
    // 4. ๋งˆ์นจํ‘œ(.)๊ฐ€ ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ œ๊ฑฐ
    if ( answer[0] === "." ){
        answer = answer.substring( 1 )
    }
    function removeLastdot() {
        if ( answer[answer.length-1] === "." ){
            answer = answer.substring( 0, answer.length-1 )
        }
    }
    removeLastdot()
    
    // 5. ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด ์†Œ๋ฌธ์ž a ๋Œ€์ž…
    if( answer.length === 0 ) { //if(answer==="")
        answer = "a"
    }
    
    // 6. ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด ์ฒซ 15๊ฐœ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐ,
    // ์ œ๊ฑฐ ํ›„ ์•„์ด๋”” ๋์— .์ด ์žˆ๋‹ค๋ฉด ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ๊ฑฐ
    if( answer.length >= 16 ){
        answer = answer.substring( 0, 15 )
        removeLastdot()
    }
    
    
    // 7. ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ฉด ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ ๊นŒ์ง€ ์•„์ด๋””์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๋ฐ˜๋ณต
    if ( answer.length <= 2 ) {
        answer = answer.padEnd( 3, answer[ answer.length -1 ] )
    }
    return answer
}

// ๋ฐฐ์—ด๊ณผ filter(), slice(), padEnd(), concat()์„ ์‚ฌ์šฉํ•œ ํ’€์ด
const filter = 'qwertyuiopasdfghjklzxcvbnm1234567890-_.';
function solution(new_id) {
    // 1. ๋ชจ๋“  ๋Œ€๋ฌธ์ž๋ฅผ ์†Œ๋ฌธ์ž๋กœ ์น˜ํ™˜ 
    new_id = new_id.toLowerCase().split("")
    // 2. ์•ŒํŒŒํŽซ ์†Œ๋ฌธ์ž, ์ˆซ์ž, -, _, . ์„ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ชจ๋“  ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐ
    let answer = new_id.filter( str => {
        return filter.includes( str )
    })
    
    // 3. ๋งˆ์นจํ‘œ(.)๊ฐ€ ๋‘ ๋ฒˆ ์ด์ƒ ์—ฐ์†๋œ๋‹ค๋ฉด ํ•˜๋‚˜์˜ ๋งˆ์นจํ‘œ๋กœ ์น˜ํ™˜
    answer = answer.filter( (str , i) => {
        return str !== "." || (str === "." && answer[i+1] !== "." )
    })
    
    // 4. ๋งˆ์นจํ‘œ(.)๊ฐ€ ์ฒ˜์Œ์ด๋‚˜ ๋์— ์œ„์น˜ํ•œ๋‹ค๋ฉด ์ œ๊ฑฐ
    if( answer[0] === "."){
        answer = answer.slice( 1 )
    }
    function removeLastDot(){
        if ( answer[answer.length -1] === "." ){
            answer = answer.slice( 0, answer.length-1 )
        }
    }
    removeLastDot()
    
    // 5. ๋นˆ ๋ฌธ์ž์—ด์ด๋ฉด ์†Œ๋ฌธ์ž a ๋Œ€์ž…
    if ( answer.length === 0 ) {
        answer.push("a")
    }
    
    // 6. ๊ธธ์ด๊ฐ€ 16์ž ์ด์ƒ์ด๋ฉด ์ฒซ 15๊ฐœ๋ฅผ ์ œ์™ธํ•œ ๋‚˜๋จธ์ง€ ๋ฌธ์ž๋ฅผ ๋ชจ๋‘ ์ œ๊ฑฐ,
    // ์ œ๊ฑฐ ํ›„ ์•„์ด๋”” ๋์— .์ด ์žˆ๋‹ค๋ฉด ๋งˆ์นจํ‘œ(.)๋ฅผ ์ œ๊ฑฐ
    if (answer.length >= 16){
        answer = answer.slice ( 0, 15 )
        removeLastDot()
    }
    
    // 7. ๊ธธ์ด๊ฐ€ 2์ž ์ดํ•˜๋ฉด ๊ธธ์ด๊ฐ€ 3์ด ๋  ๋•Œ ๊นŒ์ง€ ์•„์ด๋””์˜ ๋งˆ์ง€๋ง‰ ๋ฌธ์ž๋ฅผ ๋ฐ˜๋ณต
    if (answer.length <= 2){
        const add = new Array( 3 - answer.length ).fill(answer[  answer.length-1 ] )
        answer = answer.concat( add )
        // answer = [...answer, ...add]
    }
    return answer.join("")
}

NestJS

1. Trigger / Procedure

<aside> ๐Ÿ’ก ์ตœ๊ทผ์—๋Š” ๊ฐ€๊ธ‰์  ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ชฝ์—์„œ ํŠธ๋ฆฌ๊ฑฐ๋‚˜ ํ”„๋กœ์‹œ์ €๋ฅผ ๋งŒ๋“ ๋‹ค!

</aside>

๐Ÿ’ปย  ์‹ค์Šต - ํŠธ๋ฆฌ๊ฑฐ ๊ตฌํ˜„

  1. product ์˜ entities์— ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์ž๋™์œผ๋กœ ์‹คํ–‰๋˜๋Š” ํ•จ์ˆ˜๋ฅผ ์ž‘๋™ํ•  subscriber ๋ฅผ ์ƒ์„ฑ
// src/apis/products/entities/product.subscriber.ts

import { Connection, EntitySubscriberInterface, InsertEvent } from 'typeorm'
import { Product } from './product.entity'

@EventSubscriber()
export class ProductSubscriber implements EntitySubscriberInterface<Product> {
    constructor(connection: Connection){
        // ProductSubscriber๋ฅผ ๋„ฃ์–ด์„œ db์™€ ์—ฐ๊ฒฐํ•ด์ค€๋‹ค
        connection.subscribers.push(this)
    }

    // Product ํ…Œ์ด๋ธ”์„ ๋ฆฌ์Šจํ•œ๋‹ค 
    listenTo(){
        return Product
    }

    afterInsert( event: InsertEvent<Product> ){
        console.log(event)   
    }

}