Skip to content

Commit

Permalink
Add additional shared packages
Browse files Browse the repository at this point in the history
  • Loading branch information
ruchernchong committed Mar 2, 2025
1 parent e04da20 commit a9b0c0e
Show file tree
Hide file tree
Showing 58 changed files with 2,184 additions and 148 deletions.
3 changes: 3 additions & 0 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@
"dependencies": {
"@hono/zod-validator": "^0.4.2",
"@neondatabase/serverless": "^0.10.4",
"@sgcarstrends/config": "workspace:*",
"@sgcarstrends/schema": "workspace:*",
"@sgcarstrends/types": "workspace:*",
"@sgcarstrends/utils": "workspace:*",
"@upstash/ratelimit": "^2.0.3",
"@upstash/redis": "^1.34.0",
"date-fns": "^2.30.0",
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FuelType } from "@api/types";
import { FuelType } from "@sgcarstrends/types";

export const CACHE_TTL = 24 * 60 * 60;

Expand Down
9 changes: 0 additions & 9 deletions apps/api/src/config/redis.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { redis } from "@sgcarstrends/config";
import { Ratelimit } from "@upstash/ratelimit";
import { Hono } from "hono";
import { handle } from "hono/aws-lambda";
Expand All @@ -7,7 +8,6 @@ import { HTTPException } from "hono/http-exception";
import { logger } from "hono/logger";
import { prettyJSON } from "hono/pretty-json";
import packageJson from "../package.json" assert { type: "json" };
import redis from "./config/redis";
import v1 from "./v1";

const ratelimit = new Ratelimit({
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/lib/getCarsByFuelType.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import db from "@api/config/db";
import { getLatestMonth } from "@api/lib/getLatestMonth";
import type { FuelType } from "@api/types";
import getTrailingTwelveMonths from "@api/utils/getTrailingTwelveMonths";
import { cars } from "@sgcarstrends/schema";
import type { FuelType } from "@sgcarstrends/types";
import { getTrailingTwelveMonths } from "@sgcarstrends/utils";
import { and, asc, between, desc, eq, ilike, or } from "drizzle-orm";

const HYBRID_TYPES = [
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/lib/getUniqueMonths.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CACHE_TTL } from "@api/config";
import db from "@api/config/db";
import redis from "@api/config/redis";
import { redis } from "@sgcarstrends/config";
import { desc, getTableName } from "drizzle-orm";
import type { PgTable } from "drizzle-orm/pg-core";

Expand Down
72 changes: 12 additions & 60 deletions apps/api/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,60 +1,12 @@
// Enums
export enum FuelType {
Diesel = "Diesel",
Electric = "Electric",
Hybrid = "Hybrid",
Petrol = "Petrol",
Others = "Others",
}

export enum Collection {
Cars = "cars",
COE = "coe",
}

export enum OrderBy {
ASC = "asc",
DESC = "desc",
}

export enum VehicleClass {
CategoryA = "Category A",
CategoryB = "Category B",
CategoryC = "Category C",
CategoryD = "Category D",
CategoryE = "Category E",
}

// Base types
export type Stage = "dev" | "staging" | "prod";
export type Make = Car["make"];

// Interfaces
export interface Car {
month: string;
make: string;
fuel_type: FuelType;
number: number;
selected?: boolean;
}

export interface COE {
month: string;
bidding_no: number;
vehicle_class: string;
quota: number;
bids_success: number;
bids_received: number;
premium: number;
}

export interface UpdateParams {
collectionName: string;
zipFileName: string;
zipUrl: string;
keyFields: string[];
}

export interface LatestMonth {
latestMonth: string;
}
// Reexport all types from @sgcarstrends/types
export type {
FuelType,
Collection,
OrderBy,
VehicleClass,
Stage,
LatestMonth,
} from "@sgcarstrends/types";

// Local types that aren't shared
export type Make = string;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import getTrailingTwelveMonths from "@api/utils/getTrailingTwelveMonths";
import { getTrailingTwelveMonths } from "@sgcarstrends/utils";
import { describe, expect, it } from "vitest";

describe("getTrailingTwelveMonths", () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/utils/__tests__/slugify.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
import { deslugify, slugify } from "../slugify";
import { deslugify, slugify } from "@sgcarstrends/utils";

describe("slugify", () => {
it("should convert basic string to slug", () => {
Expand Down
8 changes: 0 additions & 8 deletions apps/api/src/utils/getTrailingTwelveMonths.ts

This file was deleted.

2 changes: 1 addition & 1 deletion apps/api/src/v1/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { getCarsByFuelType } from "@api/lib/getCarsByFuelType";
import { FuelType } from "@api/types";
import { FuelType } from "@sgcarstrends/types";
import { Hono } from "hono";
import { bearerAuth } from "hono/bearer-auth";
import { Resource } from "sst";
Expand Down
4 changes: 2 additions & 2 deletions apps/api/src/v1/routes/cars.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { CACHE_TTL, HYBRID_REGEX } from "@api/config";
import db from "@api/config/db";
import redis from "@api/config/redis";
import { getLatestMonth } from "@api/lib/getLatestMonth";
import { getUniqueMonths } from "@api/lib/getUniqueMonths";
import { groupMonthsByYear } from "@api/lib/groupMonthsByYear";
import { CarQuerySchema, MonthsQuerySchema } from "@api/schemas";
import type { Make } from "@api/types";
import getTrailingTwelveMonths from "@api/utils/getTrailingTwelveMonths";
import { zValidator } from "@hono/zod-validator";
import { redis } from "@sgcarstrends/config";
import { cars } from "@sgcarstrends/schema";
import { getTrailingTwelveMonths } from "@sgcarstrends/utils";
import { and, asc, between, desc, eq, ilike, sql } from "drizzle-orm";
import { Hono } from "hono";

Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/v1/routes/coe.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { CACHE_TTL } from "@api/config";
import db from "@api/config/db";
import redis from "@api/config/redis";
import { getUniqueMonths } from "@api/lib/getUniqueMonths";
import { groupMonthsByYear } from "@api/lib/groupMonthsByYear";
import { type COE, COEQuerySchema, MonthsQuerySchema } from "@api/schemas";
import { zValidator } from "@hono/zod-validator";
import { redis } from "@sgcarstrends/config";
import { coe, coePQP } from "@sgcarstrends/schema";
import { and, asc, desc, eq, gte, inArray, lte, max } from "drizzle-orm";
import { Hono } from "hono";
Expand Down
2 changes: 1 addition & 1 deletion apps/api/src/v1/routes/makes.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { CACHE_TTL } from "@api/config";
import db from "@api/config/db";
import redis from "@api/config/redis";
import { MakeParamSchema, MakeQuerySchema } from "@api/schemas";
import type { Make } from "@api/types";
import { zValidator } from "@hono/zod-validator";
import { redis } from "@sgcarstrends/config";
import { cars } from "@sgcarstrends/schema";
import { and, asc, desc, eq, ilike } from "drizzle-orm";
import { Hono } from "hono";
Expand Down
3 changes: 3 additions & 0 deletions apps/updater/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
"license": "MIT",
"dependencies": {
"@neondatabase/serverless": "^0.10.4",
"@sgcarstrends/config": "workspace:*",
"@sgcarstrends/schema": "workspace:*",
"@sgcarstrends/types": "workspace:*",
"@sgcarstrends/utils": "workspace:*",
"@trigger.dev/sdk": "3.3.16",
"@upstash/redis": "^1.34.3",
"adm-zip": "^0.5.16",
Expand Down
9 changes: 0 additions & 9 deletions apps/updater/src/config/redis.ts

This file was deleted.

8 changes: 4 additions & 4 deletions apps/updater/src/lib/updateCars.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { cars } from "@sgcarstrends/schema";
import { LTA_DATAMALL_BASE_URL } from "@updater/config";
import type { Car } from "@updater/types";
import { cleanSpecialChars } from "@updater/utils/cleanSpecialChars";
import { RawCar } from "@sgcarstrends/types";
import { cleanSpecialChars } from "@sgcarstrends/utils";
import { updater } from "./updater";

export const updateCars = () => {
const filename = "Monthly New Registration of Cars by Make.zip";
const url = `${LTA_DATAMALL_BASE_URL}/${filename}`;
const keyFields: Array<keyof Car> = [
const keyFields: Array<keyof RawCar> = [
"month",
"make",
"fuel_type",
"vehicle_type",
];

return updater<Car>({
return updater<RawCar>({
table: cars,
url,
keyFields,
Expand Down
2 changes: 1 addition & 1 deletion apps/updater/src/lib/updater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import path from "node:path";
import { AWS_LAMBDA_TEMP_DIR } from "@updater/config";
import { db } from "@updater/db";
import { calculateChecksum } from "@updater/utils/calculateChecksum";
import { createUniqueKey } from "@updater/utils/createUniqueKey";
import { createUniqueKey } from "@sgcarstrends/utils";
import { downloadFile } from "@updater/utils/downloadFile";
import {
type CSVTransformOptions,
Expand Down
30 changes: 6 additions & 24 deletions apps/updater/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,6 @@
export interface Car {
month: string;
make: string;
importer_type: string;
fuel_type: string;
vehicle_type: string;
number: number;
}

export interface COE {
month: string;
bidding_no: number;
vehicle_class: string;
quota: number;
bids_success: number;
bids_received: number;
premium: number;
}

export interface PQP {
month: string;
vehicle_class: string;
pqp: number;
}
// Re-export types from shared package
export type {
RawCar as Car,
RawCOE as COE,
RawPQP as PQP
} from "@sgcarstrends/types";
2 changes: 1 addition & 1 deletion apps/updater/src/utils/__tests__/cleanSpecialChars.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { cleanSpecialChars } from "@updater/utils/cleanSpecialChars";
import { cleanSpecialChars } from "@sgcarstrends/utils";
import { describe, expect, it } from "vitest";

describe("cleanSpecialChars", () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/updater/src/utils/__tests__/createUniqueKey.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createUniqueKey } from "@updater/utils/createUniqueKey";
import { createUniqueKey } from "@sgcarstrends/utils";
import { describe, expect, it } from "vitest";

describe("createUniqueKey", () => {
Expand Down
2 changes: 1 addition & 1 deletion apps/updater/src/utils/redisCache.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import redis from "@updater/config/redis";
import { redis } from "@sgcarstrends/config";

/**
* Caches a checksum value for a given file name
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
},
"scripts": {
"dev": "turbo dev",
"build": "turbo build",
"test": "turbo test",
"test:coverage": "turbo test:coverage",
"lint": "turbo lint",
Expand Down
1 change: 1 addition & 0 deletions packages/config/dist/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./redis";
1 change: 1 addition & 0 deletions packages/config/dist/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./redis";
2 changes: 2 additions & 0 deletions packages/config/dist/redis.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { Redis } from "@upstash/redis";
export declare const redis: Redis;
5 changes: 5 additions & 0 deletions packages/config/dist/redis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Redis } from "@upstash/redis";
export const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
19 changes: 19 additions & 0 deletions packages/config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@sgcarstrends/config",
"version": "0.0.0",
"license": "MIT",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"dev": "pnpm build --watch",
"lint": "biome check ."
},
"dependencies": {
"@upstash/redis": "^1.34.4",
"dotenv": "^16.4.7"
},
"devDependencies": {
"typescript": "^5.8.2"
}
}
1 change: 1 addition & 0 deletions packages/config/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./redis";
7 changes: 7 additions & 0 deletions packages/config/src/redis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Redis } from "@upstash/redis";
import "dotenv/config";

export const redis = new Redis({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN,
});
8 changes: 8 additions & 0 deletions packages/config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"declaration": true
},
"include": ["src"]
}
Loading

0 comments on commit a9b0c0e

Please sign in to comment.