Mongoose: μ €μž₯ ν›„ isNew μƒνƒœ μœ μ§€

에 λ§Œλ“  2013λ…„ 05μ›” 07일  Β·  14μ½”λ©˜νŠΈ  Β·  좜처: Automattic/mongoose

객체가 포슀트 μ €μž₯ λ―Έλ“€μ›¨μ–΄μ—μ„œ μƒˆλ‘œμš΄ 것인지 μ•„λŠ” 것이 μ—¬μ „νžˆ μ€‘μš”ν•©λ‹ˆλ‹€. μ €λŠ” 그것이 false둜 μ„€μ •λ˜μ–΄ μžˆλ‹€κ³  λ―ΏμŠ΅λ‹ˆλ‹€. 포슀트 μ €μž₯ 후에 false둜 μ„€μ •λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

new feature

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

schema.pre('save', function (next) {
  this.wasNew = this.isNew;
  next();
})

schema.post('save', function () {
  if (this.wasNew) {
     // ...
  }
})

λͺ¨λ“  14 λŒ“κΈ€

schema.pre('save', function (next) {
  this.wasNew = this.isNew;
  next();
})

schema.post('save', function () {
  if (this.wasNew) {
     // ...
  }
})

이둜 인해 wasNew 속성이 μœ μ§€λ©λ‹ˆκΉŒ?

μŠ€ν‚€λ§ˆμ— μΆ”κ°€ν•˜μ§€ μ•ŠλŠ” ν•œ

μ•ˆλ…•,

λ‚΄κ°€ κ°€μ§€κ³ μžˆλŠ” μ½”λ“œμ—μ„œ 이것을 μ‚¬μš©ν•˜λ €κ³ ν•˜λŠ”λ° 이것이 μž‘λ™ν•˜μ§€ μ•ŠλŠ” 이유λ₯Ό λͺ¨λ₯΄κ² μŠ΅λ‹ˆλ‹€. 사전 μ €μž₯μ—μ„œ wasNewκ°€ μƒμ„±λ˜κ³  trueμž…λ‹ˆλ‹€. κ·ΈλŸ¬λ‚˜ 사후 μ €μž₯μ—μ„œλŠ” 이 ν”Œλž˜κ·Έκ°€ μ •μ˜λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€(κ·Έλž˜μ„œ μ •μ˜λ˜μ§€ μ•ŠμŒμ„ μ–»μŠ΅λ‹ˆλ‹€).

λ‚΄κ°€ 잘λͺ»ν•˜κ³ μžˆλŠ” 것이 μžˆμŠ΅λ‹ˆκΉŒ? _this_의 λ²”μœ„κ°€ 두 ν•¨μˆ˜ 간에 μœ μ§€(λ˜λŠ” 바인딩)λ˜μ§€ μ•Šμ€ 것과 κ°™μŠ΅λ‹ˆλ‹€.

λ‚˜λŠ” λͺ½κ΅¬μŠ€ 버전을 μ‹œλ„ν–ˆλŠ”λ°, ν•˜λ‚˜λŠ” npm('3.9.8-pre')μ—μ„œ μ„€μΉ˜ν–ˆκ³  λ‹€λ₯Έ ν•˜λ‚˜λŠ” 이 λ¦¬ν¬μ§€ν† λ¦¬μ—μ„œ λΉŒλ“œν–ˆμŠ΅λ‹ˆλ‹€... λˆ„κ΅°κ°€ μ œμ•ˆ 사항이 μžˆμŠ΅λ‹ˆκΉŒ?

λ‚΄ μŠ€ν‚€λ§ˆκ°€ _wasNew_ ν•„λ“œλ₯Ό 포함할 ν•„μš”κ°€ μ—†λ‹€λŠ” 것을 μ΄ν•΄ν–ˆμŠ΅λ‹ˆλ‹€. λ§žμŠ΅λ‹ˆκΉŒ?

미리 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€!

μ•”ν˜Έ:

module.exports = function (schema) {

    log.info("Inside functions");

    schema.pre('save', function (next) {
        log.info('pre saving...');

        this.wasNew = this.isNew;
        var self = this;
        if (self.isNew) {
            self.information.token = require('crypto').randomBytes(48).toString('hex');
            self.profile.name = (self.email.split("@"))[0];
        }
        next();
    });

    schema.post('save', function (doc) {
        log.info('post saving...', this);

        if (this.wasNew) {
            log.info("Sending email");
            mailingService.welcome(this.email);
        }

        var bond = {
            "name": doc.profile.name,
            "owner": doc._id
        };

        log.info(bond);

        dbHelper.createBond(bond);
    });
};

μ•ˆλ…•ν•˜μ„Έμš”, μ €λŠ” _this.wasNew_λ₯Ό _doc.wasNew_둜 λ³€κ²½ν–ˆκ³  이제 μ„±κ³΅μ μœΌλ‘œ μž‘λ™ν•©λ‹ˆλ‹€! 그리고 ν•„λ“œλŠ” ν•„μš”μ— 따라 λ¬Έμ„œμ— μ €μž₯λ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

이것은 μ •ν™•ν•˜μ§€ μ•ŠμŠ΅λ‹ˆκΉŒ?

감사 ν•΄μš”!

λ„€ :)

μ—¬λŸ¬λΆ„, 이 쑰정은 λͺ¨λ“  μŠ€ν‚€λ§ˆμ˜ κΈ°λ³Έκ°’μœΌλ‘œ ν˜„μž¬ 뢄기에 μ‹€μ œλ‘œ μΆ”κ°€λ˜μ–΄μ•Ό ν•©λ‹ˆλ‹€.

wasNew κ°€ λ‚΄μž₯λ˜μ–΄μ„œλŠ” μ•ˆ λ˜λŠ” μ΄μœ κ°€ μžˆλ‚˜μš”? λͺ½κ΅¬μŠ€μ— λ„£μ–΄λ³ΌκΉŒμš”?

합리적인 아이디어, 이후 5.x λ¦΄λ¦¬μŠ€μ—μ„œ κ³ λ €ν•  κ²ƒμž…λ‹ˆλ‹€.

λͺ¨λ‘λ“€ μ•ˆλ…• !

이 λ‚΄μž₯ κΈ°λŠ₯에 λŒ€ν•œ μ—…λ°μ΄νŠΈκ°€ μžˆμŠ΅λ‹ˆκΉŒ?

wasNew 이(κ°€) λͺ½κ΅¬μŠ€ _(5.4.19)_에 μ‘΄μž¬ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€. μƒˆ λ¬Έμ„œλ₯Ό μ €μž₯ν•˜λ €κ³  ν•  λ•Œ

@darkterra wasNew 이(κ°€) μ—†μŠ΅λ‹ˆλ‹€ pre 내뢀에 생성해야 ν•˜λ©° μœ„μ˜ 주석 https://github.com/Automattic/ μ—μ„œ μ–ΈκΈ‰ν•œ λŒ€λ‘œ post λ‚΄λΆ€μ—μ„œ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€. λͺ½κ΅¬μŠ€/issues/1474#issuecomment -17669982

@thakurinbox , κ·€ν•˜μ˜ 응닡에 κ°μ‚¬λ“œλ¦½λ‹ˆλ‹€.

λ‚΄κ°€ μ›λž˜ κ²Œμ‹œλ¬Όμ˜ μ§ˆλ¬Έμ„ "postSave hook_μ—μ„œ isNew λ₯Ό 보쑴"ν•˜λŠ” 것이라고 μ˜¬λ°”λ₯΄κ²Œ μ΄ν•΄ν•œλ‹€λ©΄ @emanuelecasadio , @lbstr 및 @vkarpov15 λŠ” "postSave"에 이 데이터λ₯Ό λ‚΄μž₯ν•˜λŠ” 것이 쒋을 것이라고 λ§ν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. 'isNew' λ˜λŠ” 'wasNew' 속성인지 μ—¬λΆ€) _.

'isNew'λ‚˜ 'wasNew'κ°€ μ—†μ–΄μ„œ 'isNew' 속성이 더 이상 μ‘΄μž¬ν•˜μ§€ μ•Šκ³  λ―Έλž˜μ— 'wasNew'둜 λŒ€μ²΄λ  것인지 κΆκΈˆν•©λ‹ˆλ‹€.

_μ½”λ“œμ˜ μ•½κ°„μ˜ 평화:_

'use strict';

const mongoose = require('mongoose');  // v5.4.19

const connect = mongoose.connect('mongodb://localhost/test_mongoose', { useNewUrlParser: true });
console.log('mongoose connect: ', connect);

connect.then(connectionObject => {
  const Schema = mongoose.Schema;

  const BlogPost = new Schema({
    title: String,
    body: String,
    date: Date
  });

  BlogPost.pre('save', function(next, doc, err) {
    console.log('pre: this: ', this);
    console.log('pre: doc: ', doc);
    next();
  });

  BlogPost.post('save', function(doc, next) {
    console.log('post: this: ', this);
    console.log('post: doc: ', doc);
    next();
  });

  const BlogModel = mongoose.model('BlogPost', BlogPost);


  const newBlogPost = new BlogModel({
    title: 'This is a test',
    body: 'This is the BEST POST ever !!!!!',
    date: new Date()
  });

  console.log('newBlogPost: ', newBlogPost);

  newBlogPost.save((err, doc) => {
    if (err) {
      console.error(err);
    }
    else
    {
      console.log('callback SAVE: ', doc);
      console.log('Post SAVED :)')
    }
  });
})

_그리고 isNew λ˜λŠ” wasNew κ°€ μ—†μŠ΅λ‹ˆλ‹€:_

mongoose connect:  Promise { <pending> }

newBlogPost:  { _id: 5c90aeb34f000a254ed2b6f7,
  title: 'This is a test',
  body: 'This is the BEST POST ever !!!!!',
  date: 2019-03-19T08:56:19.011Z }

pre: this:  { _id: 5c90aeb34f000a254ed2b6f7,
  title: 'This is a test',
  body: 'This is the BEST POST ever !!!!!',
  date: 2019-03-19T08:56:19.011Z }

pre: doc:  SaveOptions {}

post: this:  { _id: 5c90aeb34f000a254ed2b6f7,
  title: 'This is a test',
  body: 'This is the BEST POST ever !!!!!',
  date: 2019-03-19T08:56:19.011Z,
  __v: 0 }

post: doc:  { _id: 5c90aeb34f000a254ed2b6f7,
  title: 'This is a test',
  body: 'This is the BEST POST ever !!!!!',
  date: 2019-03-19T08:56:19.011Z,
  __v: 0 }

callback SAVE:  { _id: 5c90aeb34f000a254ed2b6f7,
  title: 'This is a test',
  body: 'This is the BEST POST ever !!!!!',
  date: 2019-03-19T08:56:19.011Z,
  __v: 0 }

Post SAVED :)

μ’‹μ•„, λΆ„λͺ…νžˆ μ½˜μ†”μ€ 객체의 λ‹€λ₯Έ 속성과 μ΄λŸ¬ν•œ λ©”μ„œλ“œλ₯Ό ν‘œμ‹œν•  수 μ—†λŠ” 것 κ°™μ§€λ§Œ preSaveμ—μ„œ "this.isNew" 및 "this.isModified()"λ₯Ό ν˜ΈμΆœν•˜λ €κ³  ν•˜λ©΄ λ‹€μŒκ³Ό 같은 ν•­λͺ©μ΄ μžˆμŠ΅λ‹ˆλ‹€. μ½˜μ†”μ— λ‚˜νƒ€λ‚©λ‹ˆλ‹€ ...

  BlogPost.pre('save', function(next, doc, err) {
    console.log('pre: this: ', this);
    console.log('pre: doc: ', doc);
    console.log('pre: this.isNew: ', this.isNew); // true
    console.log('pre: this.isModified(): ', this.isModified()); // true

    this.wasNew = this.isNew;

    console.log('pre: this.wasNew: ', this.wasNew); // true
    next();
  });

λ”°λΌμ„œ 이 문제의 트릭이 잘 μž‘λ™ν•¨μ„ ν™•μΈν–ˆμŠ΅λ‹ˆλ‹€. λ°˜λ©΄μ— κ³Όκ±°μ—λŠ” μ½˜μ†”μ΄ Mongoose에 μ˜ν•΄ μ •μ˜λœ 개체의 κΈ°λŠ₯ 및 기타 속성을 μ΄ˆκ³Όν•˜μ—¬ ν‘œμ‹œν•  수 μžˆλ‹€κ³  ν™•μ‹ ν•©λ‹ˆλ‹€.

아직 이 κΈ°λŠ₯에 λŒ€ν•œ μ—…λ°μ΄νŠΈκ°€ μ—†μœΌλ―€λ‘œ 이 λ¬Έμ œκ°€ μ—¬μ „νžˆ μ—΄λ € μžˆμŠ΅λ‹ˆλ‹€ :+1: . μ§€κΈˆμ€ 사전 μ €μž₯μ—μ„œ wasNewλ₯Ό μˆ˜λ™μœΌλ‘œ μ„€μ •ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰