import { Meteor } from 'meteor/meteor'; import { PoblesCollection } from '/imports/api/pobles.js'; import { check } from "meteor/check"; async function insertPoble({ nomPoble, cp, comarca }) { await PoblesCollection.insertAsync({ nomPoble, cp, comarca, createdAt: new Date() }); } Meteor.startup(async () => { // If the Pobles collection is empty, add some data. if (await PoblesCollection.find().countAsync() === 0) { await insertPoble({ nomPoble: "Alaquàs", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Albal", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Aldaia", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Alfafar", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Algemesí", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Alginet", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Alcàsser", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Benetússer", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Beniparrell", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Carlet", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Catarroja", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Dosaigües", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Godelleta", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Guadassuar", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Iàtova", cp: "", comarca: "" }); await insertPoble({ nomPoble: "L'Alcúdia", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Llocnou de la Corona", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Massanassa", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Paiporta", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Parke Alkosa", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Picanya", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Sedaví", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Setaigües", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Utiel", cp: "", comarca: "" }); await insertPoble({ nomPoble: "València-Castellar Oliveral", cp: "", comarca: "" }); await insertPoble({ nomPoble: "València-Forn d'Alcedo", cp: "", comarca: "" }); await insertPoble({ nomPoble: "València-La Torre", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Xest", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Xiva", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Llombai", cp: "", comarca: "" }); await insertPoble({ nomPoble: "Tremolar", cp: "", comarca: "" }); } // We publish the entire Pobles collection to all clients. // In order to be fetched in real-time to the clients Meteor.publish("pobles", function () { return PoblesCollection.find(); }); Meteor.methods({ 'editaOAfigPoble': async function (poble) { try { if (poble.nomPoble) { return await PoblesCollection.insertAsync({ ...poble, usuari: Meteor.userId(), createdAt: new Date() }); } else { throw new Error("El nom del poble no és vàlid"); } } catch (e) { console.error(e); } }, 'eliminaPoble': async function (pobleId) { try { if (pobleId) { return await PoblesCollection.removeAsync(pobleId); } else { throw new Error("El nom del poble no és vàlid"); } } catch (e) { console.error(e); } } }); });