[SOLVED] Global function to search specific key in a collection [Using SImplSchema]

Hi there,

I’m actually trying to build a Meteor global function, which will crawl every collections i have to see if a field / key (SimpleSchema) have a specific property i added.

For this, i did this :

var collections = Mongo.Collection.getAll();
    collections.forEach((col) => {
        function getSchema(col) {
            var col = col.name;
            if (col != null) {
                return col.charAt(0).toUpperCase() + col.slice(1) + 'Schema';
            } else {
                col = null;
                return col;
            }
        }
        if (typeof col !== 'undefined' && typeof col !== null) {
            console.log(getSchema(col));
            // console.log(getSchema(col).get('partners_id', 'label'));
        }
    });

In this example, i’m trying to display my collections at the end, it works, and i can see all :

MeteorToys.ImpersonateSchema
MeteorToys.JetSetterSchema
MeteorToys.MongolSchema
MeteorToys.AutoPubSchema
… and so on …

Perhaps, the commented part : " // console.log(getSchema(col).get(‘partners_id’, ‘label’));" tell me :

“Exception in defer callback: TypeError: getSchema(…).get is not a function”.

Is there any way to complete this ?

I tried :

var collections = Mongo.Collection.getAll();
    collections.forEach((col) => {
        function getSchema(col) {
            var col = col.name;
            // console.log(col.charAt(0).toUpperCase() + col.slice(1) + 'Schema');
            if (col != null) {
                return col.charAt(0).toUpperCase() + col.slice(1) + 'Schema';
            } else {
                col = null;
                return col;
            }
        }
        if (typeof col !== 'undefined' && typeof col !== null) {
            try {
                // console.log(getSchema(col));
                console.log(getSchema(col).get('partners_id', 'label'));
            } catch (error) {
                console.log(error + ' : For collection : ' + getSchema(col.name));
            }
        }
    });

But i’m having :

7crm.js:42 TypeError: getSchema(…).get is not a function : For collection : null
meteor.js?hash=0504f43f667698535416b00eb44eb6f53161cb63:1050 Exception in defer callback: TypeError: Cannot read property ‘name’ of null
at getSchema (crm.js:28)
at crm.js:42
at Array.forEach ()
at Blaze.TemplateInstance. (crm.js:26)
at blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3398
at Function.Template._withTemplateInstanceFunc (blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3769)
at fireCallbacks (blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3394)
at Blaze.View. (blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:3474)
at fireCallbacks (blaze.js?hash=adc5286b78e5c0f8e7f56a602f77eefb5def6bf1:2014)
at Object.Tracker.nonreactive (tracker.js:603)

I don’t know why it’s not working. Can’t see what i missed. Any ideas ?

Happy coding,

B.

Your code here: getSchema(col).get('partners_id', 'label') says to use the get() method which exists from the result of getSchema(col). However, your getSchema method does not return a get method, it returns a string or null.

Ok, i think i see what you mean.

It explains why :

console.log(PipelinesSchema.get('partners_id', 'label'));

works, instead of :

console.log(col.get('partners_id', 'label')); 

Then, how could i achieve it ? It finally seems that i won’t be able to achieve the task like that.

I’ll try to work without this function

I’m not sure exactly what you want to achieve :slight_smile:

Hmmm, i’ll try to explain exactly what i want to do :

Actually, i want to be able to handle real relationships between 2 collections, as i would do in a relationnal database.

So, what i’m trying to do is writting a global function, which will on a record delete for a specific collection (for now) search in every other collections if a key with a specific pattern (normalized to be easily found). This key will tell me if it is, or not, a m2m or a o2m relation between both of my collections.

Here for example :

I have Pipelines documents, from pipelines collection, which can have Partners IDS (_id from the partners collection). If i find a “partners_id” or a “partners_ids” key in any other collection, i want in that case to set it “null” for every documents.

Sorry for my english, i’m trying my best ^^

Thanks a lot !

Ouch! That sounds like a lot of hard work - and no guarantee that you’ll be able to do that sort of operation atomically.

It sounds like you should either take a look at your MongoDB database architecture design or switch to a SQL database, where all that will be done for you by the database engine (as it should be).

You’re doing fine :slight_smile:

Yes i know what it implies, that’s the main part of the app i’m trying to build :confused: But in the process i’m trying to have, for development purpose, having a mongodb database is very interesting, due to Simpl Schema package.

I still have searched for SQL capabilities with meteor, but i wasn’t able to find something to do.

That’s why i’m keeping Meteor and Mongo. I’ve also built some shell script to automate “modules” creation (i’m trying to build something modular).

There is lot of painfull task to achieve, but if i can find a way to do this part, most of the work will be done.

Here you can find my repo : GitHub - Brawcks/twiice: Open Source application to build anything on a simple modular way.

I’ll write the readme in english soon.

Anyway, still thanks for your help ! :slight_smile:

Any NPM SQL package. I’ve used mysql with promise-mysql successfully. The only thing you don’t have out of the box is reactivity or pub/sub, but you can use Meteor methods, or use a Meteor package, or add your own pub/sub wrapper on your NPM package of choice.

Ok, i’ll take a look ! :slight_smile:

Thanks !

You can also have a look at grapher, which has functionality like this. Search for the keyword autoremoval. You can set links between your collections and grapher will auto remove documents if another linked document is deleted.

1 Like

Very very very interesting ! I’ll try to switch on it a bit, this really seams to be what i want :slight_smile: Thanks !! Topic closed :smiley: