@rnaga/wp-node 1.1.1 → 1.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +155 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
# WP-Node
|
|
2
|
+
|
|
3
|
+
## 📘 Documentation
|
|
4
|
+
|
|
5
|
+
👉 **[View Full Documentation at rnaga.github.io/wp-node →](https://rnaga.github.io/wp-node/)**
|
|
6
|
+
|
|
7
|
+
## What is WP-Node?
|
|
8
|
+
|
|
9
|
+
WP-Node is a Node.js project written in TypeScript that mirrors the WordPress database schema and functionality. It enables developers to build scalable, modern backends on top of existing WordPress data.
|
|
10
|
+
|
|
11
|
+
Key benefits include:
|
|
12
|
+
|
|
13
|
+
- No need to run WordPress PHP
|
|
14
|
+
- Type-safe interaction with WordPress tables (`posts`, `users`, `terms`, etc.)
|
|
15
|
+
- Utility classes for querying posts, terms, users, comments, and metadata
|
|
16
|
+
- Supports both Single Site and Multi Site WordPress setups
|
|
17
|
+
- CLI tools to seed databases and run custom commands
|
|
18
|
+
- Clean architecture with Dependency Injection and decorators
|
|
19
|
+
|
|
20
|
+
## Features
|
|
21
|
+
|
|
22
|
+
- **TypeScript-first**: Fully typed interfaces and schema validation using [Zod](https://zod.dev/)
|
|
23
|
+
- **[Knex.js](https://knexjs.org/) Integration**: Query builder with SQL injection prevention and fluent chaining
|
|
24
|
+
- **Dependency Injection**: Built-in decorator-based system for injecting services like `PostUtil`, `TermUtil`, and `MetaUtil`, enabling clean separation of concerns and easier testing
|
|
25
|
+
- **Hooks API**: Inspired by WordPress hooks (`do_action`, `apply_filters`), and supports implementation as async-compatible functions
|
|
26
|
+
|
|
27
|
+
## Use Cases
|
|
28
|
+
|
|
29
|
+
WP-Node is ideal for scenarios where you need direct access to WordPress database without relying on the full WordPress stack. Example use cases include:
|
|
30
|
+
|
|
31
|
+
- **Running background jobs or cron tasks** that update WordPress records — without needing a full WordPress installation
|
|
32
|
+
- **Building a lightweight REST API** using Node.js and TypeScript that interacts with WordPress data
|
|
33
|
+
- **Debugging or inspecting database records** from a modern TypeScript environment
|
|
34
|
+
- **Creating a web app** (e.g., using Next.js) that needs to pull or push data from a WordPress database, without relying on PHP codebase
|
|
35
|
+
|
|
36
|
+
## Limitations
|
|
37
|
+
|
|
38
|
+
**WP-Node Core** is designed specifically to interact with the WordPress database. It does not support traditional WordPress features such as:
|
|
39
|
+
|
|
40
|
+
- Themes and appearance settings, including updating styling
|
|
41
|
+
- WordPress Template rendering or theming APIs
|
|
42
|
+
- WordPress plugins
|
|
43
|
+
|
|
44
|
+
Its scope is intentionally limited to providing a type-safe, programmatic interface to WordPress data — not replicating the full behavior of the WordPress runtime.
|
|
45
|
+
|
|
46
|
+
## Requirements
|
|
47
|
+
|
|
48
|
+
- **Node.js** `>=22.0.0`
|
|
49
|
+
- **MySQL** or **MariaDB**
|
|
50
|
+
- Optional: Docker for local WordPress database setup
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
To spin up a sample environment with WordPress and database in Docker:
|
|
55
|
+
|
|
56
|
+
```sh
|
|
57
|
+
docker network inspect wpnet >/dev/null 2>&1 || docker network create wpnet && \
|
|
58
|
+
docker run -d --name wpdb --network wpnet -p 33306:3306 \
|
|
59
|
+
-e MYSQL_ROOT_PASSWORD=example \
|
|
60
|
+
-e MYSQL_DATABASE=wordpress \
|
|
61
|
+
-e MYSQL_USER=wp \
|
|
62
|
+
-e MYSQL_PASSWORD=wp \
|
|
63
|
+
mariadb && \
|
|
64
|
+
docker run -d --name wp --network wpnet -p 8080:80 \
|
|
65
|
+
-e WORDPRESS_DB_HOST=wpdb:3306 \
|
|
66
|
+
-e WORDPRESS_DB_USER=wp \
|
|
67
|
+
-e WORDPRESS_DB_PASSWORD=wp \
|
|
68
|
+
-e WORDPRESS_DB_NAME=wordpress \
|
|
69
|
+
wordpress
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Configuration
|
|
73
|
+
|
|
74
|
+
Create your config:
|
|
75
|
+
|
|
76
|
+
```sh
|
|
77
|
+
npx @rnaga/wp-node-cli -- config
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
It will prompt for database settings and generate:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
_wp/
|
|
84
|
+
└── config/wp.json
|
|
85
|
+
.env
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
You can configure additional settings such as:
|
|
89
|
+
|
|
90
|
+
- `staticAssetsPath`: Path for media references
|
|
91
|
+
- `multisite.enabled`: Enable or disable multisite support
|
|
92
|
+
- Custom post types, taxonomies, statuses
|
|
93
|
+
|
|
94
|
+
## CLI Usage
|
|
95
|
+
|
|
96
|
+
Install CLI tools:
|
|
97
|
+
|
|
98
|
+
```sh
|
|
99
|
+
npm i -S @rnaga/wp-node-cli -- -h
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Basic command to list posts:
|
|
103
|
+
|
|
104
|
+
```sh
|
|
105
|
+
npx @rnaga/wp-node-cli -- post list
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
Develop your own commands using decorators:
|
|
109
|
+
|
|
110
|
+
```ts
|
|
111
|
+
@command("page", { description: "Page commands" })
|
|
112
|
+
export class PageCli extends Cli {
|
|
113
|
+
@subcommand("list", { description: "List pages" })
|
|
114
|
+
async list() {
|
|
115
|
+
const context = await Application.getContext();
|
|
116
|
+
const posts = await context.postUtil.findByType("page");
|
|
117
|
+
console.log(posts);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Dependency Injection
|
|
123
|
+
|
|
124
|
+
WP-Node uses a custom `@component()` decorator to support DI scopes:
|
|
125
|
+
|
|
126
|
+
- **Singleton**: One instance for entire app
|
|
127
|
+
- **Context**: One instance per context (e.g. HTTP request)
|
|
128
|
+
- **Transient**: New instance every time
|
|
129
|
+
|
|
130
|
+
```ts
|
|
131
|
+
@component({ scope: Scope.Transient })
|
|
132
|
+
export class Post {
|
|
133
|
+
constructor(
|
|
134
|
+
public meta: Meta,
|
|
135
|
+
private logger: Logger,
|
|
136
|
+
private queryUtil: QueryUtil,
|
|
137
|
+
...
|
|
138
|
+
) {}
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
## Hooks System
|
|
143
|
+
|
|
144
|
+
- **Filter**: Modify data in chainable handlers (similar to `apply_filters`)
|
|
145
|
+
- **Action**: Fire off side effects (`do_action` equivalent)
|
|
146
|
+
|
|
147
|
+
## Contributing
|
|
148
|
+
|
|
149
|
+
Feel free to fork, open issues, or suggest improvements. This project is in active development.
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## License
|
|
154
|
+
|
|
155
|
+
MIT License.
|