Skip to content

Commit

Permalink
feat(lunox-event): driver for typeorm and drizzle (#65)
Browse files Browse the repository at this point in the history
* feat(lunox-event): typeorm connection

* test(lunox-mail): using typeorm connection

* feat(lunox-event-typeorm): add queue job and failed job models

* feat(preset): update config for preset that using typeorm

* test(lunox-event-typeorm): queue using mysql, sqlite and postgre

* test(lunox-event-typeorm): precise test for retry after failed job

* feat(lunox-event-drizzle): drizzle driver for lunoxjs-event

* chore: install nx and sync dependencies

* chore: build and test script using nx
  • Loading branch information
axmad386 authored Jul 29, 2024
1 parent fa71f1c commit e2b89f6
Show file tree
Hide file tree
Showing 89 changed files with 2,070 additions and 756 deletions.
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
PG_PORT=5432
PG_NAME=lunox
PG_USER=postgres
PG_PASSWORD=postgres
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules/
.env
.nx/
26 changes: 26 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
version: '3.8'
services:
lunox-db:
container_name: lunox-db
image: postgres:16
ports:
- ${PG_PORT}:5432
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: ${PG_PASSWORD}
POSTGRES_PG: ${PG_NAME}
PGDATA: /var/lib/postgresql/data
networks:
lunox-net:
volumes:
- postgres:/var/lib/postgresql/data
restart: unless-stopped


networks:
lunox-net:
driver: bridge
ipam:
driver: default
volumes:
postgres:
15 changes: 15 additions & 0 deletions nx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"tasksRunnerOptions": {
"default": {
"runner": "nx/tasks-runners/default",
"options": {
"cacheableOperations": ["build", "test"]
}
}
},
"targetDefaults": {
"build": {
"dependsOn": ["^build"]
}
}
}
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"scripts": {
"version:bump": "node script/version.js",
"sync": "node script/fixVersion.js && syncpack fix-mismatches && node script/fixVersion.js --workspace",
"test": "pnpm --filter '@lunoxjs/*' --bail test",
"build": "pnpm -r --workspace-concurrency 1 --sort --filter '@lunoxjs/*' --bail build"
"test": "nx run-many -t test -p @lunoxjs/*",
"build": "nx run-many -t build --all"
},
"keywords": [
"laravel",
Expand All @@ -27,6 +27,7 @@
"license": "MIT",
"devDependencies": {
"@swc/core": "^1.3.99",
"nx": "^19.5.3",
"syncpack": "^10.9.3",
"tsup": "^8.0.1"
},
Expand Down
5 changes: 3 additions & 2 deletions packages/lunox-auth/test/Feature/auth.session.typeorm.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import TestCase from "../TestCase";
import { describe, test, expect } from "vitest";
import User from "../app/Model/eloquent/User";
import User from "../app/Model/typeorm/User";
import { DB } from "@lunoxjs/typeorm";

TestCase.provider = "typeorm";
TestCase.make();
describe("Auth Session Test", () => {
test("can run Application", async () => {
expect(config("app.name")).toBe("@lunoxjs/auth");
expect(await User.query().first()).toMatchObject({
expect(await DB.use(User).findOne({where: { email: "[email protected]" }})).toMatchObject({
email: "[email protected]",
});
});
Expand Down
1 change: 0 additions & 1 deletion packages/lunox-auth/test/TestCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import UserTypeorm from "./app/Model/typeorm/User";
import bcrypt from "bcrypt";
import { DB as TypeormDb} from "@lunoxjs/typeorm";
import { DB as DrizzleDb} from "@lunoxjs/drizzle"
import { sql } from "drizzle-orm";
import { users } from "./database/drizzleSchema";

class TestCase extends BaseTestCase {
Expand Down
3 changes: 1 addition & 2 deletions packages/lunox-auth/test/app/Model/eloquent/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,13 @@ import { AuthenticatableTrait } from "../../../../src/providers/eloquent";
interface User extends Authenticatable { }
class User extends Traitable(Model).use(AuthenticatableTrait) {
static factory: () => any;
username!: string;
email!: string;
password!: string;
protected static timestamps = false;

protected static table = "users";

protected static fillable = ["username", "email", "password"];
protected static fillable = ["email", "password"];

protected static hidden = ["password"];
}
Expand Down
122 changes: 2 additions & 120 deletions packages/lunox-auth/test/config/database.ts
Original file line number Diff line number Diff line change
@@ -1,127 +1,9 @@
export default {
/*
|--------------------------------------------------------------------------
| Default Database Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the database connections below you wish
| to use as your default connection for all database work. Of course
| you may use many connections at once using the Database library.
|
*/

default: env("DB_CONNECTION", "sqlite"),

/*
|--------------------------------------------------------------------------
| Database Connections
|--------------------------------------------------------------------------
|
| Here are each of the database connections setup for your application.
| Of course, examples of configuring each database platform that is
| supported by Laravel is shown below to make development simple.
|
|
| All database work in Laravel is done through the PHP PDO facilities
| so make sure you have the driver for your particular database of
| choice installed on your machine before you begin development.
|
*/

default: "sqlite",
connections: {
sqlite: {
driver: "sqlite",
database: env("DB_DATABASE", base_path("database/database.sqlite")),
},

mysql: {
driver: "mysql",
host: env("DB_HOST", "127.0.0.1"),
port: env("DB_PORT", "3306"),
database: env("DB_DATABASE", "lunox"),
username: env("DB_USERNAME", "@lunoxjs/core"),
password: env("DB_PASSWORD", ""),
charset: "utf8mb4",
collation: "utf8mb4_unicode_ci",
prefix: "",
prefix_indexes: true,
strict: true,
engine: null,
},
pgsql: {
driver: "pgsql",
url: env("DATABASE_URL"),
host: env("DB_HOST", "127.0.0.1"),
port: env("DB_PORT", "5432"),
database: env("DB_DATABASE", "forge"),
username: env("DB_USERNAME", "forge"),
password: env("DB_PASSWORD", ""),
charset: "utf8",
prefix: "",
prefix_indexes: true,
schema: "public",
sslmode: "prefer",
},

sqlsrv: {
driver: "sqlsrv",
url: env("DATABASE_URL"),
host: env("DB_HOST", "localhost"),
port: env("DB_PORT", "1433"),
database: env("DB_DATABASE", "forge"),
username: env("DB_USERNAME", "forge"),
password: env("DB_PASSWORD", ""),
charset: "utf8",
prefix: "",
prefix_indexes: true,
},
},

/*
|--------------------------------------------------------------------------
| Migration Repository Table
|--------------------------------------------------------------------------
|
| This table keeps track of all the migrations that have already run for
| your application. Using this information, we can determine which of
| the migrations on disk haven't actually been run in the database.
|
*/

migrations: "migrations",

/*
|--------------------------------------------------------------------------
| Redis Databases
|--------------------------------------------------------------------------
|
| Redis is an open source, fast, and advanced key-value store that also
| provides a richer body of commands than a typical key-value system
| such as APC or Memcached. Laravel makes it easy to dig right in.
|
*/

redis: {
client: env("REDIS_CLIENT", "phpredis"),

options: {
cluster: env("REDIS_CLUSTER", "redis"),
},

default: {
url: env("REDIS_URL"),
host: env("REDIS_HOST", "127.0.0.1"),
password: env("REDIS_PASSWORD", null),
port: env("REDIS_PORT", "6379"),
database: env("REDIS_DB", "0"),
},

cache: {
url: env("REDIS_URL"),
host: env("REDIS_HOST", "127.0.0.1"),
password: env("REDIS_PASSWORD", null),
port: env("REDIS_PORT", "6379"),
database: env("REDIS_CACHE_DB", "1"),
database: base_path("database/database.sqlite")
},
},
};
1 change: 0 additions & 1 deletion packages/lunox-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
"sirv": "^2.0.3"
},
"devDependencies": {
"@lunoxjs/test": "workspace:*",
"@microsoft/api-extractor": "^7.38.3",
"@types/bcryptjs": "^2.4.6",
"@types/body-parser": "^1.19.5",
Expand Down
2 changes: 1 addition & 1 deletion packages/lunox-core/test/TestCase.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import app from "./bootstrap/app";
import { BaseTestCase } from "@lunoxjs/test";
import BaseTestCase from "../../lunox-test/src/BaseTestCase";
import type { Kernel } from "../src";

class TestCase extends BaseTestCase {
Expand Down
4 changes: 4 additions & 0 deletions packages/lunox-event-drizzle/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules/
dist/
.env
test/database.sqlite
1 change: 1 addition & 0 deletions packages/lunox-event-drizzle/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="@lunoxjs/core/global" />
1 change: 1 addition & 0 deletions packages/lunox-event-drizzle/mysql.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/models/mysql"
56 changes: 56 additions & 0 deletions packages/lunox-event-drizzle/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "@lunoxjs/event-drizzle",
"version": "2.0.0-beta.3.5.0",
"description": "Drizzle Connection for @lunoxjs/event",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": "./dist/index.js",
"./dist/*": "./dist/*",
"./mysql": "./dist/models/mysql/index.js",
"./sqlite": "./dist/models/sqlite/index.js",
"./postgre": "./dist/models/postgre/index.js"
},
"files": [
"dist/*"
],
"scripts": {
"build": "lunox prod",
"dev": "lunox dev",
"test": "vitest"
},
"keywords": [
"lunoxjs",
"event",
"scheduler",
"queue",
"drizzle"
],
"author": {
"name": "Akhmad Salafudin",
"email": "[email protected]",
"url": "https://github.com/axmad386"
},
"license": "MIT",
"devDependencies": {
"@lunoxjs/core": "workspace:*",
"@lunoxjs/drizzle": "workspace:*",
"@lunoxjs/event": "workspace:*",
"@lunoxjs/test": "workspace:*",
"@types/better-sqlite3": "^7.6.9",
"@types/pg": "^8.11.6",
"better-sqlite3": "^9.3.0",
"dayjs": "^1.11.10",
"drizzle-orm": "^0.29.1",
"mysql2": "^3.6.5",
"pg": "^8.11.3",
"typescript": "^5.3.2",
"vitest": "^0.34.6"
},
"peerDependencies": {
"@lunoxjs/core": "workspace:*",
"@lunoxjs/drizzle": "workspace:*",
"@lunoxjs/event": "workspace:*"
},
"type": "module"
}
1 change: 1 addition & 0 deletions packages/lunox-event-drizzle/postgre.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/models/postgre"
1 change: 1 addition & 0 deletions packages/lunox-event-drizzle/sqlite.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./dist/models/sqlite"
48 changes: 48 additions & 0 deletions packages/lunox-event-drizzle/src/Connection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DB } from "@lunoxjs/drizzle";
import { QueueJobSchema, QueueJobFailedSchema } from "@lunoxjs/event/contracts";
import { BaseQueueConnection } from "@lunoxjs/event";
import { and, asc, eq, lte } from "drizzle-orm";

class Connection extends BaseQueueConnection {
protected async storeJob(
data: Pick<QueueJobSchema, "queue" | "payload" | "available_at">,
): Promise<void> {
await (DB as any).insert(this.config.model.job).values(data);
}

protected async updateJob(queueJob: QueueJobSchema): Promise<void> {
await (DB as any).update(this.config.model.job)
.set(queueJob)
.where(eq(this.config.model.job.id, queueJob.id));
}

protected async removeJob(queueJob: QueueJobSchema): Promise<void> {
await (DB as any).delete(this.config.model.job).where(
eq(this.config.model.job.id, queueJob.id),
);
}

protected async storeFailedJob(
data: Pick<
QueueJobFailedSchema,
"queue" | "payload" | "failed_at" | "exception"
>,
): Promise<void> {
await (DB as any).insert(this.config.model.failedJob).values(data);
}

protected async getLastJob(queue: string): Promise<QueueJobSchema | null> {
const res = await (DB as any).select()
.from(this.config.model.job)
.where(
and(
eq(this.config.model.job.queue, queue),
lte(this.config.model.job.available_at, new Date()),
),
)
.orderBy(asc(this.config.model.job.id))
.limit(1);
return res?.[0] as QueueJobSchema | null;
}
}
export default Connection;
2 changes: 2 additions & 0 deletions packages/lunox-event-drizzle/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import Connection from "./Connection";
export { Connection };
Loading

0 comments on commit e2b89f6

Please sign in to comment.