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

sails-nested-blueprint

v1.7.0

Published

Blueprints for nested waterline models for use in sails 1.0

Downloads

19

Readme

CircleCI

sails-nested-blueprint

As of sails 1.0, blueprints no longer support nested creates (passing along associated models during creates). This library brings it back in a non-obstrusive way.

Usage

Blueprint (Easiest)

The blueprint will automatically figure out which model to use just like sails blueprints. blueprint exposes a create, a destroy, an update and a count, so just do this in a controller method:

let {blueprint} = require('sails-nested-blueprint')
module.exports = blueprint

### Blueprint with options (Soft Delete)
Works just like the regular blueprint example, howerver allows passing an object with options for each of the blueprint methods.
Currently only `delete` supports options such as `cascade` (causes any associated record passed in with the main record to be deleted, same as `serviceFromReq(req).destroyNested`)
and `soft` (keeps the record but sets a flag `IsDeleted` to true on the affected records).

### "Soft Delete" Setup
In order for soft delete to work you have to add a boolean attribute to the model definition or in /config/models.js (to enable all your models to support soft deletes)

```js
let {blueprint} = require('sails-nested-blueprint')
// model using these blueprints will have its `isDeleted` attribute set to true instead of being deleted from the data store.
// If associated models are passed with the main record, they will also be soft-deleted since cascade is set to true
module.exports = blueprintOptions({destroy: {soft: true, cascade: true}})

The count endpoint allows you to reach to /[model]/count with a query to retrieve the number of found elements, instead of the full JSON object.

Cached Methods

We currently provide a couple of methods that make use of a simple but extensible caching mechanism. The methods are find and update, and to be able to use them, you'll need to provide a cache object in the configuration options passed to blueprintOptions. The cache configuration object will have: a provider object containing a get, a set, a del and a keys functions, and a prefix alongsides the provider. See the following code as an example of how to use these methods on a Sails controller:

let sails = require('sails')
let redis = require('redis')
let Promise = require('bluebird')

Promise.promisifyAll(redis.RedisClient.prototype)
Promise.promisifyAll(redis.Multi.prototype)

let client = redis.createClient(sails.config.redis)
let expiration = 60 // a minute
let keysExpiration = 60 * 30 // 30 minutes
let prefix = 'my-cache'

let blueprint = require('sails-nested-blueprint').blueprintOptions({
  cache: {
    prefix,
    provider: {
      get: async key => JSON.parse(await client.getAsync(key)),
      async set(key, val) {
        let exp = (key === `${prefix}-keys`) ? keysExpiration : expiration
        try {
          await client.setexAsync(
            key,
            exp,
            JSON.stringify(val)
          )
        } catch (e) {
          console.error('Failed to setexAsync', {
            key,
            val,
          })
        }
      },
      async del(keys) {
        await client.delAsync(keys)
      },
      async keys(query) {
        await client.keys(query)
      }
    },
  },
})

module.exports.find = blueprint.find
module.exports.update = blueprint.update

NOTE caching can be bypassed by returning false on an optional a keygen property passed to the options of blueprintOptions. This function receives the request object, so it can be personalized for anything request-related.

Service

You can also use the service directly if you need to perform a nested create (or destroy) as part of some other operation. On the service, these functions are called createNested and destroyNested. To perform a soft delete with a service use destroy({soft: true}, record) or destroySoft(record) or combine both nested and soft deletion destroyNestedSoft(record)

let {service} = require('sails-nested-blueprint')
let sails = require('sails')
let SomeOperation = async (...args) => {
  let payload = getPayload(args)
  await service(sails.models, 'SomeModel').createNested(payload) // Returns 201 if successful
  doMoreStuff()
}

You can also use the helper utilities directly in a controller method to recreate what blueprint does:

let {serviceFromReq, service} = require('sails-nested-blueprint')
module.exports = {
  create: async (req, res) => res
    .status(await serviceFromReq(req).createNested(req.allParams()))
    .send()
}

It is much nicer if you use sails-async - see the index.js source for the implementation