Skip to content

Commit

Permalink
feat: add koa-jsonapi-zod and remove unneccessary boilerplate code
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed Feb 12, 2024
1 parent c287b69 commit ea3238c
Show file tree
Hide file tree
Showing 18 changed files with 128 additions and 241 deletions.
28 changes: 27 additions & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,34 @@
"rules": {
"recommended": true,
"nursery": {
"noEmptyTypeParameters": "error",
"noInvalidUseBeforeDeclaration": "error",
"noUnusedImports": "error",
"noUnusedPrivateClassMembers": "error",
"noUselessLoneBlockStatements": "error",
"noUselessTernary": "error",
"useExportType": "error",
"useImportType": "error",
"noUnusedImports": "error"
"useForOf": "error",
"useGroupedTypeImport": "error"
},
"complexity": {
"noExcessiveCognitiveComplexity": "warn",
"useSimplifiedLogicExpression": "error"
},
"correctness": {
"noNewSymbol": "error"
},
"style": {
"useBlockStatements": "error",
"useCollapsedElseIf": "error",
"useShorthandArrayType": "error",
"useShorthandAssign": "error",
"useSingleCaseStatement": "error"
},
"suspicious": {
"noApproximativeNumericConstant": "warn",
"noConsoleLog": "error"
}
}
},
Expand Down
28 changes: 27 additions & 1 deletion skeleton/base/biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,34 @@
"rules": {
"recommended": true,
"nursery": {
"noEmptyTypeParameters": "error",
"noInvalidUseBeforeDeclaration": "error",
"noUnusedImports": "error",
"noUnusedPrivateClassMembers": "error",
"noUselessLoneBlockStatements": "error",
"noUselessTernary": "error",
"useExportType": "error",
"useImportType": "error",
"noUnusedImports": "error"
"useForOf": "error",
"useGroupedTypeImport": "error"
},
"complexity": {
"noExcessiveCognitiveComplexity": "warn",
"useSimplifiedLogicExpression": "error"
},
"correctness": {
"noNewSymbol": "error"
},
"style": {
"useBlockStatements": "error",
"useCollapsedElseIf": "error",
"useShorthandArrayType": "error",
"useShorthandAssign": "error",
"useSingleCaseStatement": "error"
},
"suspicious": {
"noApproximativeNumericConstant": "warn",
"noConsoleLog": "error"
}
}
},
Expand Down
40 changes: 0 additions & 40 deletions skeleton/base/src/route/hello-world/foo.ts

This file was deleted.

7 changes: 0 additions & 7 deletions skeleton/base/src/route/hello-world/index.ts

This file was deleted.

5 changes: 1 addition & 4 deletions skeleton/base/src/route/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import type Router from "koa-tree-router";
import { registerHelloWorldRoutes } from "./hello-world/index.js";

export const registerRoutes = (router: Router): void => {
registerHelloWorldRoutes(router);
};
export const registerRoutes = (router: Router): void => {};
22 changes: 0 additions & 22 deletions skeleton/base/src/util/json-api.ts

This file was deleted.

69 changes: 0 additions & 69 deletions skeleton/base/src/util/zod.ts

This file was deleted.

15 changes: 0 additions & 15 deletions skeleton/features/postgres/base/src/entity/World.ts

This file was deleted.

3 changes: 3 additions & 0 deletions skeleton/features/postgres/base/src/mikro-orm.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ export default (async (): Promise<Options> => {
pathTs: "./src/migration",
generator,
},
discovery: {
warnWhenNoEntities: false,
},
loadStrategy: LoadStrategy.JOINED,
...postgresConfig,
});
Expand Down
18 changes: 17 additions & 1 deletion skeleton/features/postgres/base/src/util/mikro-orm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { RequestContext } from "@mikro-orm/core";
import { type QueryOrderMap, RequestContext } from "@mikro-orm/core";
import { MikroORM } from "@mikro-orm/postgresql";
import fnv1a from "@sindresorhus/fnv1a";
import { unflatten } from "flat";
import type { Next, ParameterizedContext } from "koa";
import type { Sort } from "koa-jsonapi-zod";

export const orm = await MikroORM.init();
export const em = orm.em;
Expand All @@ -22,3 +24,17 @@ await em.transactional(async (em) => {

export const requestContextMiddleware = async (_context: ParameterizedContext, next: Next) =>
RequestContext.create(em, next);

export const orderByFromJsonApiSort = <TSort extends string, TEntity>(
sort: Sort<TSort> | undefined,
): QueryOrderMap<TEntity>[] | undefined => {
if (!sort) {
return undefined;
}

return sort.map((field) =>
unflatten({
[field.field]: field.order,
}),
);
};
3 changes: 2 additions & 1 deletion skeleton/features/postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"@mikro-orm/core": "^6.0.0",
"@mikro-orm/postgresql": "^6.0.0",
"@mikro-orm/migrations": "^6.0.0",
"@sindresorhus/fnv1a": "^3.1.0"
"@sindresorhus/fnv1a": "^3.1.0",
"flat": "^6.0.1"
},
"scripts": {
"mikro-orm": "tsx ./node_modules/@mikro-orm/cli/esm"
Expand Down
1 change: 1 addition & 0 deletions skeleton/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"koa-bodyparser": "^4.3.0",
"koa-cache-control": "^2.0.0",
"koa-compress": "^5.0.1",
"koa-jsonapi-zod": "^1.1.0",
"koa-tree-router": "^0.12.1",
"source-map-support": "^0.5.21",
"winston": "^3.6.0",
Expand Down
2 changes: 2 additions & 0 deletions skeleton/templates/README.md.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Koa API following [JSON:API](https://jsonapi.org/) 1.1 specification.

See [koa-jsonapi-zod](/~https://github.com/DASPRiD/koa-jsonapi-zod) examples for integration.

## Setup

- `pnpm install`
Expand Down
13 changes: 4 additions & 9 deletions skeleton/templates/cdk/src/api-stack.ts.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
InstanceType,
IpAddresses,
IpProtocol,
Port,
SubnetType,
Vpc,
} from "aws-cdk-lib/aws-ec2";
Expand Down Expand Up @@ -75,12 +76,6 @@ export class ApiStack extends Stack {
if (!databaseCluster.secret) {
throw new Error("Database cluster has nio secret attached");
}

const databaseProxy = databaseCluster.addProxy("DatabaseProxy", {
vpc,
secrets: [databaseCluster.secret],
clientPasswordAuthType: ClientPasswordAuthType.POSTGRES_SCRAM_SHA_256,
});
{{/if}}
{{#if (has features "appconfig")}}

Expand Down Expand Up @@ -138,8 +133,8 @@ export class ApiStack extends Stack {
environment: {
PORT: "80",
{{#if (has features "postgres")}}
POSTGRES_HOSTNAME: databaseProxy.endpoint,
POSTGRES_PORT: "5432",
POSTGRES_HOSTNAME: databaseCluster.clusterEndpoint.hostname,
POSTGRES_PORT: databaseCluster.clusterEndpoint.port.toString(),
POSTGRES_SECRET: databaseCluster.secret.secretArn,
PGSSLMODE: "require",
{{/if}}
Expand All @@ -161,7 +156,7 @@ export class ApiStack extends Stack {
});

{{#if (has features "postgres")}}
albService.service.connections.allowTo(databaseProxy.connections, Port.tcp(5432));
albService.service.connections.allowToDefaultPort(databaseCluster.connections);
databaseCluster.secret.grantRead(albService.taskDefinition.taskRole);
{{/if}}
{{#if (has features "appconfig")}}
Expand Down
Loading

0 comments on commit ea3238c

Please sign in to comment.