Skip to content

Commit

Permalink
docs: update readme with basic examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davidglivar committed May 4, 2024
1 parent b678a8a commit 1b4b905
Showing 1 changed file with 134 additions and 4 deletions.
138 changes: 134 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
Supaworker is a job queue for Supabase projects.

> **Note:** This is a work in progress and is not yet ready for production use.
### License

[MIT](./LICENSE)
Expand All @@ -19,6 +17,8 @@ A worker is a Supaworker client does the following:

Timeouts, delayed retries, and scale are left to the developer.

Supaworker is _**not**_ designed to be used with Edge Functions, but instead with dedicated worker processes -- usually docker containers. This allows the developer to control the environment, runtime, dependencies, and scaling.

### Setup

Create a new migration
Expand All @@ -35,10 +35,140 @@ Then run the migration:
supabase migration up --local
```

### Installation

Add Supaworker to your project:

```bash
npm install --save supaworker-js
```

### Examples

#### Node.js

Create a new project

```bash
mkdir my-worker && cd my-worker
npm init -y
npm install --save supaworker-js
```

Edit package.json to use ESM modules:

```json
{
"type": "module"
}
```

Basic javascript example:

```js
import { createSupaworker } from "supaworker-js";

const clientOptions = {
supabase_url: process.env.SUPABASE_URL ?? "",
supabase_service_role_key: process.env.SUPABASE_SERVICE_ROLE_KEY ?? "",
};

const workerOptions = {
queue: "example",
debug: true,
};

const { enqueue, worker } = createSupaworker(
clientOptions,
workerOptions,
async (job) => {
console.log(job.payload.message);
}
);

await enqueue([
{
queue: "example",
payload: {
message: "Hello, World!",
},
},
]);

process.on("SIGINT", async () => {
await worker.stop();
process.exit();
});

await worker.start();
await worker.stop();
```
Run the worker:
```sh
SUPABASE_URL="" \
SUPABASE_SERVICE_ROLE_KEY="" \
node index.js
```
#### Bun
Create a new project
```bash
mkdir my-worker && cd my-worker
bun init -y
bun add supaworker-js
```
Basic typescript example:
```ts
import {
createSupaworker,
type SupaworkerClientOptions,
type SupaworkerOptions,
} from "supaworker-js";

const clientOptions: SupaworkerClientOptions = {
supabase_url: process.env.SUPABASE_URL ?? "",
supabase_service_role_key: process.env.SUPABASE_SERVICE_ROLE_KEY ?? "",
};

const workerOptions: SupaworkerOptions = {
queue: "example",
debug: true,
};

const { enqueue, worker } = createSupaworker<{ message: string }>(
clientOptions,
workerOptions,
async (job) => {
console.log(job.payload!.message);
}
);

await enqueue([
{
queue: "example",
payload: {
message: "Hello, World!",
},
},
]);

process.on("SIGINT", async () => {
await worker.stop();
process.exit();
});

await worker.start();
await worker.stop();
```
Run the worker:
```sh
SUPABASE_URL="" \
SUPABASE_SERVICE_ROLE_KEY="" \
bun run index.ts
```

0 comments on commit 1b4b905

Please sign in to comment.