You're Invited:Meet the Socket Team at BlackHat and DEF CON in Las Vegas, Aug 4-6.RSVP
Socket
Book a DemoInstallSign in
Socket

@public-function/wp

Package Overview
Dependencies
Maintainers
0
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@public-function/wp

Thin drizzle orm layer for wordpress.

0.6.3
latest
Source
npmnpm
Version published
Weekly downloads
19
90%
Maintainers
0
Weekly downloads
 
Created
Source

@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

Keywords

wordpress

FAQs

Package last updated on 05 Mar 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.