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

@keystonejs-contrib/view

v2.0.0

Published

Provides functionality similar to Keystone 4 views.

Downloads

6

Readme


section: packages title: View

View

This package contains functions to assist with view management in your Keystone system. This feature is ported from Keystone v4

Initialize

Initialize View support by setting ExpressJs view engine options, you have to add respective view engine package in packages.json. You must use Custom Server config.

//... keystone.prepare(....).then( ({middlewares}) => {
  const app = express();
  app.use(middlewares);
  app.use(bodyParser.urlencoded({ extended: true }));
  app.set('views', './templates');
  app.set('view engine', 'pug');
//})

you need to create templates folder in root of the project where you keep all the view templates

Query

Create Routes to use View, View constructor needs instance of Keystone and express app. For details check out the demo-projects/blog-view project.

module.exports = (keystone, app) => {
  app.use('/', async (req, res) => {
    const locals = res.locals || {};
    const view = new View(keystone, req, res);
    view.query(
      'posts',
      `{ 
          allPosts {
            title
            id
            body
            posted
            image {
              publicUrl
              filename
            }
            author {
              name
            }
          }
      }`
    );

    view.render('views/home', locals);
  });
};

Initialize this route after keystone.prepare

const homeRoute = require('../routes/view/home');
homeRoute(keystone, app);

if there are multiple objects returned in GraphQl queries then both of them are attached to the path parameter in view.query

Mutation

you can run mutation queries with view.query

HTTP methods support in View

Similar to v4, View support view.on conditionals based on HTTP methods, for example you can process for post when specific field is having specific value, usually the case when you have multiple forms on page and yo want to handle them selectively.

Improved in v5, any query you add inside view.on will get executed before any other queries, giving you single page refresh to accomplish result refresh after form post.

view.on('post', { action: 'new-post' }, async () => {
  // in the form you have to have a hidden field like this :
  // input(type='hidden', name='action', value='new-post')
  const { title, body, admin } = req.body;
  const input = { title, body, author: { connect: { id: admin } } };
  view
    .query(
      'created',
      `mutation createPost($input: PostCreateInput){
        createPost(data: $input) {id}
      }`,
      { variables: { input } }
    )
    .err(err => {
      console.log(err);
      locals.err = err;
    });
});