npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

firestore-service

v1.1.1

Published

Create firestore queries with simple HTTP methods

Downloads

18

Readme

Firestore Service

FEArmy

Create firestore queries as if you were using a REST api.

Installation

npm install firestore-service

Prerequisites

1 - Create a firestore data base, you can do it here

2 - Get your database credentials. You can find those in Project Overview -> Add firebase to your web app

Note: We strongly recommend you to save the credentials on a .env file and don't upload them to any repository.

Getting Started

Once you have your credentials and the package install you can start using firestore service.

You will need to initialize the service as soon as you can, the code should look something like this:

import firestoreService from 'firestore-service';

const firebaseConfig = {
  apiKey: xxxxxxxxxxxx,
  authDomain: xxxxxxxxxxxx,
  databaseURL: xxxxxxxxxxxx,
  projectId: xxxxxxxxxxxx,
  storageBucket: xxxxxxxxxxxx,
  messagingSenderId: xxxxxxxxxxxx
};

firestoreService.initialize(firebaseConfig);

Response format

Every HTTP method will return a response with the following body.

{
  ok: a boolean -> true: Success, false: Failure
  data: The data you requested,
  status: An HTTP status code, see the code table below,
  statusText: 'OK' or 'Failure',
  request: the actual request (GET, POST, etc.)
}

Supported methods

GET

You can get all the elements from a collection or a specific element if the element's id is specified.

Note: The path will always be collection/id/collection2/id2/.... The "url" will finish with a collection if you want an array of elements or with an id if you want a single one.

E.g.:

const response = await firestoreService.get('regions');

The response will have all the "regions"

const response = await firestoreService.get('regions/32');

The response will have the information about the region with id 32.

Extra query parameters

Firestore service allows you to use certain tools to manipulate your data in the query. For this you should add extra parameters into the function get.

  • Limit

The response will only have 20 "regions"

const response = await firestoreService.get('regions/32', { limit: 20 });
  • Filter

The response will only have users who are older than 22 years old and younger than 35

const response = await firestoreService.get('regions/32', {
  filters: [
    { field: 'age', condition: '<', value: 35 },
    { field: 'age', condition: '>', value: 22 }
  ]
});

Supported condition operators:

  • <
  • <=
  • >
  • >=
  • ==
  • array_contains

Note: If you want to combine == with any of the others you will have to create an index in your db. More info about this here

  • Order By

Allows for ordering the query result by database field and ascending/descending direction. The default orderDirection is ascending

The following query will get regions ordered by age ascending

const response = await firestoreService.get('regions', { orderBy: 'age' });

The following query will get regions ordered by age descending

const response = await firestoreService.get('regions', {
  orderBy: 'age',
  descending: true
});

The following query will get regions ordered by age and name ascending

const response = await firestoreService.get('regions', {
  orderBy: ['age', 'name']
});

Note: If you want to order your query by several fields you will have to create an index in your db,More info about this here

POST

You can create an element by calling POST with the collection's path and the body with the element's data.

E.g.:

const body = {
  firstName: 'New name'
  lastName: 'New last name'
};

firestoreService.post('regions/32/users', body)

The previous request will create a new user under the region with id 32 using the information sent in the body. It will return the created user with its id.

DELETE

You can delete a certain element from a collection by using DELETE with a path to the element.

E.g.:

firestoreService.delete('regions/32/users/1');

The user from the region with id 32 that has id 1 will be deleted. The response will be empty

PATCH

You can update an element by calling PATCH and providing only the fields desired to be updated from an element.

E.g.:

const body = {
  firstName: 'New name2'
};

firestoreService.patch('regions/32/users/1', body);

The user with id 1 that belongs to the region with id 32 will have his name altered but will keep the previous values. The response will contain the updated user

PUT

You can update an element by calling PUT and providing all the fields in an element, providing less fields will make the rest of the values null (except for the id). Providing a different id in the body will result in an error.

E.g.:

const body = {
  id: 1,
  firstName: 'New name3',
  lastName: 'New last name3'
};

firestoreService.put('regions/32/users/1', body);

The user with id 1 that belongs to the region with id 32 will be altered.The response will contain the edited user.

const body = {
  firstName: 'New name3'
};

firestoreService.put('regions/32/users/1', body);

The user with id 1 that belongs to the region with id 32 will be altered and the lastName field will be set to null. The response will contain the edited user.

const body = {
  id: 2,
  firstName: 'New name3',
  lastName: 'New last name3'
};

firestoreService.patch('regions/32/users/1', body);

An error will be thrown because of id mismatch.

Authentications

Firestore service also supports methods to authenticate users. These are not http methods, are functions to be used with the same package.

LOGIN

You can login with an already created user (You can create them in the firebase console)

const email = '[email protected]'
const password = 'xxxxxx'


const response = await firestoreService.login(email, password);

The response will have all the user info.

SIGN UP

You can sign up a new user with the sign up method.

const email = '[email protected]'
const password = 'xxxxxx'


const response = await firestoreService.signUp(email, password);

If the response is succesful, the user will be created and all it's information will be in the response.

UPDATE PROFILE

You can update a user's profile with the following method.

const body = { name: 'example name', phone: 'example phone'}
const password = 'xxxxxx'


const response = await firestoreService.updateProfile(body);

If the response is succesful, the user will be updated.

Note: The user should be logged in to perform the update

Supported status codes

OK: 200
CREATED: 201
NO_CONTENT: 204
BAD_REQUEST: 400
UNAUTHORIZED: 401
FORBIDDEN: 403
NOT_FOUND: 404
CONFLICT: 409

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Contributing

Learn how to contribute in the CONTRIBUTING.md file.

About

This project is maintained by Lucas Zibell and it was written by Wolox.

Wolox