@rnaga/wp-node 1.1.1 โ 1.1.3
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 +240 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,240 @@
|
|
|
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
|
+
Visit http://localhost:8080 in your browser to complete the WordPress setup.
|
|
73
|
+
|
|
74
|
+
### Initialize WP-Node Project
|
|
75
|
+
|
|
76
|
+
WP-Node requires an initialized configuration before use. You can scaffold a new project using the CLI.
|
|
77
|
+
|
|
78
|
+
```sh
|
|
79
|
+
mkdir /tmp/test-wp-node
|
|
80
|
+
cd /tmp/test-wp-node
|
|
81
|
+
npx @rnaga/wp-node-cli -- init
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Then enter prompts as follows:
|
|
85
|
+
|
|
86
|
+
```sh
|
|
87
|
+
โ Enter your database hostname: ยท localhost
|
|
88
|
+
โ Enter your database port: ยท 33306
|
|
89
|
+
โ Enter your database username: ยท wp
|
|
90
|
+
โ Enter your database password: ยท **
|
|
91
|
+
โ Enter your database name: ยท wordpress
|
|
92
|
+
โ Is it a multi-site? ยท No
|
|
93
|
+
โ Enter your static assets path: ยท public
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
### Project Structure
|
|
97
|
+
|
|
98
|
+
After initialization, your project will look like this:
|
|
99
|
+
|
|
100
|
+
```
|
|
101
|
+
./
|
|
102
|
+
โโโ _wp
|
|
103
|
+
โย ย โโโ config
|
|
104
|
+
โย ย โย ย โโโ index.d.ts
|
|
105
|
+
โย ย โย ย โโโ wp.json
|
|
106
|
+
โย ย โโโ settings.ts
|
|
107
|
+
โโโ .env
|
|
108
|
+
โโโ index.ts
|
|
109
|
+
โโโ package-lock.json
|
|
110
|
+
โโโ package.json
|
|
111
|
+
โโโ tsconfig.json
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
**Key files**
|
|
115
|
+
|
|
116
|
+
- `_wp/config/wp.json`: Holds configuration for WP-Node such as public path and multisite info. This file is imported by settings.ts.
|
|
117
|
+
- `_wp/settings.ts`: Initializes the WP-Node Context, including config, database access and hooks.
|
|
118
|
+
- `index.ts`: The main entry point for your WP-Node app. A basic sample is provided.
|
|
119
|
+
- `.env`: Stores sensitive environment variables, including your database credentials and other configuration values required at runtime.
|
|
120
|
+
|
|
121
|
+
### Run the App
|
|
122
|
+
|
|
123
|
+
Once the config is initialized, run the app using:
|
|
124
|
+
|
|
125
|
+
```sh
|
|
126
|
+
mvn use 22
|
|
127
|
+
npx ts-node ./index.ts
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
If everything is working correctly, youโll see SQL output like:
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
select * from `wp_posts` as `posts_5` where `posts_5`.`ID` = 1
|
|
134
|
+
[
|
|
135
|
+
{
|
|
136
|
+
ID: 1,
|
|
137
|
+
post_author: 1,
|
|
138
|
+
post_title: 'Hello world!',
|
|
139
|
+
...
|
|
140
|
+
}
|
|
141
|
+
]
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
## CLI
|
|
145
|
+
|
|
146
|
+
WP-Node CLI provides a convenient way to interact with WordPress data without writing any code.
|
|
147
|
+
|
|
148
|
+
To query a post (e.g. ID = 1), run:
|
|
149
|
+
|
|
150
|
+
```sh
|
|
151
|
+
npx @rnaga/wp-node-cli -- post get 1 -Z table -F ID,post_title,post_type
|
|
152
|
+
|
|
153
|
+
โโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโ
|
|
154
|
+
โ (index) โ Values โ
|
|
155
|
+
โโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโค
|
|
156
|
+
โ ID โ 1 โ
|
|
157
|
+
โ post_title โ 'Hello world!' โ
|
|
158
|
+
โ post_type โ 'post' โ
|
|
159
|
+
โโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโ
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### Listing Available Commands
|
|
163
|
+
|
|
164
|
+
To view all available CLI commands, run:
|
|
165
|
+
|
|
166
|
+
```
|
|
167
|
+
npx @rnaga/wp-node-cli -- -h
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
output:
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
Usage: <command> <subcommand> [options]
|
|
174
|
+
|
|
175
|
+
Commands:
|
|
176
|
+
blog Blog commands
|
|
177
|
+
comment Comment commands
|
|
178
|
+
config Generate WP config files
|
|
179
|
+
init Initialize WP with Node. (Generate wp.json and install dependencies)
|
|
180
|
+
install Initialize a new blog and create a user
|
|
181
|
+
meta Meta commands (post, comment, blog, term, user, site)
|
|
182
|
+
option Options commands
|
|
183
|
+
post Post commands
|
|
184
|
+
repl Start a REPL
|
|
185
|
+
role Role commands
|
|
186
|
+
site Site commands
|
|
187
|
+
term Term commands
|
|
188
|
+
user User commands
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
### Develop your CLI using decorators:
|
|
192
|
+
|
|
193
|
+
**Example**:
|
|
194
|
+
|
|
195
|
+
```ts
|
|
196
|
+
@command("page", { description: "Page commands" })
|
|
197
|
+
export class PageCli extends Cli {
|
|
198
|
+
@subcommand("list", { description: "List pages" })
|
|
199
|
+
async list() {
|
|
200
|
+
const context = await Application.getContext();
|
|
201
|
+
const posts = await context.postUtil.findByType("page");
|
|
202
|
+
console.log(posts);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
## Dependency Injection
|
|
208
|
+
|
|
209
|
+
WP-Node uses a custom `@component()` decorator to support DI scopes:
|
|
210
|
+
|
|
211
|
+
- **Singleton**: One instance for entire app
|
|
212
|
+
- **Context**: One instance per context (e.g. HTTP request)
|
|
213
|
+
- **Transient**: New instance every time
|
|
214
|
+
|
|
215
|
+
```ts
|
|
216
|
+
@component({ scope: Scope.Transient })
|
|
217
|
+
export class Post {
|
|
218
|
+
constructor(
|
|
219
|
+
public meta: Meta,
|
|
220
|
+
private logger: Logger,
|
|
221
|
+
private queryUtil: QueryUtil,
|
|
222
|
+
...
|
|
223
|
+
) {}
|
|
224
|
+
}
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Hooks System
|
|
228
|
+
|
|
229
|
+
- **Filter**: Modify data in chainable handlers (similar to `apply_filters`)
|
|
230
|
+
- **Action**: Fire off side effects (`do_action` equivalent)
|
|
231
|
+
|
|
232
|
+
## Contributing
|
|
233
|
+
|
|
234
|
+
Feel free to fork, open issues, or suggest improvements. This project is in active development.
|
|
235
|
+
|
|
236
|
+
---
|
|
237
|
+
|
|
238
|
+
## License
|
|
239
|
+
|
|
240
|
+
MIT License.
|