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

@verybigthings/semantic-layer

v3.0.1

Published

![NPM](https://img.shields.io/npm/l/@verybigthings/semantic-layer) ![NPM](https://img.shields.io/npm/v/@verybigthings/semantic-layer) ![GitHub Workflow Status](https://github.com/verybigthings/semantic-layer/actions/workflows/semantic-layer.yml/badge.svg?

Downloads

81

Readme

@verybigthings/semantic-layer

NPM NPM GitHub Workflow Status

Quick Start Guide

Welcome to the semantic layer library! Let's dive in and create a simple data model in just a few steps.

Installation

First, let's get the library installed (use npm or a package manager of your choice):

npm install @verybigthings/semantic-layer

Building Your First Semantic Layer

Imagine you're running a music store. You have customers, and they make purchases. Let's model this!

Step 1: Create Your Models

We'll create two models: customers and invoices.

import * as semanticLayer from "@verybigthings/semantic-layer";

// Our Customers model
const customersModel = semanticLayer
  .model()
  .withName("customers")
  .fromTable("Customer")
  .withDimension("customer_id", {
    type: "number",
    primaryKey: true,
    sql: ({ model, sql }) => sql`${model.column("CustomerId")}`,
  })
  .withDimension("first_name", {
    type: "string",
    sql: ({ model }) => model.column("FirstName"),
  })
  .withDimension("last_name", {
    type: "string",
    sql: ({ model }) => model.column("LastName"),
  });

// Our Invoices model
const invoicesModel = semanticLayer
  .model()
  .withName("invoices")
  .fromTable("Invoice")
  .withDimension("invoice_id", {
    type: "number",
    primaryKey: true,
    sql: ({ model }) => model.column("InvoiceId"),
  })
  .withDimension("customer_id", {
    type: "number",
    sql: ({ model }) => model.column("CustomerId"),
  })
  .withMetric("total", {
    type: "number",
    description: "Invoice total.",
    sql: ({ model, sql }) => sql`SUM(COALESCE(${model.column("Total")}, 0))`,
  });

Step 2: Create a Repository

Now, let's put these models together in a repository:

const repository = semanticLayer
  .repository()
  .withModel(customersModel)
  .withModel(invoicesModel)
  .joinOneToMany(
    "customers",
    "invoices",
    ({ sql, models }) =>
      sql`${models.customers.dimension(
        "customer_id"
      )} = ${models.invoices.dimension("customer_id")}`
  );

Step 3: Build a Query

With our repository set up, we can now build queries:

const queryBuilder = repository.build("postgresql");

const query = queryBuilder.buildQuery({
  members: [
    "customers.customer_id",
    "customers.first_name",
    "customers.last_name",
    "invoices.total",
  ],
  order: { "customers.customer_id": "asc" },
  limit: 10,
});

Step 4: Execute the Query

The query object contains the SQL string and bindings. You can use these with your preferred database client:

const result = await someSqlClient.query(query.sql, query.bindings);

For example, with the pg package for PostgreSQL:

const result = await pg.query(query.sql, query.bindings);

And there you have it! You've just set up a semantic layer for your music store data. This layer will make it easy to analyze customer purchases without writing complex SQL queries each time.

Read the documentation for more information.

Acknowledgments

@verybigthings/semantic-layer draws inspiration from several BI libraries, particularly Cube.dev. While our API was initially inspired by Cube.dev, it has since diverged based on our own needs and preferences.