Skip to content
This repository has been archived by the owner on Jul 25, 2024. It is now read-only.

Commit

Permalink
refactor trpc setup
Browse files Browse the repository at this point in the history
  • Loading branch information
buckhalt committed Apr 22, 2024
1 parent 2c1ce5e commit 33637b7
Show file tree
Hide file tree
Showing 11 changed files with 929 additions and 37 deletions.
69 changes: 48 additions & 21 deletions @react-vite-trpc/eslint-config/eslint.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,60 @@
module.exports = {
extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
// eslint-disable-next-line @typescript-eslint/no-var-requires
const path = require('path')

/** @type {import("eslint").Linter.Config} */
const config = {
overrides: [
{
extends: [
'plugin:@typescript-eslint/stylistic-type-checked',
'plugin:@typescript-eslint/recommended-type-checked',
],
files: ['*.ts', '*.tsx'],
parserOptions: {
project: path.join(__dirname, 'tsconfig.json'),
},
},
],
parser: '@typescript-eslint/parser',
plugins: ['@typescript-eslint', 'prettier', 'unused-imports'],
ignorePatterns: ['node_modules', '.turbo', 'dist', 'dev-dist', 'coverage'],
parserOptions: {
project: path.join(__dirname, 'tsconfig.json'),
},
plugins: ['@typescript-eslint', 'eslint-plugin-local-rules'],
extends: [
'eslint:recommended',
'plugin:@typescript-eslint/stylistic',
'plugin:@typescript-eslint/recommended',
'next/core-web-vitals',
'prettier',
],
ignorePatterns: ['node_modules', '*.stories.*', '*.test.*'],
rules: {
'prettier/prettier': ['error', { endOfLine: 'auto' }],
'no-return-await': ['error'],
'prefer-destructuring': ['error'],
'object-shorthand': ['error'],
'no-unneeded-ternary': ['error'],
'prefer-template': ['error'],
'@typescript-eslint/consistent-type-imports': ['error', { fixStyle: 'inline-type-imports' }],
'no-empty': ['error', { allowEmptyCatch: true }],
'unused-imports/no-unused-imports': 'warn',
'@next/next/no-img-element': 'off',
'import/no-anonymous-default-export': 'off',
'@typescript-eslint/consistent-type-definitions': ['error', 'type'],
'no-process-env': 'error',
'no-console': 'error',
'@typescript-eslint/consistent-type-imports': [
'warn',
{
prefer: 'type-imports',
fixStyle: 'inline-type-imports',
},
],
'@typescript-eslint/no-unused-vars': [
'error',
'warn',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
'object-curly-newline': [
'warn',
'@typescript-eslint/no-misused-promises': [
'error',
{
ObjectExpression: {
multiline: true,
minProperties: 2,
},
checksVoidReturn: false,
},
],
'no-unreachable': 'error',
'local-rules/require-data-mapper': 'error',
},
}
module.exports = config
12 changes: 8 additions & 4 deletions apps/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,19 @@
"build": "tsc -b"
},
"dependencies": {
"@libsql/client": "^0.6.0",
"@trpc/client": "^10.29.1",
"@trpc/server": "^10.29.1",
"cookie-parser": "^1.4.6",
"@trpc/client": "^10.29.1",
"drizzle-orm": "^0.30.9",
"drizzle-zod": "^0.5.1",
"express": "^4.18.2",
"express-serve-static-core": "^0.1.1",
"zod": "^3.21.4"
},
"devDependencies": {
"@react-vite-trpc/tsconfig": "workspace:*",
"@react-vite-trpc/config": "workspace:*",
"@react-vite-trpc/tsconfig": "workspace:*",
"@swc/core": "^1.3.62",
"@types/cookie-parser": "^1.4.3",
"@types/express": "^4.17.17",
Expand All @@ -33,11 +36,12 @@
"@types/node": "^18.16.17",
"cross-env": "^7.0.3",
"dotenv": "^16.1.4",
"drizzle-kit": "^0.20.17",
"envalid": "^7.3.1",
"fs-extra": "^11.1.1",
"module-alias": "^2.2.3",
"nodemon": "^2.0.22",
"typescript": "^4.9.5",
"pm2": "^5.3.0"
"pm2": "^5.3.0",
"typescript": "^4.9.5"
}
}
11 changes: 11 additions & 0 deletions apps/server/src/db/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { createClient } from '@libsql/client'
import { drizzle } from 'drizzle-orm/libsql'

import 'dotenv/config'

const turso = createClient({
url: process.env.TURSO_CONNECTION_URL!,
authToken: process.env.TURSO_AUTH_TOKEN!,
})

export const db = drizzle(turso)
14 changes: 14 additions & 0 deletions apps/server/src/db/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createInsertSchema } from 'drizzle-zod'
import { z } from 'zod'

import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core'

export const projects = sqliteTable('projects', {
id: integer('id').primaryKey(),
name: text('name').notNull(),
})

export type InsertProject = typeof projects.$inferInsert
export type SelectProjects = typeof projects.$inferSelect
export const apiProject = createInsertSchema(projects, { name: z.string() })
export const apiCreateProject = apiProject.omit({ id: true })
3 changes: 0 additions & 3 deletions apps/server/src/trpc/api/resolvers/getRole/getRole.ts

This file was deleted.

1 change: 0 additions & 1 deletion apps/server/src/trpc/api/resolvers/index.ts

This file was deleted.

4 changes: 2 additions & 2 deletions apps/server/src/trpc/api/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import type { Application } from 'express'

import { router } from 'trpc'

import { getRole } from './resolvers'
import { projectsRouter } from './routers/projects'

export type AppRouter = typeof appRouter

export type Context = inferAsyncReturnType<typeof createContext>

const appRouter = router({ getRole })
const appRouter = router({ project: projectsRouter })

const createContext = ({ req, res }: CreateExpressContextOptions) => ({
req,
Expand Down
14 changes: 14 additions & 0 deletions apps/server/src/trpc/api/routers/projects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { db } from 'db/db'
import { apiCreateProject, projects } from 'db/schema'

import { publicProcedure, router } from 'trpc'

export const projectsRouter = router({
create: publicProcedure.input(apiCreateProject).mutation(async req => {
return await db.insert(projects).values(req.input).returning()
}),

getAll: publicProcedure.query(async () => {
return await db.select(projects)
}),
})
2 changes: 1 addition & 1 deletion apps/server/src/trpc/trpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { type Context } from './api/router'

const t = initTRPC.context<Context>().create()

export const userProcedure = t.procedure
export const publicProcedure = t.procedure

export const { middleware, router } = t
4 changes: 2 additions & 2 deletions apps/web/src/components/features/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { trpc } from 'trpc'

export const Home = () => {
const { data } = trpc.getRole.useQuery()
const { data } = trpc.project.getAll.useQuery()

return <div>Current role: {data?.role}</div>
return <div>All projects:</div>
}
Loading

0 comments on commit 33637b7

Please sign in to comment.