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

linq2fire

v1.0.22

Published

Zero dependency. Supports special operators: IN, OR, !=, startsWith (^=), array-contains and many more

Downloads

61

Readme

Linq2Fire

Supports special operators: IN, NOT IN, OR, !=, startsWith (^=), array-contains (has) and many more

import linq from "linq2fire";
const db = firebase.firestore();

const printDocs = heading => docs => {
  console.log("**********", heading.toUpperCase(), "**********");
  docs.forEach(doc => console.log(doc.id, doc.data()));
  console.log();
};

const test = async () => {
  const $todos = linq(db).from("todos");

  await $todos
    .orderBy({ text: "desc" })
    .orderBy({ text: "desc" })
    .where({ category: "A" })
    .get()
    .then(printDocs("Duplicate order by"));

  await $todos.remove();

  // add single doc
  await $todos.set(1, {
    text: "Task 1"
  });
  // add multiple docs
  await $todos.set({
    1: {
      text: "Task 1",
      category: "A"
    },
    2: {
      text: "Task 2",
      category: "B",
      categories: ["A", "B"]
    },
    3: {
      text: "Task 3",
      categories: ["A"]
    },
    4: {
      text: "Task 4",
      category: "B",
      categories: ["B"]
    },
    5: {
      text: "Task 5",
      category: "A"
    },
    6: {
      text: "Other task",
      category: "C"
    }
  });

  await $todos
    .where({
      // not in operator
      "text<>": ["Task 1", "Task 2"]
    })
    .get()
    .then(printDocs("Find tasks which is not in [1, 2]"));
  
  await $todos
    .where({
      "text ^=": "Task"
    })
    .get()
    .then(printDocs("Find all tasks which starts with Task"));

  await $todos
    .orderBy({ text: "desc" })
    .first()
    .then(first => {
      console.log("Get first task ", first && first.data());
    });

  await $todos
    .where({
      // in operator
      text: ["Task 1", "Task 2"]
    })
    .get()
    .then(printDocs("Find tasks: 1, 2, 3"));

  await $todos
    .where({
      "text <": "Task 2"
    })
    .get()
    .then(printDocs("Find all tasks which has text less than Task 2"));

  await $todos
    .where({
      // not equal
      "text <>": "Task 1"
    })
    .get()
    .then(printDocs("Find all tasks which has text not equal Task 1"));

  await $todos
    .where({
      // find by id
      "@id": 1
    })
    .get()
    .then(printDocs("Find task by id"));

  await $todos
    .where({
      // multiple IN operators
      text: ["Task 1", "Task 2", "Task 3"],
      category: ["A", "B"]
    })
    .get()
    .then(printDocs("Find task with multiple IN operators"));

  await $todos
    .where({
      text: ["Task 1", "Task 2", "Task 3"],
      or: [{ category: "A" }, { category: "B" }]
    })
    .get()
    .then(printDocs("Find task with OR operator "));

  // await $todos
  //   .where({
  //     'category array_contains': 'A'
  //   })
  //   .get()
  //   .then(printDocs('Finding task using array_contains operator'));

  // get task names
  await $todos
    .select({ text: "name" })
    .get()
    .then(console.log);

  // join all items using pipe
  await $todos
    .select(true, "text")
    .pipe(String)
    .get()
    .then(console.log);

  // convert task names to uppercase
  await $todos
    .select(true, "text")
    .map("toUpperCase")
    .get()
    .then(console.log);

  await $todos
    .select(true, "text")
    .map(x => x.toUpperCase())
    .get()
    .then(console.log);

  await $todos
    .select(true, "text")
    .get()
    .then(console.log);

  await $todos
    .where({
      or: {
        category: "A",
        text: "Task 3"
      }
    })
    .get()
    .then(printDocs("Find task with OR operator "));

  await $todos
    .where({
      "categories has": "A"
    })
    .get()
    .then(printDocs("Find task using has operator"));

  await $todos
    .where({
      "categories array-contains": "A"
    })
    .get()
    .then(printDocs("Find task using array-contains operator"));

  // support pagination
  const pagination = $todos
    .limit(1)
    .orderBy({
      text: "asc"
    })
    .where({
      "text <>": "Task 1"
    });

  await pagination
    .get()
    .then(printDocs("Find all tasks which has text not equal Task 1. Page 1"));

  await pagination
    .next()
    .then(printDocs("Find all tasks which has text not equal Task 1. Page 2"));
  await pagination
    .next()
    .then(printDocs("Find all tasks which has text not equal Task 1. Page 3"));
};

test();

References:

linq2fire(db):LinqDb

Create a linq object to wrap db

LinqDb.from(collectionName): LinqCollection

Create a linq object to wrap collection

LinqDb.from(collection, callback): LinqDb

Create a linq object to wrap collection, then pass it to callback. This method is useful for chaining calls

linq2fire(collection): LinqCollection

Create a linq object to wrap collection

LinqCollection.select(fieldName1: String, fieldName2: String, ...): LinqCollection

Projects each item of a result set into a new object with specific fields.

LinqCollection.select(fieldMap: Object): LinqCollection

Projects each item of a result set into a new object with specific fields.

LinqCollection.select(valueOnly: Boolean, field: String): LinqCollection

Transform result set into a array of field value

LinqCollection.select(customSelector: Function(data: Object, doc: DocumentSnapshot)): LinqCollection

Projects each item of a result set by using custom selector.

LinqCollection.limit(count): LinqCollection

Limit result set

LinqCollection.where(conditions): LinqCollection

Filter result set by multiple conditions { text: 'abc', 'age >': 100, fieldValueMustBeIn: [1, 2, 3, 4, 5], 'field <>': 0 } Support operators: >, <, >=, <=, =, ==, ===, <>, !=, !==, has, array-contains. 'has' is shorthand of 'array-contains'

LinqCollection.orderBy(fields): LinqCollection

Sort result set by specified fields { field1: 'asc', field2: 'desc' }

LinqCollection.get(options): Promise

Get all documents which is satisfied query condition

LinqCollection.next(options): Promise

Get next result set which starts after last result set

LinqCollection.set(docsOrData, applyToResultSet): Promise

LinqCollection.update(docsOrData, applyToResultSet): Promise

LinqCollection.remove(): Promise

Remove all documents which is satisfied query condition