How are documents in CollectionFS able to store methods in MongoDB? - meteor

I'm using the Amazon S3 storage adapter with CollectionFS.
Normally you can't store a method into MongoDB. For example, I can't have a document stored in my CustomerOrders collection with a method on it that modifies the document directly with a discount code.
CustomerOrders.findOne().addDiscountCode('SAVE50PERCENT')
But with CollectionFS and the S3 storage adapter I'm able to run a remove function to delete an item from my Images collection as well as delete it from my Amazon S3 bucket:
Images.findOne().remove(function(error, success){
console.log(success)
});
The similarity in syntax struck me. It's basically saying:
Go into the Images collection
find a random document
return it as an object
run the .remove() method that's on that object
However, actually declaring the collection is different.
Instead of doing the following to create my Images collection,
Images = new Mongo.Collection('images');
I have to do:
Images = new FS.Collection("images", {
stores: [storeName],
filter: {
allow: {
contentTypes: ['image/*']
}
}
})
I'm guessing that FS.Collection extended Mongo.Collection's findOne() method so the code can:
Go into the Images collection
find a random document
return it as an object
add a .remove() method onto this object
return this extended object
run the .remove() method that's on that object
Is this kind of correct? The .remove method isn't actually stored in the document itself. It's simply added onto the document object that's returned in findOne()?

The remove method comes from a prototype in the FS Collection package (which extends the typical collection capabilities and modifies default behavior), so you are correct.
If you take a look at the cfs:collection package, and the api.common.js, you can see exactly how these methods are defined:
api.common.js

Related

Retrieving distinct properties [duplicate]

I would like to be able to provide a list of all the properties across all documents in a collection.
The best way I can come up with is to query for all documents and then build the list in the client, but this feels wrong.
The only way to do what you want is to read all of the documents. However, if you are worried about bandwidth, then you can do it in a stored procedure that only returns the list of properties.
If you take that route, I recommend that you start with the countDocuments sproc here and be prepared to call as many times as necessary until the continuation comes back empty and there are no 429 errors... or use documentdb-utils which takes care of that for you.
Alternatively, I could give you a full on example here. Just let me know.
Another approach would be to maintain a list of properties as documents are being written. This would be preferred if you need this list often.
You can store Documents with any kind of structure in a collection, they could be all different.
You are not restricted in a collection to store all objects from the same "schema".
So, getting all the properties available on a collection is not really something supported by the DocumentDB API or SDK, you either, read the whole collection, or rely on some sort of convention that you make when you create objects.
You can use Slazure for this. Example follows which lists all property names for a given set of documents:
using SysSurge.Slazure.AzureDocumentDB.Linq;
using SysSurge.Slazure.Core;
using SysSurge.Slazure.Core.Linq.QueryParser;
public void ShowPropertyNames()
{
// Get a reference to the TestCstomers collection
dynamic storage = new QueryableStorage<DynDocument>("URL=https://contoso.documents.azure.com:443/;DBID=DDBExample;TOKEN=VZ+qKPAkl9TtX==");
QueryableCollection<DynDocument> collection = storage.TestCustomers;
// Build collection query
var queryResult = collection.Where("SignedUpForNewsletter = true and Age < 22");
foreach (DynDocument document in queryResult)
{
foreach (KeyValuePair<string, IDynProperty> keyValuePair in document)
{
Console.WriteLine(keyValuePair.Key);
}
}
}

How do I query a sub collection in EmberFire?

I am currently trying to fetch data from FireStore using EmberFire. Right now, my collection is /users and in there I store a user ID. Under the user ID I create another subcollecion, containing an array called /presets.
I want to use EmberFire to retrieve the presets for the currently logged in user. How can I tell this to EmberFire?
I tried fetching other data using EmberFire and it worked fine. For example, fetching documents from a collection works perfectly fine, I just have never used sub collections. Hence the question.
What I would like to achieve is something like
this.store.query('/users/pLvAT0TSbAjsnXoVmMF7yEG3mkW2/presets')
to get to the data stored in (collection users) -> (document pLvAT0TSbAjsnXoVmMF7yEG3mkW2) -> (collection presets).
Of course I would like to then use the traditional workflow to turn the documents in presets into views.
Right now, I am only able to work with a single collection. Nested collections are not something I am able to work with.
Does anyone have an idea on how to solve this?
A general answer would be:
this.store.find('users', 'pLvAT0TSbAjsnXoVmMF7yEG3mkW2').then((user)=>{
return user.get('presets');
})
But it assumes some things done "the Ember way":
There's User model
There's a Preset model
There's a hasMany relationship between User and Preset like this:
// app/models/user.js
import DS from 'ember-data';
const { Model, attr, hasMany } = DS;
export default Model.extend({
presets: hasMany('preset', { subcollection: true })
});
Although the current version of Emberfire (v3-rc2) doesn't work pretty well with Subcollections, you'll be able to fetch records, but not create or update one.
Sources:
Emberfire guides (right now kind of empty, but hopefully someday it'll have good info)

how to display fetch data form firebase in backbone?

I am learning backbone.js. I am using firebase for API. I can't able to display data. Can anyone help me please?
I follow instruction mention on https://github.com/firebase/backbonefire.
var realtimeList = new RealtimeList();
realtimeList.on('sync', function(collection) {
console.log('collection is loaded ', collection);
// todoView.render();
});
Collection is object.
DEMO
Your view element is not part of DOM. You need to add it to DOM. I've used el property to do so for the sake of demo.
Also you need to stringify the JavaScript object returned by toJSON() to display in DOM.
Updated demo.
Your API is returning empty collection at the moment so I've used the todoItem model created locally for demo. But it isn't part of the Firebase collection. You need to pass the collection to view and render the models inside it in order to view realtime data.

Different parameters in Meteor call stubs

I have a meteor method that's supposed to handle file/image uploads by passing a cdn key, which is just a string.
For latency compensation though, I want to add the actual image blob to LocalMongo, that way I can add an image preview.
This is a problem since I want to just pass a string key to my server method, while I want to pass a file blob to my client method stub. Does Meteor support this? I don't want to pass the image blob to my server (as doing so would serialize the blob/make the call costly).
A solution I'm thinking of is to just define two Meteor methods with different names, the first one being for the client and the other for the server, and just calling them both with the proper parameters. Is this the proper way to do this in Meteor?
EDIT: My solution above doesn't actually work because Meteor realizes there is no method on the server (and nukes the local changes of my client method)
Just a suggestion, you can save the file blob in a Session variable and access in the method when the method stub is called from client, like this,
Meteor.methods({
'yourMethod': function (key) {
if (Meteor.isClient) {
var fileBlob = Session.get('my-file-blob'); //set this variable just before calling this method. And don't forget to remove it when template is destroyed.
} else {
}
}
});
Like I said, I didn't test it but just a suggestion. Hope it helps.

Grails - Removing an item from a hasMany association List on data bind?

Grails offers the ability to automatically create and bind domain objects to a hasMany List, as described in the grails user guide.
So, for example, if my domain object "Author" has a List of many "Book" objects, I could create and bind these using the following markup (from the user guide):
<g:textField name="books[0].title" value="the Stand" />
<g:textField name="books[1].title" value="the Shining" />
<g:textField name="books[2].title" value="Red Madder" />
In this case, if any of the books specified don't already exist, Grails will create them and set their titles appropriately. If there are already books in the specified indices, their titles will be updated and they will be saved. My question is: is there some easy way to tell Grails to remove one of those books from the 'books' association on data bind?
The most obvious way to do this would be to omit the form element that corresponds to the domain instance you want to delete; unfortunately, this does not work, as per the user guide:
Then Grails will automatically create
a new instance for you at the defined
position. If you "skipped" a few
elements in the middle ... Then Grails
will automatically create instances in
between.
I realize that a specific solution could be engineered as part of a command object, or as part of a particular controller- however, the need for this functionality appears repeatedly throughout my application, across multiple domain objects and for associations of many different types of objects. A general solution, therefore, would be ideal. Does anyone know if there is something like this included in Grails?
removeFrom*
Opposite of the addTo method in that it removes instances from an association.
Examples
def author = Author.findByName("Stephen King")
def book = author.books.find { it.title = 'The Stand' }
author.removeFromBooks(book)
Just ran into this issue myself. It's easy to solve. Grails uses java.util.Set to represent lists. You can just use the clear() method to wipe the data, and then add in the ones you want.
//clear all documents
bidRequest.documents.clear()
//add the selected ones back
params.documentId.each() {
def Document document = Document.get(it)
bidRequest.documents.add(document)
log.debug("in associateDocuments: added " + document)
};
//try to save the changes
if (!bidRequest.save(flush: true)) {
return error()
} else {
flash.message = "Successfully associated documents"
}
I bet you can do the same thing by using the "remove()" method in the case that you don't want to "clear()" all the data.
For a good explanation of deleting a collection of child objects with GORM have a look at the Deleting Children section of this blog post - GORM gotchas part 2
It's recommended reading, as are parts 1 and 3 of the series.
I am just starting to learn Grails myself and saw your question as an interesting research exercise for me. I do not think you can use the conventional data binding mechanism - as it fills in the blanks using some kind of Lazy map behind the scenes. So for you to achieve your goal your "save" method (or is it a function?) is unlikely to contain anything like:
def Book = new Book(params)
You need a mechanism to modify your controller's "save" method.
After some research, I understand you can modify your scaffolding template which is responsible for generating your controller code or runtime methods. You can get a copy of all the templates used by Grails by running "grails install-templates" and the template file you would need to modify is called "Controller.groovy".
So in theory, you could modify the "save" method for your whole application this way.
Great! You would think that all you need to do now is modify your save method in the template so that it iterates through the object entries (e.g. books) in the params map, saving and deleting as you go.
However, I think your required solution could still be quite problematic to achieve. My instinct tells me that there are many reasons why the mechanism you suggest is a bad idea.
For one reason, off the top of my head, imagine you had a paginated list of books. Could that mean your "save" could delete the entire database table except the currently visible page? Okay, let us say you manage to work out how many items are displayed on each page, what if the list was sorted so it was no longer in numerical order - what do you delete now?
Maybe multiple submit buttons in your form would be a better approach (e.g. save changes, add, delete). I have not tried this kind of thing in Grails but understand actionSubmit should help you achieve multiple submit buttons. I certainly used to do this kind of thing in Struts!
HTH
I'm just running into this same issue.
My application's domain is quite simple: it has Stub objects which have a hasMany relationship with Header objects. Since the Header objects have no life of their own, they're entirely managed by the Stub controller and views.
The domain class definitions:
class Stub {
List headers = new ArrayList();
static hasMany = [headers:Header]
static mapping = {headers lazy: false}
}
class Header {
String value
static belongsTo = Stub
}
I've tried the "clear and bind" method but the end result is that the "cleared" objects are left over in the database and grails will just create new instances for the ones that were not removed from the relationship. It does seem to work from an user's perspective, but it will leave lots of garbage objects in the database.
The code in the controller's update() method is:
stubInstance.headers.clear()
stubInstance.properties = params
An example: while editing the -many side of this relationship I have (for a given Stub with id=1):
<g:textField name="headers[0].value" value="zero" id=1 />
<g:textField name="headers[1].value" value="one" id=2 />
<g:textField name="headers[2].value" value="two" id=3 />
in the database there are 3 Header instances:
id=1;value="zero"
id=2;value="one"
id=3;value"two"
after removing header "one" and saving the Stub object the database will have headers:
id=1;value="zero"
id=2;value="one"
id=3;value"two"
id=4;value="zero"
id=5;value="two"
and the Stub object will now have an association with Headers with id=4 and id=5...
Furthermore, without the clearing of the list, if an index is not present in the submitted request.headers list, on data binding grails will keep the existing object at that location unchanged.
The solution that occurs to me is to bind the data, then check the Stub's headers for elements that are not present in the submitted list and remove them.
This looks like a pretty simple scenario, isn't there any built-in functionality to address it?
It's a bit overkill to have to write your own synchronization logic for maintaining relationships, especially when the quirks that make it non-trivial are caused by grails itself.
What about deletion, shouldn't the clear()'ed elements be gone from the database? Am I missing something in the relationship or domain object definitions?
class Stub {
List headers = new ArrayList();
static hasMany = [headers:Header]
static mapping = {
headers lazy: false
**headers cascade: "all-delete-orphan"**
}
}
class Header {
String value
static belongsTo = Stub
}
I have added the cascade property on the owning side of relationship and Now if you try to save the stub, it will take care of removing deleted items from the collection and delete them from the DataBase.

Resources