Mongoose: How to update an existing element in array? - javascript

I was wondering if there is a better way to update an existing element in an array instead of fetching database three times. If you have any ideas I would appreciate it. Thank you!
const creatStock = async (symbol, webApiData) => {
try {
// reversed array
const webApiDataReversed = webApiData.reverse();
const query = { symbol };
const update = { $addToSet: { data: webApiDataReversed } };
const options = { upsert: true, new: true };
// create/update Stock
const stockResult = await Stock.findOneAndUpdate(query, update, options);
const lastElement = stockResult.data.length - 1;
const updatePull = {
$pull: { data: { date: stockResult.data[lastElement].date } },
};
// removes last date from data array
await Stock.findOneAndUpdate(query, updatePull);
// update Stock
await Stock.findOneAndUpdate(query, update);
} catch (ex) {
console.log(`creatStock error: ${ex}`.red);
}
};
Schema
const ChildSchemaData = new mongoose.Schema({
_id: false,
date: { type: mongoose.Types.Decimal128 },
open: { type: mongoose.Types.Decimal128 },
high: { type: mongoose.Types.Decimal128 },
low: { type: mongoose.Types.Decimal128 },
close: { type: mongoose.Types.Decimal128 },
volume: { type: mongoose.Types.Decimal128 },
});
const ParentSchemaSymbol = new mongoose.Schema({
symbol: {
type: String,
unique: true,
},
// Array of subdocuments
data: [ChildSchemaData],
});
module.exports.Stock = mongoose.model('Stock', ParentSchemaSymbol);
Output

Well, if you don't need to return the updated document, Please try this one - this will just return a write result, with this things can be achieved in one DB call :
const creatStock = async (symbol, webApiData) => {
try {
// reversed array
const webApiDataReversed = webApiData.reverse();
const query = { symbol };
await Stock.bulkWrite([
{
updateOne:
{
"filter": query,
"update": { $pop: { data: 1 } }
}
}, {
updateOne:
{
"filter": query,
"update": {
$addToSet: {
data: webApiDataReversed
}
}
}
}
])
} catch (ex) {
console.log(`creatStock error: ${ex}`.red);
}
};
Ref : mongoDB bulkWrite

you can do like this way :
const creatStock = async (symbol, webApiData) => {
try {
// reversed array
const webApiDataReversed = webApiData.reverse();
const query = { symbol };
let stock = await Stock.findOne(query);
if(stock){
let stockData = JSON.parse(JSON.stringify(stock.data));
if(stockData.length>0){
stockData.pop();
}
stockData.concat(webApiDataReversed);
stock.data = stockData;
await stock.save();
}
} catch (ex) {
console.log(`creatStock error: ${ex}`.red);
}

Related

why does my cloud function keep giving an error (CloudFirestore with ForOf Loop)?

My function getDocuments() in summary consists in that I pass some parameters in an array (like the path, the name of the document, if I want to section it by parts) and based on that array I return the content of each document through a loop (ForOf), the function I do it more than anything to save me too many lines of code, the problem is that it always throws me an error that I do not know what it is.
Can you help me? Please
Cloud function
export const employees = functions.https.onRequest((request, response) => {
corsHandler(request, response, async () => {
return await security.securityLayer(
{ _definedMethod: "GET", userValue: request.method },
{ _definedType: true, _definedLevel: [4], _definedSeconds: 12, userToken: request.header("_token") },
{ required: false },
{ required: false }
).then(async (answer) => {
if (answer.status === 400 || answer.status === 401) {
return response.status(answer.status).send(answer);
}
return await security.getDocuments([
{ collection: "Core/", documentName: "Centers", options: { idReturn: "centros", nestedProperties: [] } },
{
collection: "Core/", documentName: "Employees", options: {
idReturn: "personal",
nestedProperties: [
{ idReturn: "employees", name: "employee" },
{ idReturn: "submanager", name: "submanager" },
{ idReturn: "manager", name: "manager" }
],
},
},
], SPECIAL_CODE).then((documents) => response.status(documents.status).send(documents))
.catch(() => response.status(500).send(security.error500(SPECIAL_CODE, 2)));
}).catch(() => response.status(500).send(security.error500("SPECIAL_CODE", 1)));
});
});
async function
export async function getDocuments(
documents: {
collection: string,
documentName: string,
options: {
idReturn: string,
nestedProperties: {
idReturn: string,
name: string
}[]
}
}[],
code: string):
Promise<{ status: 201, code: string, subcode: number, devDescription: string, data: any }> {
const data: any = {};
const response: { devDescription: string, subcode: number } = { devDescription: "The document was found and retrieved successfully.", subcode: 1 };
if (documents.length > 1) {
response.devDescription = "Documents were found and obtained successfully.";
response.subcode = 2;
}
for (const iterator of documents) {
const docRef = { path: iterator.collection, name: iterator.documentName };
const options = { id: iterator.options.idReturn, nestedProperties: iterator.options.nestedProperties };
const doc = await database.collection(docRef.path).doc(docRef.name).get();
if (!doc.exists) {
data[options.id] = "The document " + docRef.name + " does not exist in the specified path: " + docRef.path;
if (documents.length === 1) {
response.devDescription = "The document was not found. Check the DATA for more information.";
response.subcode = 3;
} else {
response.devDescription = "One, several or all documents were not found. Check the DATA for more information.";
response.subcode = 3;
}
} else {
const docData: any = doc.data();
if (options.nestedProperties.length === 0) {
data[options.id] = docData;
} else {
for (const nested of options.nestedProperties) {
data[options.id][nested.idReturn] = _.get(docData, nested.name);
}
}
}
}
return { status: 201, code: code, subcode: response.subcode, devDescription: response.devDescription, data: data };
}
I was investigating and I saw that what was causing the error was obviously the loop (ForOf), to solve it I used the Promise.all() method, so the actual code that works for me is the following
export async function getDocuments(
documents: {
collection: string,
documentName: string,
path?: string,
options: {
idReturn: string,
nestedProperties: {
idReturn: string,
name: string
}[]
}
}[],
code: string):
Promise<{ status: number, code: string, subcode: number, devDescription: string, data: any }> {
const idPrimary: any = Object.values(
documents.reduce((c: any, v: any) => {
const k = v.options.idReturn;
c[k] = c[k] || [];
c[k].push(v);
return c;
}, {})
).reduce((c: any, v: any) => (v.length > 1 ? c.concat(v) : c), []);
if (idPrimary.length > 0) {
return {
status: 400, code: code, subcode: 0, data: idPrimary,
devDescription: "Some return IDs are repeated, check your code and replace the return IDs with unique IDs, for more information see the DATA section." };
}
const response: { devDescription: string, subcode: number } = { devDescription: "The document was found and retrieved successfully.", subcode: 1 };
const queries = [];
if (documents.length > 1) {
response.devDescription = "Documents were found and obtained successfully.";
response.subcode = 2;
}
documents.map((document) => {
if (document.path === undefined) {
document.path = document.collection + "/" + document.documentName;
}
});
for (const iterator of documents) {
queries.push(database.collection(iterator.collection).doc(iterator.documentName).get());
}
return Promise.all(queries).then((snapShot) => {
const data: any = {};
snapShot.forEach((doc) => {
const docProperties = documents.find((item) => item.path === doc.ref.path) ?? null;
if (!doc.exists) {
if (docProperties !== null) {
data[docProperties.options.idReturn] = "The document " + doc.id + " does not exist in the specified path: " + doc.ref.path;
if (documents.length === 1) {
response.devDescription = "The document was not found. Check the DATA for more information.";
response.subcode = 3;
} else {
response.devDescription = "One, several or all documents were not found. Check the DATA for more information.";
response.subcode = 3;
}
}
} else {
if (docProperties !== null) {
const docData: any = doc.data();
if (docProperties.options.nestedProperties.length === 0) {
data[docProperties.options.idReturn] = docData;
} else {
data[docProperties.options.idReturn] = {};
for (const nested of docProperties.options.nestedProperties) {
if (nested.name === undefined) {
data[docProperties.options.idReturn][nested.idReturn] = _.get(docData, nested.idReturn);
} else {
data[docProperties.options.idReturn][nested.idReturn] = _.get(docData, nested.name);
}
}
}
}
}
});
return { status: 201, code: code, subcode: response.subcode, devDescription: response.devDescription, data: data };
});
}

Mongoose find by a subdocument's value

I have 2 schemas
const schema = Schema({
headLine: {
type: String,
required: false
},
availableDays: [{
type: Schema.Types.ObjectId,
ref: AvailableDay
}]
}, {collection: 'providers', timestamps: true});
module.exports = mongoose.model("Provider", schema);
const schema = Schema({
day: {
type: String,
enum: ['Mondays','Tuesdays','Wednesdays','Thursdays','Fridays','Saturdays','Sundays']
},
timeFrom: String,
timeTo: String
}, {collection: 'availableDays', timestamps: true});
module.exports = mongoose.model("AvailableDay", schema);
Then in a route I call to a repository like this
router.get('/', async (req, res) => {
const match = {};
const sort = {};
const options = {};
// Arrange sort
if(req.query.sortBy){
const sortArray = JSON.parse(req.query.sortBy);
sortArray.map(e => sort[e[0]] = e[1] && e[1] === 'desc' ? -1 : 1);
options['sort'] = sort
}
// Get the pagination: limit how many, skip where it starts
if(req.query.limit) {
options['limit'] = parseInt(req.query.limit);
}
if(req.query.skip) {
options['skip'] = parseInt(req.query.skip);
}
const docs = await ProviderRepository.findBy(match, {}, options);
res.status(200).json(docs)
});
So what I need here is to filter providers for an AvailableDay monday and return the docs and count the total docs for pagination. I'm doing something like this without success
const findBy = async (params, projection = "", options = {}, callback) => {
const data = () => {
Provider.find(params, projection, options)
.populate([{path: 'user', match: {gender: 'F'}}]).exec((error, e) => {
if (error) {
console.log('error:', error)
return {error: error}; // returns error in json
}
return e.filter(i => i.user);
});
};
const total = await Provider.countDocuments(params).exec();
return {data(), total}
}
Thanks in advance
Use mongoose-aggregate-paginate-v2 and update your schema. If you use that package then you have to convert your queries from populate to aggregate style.
STEP 1: Update schema. Sample Schema:
const mongoose = require('mongoose');
const mongoosePaginate = require('mongoose-aggregate-paginate-v2');
const Schema = mongoose.Schema;
let definition = {
headLine: {
type: String,
required: false
},
availableDays: [{
type: Schema.Types.ObjectId,
ref: AvailableDay
}]
};
let options = {
collection: 'providers'
};
let providerSchema = new Schema(definition, options);
providerSchema.plugin(mongoosePaginate);
module.exports = mongoose.model('providers', providerSchema);
STEP 2: Update controller. Sample code in controller:
router.get('/', async (req, res) => {
const match = {}
const sort = {
// Fill it based on your sort logic.
}
const paginateOptions = {
page: req.query.page, // Page number like: 1, 2, 3...
limit: req.query.limit // Limit like: 10, 15, 20...
};
ProviderRepository
.findBy(match, {}, sort, paginateOptions)
.then(() => {
res.status(200).json(docs)
})
.catch(() => {
res.status(HTTP_ERROR_CODE).json({ "error": "Your error message" })
})
});
STEP 3: Update manager. Sample code in manager:
const findBy = (match, projection, sort, paginateOptions) => {
if (!paginateOptions) {
paginateOptions = {
pagination: false
};
}
let providerAggregate = providerSchema.aggregate([
{
$lookup: {
from: "availableDays",
let: { days: "$availableDays" },
pipeline: [
{
$match: {
$expr: {
$in: ["$$availableDays", "$day"]
}
}
}
],
as: "availableDays"
}
},
{
$lookup: {
from: "users", // I dont know the collection name
let: { user_id: "$user" }
pipeline: [
{
$match: {
"gender": 'F',
$expr: {
$eq: ["$_id", "$$user_id"]
}
}
}
],
as: "users"
}
}
{ $sort: sort }
]);
return providerSchema
.aggregatePaginate(providerAggregate, paginateOptions)
.then(res => {
return res;
})
.catch(err => {
throw err;
});
};

How to distinct query and get last data date? (parse server)

like js code
when I do this
it will query up to 1000 times,
can it query once?
const promises = idList.map(async id => {
const query = new Parse.Query("results");
query.equalTo("id", id);
query.descending("createdAt");
query.first()
});
const prPool = await Promise.all(promises);
You need to use aggregate. It would be something like this:
const pipeline = [
{ match: { id: id } },
{ sort: { createdAt: -1 } },
{ group: { objectId: '$id', lastCreatedAt: { $first: '$createdAt' } } }
];
const query = new Parse.Query('results');
const prPool = await query.aggregate(pipeline);

Image Upload in GraphQL

how can i handle image uploads in graphql
Through multer using express route to handle upload and query from graphql to view the images and other data
app.use('/graphql', upload);
app.use('/graphql', getData, graphqlHTTP(tokenData => ({
schema,
pretty: true,
tokenData,
graphiql: true,
})));
This is a duplicate of How would you do file uploads in a React-Relay app?
In short, yes you can do a file upload in graphql with react + relay.
You need to write the Relay update store action, for example:
onDrop: function(files) {
files.forEach((file)=> {
Relay.Store.commitUpdate(
new AddImageMutation({
file,
images: this.props.User,
}),
{onSuccess, onFailure}
);
});
},
Then implement a mutation for the Relay store
class AddImageMutation extends Relay.Mutation {
static fragments = {
images: () => Relay.QL`
fragment on User {
id,
}`,
};
getMutation() {
return Relay.QL`mutation{ introduceImage }`;
}
getFiles() {
return {
file: this.props.file,
};
}
getVariables() {
return {
imageName: this.props.file.name,
};
}
getFatQuery() {
return Relay.QL`
fragment on IntroduceImagePayload {
User {
images(first: 30) {
edges {
node {
id,
}
}
}
},
newImageEdge,
}
`;
}
getConfigs() {
return [{
type: 'RANGE_ADD',
parentName: 'User',
parentID: this.props.images.id,
connectionName: 'images',
edgeName: 'newImageEdge',
rangeBehaviors: {
'': 'prepend',
},
}];
}
}
In you server-side schema, preform update
const imageMutation = Relay.mutationWithClientMutationId({
name: 'IntroduceImage',
inputFields: {
imageName: {
type: new GraphQL.GraphQLNonNull(GraphQL.GraphQLString),
},
},
outputFields: {
newImageEdge: {
type: ImageEdge,
resolve: (payload, args, options) => {
const file = options.rootValue.request.file;
//write the image to you disk
return uploadFile(file.buffer, filePath, filename)
.then(() => {
/* Find the offset for new edge*/
return Promise.all(
[(new myImages()).getAll(),
(new myImages()).getById(payload.insertId)])
.spread((allImages, newImage) => {
const newImageStr = JSON.stringify(newImage);
/* If edge is in list return index */
const offset = allImages.reduce((pre, ele, idx) => {
if (JSON.stringify(ele) === newImageStr) {
return idx;
}
return pre;
}, -1);
return {
cursor: offset !== -1 ? Relay.offsetToCursor(offset) : null,
node: newImage,
};
});
});
},
},
User: {
type: UserType,
resolve: () => (new myImages()).getAll(),
},
},
mutateAndGetPayload: (input) => {
//break the names to array.
let imageName = input.imageName.substring(0, input.imageName.lastIndexOf('.'));
const mimeType = input.imageName.substring(input.imageName.lastIndexOf('.'));
//wirte the image to database
return (new myImages())
.add(imageName)
.then(id => {
//prepare to wirte disk
return {
insertId: id,
imgNmae: imageName,
};
});
},
});
All the code above you can find them in this repo https://github.com/bfwg/relay-gallery
There is also a live demo http://fanjin.io

Promise in cron job

I try to run the code without success.
Only the first call works in start of node.
Here is my current code:
const Product = require('../models/Product');
const Price = require('../models/Price');
const cron = require('node-cron');
const amazon = require('amazon-product-api');
const util = require('util');
const _ = require('underscore')
/**
* Cron job
* Tracking price
*/
exports.track = () => {
cron.schedule('* * * * *', () => {
const client = amazon.createClient({
awsId: process.env.AWS_ID,
awsSecret: process.env.AWS_SECRET,
assocId: process.env.AWS_TAG
});
Promise.all([Product.getAsin()])
.then(([asin]) => {
let listId = _.pluck(asin, '_id');
let listAsin = _.pluck(asin, 'asin');
if (asin.length === 0) {
return
}
client.itemLookup({
idType: 'ASIN',
itemId: listAsin,
domain: 'webservices.amazon.fr',
responseGroup: 'ItemAttributes,OfferFull,SalesRank'
}).then((results) => {
for(i=0; i<listId.length; i++){
results[i].id = listId[i];
}
for(res of results) {
Price.addPrice({
asin: res.ASIN[0],
product: res.id,
salePrice: res.Offers[0].Offer[0].OfferListing[0].Price[0].Amount[0],
})
}
console.log(listId);
Product.makeUpdate(listId);
}).catch(function(err) {
console.log(err);
console.log(util.inspect(err, true, null));
});
})
.catch((err) => {
console.log(err);
})
})
}
Requests to MongoDB are asynchronous.
Product
const mongoose = require('mongoose');
mongoose.Promise = Promise;
const _ = require('underscore');
const moment = require('moment');
const productSchema = new mongoose.Schema({
name: String,
domain: String,
originUrl: { type: String, unique: true },
check: { type: Number, default: 0 },
ean: String,
asin: String
}, { timestamps: true });
Object.assign(productSchema.statics, {
getAsin() {
return this.find(
{ updatedAt: { $lt: oneMin },
asin: { $ne: null }
}
).limit(10)
.select({ asin: 1 })
.exec()//.then((tuples) => _.pluck(tuples, 'asin'))
},
makeUpdate(id) {
console.log('list des ID updated => ' + id);
return this.update({ _id: { $in: id } }, { $inc : { "check": 1 } } , {multi: true}).exec();
}
});
const Product = mongoose.model('Product', productSchema);
module.exports = Product;
const oneMin = moment().subtract(1, 'minutes').format();
Also, since I'm absolutely new to JavaScript and Node.js in general, any best practices or general tips will be greatly appreciated! :)

Categories

Resources