Skip to content

Latest commit

 

History

History
198 lines (140 loc) · 6.43 KB

README.md

File metadata and controls

198 lines (140 loc) · 6.43 KB

Defer.run and RedwoodJS Example

An example of a RedwoodJS app using Defer to run background jobs.

Setup

  1. Clone this repo

  2. See directory structure

  3. Be sure to generate your Prisma client

  4. Sign up for Defer

  5. Create an app and link to your repo

  6. Set your DEFER_TOKEN if running locally and want to run hosted jobs

  7. In defer build settings, in Advanced options add the Pre-build command:

yarn rw prisma generate

Notes

  • Prisma client must be generated and
yarn rw prisma generate
  • If you want to use Redwood logger, be sure to set level to info since jobs run as production
export const logger = createLogger({ options: { level: 'info' } })

Directory Structure

- api
 +-- src
  +-- functions
      +- <>                     // Open api endpoint functions to invoke jobs
      +- characters
        +- characters.ts        // Invokes backgroundService job
      +- runHelloWorld
        +- runHelloWorld.ts     // Invokes helloWorld job
      +- runDelayedTime
        +- runDelayedTime.ts    // Invokes delayedTime job
      +- sendEmail
        +- sendEmail.ts         // Invokes sendMail job
  +-- mailer
    +-- templates               // Email templates
        +-- react               // React email templates
    +-- clients
      +- resend.ts              // Resend email client
    +- index
  +-- jobs                      // Jobs can be Defer (or others like Inngest)
    +-- clients
      +- defer.ts               // Defer client (exports handy methods)
    +- index
    +-- defer                   // Directory where Defer functions are stored
        +- backgroundService.ts // Example of of deferring a RedwoodJS api service
        +- delayedTime.ts       // Example Delayed function
        +- helloWorld.ts        // Example background function
        +- time.ts              // Example cron jon function
        +- emailMeEveryHour.ts  // Sends an email once an hour via Resend
        +- kesselRun.ts         // Fetch data via Prisma
        +- sendMail.ts          // Send and email
  +-- lib
      +- logger.ts              // RedwoodJS pino-based logger

README

Welcome to RedwoodJS!

Prerequisites

Start by installing dependencies:

yarn install

Then change into that directory and start the development server:

cd my-redwood-project
yarn redwood dev

Your browser should automatically open to http://localhost:8910 where you'll see the Welcome Page, which links to many great resources.

The Redwood CLI

Congratulations on running your first Redwood CLI command! From dev to deploy, the CLI is with you the whole way. And there's quite a few commands at your disposal:

yarn redwood --help

For all the details, see the CLI reference.

Prisma and the database

Redwood wouldn't be a full-stack framework without a database. It all starts with the schema. Open the schema.prisma file in api/db and replace the UserExample model with the following Post model:

model Post {
  id        Int      @id @default(autoincrement())
  title     String
  body      String
  createdAt DateTime @default(now())
}

Redwood uses Prisma, a next-gen Node.js and TypeScript ORM, to talk to the database. Prisma's schema offers a declarative way of defining your app's data models. And Prisma Migrate uses that schema to make database migrations hassle-free:

yarn rw prisma migrate dev

# ...

? Enter a name for the new migration: › create posts

rw is short for redwood

You'll be prompted for the name of your migration. create posts will do.

Now let's generate everything we need to perform all the CRUD (Create, Retrieve, Update, Delete) actions on our Post model:

yarn redwood g scaffold post

Navigate to http://localhost:8910/posts/new, fill in the title and body, and click "Save":

Did we just create a post in the database? Yup! With yarn rw g scaffold <model>, Redwood created all the pages, components, and services necessary to perform all CRUD actions on our posts table.

Frontend first with Storybook

Don't know what your data models look like? That's more than ok—Redwood integrates Storybook so that you can work on design without worrying about data. Mockup, build, and verify your React components, even in complete isolation from the backend:

yarn rw storybook

Before you start, see if the CLI's setup ui command has your favorite styling library:

yarn rw setup ui --help

Testing with Jest

It'd be hard to scale from side project to startup without a few tests. Redwood fully integrates Jest with the front and the backends and makes it easy to keep your whole app covered by generating test files with all your components and services:

yarn rw test

To make the integration even more seamless, Redwood augments Jest with database scenarios and GraphQL mocking.

Ship it

Redwood is designed for both serverless deploy targets like Netlify and Vercel and serverful deploy targets like Render and AWS:

yarn rw setup deploy --help

Don't go live without auth! Lock down your front and backends with Redwood's built-in, database-backed authentication system (dbAuth), or integrate with nearly a dozen third party auth providers:

yarn rw setup auth --help

Next Steps

The best way to learn Redwood is by going through the comprehensive tutorial and joining the community (via the Discourse forum or the Discord server).

Quick Links