Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- let Schema = require('mongoose').Schema;
- const itens = db.model('itens', Schema({
- nome: String,
- createAt: { type: Date, default: Date.now },
- active: { type: Boolean, default: true }
- }, { collection: 'itens' }));
- const receitas = db.model('receitas', Schema({
- nome: String,
- itensReceita: [{
- _item: { type: Schema.Types.ObjectId, ref: 'itens' },
- quantidade: Number
- }],
- createAt: { type: Date, default: Date.now },
- active: { type: Boolean, default: true }
- }, { collection: 'receitas' }));
- const ordemProducao = db.model('ordemProducao', Schema({
- _receita: { type: Schema.Types.ObjectId, ref: 'receitas' },
- quantidadeFazer: Number,
- itens: []
- }, { collection: 'ordemProducao' }));
- // Criando uma receita
- (async () => {
- let lupuloTalvez = await itens.findOne({ nome: 'lupulo' }).exec();
- let cevadaTalvez = await itens.findOne({ nome: 'cevada' }).exec();
- receitas.save({
- nome: 'cerveja do lukinhas',
- itensReceita: [
- {
- _item: lupuloTalvez,
- quantidade: 2
- },
- {
- _item: cevadaTalvez,
- quantidade: 4
- },
- ]
- });
- })();
- // Criando uma ordem de producao
- (async () => {
- let qtdProduzir = 6;
- let cervejaLukinhas = await receitas.findOne({ nome: 'cerveja do lukinhas' }).exec();
- let itensCalculado = cervejaLukinhas.itensReceita.map(item => {
- item.quantidade = quantidade * qtdProduzir;
- return item;
- });
- ordemProducao.save({
- _receita: cervejaLukinhas.id,
- quantidadeFazer: qtdProduzir,
- itens: itensCalculado
- });
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement