Skip to content

Updating; removing typeORM, adding ddb-toolbox and prisma #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53,374 changes: 32,351 additions & 21,023 deletions package-lock.json

Large diffs are not rendered by default.

19 changes: 13 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@
"scripts": {
"start:infra": "npm start -w template-infra",
"start:build": "tsc --build --watch",
"start": "npm run build && concurrently npm:start:*",
"start:watch": "tsc --build --watch --incremental",
"start": "npm run build:incremental && concurrently -c green.bold --kill-others npm:start:*",
"build": "tsc --build",
"db:init:local": "npm run db:init:local -w template-repo",
"build:incremental": "npm run db:generate && tsc --build --incremental",
"db:init:local": "npm run init:local -w template-repo",
"db:seed": "npm run seed -w template-repo",
"db:start": "npm run start:db -w template-infra",
"db:migrate:dev": "npm run db:start && npm run migrate:dev -w template-repo",
"db:migrate:deploy": "npm run migrate:deploy -w template-repo",
"db:generate": "npm run generate -w template-repo",
"deploy:dev": "npm run build && npm run deploy:dev -w template-infra",
"deploy:prd": "npm run build && npm run deploy:prd -w template-infra",
"prettier:exc": "./node_modules/.bin/prettier --config .prettierrc --write",
Expand All @@ -27,15 +34,14 @@
"packages/*"
],
"dependencies": {
"@aws-cdk/assert": "1.110.0",
"@aws-cdk/assert": "1.115.0",
"@aws-sdk/client-dynamodb": "^3.19.0",
"@jdpnielsen/http-error": "^1.2.1",
"@jetkit/cdk": "0.4.10",
"@jetkit/cdk": "1.115.1",
"@types/jest": "26.0.23",
"aws-cdk": "1.110.0",
"aws-cdk": "1.115.0",
"aws-xray-sdk": "^3.3.3",
"slugify": "^1.5.3",
"typeorm": "^0.2.32",
"uuid": "^3.3.2"
},
"devDependencies": {
Expand All @@ -51,6 +57,7 @@
"eslint-config-next": "^11.0.1",
"husky": "^4.3.0",
"jest": "^26.6.3",
"jest-mock-extended": "^1.0.18",
"lint-staged": "^10.4.0",
"prettier": "^2.3.1",
"ts-jest": "^26.5.6",
Expand Down
10 changes: 1 addition & 9 deletions packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
"main": "build/index.js",
"private": true,
"scripts": {
"db:dropcreatedb": "dropdb <%= title %> --if-exists && createdb <%= title %>",
"db:migrate": "ts-node ../../node_modules/typeorm/cli.js migration:run",
"db:migrate:generate": "npm run build:core && ts-node ../../node_modules/typeorm/cli.js migration:generate -n $name",
"db:dropcreatedb:test": "dropdb <%= title %>_test --if-exists && createdb <%= title %>_test",
"db:seed": "ts-node ./src/db/runSeed.ts",
"db:init:local": "npm run build:core && npm run db:dropcreatedb && npm run db:migrate && npm run db:seed",
"test": "export NODE_ENV=test && npm run db:dropcreatedb:test && jest -i",
"build": "tsc",
"start": "tsc -w"
},
"author": "",
Expand All @@ -29,7 +21,7 @@
"class-validator": "^0.12.2"
},
"devDependencies": {
"@aws-cdk/aws-apigatewayv2": "1.110.0",
"@aws-cdk/aws-apigatewayv2": "1.115.0",
"@babel/helper-validator-option": "^7.12.0",
"@types/aws-lambda": "^8.10.62"
}
Expand Down
14 changes: 7 additions & 7 deletions packages/api/src/api/tenant.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { HttpMethod } from "@aws-cdk/aws-apigatewayv2"
import { ApiView, ApiViewBase, apiViewHandler, ApiHandler } from "@jetkit/cdk"
import { db, Tenant } from "template-repo"
import { ApiView, ApiViewBase, apiViewHandler, ApiHandler, Duration } from "@jetkit/cdk"
import { TenantRepository } from "template-repo"

@ApiView({
path: "/tenant",
methods: [HttpMethod.GET],
grantDatabaseAccess: true,
memorySize: 1024,
timeout: Duration.seconds(10),
})
export class TenantApi extends ApiViewBase {
override get: ApiHandler = async () => {
// list tenants
const tenants = await (await db.getConnection()).manager.find(Tenant)
return JSON.stringify({
tenants,
})
const repo = new TenantRepository()
const tenants = await repo.list()
return JSON.stringify(tenants)
}
}
export const handler = apiViewHandler(__filename, TenantApi)
5 changes: 4 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"start": "tsc -w",
"build": "tsc"
},
"author": ""
"author": "",
"dependencies": {
"aws-sdk": "^2.953.0"
}
}
27 changes: 27 additions & 0 deletions packages/common/src/awsClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable @typescript-eslint/no-empty-function */
/* eslint-disable @typescript-eslint/no-var-requires */
import AWS from "aws-sdk"

// import AWSXRay from "aws-xray-sdk"
// AWSXRay.setContextMissingStrategy(() => {})

// // capture HTTP requests
// AWSXRay.captureHTTPsGlobal(require("http"))
// AWSXRay.captureHTTPsGlobal(require("https"))
// AWSXRay.capturePromise()

// clients
const isTest = process.env.JEST_WORKER_ID
const config: AWS.DynamoDB.ClientConfiguration = {
...(isTest && {
endpoint: process.env.MOCK_DYNAMODB_ENDPOINT,
sslEnabled: false,
region: "local-env",
}),
}

export const dynamoClient = new AWS.DynamoDB(config)
export const documentClient = new AWS.DynamoDB.DocumentClient({
...config,
service: dynamoClient,
})
10 changes: 10 additions & 0 deletions packages/common/src/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Available environment variables
*/
export enum EnvVars {
// dynamo table name
appTable = "APP_TABLE",

// for JWT etc
appSecret = "APP_SECRET",
}
3 changes: 3 additions & 0 deletions packages/common/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// common shared code and configuration

export const appName = "template"

export * from "./awsClient"
export * from "./env"
10 changes: 10 additions & 0 deletions packages/infra/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: '3.6'

services:
pg:
image: postgres:13
restart: always
environment:
POSTGRES_PASSWORD: localdev
ports:
- 5434:5432
15 changes: 8 additions & 7 deletions packages/infra/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
"watch": "tsc -w",
"test": "jest",
"cdk": "cdk",
"start": "sam-beta-cdk local start-api -n locals.json --warm-containers EAGER --port 4000",
"start:db": "docker-compose up --no-recreate -d",
"start": "npm run start:db && sam-beta-cdk local start-api -n locals.json --warm-containers LAZY --port 4010",
"deploy:dev": "cdk deploy template-dev",
"deploy:prd": "cdk deploy"
},
"devDependencies": {
"@aws-cdk/assert": "1.110.0",
"@aws-cdk/aws-apigatewayv2": "1.110.0",
"@aws-cdk/aws-ec2": "1.110.0",
"@aws-cdk/assert": "1.115.0",
"@aws-cdk/aws-apigatewayv2": "1.115.0",
"@aws-cdk/aws-ec2": "1.115.0",
"@types/jest": "^26.0.10",
"@types/node": "10.17.27",
"aws-cdk": "1.110.0",
"aws-cdk": "1.115.0",
"jest": "^26.4.2",
"@aws-cdk/aws-rds": "1.110.0",
"@aws-cdk/core": "1.110.0",
"@aws-cdk/aws-rds": "1.115.0",
"@aws-cdk/core": "1.115.0",
"ts-jest": "^26.2.0",
"ts-node": "^10.0.0"
},
Expand Down
7 changes: 7 additions & 0 deletions packages/repo/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#using-environment-variables

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server (Preview) and MongoDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

DATABASE_URL="postgresql://postgres:localdev@localhost:5434/myapp?connection_limit=5"
32 changes: 13 additions & 19 deletions packages/repo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,28 @@
"main": "build/index.js",
"private": true,
"scripts": {
"build": "tsc",
"start": "tsc -w",
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint src/**/*.ts",
"db:init:local": "npm run db:dropcreatedb && npm run db:migrate && npm run seed:run",
"db:init:remote:dev": "npm run db:migrate && npm run seed:run",
"db:dropcreatedb": "dropdb <%= title %> --if-exists && createdb <%= title %>",
"db:dropcreatedb:test": "dropdb <%= title %>_test --if-exists && createdb <%= title %>_test",
"db:migrate:generate": "ts-node ./node_modules/typeorm/cli.js migration:generate -n $name",
"db:migrate": "ts-node ./node_modules/typeorm/cli.js migration:run",
"seed:config": "ts-node ./node_modules/typeorm-seeding/dist/cli.js config",
"seed:run": "ts-node ./node_modules/typeorm-seeding/dist/cli.js seed"
"init:local": "npm run dropcreatedb && npm run migrate:dev",
"introspect": "prisma db pull && npm run generate",
"generate": "prisma generate",
"migrate:dev": "prisma migrate dev",
"migrate:deploy": "prisma migrate deploy",
"push": "prisma db push",
"dropcreatedb": "dropdb devbase --if-exists && createdb devbase",
"seed": "prisma db seed --preview-feature"
},
"author": "",
"license": "",
"dependencies": {
"class-validator": "^0.12.2",
"@prisma/client": "^2.27.0",
"dynamodb-toolbox": "^0.3.4",
"pg": "^8.6.0",
"ts-node": "^10.0.0",
"typeorm": "^0.2.34",
"typeorm-aurora-data-api-driver": "^2.1.1",
"typeorm-naming-strategies": "^2.0.0"
"uuid": "^8.3.2"
},
"devDependencies": {
"@types/faker": "^5.1.2",
"@types/node": "^14.11.8",
"@types/pg": "^7.14.5",
"faker": "^5.1.0",
"fishery": "^1.0.1"
"fishery": "^1.0.1",
"prisma": "^2.27.0"
}
}
18 changes: 18 additions & 0 deletions packages/repo/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
generator client {
provider = "prisma-client-js"

// RHEL is for amazon linux - useful for docker local dev and packaging
binaryTargets = ["native", "rhel-openssl-1.0.x"]
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}


model Tenant {
id String @id @default(dbgenerated("uuid_generate_v4()")) @db.Uuid
name String @unique
}

18 changes: 18 additions & 0 deletions packages/repo/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PrismaClient } from "@prisma/client"
import { tenantFactory } from "../src/factory/tenant"

const prisma = new PrismaClient()

export async function main() {
// create tenants
await prisma.tenant.createMany({ data: [...Array(5)].map(() => tenantFactory.build()) })
}

main()
.catch((e) => {
console.error(e)
process.exit(1)
})
.finally(async () => {
await prisma.$disconnect()
})
3 changes: 3 additions & 0 deletions packages/repo/src/client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { PrismaClient } from "@prisma/client"

export const prisma = new PrismaClient()
7 changes: 4 additions & 3 deletions packages/repo/src/factory/tenant.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import faker from "faker/locale/en_US"
import { Factory } from "fishery"
import { Tenant } from "../model/tenant"
import { v4 } from "uuid"

export const tenantFactory: Factory<Tenant> = Factory.define<any>(() => ({
name: faker.company.companyName(),
export const tenantFactory = Factory.define(() => ({
id: v4(),
name: faker.company.companyName() + " " + faker.company.companySuffix(),
}))
14 changes: 2 additions & 12 deletions packages/repo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
import { DatabaseManager } from "@jetkit/cdk"
import { Tenant } from "./model/tenant"
export { prisma } from "./client"

// DB models
export { Tenant } from "./model/tenant"

export { DatabaseManager }

const db = new DatabaseManager({
entities: [Tenant],
printQueries: true,
})
export { db }
export * from "./repository/tenant"
34 changes: 34 additions & 0 deletions packages/repo/src/model/dynamo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { Table } from "dynamodb-toolbox"
import { TableConstructor } from "dynamodb-toolbox/dist/classes/Table"
import { documentClient as DocumentClient, EnvVars } from "template-common"

/**
* Single DynamoDB table to hold our entire application.
* https://github.com/jeremydaly/dynamodb-toolbox#conventions-motivations-and-migrations-from-v01
* Good explanation: https://www.youtube.com/watch?v=BnDKD_Zv0og
*/
export const devBaseTableOptions: TableConstructor = {
// Specify table name (used by DynamoDB)
name: process.env[EnvVars.appTable] || "App",

partitionKey: "pk",
sortKey: "sk",

attributes: {
GSI1pk: "string",
GSI1sk: "string",
GSI2pk: "string",
GSI2sk: "string",
},

indexes: {
GSI1: { partitionKey: "GSI1pk", sortKey: "GSI1sk" },
// GSI2: { partitionKey: "test" },
// LSI1: { partitionKey: 'pk', sortKey: 'other_sk' },
// LSI2: { sortKey: 'data' }
},

DocumentClient,
}

export const DevBaseTable = new Table(devBaseTableOptions)
11 changes: 0 additions & 11 deletions packages/repo/src/model/tenant.ts

This file was deleted.

14 changes: 14 additions & 0 deletions packages/repo/src/singleton.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { PrismaClient } from "@prisma/client"
import { mockDeep, MockProxy, mockReset } from "jest-mock-extended"
import { prisma } from "./client"

jest.mock("./client", () => ({
__esModule: true,
prisma: mockDeep<PrismaClient>(),
}))

beforeEach(() => {
mockReset(prismaMock)
})

export const prismaMock = prisma as unknown as MockProxy<PrismaClient>
2 changes: 1 addition & 1 deletion tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"sourceMap": true /* Generates corresponding '.map' file. */,
"resolveJsonModule": true, // allow importing JSON files
"experimentalDecorators": true, // enable decorators
"emitDecoratorMetadata": true, // for runtime use of decorators (typeORM)
"emitDecoratorMetadata": true, // for runtime use of decorators
// "baseUrl": "packages", // allow absolute path imports for all project packages
"strictPropertyInitialization": false, // for TypeORM
// "noUnusedLocals": true /* Report errors on unused locals. */,
Expand Down