Skip to content

Commit

Permalink
set up test infrastructure
Browse files Browse the repository at this point in the history
  • Loading branch information
hyper-neutrino committed Jul 18, 2024
1 parent d6c8d49 commit 029af8d
Show file tree
Hide file tree
Showing 16 changed files with 1,109 additions and 153 deletions.
8 changes: 6 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
{
"scripts": {
"migrate": "pnpm -C packages/db migrate",
"test": "pnpm -C packages/test start"
},
"devDependencies": {
"prettier": "^3.3.3"
},
"dependencies": {
"tsx": "^4.16.2"
}
"@transgg/config": "workspace:^",
"@transgg/db": "workspace:^",
"esrun": "^3.2.26"
},
"packageManager": "[email protected]+sha512.77b89e9be77a2b06ad8f403a19cae5e22976f61023f98ad323d5c30194958ebc02ee0a6ae5d13ee454f6134e4e8caf29a05f0b1a0e1d2b17bca6b6a1f1159f86"
}
1 change: 1 addition & 0 deletions packages/config/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.ts
13 changes: 13 additions & 0 deletions packages/config/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import CONFIG from "./config.ts";

export type Configuration = {
DATABASE: {
HOST: string;
PORT: number;
USERNAME: string;
PASSWORD: string;
NAME: string;
};
};

export default CONFIG as Configuration;
4 changes: 4 additions & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "@transgg/config",
"type": "module"
}
15 changes: 15 additions & 0 deletions packages/db/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { defineConfig } from "drizzle-kit";
import CONFIG from "../config/index.ts";

export default defineConfig({
schema: "./src/schema.ts",
out: "./drizzle",
dialect: "mysql",
dbCredentials: {
host: CONFIG.DATABASE.HOST,
port: CONFIG.DATABASE.PORT,
user: CONFIG.DATABASE.USERNAME,
password: CONFIG.DATABASE.PASSWORD,
database: CONFIG.DATABASE.NAME,
},
});
4 changes: 4 additions & 0 deletions packages/db/drizzle/0000_gorgeous_azazel.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE `test` (
`id` int AUTO_INCREMENT NOT NULL,
CONSTRAINT `test_id` PRIMARY KEY(`id`)
);
40 changes: 40 additions & 0 deletions packages/db/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"version": "5",
"dialect": "mysql",
"id": "28145d42-50b2-42f4-bda1-61e94e2064e7",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"test": {
"name": "test",
"columns": {
"id": {
"name": "id",
"type": "int",
"primaryKey": false,
"notNull": true,
"autoincrement": true
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {
"test_id": {
"name": "test_id",
"columns": [
"id"
]
}
},
"uniqueConstraints": {}
}
},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
},
"internal": {
"tables": {},
"indexes": {}
}
}
13 changes: 13 additions & 0 deletions packages/db/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "7",
"dialect": "mysql",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1721312426379,
"tag": "0000_gorgeous_azazel",
"breakpoints": true
}
]
}
3 changes: 3 additions & 0 deletions packages/db/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import * as tables from "./src/schema.ts";
export { db } from "./src/db.ts";
export { tables };
16 changes: 16 additions & 0 deletions packages/db/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "@transgg/db",
"type": "module",
"scripts": {
"migrate:generate": "pnpm drizzle-kit generate",
"migrate:run": "pnpm esrun ./src/migrate.ts",
"migrate": "pnpm migrate:generate && pnpm migrate:run"
},
"dependencies": {
"drizzle-orm": "^0.32.0",
"mysql2": "^3.10.3"
},
"devDependencies": {
"drizzle-kit": "^0.23.0"
}
}
14 changes: 14 additions & 0 deletions packages/db/src/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import CONFIG from "@transgg/config";
import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import * as schema from "./schema.ts";

export const connection = await mysql.createConnection({
host: CONFIG.DATABASE.HOST,
port: CONFIG.DATABASE.PORT,
user: CONFIG.DATABASE.USERNAME,
password: CONFIG.DATABASE.PASSWORD,
database: CONFIG.DATABASE.NAME,
});

export const db = drizzle(connection, { schema, mode: "default" });
5 changes: 5 additions & 0 deletions packages/db/src/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { migrate } from "drizzle-orm/mysql2/migrator"
import { connection, db } from "./db.ts"

await migrate(db, { migrationsFolder: "./drizzle" });
await connection.end();
5 changes: 5 additions & 0 deletions packages/db/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { int, mysqlTable } from "drizzle-orm/mysql-core";

export const test = mysqlTable("test", {
id: int("id").notNull().primaryKey().autoincrement(),
});
4 changes: 3 additions & 1 deletion packages/test/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
console.log("Hello, World!");
import { db } from "@transgg/db";

console.log((await db.query.test.findMany()).map((entry) => entry.id));
6 changes: 5 additions & 1 deletion packages/test/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"name": "@transgg/test",
"scripts": {
"start": "pnpm tsx index.ts"
"start": "pnpm esrun index.ts"
},
"dependencies": {
"@transgg/config": "workspace:^"
}
}
Loading

0 comments on commit 029af8d

Please sign in to comment.