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

@public-function/wp

v0.6.1

Published

Thin drizzle orm layer for wordpress.

Downloads

26

Readme

@public-function/wp

A thin drizzle based orm layer for the WordPress database.

Why?

Because sometimes performance matters!

Most of the time it is totally fine to use the wp-rest api. But when performance matters you do not want to spin up a WordPress stack just to query some data in the database. Especially if you have direct access to the database in your node setup.

0.x !

Things will eventually change on the way to 1.0.

Breaking Changes:

  • 0.4.0 single argument constructor

Getting started

Install packages.

npm i @public-function/wp drizzle-orm mysql2 

Initialize the database connection.

import mysql from 'mysql2/promise';
import connect from '@public-function/wp';

const pool = mysql.createPool({
    host: process.env.HOST,
    user: process.env.DB_USER,
    database: process.env.DB_NAME,
    password: process.env.DB_PASSWORD,
});

const wp = connect({ 
    db:{ 
        client: pool, 
        prefix: "wp_" 
    }
});

Get typesafe results via drizzle schema objects.

const siteUrl = (
    await db
        .select()
        .from(wp.options)
        .where(eq(wp.options.name, "siteurl"))
)[0];

or even simpler with the wrapper function.

import {getOption} from '@public-function/wp';
const siteUrl = await getOption(wp, "siteurl");

All default wordpress tables are defined:

  • wp.options
  • wp.posts
  • wp.postMeta
  • wp.comments
  • wp.commentMeta
  • wp.users
  • wp.userMeta
  • wp.terms
  • wp.termMeta
  • wp.termTaxonomy
  • wp.termRelationships

Hydration

Use hydration functions to easily collect meta or term data.

Posts

import {
    hydratePostsWithMeta,
    hydratePostWithTerms,
    hydratePosts,
} from "@pubic-function/wp";
const posts = await wp.db.select().from(wp.posts);
const postsWithTerms = await hydratePostWithTerms(wp, posts);
const postsWithMeta = await hydratePostsWithMeta(wp, posts);
const postsWithTermsAndMeta = await hydratePosts(wp, posts);

Comments

import {hydrateCommentsWithMeta} from "@pubic-function/wp";
const comments = await wp.db.select().from(wp.comments);
const commentsWithMeta = await hydrateCommentsWithMeta(wp, comments);

Users

import {hydrateUsersWithMeta} from "@pubic-function/wp";
const users = await wp.db.select().from(wp.users);
const usersWithMeta = await hydrateUsersWithMeta(wp, users);

Terms

import {hydrateTermsWithMeta} from "@pubic-function/wp";
const terms = await wp.db.select().from(wp.terms);
const termsWithMeta = await hydrateTermsWithMeta(wp, terms);

Wrapper functions

There are some query functions for typical use cases like loading posts in a WP_Query like manner. All results are automatically hydrated with meta and term data.

WP_Query

import {queryPosts} from "@pubic-function/wp";

const posts = await queryPosts(wp, {
    
});

WP_Comment_Query

import {queryComments} from "@pubic-function/wp";

const comments = await queryComments(wp, {
    
});

WP_User_Query

import {queryUsers} from "@pubic-function/wp";

const users = await queryUsers(wp, {
    
});

WP_Taxonomy_Query

import {queryTerms} from "@pubic-function/wp";

const terms = await queryTerms(wp, {
    
});

Menu

import {getMenu} from "@pubic-function/wp";

const menu = await getMenu(wp, "menu-1");

Option

import {getOption} from '@public-function/wp';

const siteUrl = await getOption(wp, "siteurl");

Tests

Start the WordPress docker container:

docker compose up -d

Run tests:

npm run test