The Journey of #100DaysOfCode (@henna_dev)

#Day86 of #100DaysOfCode

The last two weeks have been difficult but I am in a much better place now. I made peace with things beyond my control. :dove: My tarot card prediction also highlighted the same :wink:

I am freaking out a bit as I am only 9 days away from my Devoxx talk and I still have a long way to go to finish and practice but I am hopeful I will be able to do it… because I don’t have any alternative :stuck_out_tongue:

Today I spent most of the time understanding Realm backend, the task tracker tutorial uses a lot of functions, triggers, and even partition strategies… I am appreciative of @Nathan_Contino from the docs team, for being patient with me and answering my never-ending questions… :purple_heart:

Some of the functions I understood

CreateNewUserDocument

This function is called when a new user registers on the application

exports = async function createNewUserDocument({user}) {
  const cluster = context.services.get("mongodb-atlas");
  const users = cluster.db("tracker").collection("User");
  return users.insertOne({
    _id: user.id,
    _partition: `user=${user.id}`,
    name: user.data.email,
    canReadPartitions: [`user=${user.id}`],
    canWritePartitions: [`project=${user.id}`],
    memberOf: [
      {"name": "My Project", "partition": `project=${user.id}`}
    ],
  });
};

This is a hybrid partition strategy approach, user, and channel. Learn more on partition strategies.

The most interesting part was Sync permissions. It checks for read and write partitions by calling functions:

canReadPartition

exports = async function(partitionValue) {
  const cluster = context.services.get("mongodb-atlas");
  const userCollection = cluster.db("tracker").collection("User");
  try {
    const user = await userCollection.findOne({ _id: context.user.id });
    // If the user's canReadPartitions array contains the partition, they may read the partition
    return user.canReadPartitions && user.canReadPartitions.includes(partitionValue);
  } catch (error) {
    console.error(`Couldn't find user ${context.user.id}: ${error}`);
    return false
  }
}

canWritePartition

exports = async function(partitionValue) {
  const cluster = context.services.get("mongodb-atlas");
  const userCollection = cluster.db("tracker").collection("User");
  try {
    const user = await userCollection.findOne({ _id: context.user.id });
    // If the user's canWritePartitions array contains the partition, they may write to it
    return user.canWritePartitions && user.canWritePartitions.includes(partitionValue);
  } catch (error) {
    console.error(`Couldn't find user ${context.user.id}: ${error}`);
    return false
  }
};

I started a new topic in JavaScript: Asynchronous Operations

An asynchronous operation is one that allows the computer to ā€œmove onā€ to other tasks while waiting for the asynchronous operation to complete. Asynchronous programming means that time-consuming operations don’t have to bring everything else in our programs to a halt.

Operations like making a network request or querying a database can be time-consuming, but JavaScript allows us to execute other tasks while awaiting their completion.

What is a Promise?

Promises are objects that represent the eventual outcome of an asynchronous operation. A Promise object can be in one of three states:

  • Pending : The initial state— the operation has not completed yet.
  • Fulfilled : The operation has completed successfully and the promise now has a resolved value . For example, a request’s promise might resolve with a JSON object as its value.
  • Rejected : The operation has failed and the promise has a reason for the failure. This reason is usually an Error of some kind.

All promises eventually settle, enabling to write the logic for what to do if the promise fulfills or if it rejects.

That is all for today. Tomorrow will spend time on the presentation and Restaurant App.

Cheers, :performing_arts:

1 Like