0

I have made the following collection in meteor:

CodesData = new Mongo.Collection('CodesData');

CodesDataSchema = new SimpleSchema({
    code: {
        label: "Code",
        type: Number
    },
    desc: {
        label: "Description",
        type: String,
    }
});

CodesData.attachSchema(CodesDataSchema);

Now I want to prefill this collection with some data. For example: code: 1 desc: "hello". How can I do this manually and easily?

fangio
  • 1,746
  • 5
  • 28
  • 52
  • Can you please specify a bit more on how to prefill? Thosuand times the same values? Each time a different value? What apart from `CodesData.insert` inside a loop is the challenge here? – Jankapunkt Jul 09 '18 at 14:06
  • I want to prefill it, so If I start my website, the codes with the descriptions are already in the database. I am new with meteor and I don't know how to do this easily – fangio Jul 09 '18 at 14:23
  • And the codes are very different, it can't be done in a loop. I am wondering if I could do this for example by the command line (cmd). – fangio Jul 09 '18 at 14:30

1 Answers1

3

You can use Meteor.startup to run some actions on your collection once the server app has been loaded and is starting:

CodesData = new Mongo.Collection('CodesData'); 
CodesDataSchema = new SimpleSchema({ code: { label: "Code", type: Number }, desc: { label: "Description", type: String, } }); 
.attachSchema(CodesDataSchema);

Meteor.startup(()=>{
  // Only fill if empty, otherwise
  // It would fill on each startup
  if (CodesData.find().count() === 0) {
    CodesData.insert({ code: 1, description: 'some description' });
  }
});

If you have a lot of data to prefill you can define it in a JSON and load it on startup:

Consider the following json named as pre:

{
  codesdata: [
    { code: 1, description: 'foo' },
    { code: 7, description: 'bar' }
  ]
}

Meteor.startup(()=>{
  const preData = JSON.parse( pre );
  preData.codesData.forEach( entry => {
    CodesData.insert( entry );
  });
});

This allows you to manage your prefill more easily and also let's you version control the json if desired ( and no sensitive data is revealed ).

Considerations:

The function Meteor.startup runs on each start. So you should consider how to avoid unnecessary inserts / prefill that create doubles. A good way is to check if the collection is empty (see first example).

You may put the startup code an another js file in order to separate definitions from startup routines.

The current script does not differentiate between server or client. You should consider to do this on server and create a publication / subscription around it.

More readings:

https://docs.meteor.com/api/core.html#Meteor-startup

Importing a JSON file in Meteor

https://docs.meteor.com/api/core.html#Meteor-settings

Jankapunkt
  • 8,128
  • 4
  • 30
  • 59
  • It does not work I got, pre is not defined. I made a pre.JSON file and putted it in the same directory as my collection. _Codes.js:34 Uncaught ReferenceError: pre is not defined => const preData = JSON.parse( pre.JSON ) – fangio Jul 10 '18 at 10:06
  • Please read the link on how to import a JSON file in Meteor. I just provided a schematic approach. – Jankapunkt Jul 10 '18 at 11:31
  • I made a mistake, forgot " " – fangio Jul 10 '18 at 11:38
  • the code: find().count() === 0 does not work, it logs 0 while there are already elements in the DB – fangio Jul 11 '18 at 12:31