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 🙏

© 2025 – Pkg Stats / Ryan Hefner

ember-scopes

v0.0.3

Published

Scopes for ember

Readme

ember-scopes

This is a simple addon to use Rails style model scopes for classes.

Concepts:

  • Owner: this is a registry where you can lookup global objects, its just passed to Scopes, you can ignore it if you don't need global lookups.
  • ClientWrapper: A Class that implements all the finishers, it serves as a middleware for the actual client like the ember data store or any other client you might be using.
  • Scope: A function that returns a function or a Pojo with local and remote functions
  • Query: A proxy that let us build the query step by step to finally execute any of the ClientWrapper finishers
//Lets say this is our ember data store, or any class with final finishers
const StoreService = {
  peekAll(modelName) {
    return [
      { name: 'Alberto', age: 15 },
      { name: 'Daniel', age: 30 },
      { name: 'Humberto', age: 50 },
    ];
  },
};

//First lets define a custom ClientWrapper that will act as middleware to the provided client
class StoreWrapper extends ClientWrapper {
  peekAll(queries, klass, opts) {
    //[{ name: 'Alberto', age: 15 }, { name: 'Daniel', age: 30 }, { name: 'Humberto', age: 50 }]
    const collection = this.client.peekAll();

    //Local filtering is a helper function which executes all of the callbacks
    //But you can basically do whatever you need here
    return this.localFiltering(collection, queries);
  }
}

//Create an instance
const Wrapper = new StoreWrapper(StoreService);

//Maybe you want to pass getOwner(this), or use it as context
const owner = {};

//Finally you can create an Object or a Class where your scopes live.
class ClassWithScopes {
  @scope
  lessThan(owner, prop, n) {
    return {
      local: (record) => record[prop] < n,
      remote: () => {
        return {
          //or whatever your server receives
          [`${prop}`]: { lt: n },
        };
      },
    };
  }

  @scope
  biggerThan(owner, prop, n) {
    return {
      local: (record) => record[prop] > n,
      remote: () => {
        return {
          //or whatever your server receives
          [`${prop}`]: {
            gt: n,
          },
        };
      },
    };
  }
}

//Finally we create the query
let q = Query(owner, Wrapper, ClassWithScopes);

//And use it.
q.lessThan('age', 10).biggerThan(20).peekAll(); // []

let q = Query(owner, Wrapper, ClassWithScopes);
q.lessThan('age', 50).biggerThan(20).peekAll(); // [{ name: 'Daniel', age: 30 }]

It's recommended for ergonomics that you hide the complexity of creating queries behind a service or util, for instance the ember data store. This addon comes with a built in EmberDataStoreClientWrapper for convenience

import { EmberDataStoreClientWrapper, Query } from 'ember-scopes';

export default class Store extends EDStore {
  for(modelName) {
    let ModelKlass = this.modelFor(modelName);
    return Query(getOwner(this), EmberDataStoreClientWrapper(this), ModelKlass);
  }
}

//Now you can just do
class ComponentA extends GlimmerComponent {
	@service store;

	constructor() {
		super(...arguments);
		//We suppose Model User Class have a scope defined.
		this.store.for('user').biggerThan('age', 10).peekAll();
	}
}

Compatibility

  • Ember.js v3.20 or above
  • Ember CLI v3.20 or above
  • Node.js v12 or above

Installation

ember install ember-scopes

Usage

[Longer description of how to use the addon in apps.]

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.