Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,40 @@ jobs:

- name: Typecheck
run: pnpm typecheck

test:
runs-on: ubuntu-latest

services:
mysql:
image: mysql:8.0
env:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: queue
ports:
- 3306:3306
options: >-
--health-cmd="mysqladmin ping"
--health-interval=10s
--health-timeout=5s
--health-retries=3

steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v2
- uses: actions/setup-node@v4
with:
node-version: 22
cache: "pnpm"
registry-url: https://registry.npmjs.org

- shell: bash
run: corepack enable

- shell: bash
run: pnpm install

- name: Run tests
run: pnpm test
env:
DATABASE_URL: mysql://root:root@localhost:3306/queue
5 changes: 5 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
name: Publish Package to npm
on:
workflow_dispatch:
push:
# branches: [main]
tags:
- "*"
release:
types: [published]
jobs:
Expand Down
1 change: 1 addition & 0 deletions LICENCE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
Copyright 2024 lilac
Copyright 2024 Mohamed Bassem

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Liteque
# MySQL Lite Queue

![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/hoarder-app/liteque/ci.yml) ![NPM Version](https://img.shields.io/npm/v/liteque)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/rabrain/mysql-queue/ci.yml) ![NPM Version](https://img.shields.io/npm/v/mysql-queue)


A simple typesafe mysql-based job queue for Node.js.
Expand All @@ -14,18 +14,18 @@ $ npm install mysql-queue
## Usage

```ts
import { buildDBClient, Runner, SqliteQueue } from "liteque";
import { connect, Runner, LiteQueue } from "mysql-queue";
import { z } from "zod";

const db = buildDBClient(":memory:", true);
const db = connect("mysql://root:root@localhost:3306/queue");

const requestSchema = z.object({
message: z.string(),
});
const ZRequest = z.infer<typeof requestSchema>;

// Init the queue
const queue = new SqliteQueue<ZRequest>("requests", db, {
const queue = new LiteQueue<ZRequest>("requests", db, {
defaultJobArgs: {
numRetries: 2,
},
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "mysql-queue",
"description": "A lite job queue for Node.js",
"author": "lilac <hi@rabain.com>",
"version": "0.1.0",
"version": "0.1.1",
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -15,21 +15,21 @@
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/hoarder-app/liteque.git"
"url": "git+https://github.com/rabrain/mysql-queue.git"
},
"keywords": [
"queue"
],
"dependencies": {
"async-mutex": "^0.4.1",
"drizzle-orm": "^0.33.0",
"drizzle-orm": "^0.38.2",
"eslint": "^9.17.0",
"mysql2": "^3.11.5",
"zod": "^3.22.4"
},
"devDependencies": {
"@tsconfig/node21": "^21.0.3",
"drizzle-kit": "^0.24.02",
"drizzle-kit": "^0.30.1",
"typescript": "^5.6.3",
"vitest": "^1.3.1"
},
Expand Down
33 changes: 18 additions & 15 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import mysql from "mysql2/promise";
import path from "node:path";
import * as schema from "./schema";

export type Database = MySql2Database<typeof schema>;

export const affectedRows = (rawResult: MySqlRawQueryResult) => {
return rawResult[0].affectedRows
};

const defaultURL = 'mysql://root:root@localhost:3306/queue'

export function buildDBClient(url?: string, runMigrations = false) {
const connection = mysql.createPool(url ?? defaultURL);
const db = drizzle(connection, { schema, mode: 'planetscale' });

if (runMigrations) {
migrateDB(db);
}
export async function connect(url: string) {
const connection = await mysql.createConnection(url);
const db = drizzle(connection, { schema, mode: 'default' });
return db;
}

export function migrateDB(db: MySql2Database<any>) {
migrate(db, {
return migrate(db, {
migrationsFolder: path.join(__dirname, '../drizzle')
});
}
10 changes: 6 additions & 4 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
timestamp,
mysqlTable,
unique,
varchar,
} from "drizzle-orm/mysql-core";

export const createTable = mysqlTable;
Expand All @@ -19,19 +20,20 @@ export const tasksTable = createTable(
"tasks",
{
id: integer("id").notNull().primaryKey().autoincrement(),
queue: text("queue").notNull(),
queue: varchar("queue", { length: 255 }).notNull(),
payload: text("payload").notNull(),
createdAt: createdAtField(),
status: text("status", {
status: varchar("status", {
length: 50,
enum: ["pending", "running", "pending_retry", "failed"],
})
.notNull()
.default("pending"),
expireAt: timestamp("expireAt", { mode: "date" }),
allocationId: text("allocationId").notNull(),
allocationId: varchar("allocationId", { length: 50 }).notNull(),
numRunsLeft: integer("numRunsLeft").notNull(),
maxNumRuns: integer("maxNumRuns").notNull(),
idempotencyKey: text("idempotencyKey"),
idempotencyKey: varchar("idempotencyKey", { length: 255 }),
},
(tasks) => ({
queueIdx: index("tasks_queue_idx").on(tasks.queue),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
CREATE TABLE `tasks` (
`id` int AUTO_INCREMENT NOT NULL,
`queue` text NOT NULL,
`queue` varchar(255) NOT NULL,
`payload` text NOT NULL,
`createdAt` timestamp NOT NULL,
`status` text NOT NULL DEFAULT ('pending'),
`status` varchar(50) NOT NULL DEFAULT 'pending',
`expireAt` timestamp,
`allocationId` text NOT NULL,
`allocationId` varchar(50) NOT NULL,
`numRunsLeft` int NOT NULL,
`maxNumRuns` int NOT NULL,
`idempotencyKey` text,
`idempotencyKey` varchar(255),
CONSTRAINT `tasks_id` PRIMARY KEY(`id`),
CONSTRAINT `tasks_queue_idempotencyKey_unique` UNIQUE(`queue`,`idempotencyKey`)
);
Expand Down
16 changes: 9 additions & 7 deletions src/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "5",
"dialect": "mysql",
"id": "d5ac27ee-fb1f-4d8b-977d-23ef372e91b0",
"id": "4b17721e-a90c-4cdb-beb5-2d26dce5c576",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"tasks": {
Expand All @@ -16,7 +16,7 @@
},
"queue": {
"name": "queue",
"type": "text",
"type": "varchar(255)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
Expand All @@ -37,11 +37,11 @@
},
"status": {
"name": "status",
"type": "text",
"type": "varchar(50)",
"primaryKey": false,
"notNull": true,
"autoincrement": false,
"default": "('pending')"
"default": "'pending'"
},
"expireAt": {
"name": "expireAt",
Expand All @@ -52,7 +52,7 @@
},
"allocationId": {
"name": "allocationId",
"type": "text",
"type": "varchar(50)",
"primaryKey": false,
"notNull": true,
"autoincrement": false
Expand All @@ -73,7 +73,7 @@
},
"idempotencyKey": {
"name": "idempotencyKey",
"type": "text",
"type": "varchar(255)",
"primaryKey": false,
"notNull": false,
"autoincrement": false
Expand Down Expand Up @@ -140,9 +140,11 @@
"idempotencyKey"
]
}
}
},
"checkConstraint": {}
}
},
"views": {},
"_meta": {
"schemas": {},
"tables": {},
Expand Down
4 changes: 2 additions & 2 deletions src/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
{
"idx": 0,
"version": "5",
"when": 1734791710708,
"tag": "0000_stiff_martin_li",
"when": 1734868297153,
"tag": "0000_tiresome_gamma_corps",
"breakpoints": true
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { LiteQueue } from "./queue";
export { buildDBClient, migrateDB } from "./db";
export { connect, migrateDB } from "./db";
export type { QueueOptions, RunnerOptions, RunnerFuncs } from "./options";
export { Runner } from "./runner";

Expand Down
Loading
Loading