[개발] mongoose 의 virtual
출처 : https://masteringjs.io/tutorials/mongoose/virtuals
개요
Mongoose virtuals 는 Mongoose 문서에서 계산 된 속성입니다. MongoDB 자체에 저장되지 않는 값이며, 액세스 할 때마다 계산됩니다.
스팀잇으로 보면 스팀 블록체인에는 markdown 형식의 컨텐츠가 저장 되지만 frontend ( 스팀잇 닷컴 ) 에서는 마크다운 파서를 통해 html 형태로 보여지는 것이죠 :)
markdown 을 html 로
이렇게 DB 에 저장된 markdown 문법의 text 를 아래와 같이 mongoose 의 virtuals 를 이용하여 html 로 출력을 할 수 있도록 처리할 수 있습니다.
소스예시
// Markdown parser
const marked = require('marked');
const blogPostSchema = new Schema({ content: String });
// A _virtual_ is a schema property that is **not** stored in MongoDB.
// It is instead calculated from other properties in the document.
blogPostSchema.virtual('html').get(function() {
// In the getter function, `this` is the document. Don't use arrow
// functions for virtual getters!
return marked(this.content);
});
const BlogPost = mongoose.model('BlogPost', blogPostSchema);
const doc = new BlogPost({ content: '# Hello' });
doc.html; // "<h1 id="hello">Hello</h1>"
맺음말
아래를 다 구현할 수 있을지 ... 후후 / 일단 늘 꿈은 크게 ... ;; 이게 몇년째인지 헙
- nodejs + mongodb with mongoose => steem blockchain data archive
- nestjs => rest api server
- vuejs => web frontend
- flutter => mobile
@wonsama transfered 10 KRWP to @krwp.burn. voting percent : 9.37%, voting power : 91.48%, steem power : 1743151.61, STU KRW : 1200.
@wonsama staking status : 1142.929 KRWP
@wonsama limit for KRWP voting service : 5.714 KRWP (rate : 0.005)
What you sent : 10 KRWP
Refund balance : 4.286 KRWP [52679717 - d97a112d5e2b4460a81c02d203b2aa1d143d2a33]