Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. let Schema = require('mongoose').Schema;
  2.  
  3. const itens = db.model('itens', Schema({
  4. nome: String,
  5. createAt: { type: Date, default: Date.now },
  6. active: { type: Boolean, default: true }
  7. }, { collection: 'itens' }));
  8.  
  9. const receitas = db.model('receitas', Schema({
  10. nome: String,
  11.  
  12. itensReceita: [{
  13. _item: { type: Schema.Types.ObjectId, ref: 'itens' },
  14. quantidade: Number
  15. }],
  16.  
  17. createAt: { type: Date, default: Date.now },
  18. active: { type: Boolean, default: true }
  19. }, { collection: 'receitas' }));
  20.  
  21. const ordemProducao = db.model('ordemProducao', Schema({
  22. _receita: { type: Schema.Types.ObjectId, ref: 'receitas' },
  23. quantidadeFazer: Number,
  24. itens: []
  25. }, { collection: 'ordemProducao' }));
  26.  
  27.  
  28.  
  29. // Criando uma receita
  30. (async () => {
  31. let lupuloTalvez = await itens.findOne({ nome: 'lupulo' }).exec();
  32. let cevadaTalvez = await itens.findOne({ nome: 'cevada' }).exec();
  33.  
  34. receitas.save({
  35. nome: 'cerveja do lukinhas',
  36. itensReceita: [
  37. {
  38. _item: lupuloTalvez,
  39. quantidade: 2
  40. },
  41. {
  42. _item: cevadaTalvez,
  43. quantidade: 4
  44. },
  45. ]
  46. });
  47. })();
  48.  
  49. // Criando uma ordem de producao
  50. (async () => {
  51. let qtdProduzir = 6;
  52.  
  53. let cervejaLukinhas = await receitas.findOne({ nome: 'cerveja do lukinhas' }).exec();
  54.  
  55. let itensCalculado = cervejaLukinhas.itensReceita.map(item => {
  56. item.quantidade = quantidade * qtdProduzir;
  57. return item;
  58. });
  59.  
  60. ordemProducao.save({
  61. _receita: cervejaLukinhas.id,
  62. quantidadeFazer: qtdProduzir,
  63. itens: itensCalculado
  64. });
  65. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement