Skip to content

Commit

Permalink
Prisma 5 (#50)
Browse files Browse the repository at this point in the history
* Prisma 5

* remove separate 'common' package

* fix eslint and paths

* WIP

* WIP

* WIP

* WIP

* WIP

* gql generation improvements

* lint

* build dir

* no -b

* web vitest

* WIP
  • Loading branch information
revmischa authored Aug 9, 2023
1 parent c14e885 commit a82ed9e
Show file tree
Hide file tree
Showing 60 changed files with 12,343 additions and 10,026 deletions.
3 changes: 3 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
PRISMA_CONNECTION_LIMIT=5

# for local dev environments which use docker-compose, you can disable running migrations in AWS by setting this to false
RUN_DB_MIGRATIONS=true

# set this to enable a bastion EC2 host you can tunnel through to connect to the database
# create a keypair in your desired account and region and add the name here
# SSH_KEYPAIR_NAME=
30 changes: 0 additions & 30 deletions .eslintrc.cjs

This file was deleted.

63 changes: 63 additions & 0 deletions .eslintrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
root: true
env:
browser: false
node: true
es6: true
extends:
- "eslint:recommended"
- "plugin:@typescript-eslint/eslint-recommended"
- "plugin:@typescript-eslint/recommended"
- "plugin:import/recommended"
- "plugin:import/typescript"
globals:
Atomics: readonly
SharedArrayBuffer: readonly
parser: "@typescript-eslint/parser"
parserOptions:
ecmaVersion: 2019
sourceType: module
plugins:
- "@typescript-eslint"
ignorePatterns:
- node_modules
- build
- "prisma-generated-client"
- generated/
- cdk.out
- __mocks__
settings:
import/parsers:
"@typescript-eslint/parser": [".ts", ".tsx"]
import/ignore:
- \.test\.ts$ # tests aren't imported
- \.(scss|less|css)$ # can't parse unprocessed CSS modules
- node_modules
- build/
- \.next
import/resolver:
typescript:
alwaysTryTypes: true
project:
- "tsconfig.json"
- "*/tsconfig.json"
rules:

"@typescript-eslint/explicit-module-boundary-types": off

"@typescript-eslint/no-unused-vars": off

"@typescript-eslint/no-non-null-assertion": off

"@typescript-eslint/no-empty-interface": off

"@typescript-eslint/no-explicit-any": off

"@typescript-eslint/triple-slash-reference": off

# gets confused about imported Function from SST etc
"@typescript-eslint/ban-types": off

# makes switch/case statements really annoying
"no-case-declarations": off

4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ jobs:
# Build
- uses: pnpm/action-setup@v2
with:
version: 7
version: 8
- name: Install Node
uses: actions/setup-node@v3
with:
node-version: '16'
node-version: '18'
cache: pnpm

- name: Install dependencies
Expand Down
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ pnpm i # install dependencies + generate types from DB and GQL schemas
pnpm dev # start local dev server
```

### Config

Set your default AWS profile and region in `sst.config.ts`.

Edit .env or .env.$stage to set infrastructure vars.


## Features

All features are optional, delete what you don't need.
Expand All @@ -32,7 +39,7 @@ Click links to learn more and view documentation.
- 🌤 [AWS CDK](https://aws.amazon.com/cdk/) - cloud-native infrastructure as code
-[GraphQL API](https://docs.serverless-stack.com/constructs/GraphQLApi)
-[AWS AppSync](https://docs.aws.amazon.com/appsync/latest/devguide/what-is-appsync.html) - serverless GraphQL AWS service
-[Code generation](https://www.graphql-code-generator.com/) of [TypeScript](https://www.graphql-code-generator.com/docs/guides/front-end-typescript-only) + [apollo client](https://www.graphql-code-generator.com/plugins/typescript-react-apollo)
-[Code generation](https://www.graphql-code-generator.com/) of [TypeScript](https://www.graphql-code-generator.com/docs/guides/front-end-typescript-only) + [react client](https://the-guild.dev/graphql/codegen/docs/guides/react-vue)
- 🌐 [REST API gateway](https://docs.serverless-stack.com/api)
- 🖥 [NextJS](https://nextjs.org/) frontend w/ Material-UI
- 🎨 [Material-UI](https://mui.com/material-ui/getting-started/overview/) - react components and styling solution
Expand Down Expand Up @@ -71,7 +78,7 @@ pnpm dev:web
### Run/generate DB migrations locally

```shell
pnpm db:migrate:dev
pnpm db:migrate
```

### Just watch and perform type-checking
Expand Down
10 changes: 5 additions & 5 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
"@aws-sdk/client-secrets-manager": "~3.145.0",
"@aws-sdk/signature-v4-crt": "^3.130.0",
"@middy/core": "^2.5.7",
"@prisma/client": "4.13.0",
"@prisma/internals": "4.13.0",
"@prisma/migrate": "4.13.0",
"@prisma/client": "5.1.1",
"@prisma/internals": "5.1.1",
"@prisma/migrate": "5.1.1",
"@sentry/serverless": "^7.8.1",
"aws-lambda": "^1.0.7",
"base64url": "^3.0.1",
Expand All @@ -41,7 +41,7 @@
},
"devDependencies": {
"@babel/core": ">=7.0.0 <8.0.0",
"@chax-at/transactional-prisma-testing": "^0.5.0",
"@chax-at/transactional-prisma-testing": "^1.1.0",
"@faker-js/faker": "^7.5.0",
"@types/aws-lambda": "^8.10.70",
"@types/memoizee": "^0.4.7",
Expand All @@ -53,7 +53,7 @@
"fishery": "^2.2.2",
"graphql": "^16.5.0",
"graphql-tag": "^2.12.6",
"prisma": "4.13.0",
"prisma": "5.1.1",
"uuid": "^9.0.0"
},
"engines": {
Expand Down
3 changes: 2 additions & 1 deletion backend/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

generator client {
provider = "prisma-client-js"
previewFeatures = ["interactiveTransactions", "jsonProtocol"]
previewFeatures = []
binaryTargets = ["native"]
}

datasource db {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/resolver/greeting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { defaultAppSyncMiddleware } from '@backend/middleware/lambda';
import { logger } from '@backend/util/logger';
import { incrementMetric } from '@backend/util/metrics';
import { AppSyncResolverEvent } from 'aws-lambda';
import { GQL } from 'common';
import { GQL } from '@common/gql';

export const GREETING = 'Yo yo';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
Not really using a public API.
*/
import type { PrismaClientInitializationError } from '@prisma/client/runtime';
import { Migrate } from '@prisma/migrate/dist/Migrate.js';
import { ensureDatabaseExists } from '@prisma/migrate/dist/utils/ensureDatabaseExists';
import { printFilesFromMigrationIds } from '@prisma/migrate/dist/utils/printFiles';
import chalk from 'chalk';
import { GetSecretValueCommand, SecretsManagerClient } from '@aws-sdk/client-secrets-manager';
import { PrismaClient } from '@prisma/client';
import { sleep, isProd } from 'common';
import { Prisma, PrismaClient } from '@prisma/client';
import { isProd } from '@common/env';
import { sleep } from '@common/util';

export const handler = async (): Promise<string> => {
const schemaPath = '/var/task/backend/prisma/schema.prisma';
Expand All @@ -22,7 +22,7 @@ export const handler = async (): Promise<string> => {
const client = new PrismaClient();
await client.$connect();
} catch (ex) {
const errorCode = (ex as PrismaClientInitializationError).errorCode;
const errorCode = (ex as Prisma.PrismaClientInitializationError).errorCode;
if (errorCode == 'P1001') {
// timed out waiting to reach DB server
// it might be waking up from slumber
Expand Down
6 changes: 3 additions & 3 deletions backend/src/db/seed/truncate.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { isProd } from '@common/env';
import { Prisma, PrismaClient } from '@prisma/client';
import { isProd } from 'common';

export async function truncateAllTables(prisma: PrismaClient | Prisma.TransactionClient) {
if (isProd()) throw new Error('Please stop whatever you are doing right now.');
if (isProd()) throw new Error('Please stop whatever you are doing right now.');

await prisma.$executeRawUnsafe(`
await prisma.$executeRawUnsafe(`
DO $$ DECLARE
r RECORD;
BEGIN
Expand Down
2 changes: 1 addition & 1 deletion backend/src/middleware/sentryLambdaWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/
import { AWSLambda } from '@sentry/serverless';
import { Handler } from 'aws-lambda';
import { getSstStage } from 'common';
import { getSstStage } from '@common/env';

if (process.env['_HANDLER']) {
AWSLambda.init({
Expand Down
2 changes: 1 addition & 1 deletion backend/src/util/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Logger } from '@aws-lambda-powertools/logger';
import { APP_NAME } from 'common';
import { APP_NAME } from '@common/index';

export const logger = new Logger({ serviceName: APP_NAME });
3 changes: 2 additions & 1 deletion backend/src/util/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// https://awslabs.github.io/aws-lambda-powertools-typescript/latest/core/metrics/

import { Metrics, MetricUnits } from '@aws-lambda-powertools/metrics';
import { APP_NAME, getSstApp, getSstStage } from 'common';
import { getSstStage, getSstApp } from '@common/env';
import { APP_NAME } from '@common/index';

const stage = getSstStage();
const app = getSstApp();
Expand Down
13 changes: 9 additions & 4 deletions backend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
{
"extends": "../tsconfig.base.json",
"include": ["src"],
"compilerOptions": {
"outDir": "./dist",
"module": "esnext",
"target": "es2021"
"paths": {
"@common/*": ["../common/src/*"],
"@backend/*": ["./src/*"]
},
"baseUrl": ".",
"rootDir": "..",
"outDir": "./.build"
},
"references": [{ "path": "../common" }]
"files": ["./src/index.d.ts", "../common/src/index.d.ts"],
"include": ["src/**/*", "../common/src/**/*"]
}
31 changes: 0 additions & 31 deletions common/graphql/codegen.yml

This file was deleted.

10 changes: 0 additions & 10 deletions common/graphql/documents/greeting.ts

This file was deleted.

24 changes: 8 additions & 16 deletions common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,25 @@
"name": "common",
"version": "1.0.0",
"description": "Code shared between the client and server",
"main": "./dist/src/index.js",
"main": "build/index.js",
"type": "module",
"types": "./src/index.ts",
"scripts": {
"build": "tsc",
"start": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1",
"graphql:generate": "cross-env DEBUG=1 graphql-codegen --config graphql/codegen.yml",
"generate": "pnpm run graphql:generate",
"lint": "eslint --ext .js,.ts,.tsx src graphql/documents",
"clean": "rimraf dist"
"lint": "eslint --ext .js,.ts,.tsx src",
"clean": "rimraf dist",
"graphql:generate": "graphql-codegen --config ../graphql/codegen.ts"
},
"devDependencies": {
"@graphql-codegen/cli": "^2.6.2",
"@graphql-codegen/schema-ast": "^2.4.1",
"@graphql-codegen/typescript": "^2.4.11",
"@graphql-codegen/typescript-operations": "^2.4.0",
"@graphql-codegen/typescript-react-apollo": "*",
"@apollo/client": "^3.6.5",
"graphql": "^16.5.0",
"@types/node": "^18.0.0",
"typescript": "^4.7.4",
"graphql-tag": "^2.12.6"
"@graphql-codegen/cli": "5.0.0",
"@graphql-codegen/client-preset": "4.1.0"
},
"keywords": [],
"author": "",
"peerDependencies": {
"@apollo/client": "^3.6.5",
"graphql": "^16.5.0"
}
}
}
File renamed without changes.
5 changes: 3 additions & 2 deletions common/src/gql.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// import this for all graphql types
export * as GQL from '../graphql/generated/gql.js';
// import this for all graphql defined types
// read more: https://the-guild.dev/graphql/codegen/docs/guides/react-vue
export * as GQL from './generated/gql/graphql';
11 changes: 3 additions & 8 deletions common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// TODO: I don't want to export any of these from index.ts
// I want package subpath exports in package.json to work properly with TypeScript
// so that users can say `import { sleep } from 'common/util/sleep'`
// this is still quite experimental though
// https://devblogs.microsoft.com/typescript/announcing-typescript-4-7-beta/#package-json-exports-imports-and-self-referencing
export * from './env.js';
export * from './gql.js';
export * from './util/index.js';
export * from './env';
export * from './gql';
export * from './util/index';

// change this
export const APP_NAME = 'myapp';
2 changes: 1 addition & 1 deletion common/src/util/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export * from './sleep.js';
export * from './sleep';
Loading

0 comments on commit a82ed9e

Please sign in to comment.