I am fairly new to Meteor and trying to code my first bigger Meteor application. I am using the Meteor synced cron package to access an API in specific time intervals. Everything is working fine. But I want to access the cronHistory collection which is maintained by the **Synced cron package* The name of the collection is 'cronHistory'. Is there a way to get a reference to this database on the server side ?
Something like:
import { Mongo } from 'meteor/mongo';
//Not working because collection already exists. I only want to get the collection by name, not creating a new one.
export default CronHistory = new Meteor.Collection('cronHistory');
I already found a lot of suggestions and implementations, but all of them were intended to access the mongodb from the client side.
Anyone have some suggestions ?
There is no supported way to access collections by name on the server. You can, however, access the SyncedCron collection simply by SyncedCron._collection.
Related
I'm creating a REST API.
I would like to implement an indepotent PUT operation, that either creates or updates the specific resource in database.
I'm using node.js, postgreSQL and sequelize.
The problem is that sequelize upsert returns either true or false depending on wheter the resource got updated or created.
But I need to be able to send unique identifier (column id) back to the client, if the resource got created.
One solution that I tried was trying to find the exact same resource by specifing every single column send from client in "where" property of sequelize findOne query. But it throws errors, if client send additional columns that are not in database. And this shouldn't be the case in my implementation.
Is it possible to implement this? Optimally without some performance overhead.
Thanks
Getting the ID back from upsert in Sequelize is not possible at the moment. For more information see: https://github.com/sequelize/sequelize/issues/3354
A possible solution to this issue is to force the user (or the REST API client in this case) to supply the ID of a record in case update is requested, and to not supply an ID in case of creating a new record. This is generally a good idea as it prevents a round trip to the DB, though it makes your API a bit more strict.
am very new to both mongodb and java script.
Now i need to connect to my local mongodb instance using java script in order to get the list of documents.
any help?,
thanks in advance.
You can use mongoose to connect to a mongoDB database : http://mongoosejs.com/
Take a look at here,
https://mongodb.github.io/node-mongodb-native/driver-articles/mongoclient.html
var MongoClient = require('mongodb').MongoClient
, Server = require('mongodb').Server;
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
var db1 = mongoClient.db("mydb");
mongoClient.close();
});
In order to connect to database, you need to connect your application to a server( not sure if the application could connect to database directly but from what I learnt, it seems server side language is needed). Thus, if you want to connect to database with javascript only, you may consider using node.js+express to connect to mongoDb. I think this link is quite useful. I also learnt from this website. This website teaches you how to do that from scratch and instructions can be easily followed. If you prefer to use php for server side, I just googled this website which might help. It seems to me that php is easier to learn but if you only need basic CRUD operation, the website mentioned is enough.
I have built a small phonegap app in backbone.js which connects to a mongodb backend(hosted on mongolab) I can successfully retrieve and add to collections using the following url in my Backbone.Collection:
url: 'https://api.mongolab.com/api/1/databases/site_manager/collections/items?apiKey=****************'
but when I try to delete a model from the collection it does not remove it from the db. I have set the urlRoot of my model with an /:id attribute:
urlRoot : 'https://api.mongolab.com/api/1/databases/site_manager/collections/items/:id?apiKey=**********************'
but it still doesn't seem to be working - I'm not sure if i've put the id attribute in the right place. Everything works locally with a local server/db so I'm sure it has to do with the URL. Can anyone help with this please?
I am aware of security risks of accessing my db directly from the clientside with the API key but htis is just for a quick prototype.
Thanks
Have you tried to use Fiddler or
other tool that will allow you to monitor the HTTP requests and the returned status code that your application makes? At least you will be able to define where is problem.
In the ember guides on models it says (1) :
Ember Data is also designed to work with streaming APIs like socket.io, Firebase, or WebSockets. You can open a socket to your server and push changes to records into the store whenever they occur.
I tried writing a custom adapter that uses a websocket but i'm not getting very far. I couldn't find any working examples anywhere.
This is my totally unfinished prototype:
DS.WSAdapter = DS.Adapter.extend(Ember.Evented, {
websocket: undefined,
init: function () {
if(this.websocket === undefined)
{
this.websocket = new WebSocket('ws://localhost:8887');
this.websocket.onopen = function(e) {
console.log("Connection established!");
};
this.websocket.onmessage = function(e) {
// What to do here?
};
}
this._loadData();
},
//....
Can somone please help me with the websocket adapter?
My main problem is that I have no clue what to do when the websocket.onmessage() gets executed. I can't even access the store (using DS.get('defaultStore')) or anything
I don't have experience working directly with sockets in Ember, however I have recently completed an Ember Data + Firebase adapter which should follow very similar methodologies.
You should, at the least, be able to use it as inspiration:
https://github.com/sandersonet/ember-data-firebase
Firebase does provide an additional layer of abstraction from the sockets underneath, but the methodologies are very similar.
Have a look at http://emberjs.com/guides/models/frequently-asked-questions/#toc_how-do-i-inform-ember-data-about-new-records-created-on-the-backend
Some applications may want to add or update records in the store
without requesting the record via store.find. To accomplish this you
can use the DS.Store's push, pushPayload, or update methods. This is
useful for web applications that have a channel (such as SSE or Web
Sockets) to notify it of new or updated records on the backend.
Basically, you need to deserialize data you receive in your onmessage hook and push new objects to the data store using store.push('model', record) or alternative methods.
I'm currently researching how to add persistence to a realtime twitter json feed in node.
I've got my stream setup, it's broadcasting to the client, but how do i go about storing this data in a json database such as couchdb, so i can access the stores json when the client first visits the page?
I can't seem to get my head around couchdb.
var array = {
"tweet_id": tweet.id,
"screen_name": tweet.user.screen_name,
"text" : tweet.text,
"profile_image_url" : tweet.user.profile_image_url
};
db.saveDoc('tweet', strencode(array), function(er, ok) {
if (er) throw new Error(JSON.stringify(er));
util.puts('Saved my first doc to the couch!');
});
db.allDocs(function(er, doc) {
if (er) throw new Error(JSON.stringify(er));
//client.send(JSON.stringify(doc));
console.log(JSON.stringify(doc));
util.puts('Fetched my new doc from couch:');
});
These are the two snippets i'm using to try and save / retrieve tweet data. The array is one individual tweet, and needs to be saved to couch each time a new tweet is received.
I don't understand the id part of saveDoc - when i make it unique, db.allDocs only lists ID's and not the content of each doc in the database - and when it's not unique, it fails after the first db entry.
Can someone kindly explain the correct way to save and retrieve this type of json data to couchdb?
I basically want to to load the entire database when the client first views the page. (The database will have less than 100 entries)
Cheers.
You need to insert the documents in the database. You can do this by inserting the JSON that comes from the twitter API or you can insert one status at a time (for loop)
You should create a view that exposes that information. If you saved the JSON directly from Twitter you are going to need to emit several times in your map function
There operations (ingestion and querying) are not the same thing, so you should really do them at the different times in your program.
You should consider running a bg process (maybe in something as simple as a setInterval) that updates your database. Or you can use something like clarinet (http://github.com/dscape/clarinet) to parse the Twitter streaming API directly.
I'm the author of nano, and here is one of the tests that does most of what you need:
https://github.com/dscape/nano/blob/master/tests/view/query.js
For the actual query semantics and for you learn a bit more of how CouchDB works I would suggest you read:
http://guide.couchdb.org/editions/1/en/index.html
I you find it useful I would suggest you buy the book :)
If you want to use a module to interact with CouchDB I would suggest cradle or nano.
You can also use the default http module you find in Node.js to make requests to CouchDB. The down-side is that the default http module tends to be a little verbose. There are alternatives that give you an better API to deal with http requests. The request is really popular.
To get data you need to make a GET request to a view you can find more information here. If you want to create a document you have to use PUT request to your database.