@beeanco/wp-typeorm
TypeScript icon, indicating that this package has built-in type declarations

0.1.2 • Public • Published

@beeanco/wp-typeorm

TypeORM definitions for WordPress

Installation

With node.js installed, use npm to install this package and its peer dependencies:

npm install @beeanco/wp-typeorm typeorm

You also need a MySQL package: You can use both mysql and mysql2, we recommend (and test with) the latter:

npm install mysql2 # or mysql

Usage

Setting up a connection

import { createConnection } from 'typeorm';
import { entities } from '@beeanco/wp-typeorm';

export function connect() {
  return createConnection({
    type: 'mysql',
    database: 'wordpress',
    username: 'myusername',
    password: 'mypassword',
    entities: [
      ...entities, // Add WordPress entities
      // Your own entities come here...
    ],
    synchronize: false,
    entityPrefix: 'wp_',
  });
}

// Now, call connect() somewhere to create database connection, e.g. in your server file

Getting posts

import { getRepository } from 'typeorm';
import { Post } from '@beeanco/wp-typeorm';

export async function runSample() {
  // Get the post repository
  const postRepository = await getRepository(Post);

  // Get a single post by id
  const byId = await postRepository.findOne(123);

  // Get the same post, but include metadata and terms this time
  const withMeta = await postRepository.findOne({
    where: { id: 123 },
    relations: ['parent', 'author', 'meta', 'terms', 'terms.taxonomy', 'terms.taxonomy.term'],
  });

  // Get all posts of type 'product' (e.g. WooCommerce)
  const allProducts = await postRepository.find({ where: { type: 'product' } });
}

// This sample could be called like this:
runSample().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

For all fields and relations, check the Post entity's source code.

Package Sidebar

Install

npm i @beeanco/wp-typeorm

Weekly Downloads

5

Version

0.1.2

License

MIT

Unpacked Size

49.2 kB

Total Files

34

Last publish

Collaborators

  • lhechenberger