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

@enio.ai/path-schema

v0.2.0

Published

path-schema is set of utils to define your firebase database schema

Downloads

33

Readme

News

Key Features

  1. Model your no-sql database schema with ease using a simple, human-readable syntax. This will help you move away from maintaining cluttered code with constant variables and enums to manage your database paths.
  2. Built-in syntax validation ensures your database schema is always in check.
  3. Get autogenerated TypeScript interfaces for your database references to enjoy intellisense.
  4. Read paths and hydrate dynamic values with .pathWithKeys().

This library was designed with Firebase database use cases in mind.

We've got even more in store for you - support for database schemas with circular references and a CLI version of getDefinitions() and config file are in the works.

Status: Beta :alembic:

This project is in "beta" release, which means; it's all new and needs feedback from the community to improve it further. It's all new and mostly solves our use case to map firebase database paths neatly.

If you find this project useful and want to see more projects like this, consider signing up for support. You can find details are at the very end. :point_down: :seedling:

Installation

Using npm:

$ npm i -g npm
$ npm i --save @enio.ai/path-schema

How to Use

You can import utils from path-schema just as you would with other npm packages.

import { getPaths } from '@enio.ai/path-schema'

Overview

API

Database Schema

To get started, you need to write your database schema as a string. Each line in the string represents a node in your schema, and the relationships between nodes are defined using the » symbol. For example:

@root » #opensource #enterprise
#opensource » #projects
#enterprise » #projects
#projects » #badges
#badges » #coverageBadge #buildBadge
#coverageBadge
#buildBadge

In this example, the @root node is the root of the database, and it has two children, #opensource and #enterprise. The #projects node is a child of both the #opensource and #enterprise nodes, and the #badges node is a child of the #projects node. The #coverageBadge and #buildBadge nodes are children of the #badges node.

Database Reference

The database reference is an object created at runtime that contains the path details of every node of your database represented by the schema, which can be accessed through dot notation.

To generate a database reference, use the getPaths() function and pass in your schema string as an argument.

import { getPaths } from '@enio.ai/path-schema'

const dbRefs = getPaths(yourSchemaString)

Using the previous example, you can simply pass a template string directly.

import { getPaths } from '@enio.ai/path-schema'

const dbRefs = getPaths(`
  @root » #opensource #enterprise
  #opensource » #projects
  #enterprise » #projects
  #projects » #badges
  #badges » #coverageBadge #buildBadge
  #coverageBadge
  #buildBadge
`)

TypeScript Interface

Because the database reference is instanced at runtime, to get the benefit of IntelliSense to access properties in dot notation when you are using TypeScript, the getPaths() accepts an interface or type.

Here is an example;

import type { Reference } from '@enio.ai/path-schema'
import { getPaths } from '@enio.ai/path-schema'

type RootPaths = 'opensource' | 'enterprise'
type ProjectCategoryPaths = 'projects'
type ProjectPaths = 'badges'
type BadgePaths = 'coverageBadge' | 'buildBadge'

type Badges = Reference & {
  [key in BadgePaths]: Reference
}

type Project = Reference & {
  [key in ProjectPaths]: Badges
}

type ProjectCategories = Reference & {
  [key in ProjectCategoryPaths]: Project
}

type DB = Reference & {
  [key in RootPaths]: ProjectCategories
}

const dbRefs = getPaths<DB>(`
  @root » #opensource #enterprise
  #opensource » #projects
  #enterprise » #projects
  #projects » #badges
  #badges » #coverageBadge #buildBadge
  #coverageBadge
  #buildBadge
`)

Help with Advanced Database Schemas

Database schemas vary in complexity, and creating an interface could become less straightforward. You can use getDefinition().

getDefinition() takes in your schema string as an argument. The function will return a string that represents the TypeScript definitions for your database schema.

import { getDefinition } from '@enio.ai/path-schema'

console.log(
  getDefinition(`
  @root » #opensource #enterprise
  #opensource » #projects
  #enterprise » #projects
  #projects » #badges
  #badges » #coverageBadge #buildBadge
  #coverageBadge
  #buildBadge
`)
)

And that's it! You should now have a good understanding of how to model your own database schema using the getDefinition and getPaths functions. If you have any questions or need further clarification, feel free to ask.

Contributors :link:

Sponsors

(This list will be automatically generated in the future.)