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-sequelize

v0.8.0

Published

A simple Sequelize like ORM for Firestore

Downloads

30

Readme

Firestore Sequelize

Build Status

A simple Sequelize like ORM for Firestore

Simple Firebase ORM

If you like to use Sequelize and use models in your backend projects try to use FirestoreSequelize, some features:

  • Create Models for your Collections;
  • Construct Select queries like Sequelize using where and orderBy;
  • Default Attributes values for Collection Models;
  • Sync command to update Collection Structure;
  • Any level of Subcollections;
  • Typescript and JSDoc support;

Installation

npm i firestore-sequelize

or using yarn

yarn add firestore-sequelize

Initialization

First initialize your firebase-admin

const admin = require("firebase-admin");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
});

And then initialize firestore-sequelize

const sequelize = require("firestore-sequelize")
sequelize.initializeApp(admin)

Model Definition

const { defineModel, DataTypes } = require("firestore-sequelize");
const User = defineModel("users", {
  login: "",
  name: "",
  email: "",
  admin: {
    type: DataTypes.BOOLEAN,
    required: true,
    default: false,
  },
  coins: 0,
  years: {
    type: 'number',
  },
});

// Subcollections
const Team = defineModel("teams", {
  name: {
    type: DataTypes.STRING,
    required: true,
  },
  specialization: {
    type: DataTypes.STRING,
    required: true,
  },
}, {
  subcollections: [User],
})

API

| Static Methods | Return Type | | ------------------------------------------- | ----------------------------------------------------- | | create() | Promise<Model> | | findOne() | Promise<Model \| null> | | findOrCreate() | Promise<[Model, boolean]> | | findAll() | Promise<Model[]> | | update() | Promise<WriteResult> \| Promise<WriteResult[]> | | destroy() | Promise<WriteResult> \| Promise<WriteResult[]> | | drop() | Promise<WriteResult> \| Promise<WriteResult[]> | | docIds() | Promise<Model[]> | | sync() | Promise<Model[]> |

| Methods | Return Type | | ------------------------------------------- | ----------------------------------------------------- | | getId() | string | | setId() | void | | save() | Promise<WriteResult> | | update() | Promise<WriteResult> | | destroy() | Promise<WriteResult> | | collectionCreate() | Promise<Model> | | collectionFindOne() | Promise<Model \| null> | | collectionFindOrCreate() | Promise<[Model, boolean]> | | collectionFindAll() | Promise<Model[]> | | collectionUpdate() | Promise<WriteResult> \| Promise<WriteResult[]> | | collectionDestroy() | Promise<WriteResult> \| Promise<WriteResult[]> | | collectionDrop() | Promise<WriteResult> \| Promise<WriteResult[]> |

| Properties | Value Type | | ------------------------------------------- | ----------------------------------------------------- | | ref | DocumentReference | | path | string |

Filtering

where clauses

Use standard firestore operations ==, >, >=, <, <=, !=, in, not-in, array-contains, array-contains-any

const users = await User.findAll({
  where: {
    years: {
      '>': 10,
      '<=': 40,
    },
  },
})

id clauses

Use id clauses to filter by documentId

const users = await User.findAll({
  id: {
    'not-in': [
      'e855cafd-6578-441d-afb8-efc37de90b8f',
      '776b0026-aff0-48c2-a952-69619d7578c4',
    ],
  },
})

You can combine id and where clauses

const users = await User.findAll({
  where: {
    years: {
      '>': 10,
      '<=': 40,
    },
  },
  id: {
    'not-in': [
      'e855cafd-6578-441d-afb8-efc37de90b8f',
      '776b0026-aff0-48c2-a952-69619d7578c4',
    ],
  },
})

CRUD operations

create

Create record

const user = await User.create({
  login: "john",
  name: "John Doe" ,
  years: 40,
  email: "[email protected]",
  admin: false,
});

Another way to create user (not recommended)

const user = new User({
  login: "john",
  name: "John Doe",
  email: "[email protected]",
  years: 40,
  admin: false,
}, {
  id: 'john',
});
await user.save(true)

findOne

Find by id

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});

Find using where

const user = await User.findOne({
  where: {
    years: {
      '>': 20,
      '<=': 50,
    },
  },
})

findOrCreate

Find or create if not found

const [user, created] = await User.findOrCreate({
  where: { login: "jane" },
  defaults: {
    login: "jane",
    email: "[email protected]",
    name: "Jane Doe",
    admin: false,
  },
})

findAll

Find all not admin users ordered by login using where and order

const users = await User.findAll({
  where: { admin: false },
  order: [["login", "asc"]],
});

Find all users by ids ordered by name

const users = await User.findAll({
  id: [
    "e855cafd-6578-441d-afb8-efc37de90b8f",
    "776b0026-aff0-48c2-a952-69619d7578c4",
  ],
  order: [["name"]],
})
// or
const users = await User.findAll({
  id: {
    '>': '12bc',
    '<': 'def',
  },
  order: [["name"]],
})

update

Update user name using static class

await User.update({
  name: "Johnny Doe",
  coins: admin.firestore.FieldValue.increment(10),
}, {
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});

Update user name using user instance

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
await user.update({
  name: 'Johnny Doe',
  coins: admin.firestore.FieldValue.increment(10),
})

Another way to update

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
user.name = "Johnny Doe";
user.coins += 10
await user.save();

destroy

Delete User record using static class. By default all subcollections will be deleted, see Subcollections

await User.destory({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});

Use ignoreSubcollections: true to prevent subcollections from being deleted

await User.destroy({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
  ignoreSubcollections: true,
});

Delete User record using user instance

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
await user.destroy();

Delete all User records

await User.destroy({ force: true })
// or
await User.drop() // syntactic sugar for `User.destroy({ force: true })`

Subcollections

const Photo = defineModel("photos", {
  url: '',
  name: '',
  description: '',
});
const User = defineModel("users", {
  login: "",
  name: "",
  email: "",
  admin: {
    type: DataTypes.BOOLEAN,
    required: true,
  },
}, {
  subcollections: [Photo]
});

collectionCreate

Create photo using user instance

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
const photo = await user.collectionCreate('photos', {
  url: 'https://example.com/user/john/photos/photo1.jpg',
})

Create photo using Photo static class (not recommended)

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
const photo = await Photo.create({
  url: 'https://example.com/user/john/photos/photo2.png',
  name: 'photo2',
}, {
  parentPath: user.path,
})

collectionFindOne

Find photo using user instance

const photo = await user.collectionFindOne('photos', {
  where: { name: 'photo2' },
})

Find photo using Photo static class (not recommended)

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
const photo = await Photo.findOne({
  where: { name: 'photo2' },
}, {
  parentPath: user.path,
})

collectionFindAll

Find all photos using user instance

const photos = await user.collectionFindAll('photos', {
  where: {
    name: { '>=': 'photo' },
  }
})

collectionUpdate

Update photo using user instance

await user.collectionUpdate('photos', {
  description: "John's photo",
}, {
  where: { name: 'photo2' },
})

Update photo using photo instance

const photo = await user.collectionFindOne('photos', {
  where: { name: 'photo2' }
})
await photo.update({
  description: 'New photo',
})

collectionDestroy

Delete photo using user instance

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
await user.collectionDestroy('photos', {
  where: { name: 'photo2' },
})

Delete photo using photo instance

const photo = await user.collectionFindOne('photos', {
  where: { name: 'photo2' }
})
await photo.destroy()

Delete all photos of user

const user = await User.findOne({
  id: "e855cafd-6578-441d-afb8-efc37de90b8f",
});
await user.collectionDrop('photos')