Get all in MongoDB collection using SocialCMS with Breeze - node.js

I'm using SocialCMS Database middleware https://github.com/dai-shi/social-cms-backend with BreezeJS support.
I'm able to save changes to a MongoDB collection fine using manager.saveChanges() and manager.acceptChanges() and retrieve records in local cache using getEntities()
Using this middleware
With BreezeJS support:
var SCB_options = {
mongodb_url: 'mongodb://localhost:27017/socialcmsdb',
breeze_mongo: true,
routes: [{
object_type: 'user',
object_prefix: '/breeze-service/users'
}, {
object_type: 'post',
object_prefix: '/breeze-service/posts'
}, {
object_prefix: '/breeze-service/SaveChanges'
}]
};
How do I retrieve all remote records belonging to a particular route? For example, I want to retrieve all total posts remotely not in the users cache.
Do I modify app.js, my Angular apps datacontext, both or neither?

I solved this problem. It's a bit hard to discern from the Social CMS docs that the routes are local first. Which is great if you want to route without internet access! But I needed to create new routes outside of the SCB_options in order to request mongoDB directly.

Related

How to make a private call when using SSR Nuxt?

I am writing a headless solution for a WordPress website and noticed that for one particular endpoint, I need to authenticate to pull some data that will be used publicly. But, I'm concerned that where I'm using it will expose it to the web.
In my store/index.js I use the nuxtServerInit action method to execute some actions and I pass them some objects they need to fulfill their tasks:
async nuxtServerInit ({ dispatch }, { $axios, app }) {
await dispatch('initialize', { $axios, app })
},
$axios is passed because it will be used to query the API, and app is passed to help build the options to authenticate the request.
Is this a security vulnerability in Nuxt SSR? I think it is. If so, where are the only valid areas you can use secrets? asyncData ()?
If you're using SSR, you can use the privateRuntimeConfig runtime object and pass your secret in the nuxt.config.js file
export default {
privateRuntimeConfig: {
apiSecret: process.env.API_SECRET
}
}
If you read the documentation of nuxtServerInit, you can see that
Vuex action that is called only on server-side to pre-populate the store
Since this method is server-side only, you can use apiSecret (in my example) and it should be totally fine security-wise.
PS: Keep in mind that everything beyond what is generated on the server (hence, with NodeJS or nuxtServerInit) is "public". So your VueJS's client code lifecycle hooks are public: mounted(), fetch(), asyncData() because they will be visible on your browser's devtools.
Also, should your endpoint be that critical? If so, nuxtServerInit is the good way to go. If you need to fetch some more data in a "private way", you'll need to proxy it through some backend to hide the sensitive info and retrieve only the useful public data.

Generate Express Routes Dynamically from MongoDB Data which changes every few Minutes

I want to create a web page that dynamically renders data from mongoDB.
I am crawling articles on the internet and then saving data related to that in a MongoDB.
Now I want to create dynamic routes within express (e.g. page/:word), where word is a word taken from the crawled articles. If you use that route you get some information and statistics about the word (e.g. when it's used most)
The Problem I am having now is, that once i started my NodeJS Express Server the routes aren't updatet because once the data is loaded from the MongoDB it's not updatet later, when there is for example a new word in the database.
Is there any way to update these routes dynamically when I change data in the MongoDB ?
Btw: I am Using Handlebars to render the webpage, would all of that be easier with Angular ?
Thank you so much for your help!
You could check the database for each request, to see if the word can be found in the database:
app.get('/page/:word', (req, res) => {
collection.find({ word : req.params.word }).toArray().then(results => {
if (results.length) {
...word found...
} else {
...word not found...
}
});
});

Sails.js - use login instead of id for REST api

I'm using Sails.js framework, and I have User model with login field.
I also have User controller that allows me to send request like
http://localhost:1337/user/:id
And it returns given user data. However I'd prefer to use user login instead of user id so I could use /user/mylogin instead of /user/564a0aacecf0e8fb20c38a4e. Is there any way to do it without creating routes myself (I like how sails handle all default routes including relations like /user/:id/comments and I dont want to rebuild all of those just to use login instead of id)
You could do /user?login=username. This is handled by sails's blueprint api: http://sailsjs.org/documentation/reference/blueprint-api/find-where
If you really want it you can have it. But you have to get your hands a bit dirty. There are a few possibe ways. But I am pointing you out the easiest way seemed to me.
Add a route like following in your config/routes.js.
'/user/:myLogin': {
controller: 'user',
action:'getUserByUserLogin'
}
Add an action named getUserByUserLogin in your
api/controller/UserController.js. You can access the myLogin
value from request object from the controller action with
req.param("myLogin").
module.exports = {
getUserByUserLogin: function(req, res){
var myLogin = req.param("myLogin");
/* Do whatever you want */
}
};

How can I send a user's coordinates from phonegap to a remote server/mongodb

Specifically gps coordinates?
Basically the user will be able to view the location of other users on a google map. So I need to figure out how to send the users coordinates to my express app. Once they are stored in a mongodb, all of the coordinates can be rendered on a user's google map.
The basic question is: How can I take the user's coordinates from phonegap and store them in a remote server/mongodb?
I think an XMLHttpRequest Post request will send the relevant data, but how can I set up my server so that it receives that data and stores it using mongoose?
Always appreciate the help. Thanks!
Your question is rather broad. But i'll try to point you in the right direction.
1. Read the phonegap api-docs on geolocation
Here you will find out how to getCurrentPosition and extract other useful data from the client.
Phonegap documentation
2. Send data to your server
With plain JavaScript, zizzle.js, jQuery or any other framework you like you set up a post request to the server with the data.
jQuery Docs: examples using jQuery.post()
3. Recieve data
I don't know what server framework your using, but if you haven't decided yet, go with sails.js. A great framework for CRUD ops / rest-api.
Set up a model for your coordinates
Set up a simple controller in sails
4. Store data in MongoDb
Using Sails.js its very easy to persist data in any source you like. Set ut the config for your mongodb, and read the docs on sailsjs.
Here is a simple create using sails example:
// For example
User.create({
name: 'Mike',
age: 13,
phoneNumber: '(512)-555-5555'
}).done(function(err, user) {
// Error handling
if (err) {
return console.log(err);
// The User was created successfully!
} else {
console.log("User created:", user);
}
});

Saving a user's favorites

I am trying to associate a user authenticated with passport.js with a backbone.js collection or an array of ids (I don't which solution is the best) and save this in mongoDB.
I use node.js on server side.
Is it possible?
(for instance saving a user's favorites).
You can define a User schema like this:
var User = new Schema({
favorites: [{
whatHaveYou: String,
}]
})
Now, with using passport.js, after you have authenticating the user with this schema, you will be able to access the favorites with: req.user.favorites.
I suggest you go throw this demo project on github for setting up a node-express-mongoose project with passportjs if you have any more questions: https://github.com/madhums/node-express-mongoose-demo, this helped me a lot.

Resources