0.11.0 • Published 2 years ago

firestore-repo v0.11.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Introduction

Firestore Repo is a framework aimed at creating business and application code that is implementation-agnostic.

Advantages to this approach: • Testable application and business code • Understandable code • Help avoid product lock in. Perhaps swap implementation out for a different database product.

Installation

npm i firestore-repo

Basic usage

Example: A basic application for managing user tasks. This Service class indirectly depends on Firestore through dependency injection. It could then be used within an express app to build a rest api.

class Service {
  private _runTransaction: TransactionBlock;

  constructor(factory: UnitOfWorkFactory) {
    this._runTransaction = (handler) => factory.create(handler);
  }

  //Creates a new user Task in firestore.
  createTask(
    name: string,
    deadline: Date | undefined,
    assignedUser: string | undefined
  ): Promise<Task> {
    return this._runTransaction(async (uow) => {
      const newTask = new Task('someRandomId', name, deadline, assignedUser);
      uow.tasks.add(newTask);
      return newTask;
    });
  }

  //Retrieves a new user Task in firestore.
  getTask(id: string): Promise<Task> {
    return this._runTransaction((uow) => uow.tasks.get(id));
  }

  //Updates a user Task in firestore.
  updateTask(
    id: string,
    name: string | undefined,
    deadline: Date | undefined,
    assignedUser: string | undefined
  ): Promise<Task> {
    return this._runTransaction(async (uow) => {
      const task = await uow.tasks.get(id);
      if (name !== undefined) {
        task.name = name;
      }
      if (deadline && assignedUser) {
        task.assign(assignedUser, deadline);
      }
      return task;
    });
  }

  //Removes a Task from firestore.
  deleteTask(id: string): Promise<Task> {
    return this._runTransaction(async (uow) => {
      const task = await uow.tasks.get(id);
      uow.tasks.remove(task);
      return task;
    });
  }
}
0.11.0

2 years ago

0.10.0

2 years ago

0.9.0

2 years ago

0.8.1

2 years ago

0.8.0

2 years ago

0.7.1

2 years ago

0.7.0

2 years ago

0.6.0

2 years ago

0.3.0

3 years ago

0.5.4

3 years ago

0.5.3

3 years ago

0.5.5

3 years ago

0.5.0

3 years ago

0.4.0

3 years ago

0.3.1

3 years ago

0.5.2

3 years ago

0.5.1

3 years ago

0.2.1

3 years ago

0.2.0

3 years ago

0.2.3

3 years ago

0.2.2

3 years ago

0.2.4

3 years ago

0.1.0

3 years ago

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.7

3 years ago

0.0.6

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago

0.0.0

3 years ago