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

liquid-mongoose-data-loader

v1.1.1

Published

A Liquid extension for dynamically loading data from MongoDB models

Downloads

40

Readme

Liquid Mongoose Data Loader

A NPM package to dynamically load MongoDB data into Liquid templates.

Installation

npm install liquid-mongoose-data-loader

Usage

Here's an example of how to use this package:

const express = require('express');
const mongoose = require('mongoose');
const { Liquid } = require('liquidjs');
const dotenv = require('dotenv').config();
const path = require('path');

// Load liquid-mongo-data-loader
const dataLoader = require('liquid-mongoose-data-loader');

//Routes
const renderPage = require('./routes/renderPage');

// Initialize express
const app = express();

// Connect to MongoDB
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));

mongoose.connect(process.env.MONGO_URL, {
    useNewUrlParser: true,
    useUnifiedTopology: true
}).then(() => {
    console.log('Connected to MongoDB');
    console.log('http://localhost:5000');
}).catch((error) => {
    console.error('Error connecting to MongoDB:', error);
});

// Load Liquid engine
const engine = new Liquid({
    root: './views',
    extname: '.liquid',
    dynamicPartials: true,
});

// Load models and custom Liquid tags
DataLoader(engine, {
  modelsPath: './models'
});

app.engine('liquid', engine.express()); // register liquid engine
app.set('views', './views');    // specify the views directory
app.set('view engine', 'liquid');   // register the template engine

// Routes
app.use('/', renderPage);

API

liquidMongoDataLoader(engine, options)

Loads models and custom Liquid tags.

Parameters

  • engine : Instance of LiquidJS.
  • options : Object containing the path to the models.

Example

dataLoader(engine, {
  modelsPath: './models'
});

Example

Assuming you have a Book model defined in models/books.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const bookSchema = new Schema({
    title: String,
    author: String,
    year: Number
});

module.exports = mongoose.model('Book', bookSchema);

In your Liquid template, you would reference this model as book:

{% load model: 'books', key: 'all_books' %}

Tag Parameters

Examples :

{% load model: 'book', key: 'all_books' %}
{% load model: 'book', item: '1984', findOne: 'title', key: 'specific_book' %}
{% load model: 'book', item: 'George Orwell', findOne: 'author', key: 'author_books' %}

model

model parameter specifies the javascript file name without the extension.

item

Specifies the value to search for in the model. The module uses this value to search for a specific item in the model.

findOne

Specifies the field to search for in the model. The module uses this field to search for items by a specific field. (in this case, the title or author field)

key

Specifies the key to use when storing the results of the query. The key is used to reference the results in the template.

Template example

Rendering a template

Given a template index.liquid:


{% load model: 'book', key: 'all_books' %}
{% load model: 'book', item: '1984', findOne: 'title', key: 'specific_book' %}
{% load model: 'book', item: 'George Orwell', findOne: 'author', key: 'author_books' %}


<section>
        <h2>All Books</h2>
        <ul>
            {% for book in all_books %}
            <li>{{ book.title }} by {{ book.author }}: {{ book.year }}</li>
            {% endfor %}
        </ul>
    </section>
    <section>
        <h2>Specific Book</h2>
    <ul>
        {% for book in specific_book %}
            {% if book %}
                <li>{{ book.title }} by {{ book.author }}: {{ book.year }}</li>
            {% else %}
                <li>No book found.</li>
            {% endif %}
        {% endfor %}
    </ul>
    </section>
    <section>
        <h2>Books by George Orwell</h2>
        <ul>
            {% for book in author_books %}
                {% if book %}
                    <li>{{ book.title }} by {{ book.author }}: {{ book.year }}</li>
                    {% else %}
                    <li>No books by George Orwell found.</li>
                {% endif %}
            {% endfor %}
        </ul>
    </section>
    <section>
        <h2>Books from 1949</h2>
        <ul>
            {% if book_year and book_year.size > 0 %}
                {% for book in book_year %}
                    <li>{{ book.title }} by {{ book.author }}: {{ book.year }}</li>
                {% else %}
                    <li>No book from 1949 found.</li>
                {% endfor %}
            {% else %}
                <li>No book from 1949 found.</li>
            {% endif %}
         </ul>
    </section>