mongodbmeteorminimongo

Add array object to minimongo


I have a chat app, that is using Ionic 2 and Meteor with MongoDB. It works perfectly.

However, everything is stored in the MongoDB on the server, so each time a user wants to view their messages, they need to be connected to the Meteor/Mongo Server running in the cloud. Also, if one user deletes their chat, it will delete the chat on the MongoDB, and the corresponding other user will also have their chat deleted.

I would like similar functionality as WhatsApp where the messages are held locally on the device (I am using SQLite), and only new messages are held in the cloud until both users download them.

Currently my app iterates over a Mongo.Cursor<Chat> object. It also observes this object (this.chats.observe({changed: (newChat, oldChat) => this.disposeChat(oldChat), removed: (chat) => this.disposeChat(chat)});).

I get chat data from SQLlite that I have stored locally (Array<Chat>).

Question

Is it possible to add the SQLite data (Array<Chat>) to the Mongo.Cursor<Chat>? When I do so, I want to just add to minimongo and not MongoDB on the server.

Thanks

UPDATE

Asp per advise below, I do the following:

let promise: Promise<Mongo.Cursor<Chat>> = new Promise<Mongo.Cursor<Chat>>(resolve => {
  this.subscribe('chats', this.senderId, registeredIds, () => {
    let chats: Mongo.Cursor<Chat> = Chats.find(
      { memberIds: { $in: registeredIds } },
      {
        sort: { lastMessageCreatedAt: -1 },
        transform: this.transformChat.bind(this),
        fields: { memberIds: 1, lastMessageCreatedAt: 1 }
      }
    );

    this.localChatCollection = new Mongo.Collection<Chat>(null);
    console.log(this.localChatCollection);

    chats.forEach(function (chat: Chat) {
      console.log('findChats(): add chat to collection: ' + chat);
      this.localChatCollection.insert(chat);
    });

Will update if it works.

UPDATE

When I do the following, it inserts the chat object:

      let promise: Promise<Mongo.Collection<Chat>> = this.findChats();
      promise.then((data: Mongo.Collection<Chat>) => {

        let localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null);
        data.find().forEach(function (chat: Chat) {
          console.log('==> ' + chat);
          localChatCollection.insert(chat);
        });

However, if I define the localChatCollection globally, it does not insert the chat object. There are no errors but the process just stops on the insert line.

private localChatCollection: Mongo.Collection<Chat> = new Mongo.Collection<Chat>(null);
....
         this.localChatCollection.insert(chat);

Any ideas how I can get this to insert into a globally defined collection?


Solution

  • Is it possible to add the SQLite data (Array) to the Mongo.Cursor? When I do so, I want to just add to minimongo and not MongoDB on the server.

    Meteor itself knows nothing about SQLite, but it sounds like you have that part of it working.

    To just add to minimongo and not the mongodb server, you're looking for a client-side collection. Just pass null in as the first parameter to the call to create your collection i.e.

    var localChatCollection = new Mongo.Collection(null)
    

    You can then insert to localChatCollection the same way you would with a synchronized collection.

    Source: Meteor docs