From 9ad9b34502df07c95369efc394942aa1cf1151a9 Mon Sep 17 00:00:00 2001 From: Ru Chern Chong Date: Wed, 25 Dec 2024 23:02:41 +0800 Subject: [PATCH] Update schema to use package --- .github/workflows/sst.deploy.yml | 9 +++--- .github/workflows/sst.dev.yml | 1 + .github/workflows/sst.prod.yml | 1 + .github/workflows/sst.staging.yml | 1 + .npmrc | 2 ++ package.json | 1 + pnpm-lock.yaml | 40 +++++++++++++++++++++++ src/lib/getCarsByFuelType.ts | 2 +- src/schema.ts | 54 ------------------------------- src/v1/routes/cars.ts | 2 +- src/v1/routes/coe.ts | 2 +- src/v1/routes/makes.ts | 2 +- src/v1/routes/months.ts | 2 +- 13 files changed, 56 insertions(+), 63 deletions(-) create mode 100644 .npmrc delete mode 100644 src/schema.ts diff --git a/.github/workflows/sst.deploy.yml b/.github/workflows/sst.deploy.yml index b1f4292..19ce8b6 100644 --- a/.github/workflows/sst.deploy.yml +++ b/.github/workflows/sst.deploy.yml @@ -23,6 +23,11 @@ on: UPSTASH_REDIS_REST_URL: required: true +permissions: + id-token: write + contents: read + packages: read + jobs: deploy: runs-on: ubuntu-latest @@ -39,10 +44,6 @@ jobs: concurrency: group: merge-${{ github.ref }} - permissions: - id-token: write - contents: read - steps: - uses: actions/checkout@v4 - uses: pnpm/action-setup@v4 diff --git a/.github/workflows/sst.dev.yml b/.github/workflows/sst.dev.yml index ddda843..64ee134 100644 --- a/.github/workflows/sst.dev.yml +++ b/.github/workflows/sst.dev.yml @@ -9,6 +9,7 @@ on: permissions: id-token: write contents: read + packages: read jobs: dev: diff --git a/.github/workflows/sst.prod.yml b/.github/workflows/sst.prod.yml index 4cf5702..2b352df 100644 --- a/.github/workflows/sst.prod.yml +++ b/.github/workflows/sst.prod.yml @@ -9,6 +9,7 @@ on: permissions: id-token: write contents: read + packages: read jobs: prod: diff --git a/.github/workflows/sst.staging.yml b/.github/workflows/sst.staging.yml index b506226..271bcd2 100644 --- a/.github/workflows/sst.staging.yml +++ b/.github/workflows/sst.staging.yml @@ -11,6 +11,7 @@ on: permissions: id-token: write contents: read + packages: read jobs: staging: diff --git a/.npmrc b/.npmrc new file mode 100644 index 0000000..8e545c6 --- /dev/null +++ b/.npmrc @@ -0,0 +1,2 @@ +@sgcarstrends:registry=https://npm.pkg.github.com/ +//npm.pkg.github.com/:_authToken=${GITHUB_TOKEN} diff --git a/package.json b/package.json index 491b314..779b0a3 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,7 @@ "dependencies": { "@hono/zod-validator": "^0.4.2", "@neondatabase/serverless": "^0.10.4", + "@sgcarstrends/schema": "^0.2.1", "@upstash/ratelimit": "^2.0.3", "@upstash/redis": "^1.34.0", "date-fns": "^2.30.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 324e403..75cb7bd 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -14,6 +14,9 @@ importers: '@neondatabase/serverless': specifier: ^0.10.4 version: 0.10.4 + '@sgcarstrends/schema': + specifier: ^0.2.1 + version: 0.2.1(@neondatabase/serverless@0.10.4)(@types/pg@8.11.6) '@upstash/ratelimit': specifier: ^2.0.3 version: 2.0.3 @@ -686,6 +689,9 @@ packages: cpu: [x64] os: [win32] + '@sgcarstrends/schema@0.2.1': + resolution: {integrity: sha512-EQaco4VBZGYXWB08oIkh0/wiWS70qi8xmNOp9NrwzFGhouP+hWWpxEsUjidjIRwsvjdCZ+uP2dP48yWuA2bghQ==, tarball: https://npm.pkg.github.com/download/@sgcarstrends/schema/0.2.1/f3e61e6a1bd39dfe64345c30e7db43ed8bc9f2e2} + '@types/estree@1.0.6': resolution: {integrity: sha512-AYnb1nQyY49te+VRAVgmzfcgjYS91mY5P0TKUDCLEM+gNnA+3T6rWITXRLYCpahpqSQbN5cE+gHpnPyXjHWxcw==} @@ -1712,6 +1718,40 @@ snapshots: '@rollup/rollup-win32-x64-msvc@4.27.3': optional: true + '@sgcarstrends/schema@0.2.1(@neondatabase/serverless@0.10.4)(@types/pg@8.11.6)': + dependencies: + drizzle-orm: 0.38.2(@neondatabase/serverless@0.10.4)(@types/pg@8.11.6) + transitivePeerDependencies: + - '@aws-sdk/client-rds-data' + - '@cloudflare/workers-types' + - '@electric-sql/pglite' + - '@libsql/client' + - '@libsql/client-wasm' + - '@neondatabase/serverless' + - '@op-engineering/op-sqlite' + - '@opentelemetry/api' + - '@planetscale/database' + - '@prisma/client' + - '@tidbcloud/serverless' + - '@types/better-sqlite3' + - '@types/pg' + - '@types/react' + - '@types/sql.js' + - '@vercel/postgres' + - '@xata.io/client' + - better-sqlite3 + - bun-types + - expo-sqlite + - knex + - kysely + - mysql2 + - pg + - postgres + - prisma + - react + - sql.js + - sqlite3 + '@types/estree@1.0.6': {} '@types/node@20.14.13': diff --git a/src/lib/getCarsByFuelType.ts b/src/lib/getCarsByFuelType.ts index 97bb16e..57d0427 100644 --- a/src/lib/getCarsByFuelType.ts +++ b/src/lib/getCarsByFuelType.ts @@ -1,6 +1,6 @@ import db from "@/config/db"; import { getLatestMonth } from "@/lib/getLatestMonth"; -import { cars } from "@/schema"; +import { cars } from "@sgcarstrends/schema"; import type { FuelType } from "@/types"; import getTrailingTwelveMonths from "@/utils/getTrailingTwelveMonths"; import { and, asc, between, desc, eq, ilike, or } from "drizzle-orm"; diff --git a/src/schema.ts b/src/schema.ts deleted file mode 100644 index b715272..0000000 --- a/src/schema.ts +++ /dev/null @@ -1,54 +0,0 @@ -import { index, integer, pgTable, text, uuid } from "drizzle-orm/pg-core"; - -export const cars = pgTable( - "cars", - { - id: uuid("id").defaultRandom().primaryKey(), - month: text("month"), - make: text("make"), - importer_type: text("importer_type"), - fuel_type: text("fuel_type"), - vehicle_type: text("vehicle_type"), - number: integer("number"), - }, - (table) => [ - index("month_make_idx").on(table.month, table.make), - index("month_idx").on(table.month), - index("make_idx").on(table.make), - index("fuel_type_idx").on(table.fuel_type), - index("make_fuel_type_idx").on(table.make, table.fuel_type), - index("number_idx").on(table.number), - ], -); - -export const coe = pgTable( - "coe", - { - id: uuid("id").defaultRandom().primaryKey(), - month: text("month"), - bidding_no: integer("bidding_no"), - vehicle_class: text("vehicle_class"), - quota: integer("quota"), - bids_success: integer("bids_success"), - bids_received: integer("bids_received"), - premium: integer("premium"), - }, - (table) => [ - index("month_vehicle_idx").on(table.month, table.vehicle_class), - index("vehicle_class_idx").on(table.vehicle_class), - index("month_bidding_no_idx").on(table.month, table.bidding_no), - index("premium_idx").on(table.premium), - index("bids_idx").on(table.bids_success, table.bids_received), - index("month_bidding_no_vehicle_class_idx").on( - table.month.desc(), - table.bidding_no.desc(), - table.vehicle_class, - ), - ], -); - -export type InsertCar = typeof cars.$inferInsert; -export type SelectCar = typeof cars.$inferSelect; - -export type InsertCOE = typeof coe.$inferInsert; -export type SelectCOE = typeof coe.$inferSelect; diff --git a/src/v1/routes/cars.ts b/src/v1/routes/cars.ts index c6232d9..eaa00dd 100644 --- a/src/v1/routes/cars.ts +++ b/src/v1/routes/cars.ts @@ -4,7 +4,7 @@ import redis from "@/config/redis"; import { getLatestMonth } from "@/lib/getLatestMonth"; import { getUniqueMonths } from "@/lib/getUniqueMonths"; import { groupMonthsByYear } from "@/lib/groupMonthsByYear"; -import { cars } from "@/schema"; +import { cars } from "@sgcarstrends/schema"; import { CarQuerySchema, MonthsQuerySchema } from "@/schemas"; import type { Make } from "@/types"; import getTrailingTwelveMonths from "@/utils/getTrailingTwelveMonths"; diff --git a/src/v1/routes/coe.ts b/src/v1/routes/coe.ts index e98173b..2fbaaf6 100644 --- a/src/v1/routes/coe.ts +++ b/src/v1/routes/coe.ts @@ -4,7 +4,7 @@ import redis from "@/config/redis"; import { getLatestMonth } from "@/lib/getLatestMonth"; import { getUniqueMonths } from "@/lib/getUniqueMonths"; import { groupMonthsByYear } from "@/lib/groupMonthsByYear"; -import { coe } from "@/schema"; +import { coe } from "@sgcarstrends/schema"; import { type COE, COEQuerySchema, MonthsQuerySchema } from "@/schemas"; import { zValidator } from "@hono/zod-validator"; import { and, asc, desc, eq, gte, lte } from "drizzle-orm"; diff --git a/src/v1/routes/makes.ts b/src/v1/routes/makes.ts index 02df94d..8d59d88 100644 --- a/src/v1/routes/makes.ts +++ b/src/v1/routes/makes.ts @@ -1,7 +1,7 @@ import { CACHE_TTL } from "@/config"; import db from "@/config/db"; import redis from "@/config/redis"; -import { cars } from "@/schema"; +import { cars } from "@sgcarstrends/schema"; import { MakeParamSchema, MakeQuerySchema } from "@/schemas"; import { zValidator } from "@hono/zod-validator"; import { and, asc, desc, eq, ilike } from "drizzle-orm"; diff --git a/src/v1/routes/months.ts b/src/v1/routes/months.ts index 48adc45..b5cac84 100644 --- a/src/v1/routes/months.ts +++ b/src/v1/routes/months.ts @@ -1,5 +1,5 @@ import { getLatestMonth } from "@/lib/getLatestMonth"; -import { cars, coe } from "@/schema"; +import { cars, coe } from "@sgcarstrends/schema"; import { LatestMonthQuerySchema } from "@/schemas"; import { zValidator } from "@hono/zod-validator"; import { Hono } from "hono";