Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yamcodes committed Oct 18, 2023
2 parents e575969 + d6a64c6 commit 327a915
Show file tree
Hide file tree
Showing 21 changed files with 73 additions and 51 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [0.1.0](https://github.com/agnyz/elysia-realworld-example-app/compare/v0.0.1...v0.1.0) (2023-10-18)


### Features

* data faker ([#69](https://github.com/agnyz/elysia-realworld-example-app/issues/69)) ([528bf3d](https://github.com/agnyz/elysia-realworld-example-app/commit/528bf3d3f803f30abf9dfb18b8de9a0a610cc073))
* PR template ([581ba87](https://github.com/agnyz/elysia-realworld-example-app/commit/581ba8751434c6afd4c1f95102f5ef4e3d70e2c1)), closes [#39](https://github.com/agnyz/elysia-realworld-example-app/issues/39)

## 0.0.1 (2023-10-16)


Expand Down
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ You can run `bun lint --apply` to apply any safe fixes automatically.
Run the tests. We mostly using [Vitest](https://vitest.dev/) - a replacement of [Jest](https://jestjs.io/).
You can filter the tests to be run by `nr test [match]`, for example, `nr test foo` will only run test files that contain `foo`.
You can filter the tests to be run by `bun test [match]`, for example, `bun test foo` will only run test files that contain `foo`.
Config options are often under the `test` field of `vitest.config.ts` or `vite.config.ts`.
Vitest runs in [watch mode by default](https://vitest.dev/guide/features.html#watch-mode), so you can modify the code and see the test result automatically, which is great for [test-driven development](https://en.wikipedia.org/wiki/Test-driven_development). To run the test only once, you can do `nr test --run`.
Vitest runs in [watch mode by default](https://vitest.dev/guide/features.html#watch-mode), so you can modify the code and see the test result automatically, which is great for [test-driven development](https://en.wikipedia.org/wiki/Test-driven_development). To run the test only once, you can do `bun test --run`.
For some projects, we might have multiple types of tests set up. For example `nr test:unit` for unit tests, `nr test:e2e` for end-to-end tests. `nr test` commonly run them together, you can run them separately as needed.
For some projects, we might have multiple types of tests set up. For example `bun test:unit` for unit tests, `bun test:e2e` for end-to-end tests. `bun test` commonly run them together, you can run them separately as needed.
### `bun docs`
Expand Down Expand Up @@ -121,9 +121,9 @@ This section is for maintainers with write access, or if you want to maintain yo

Before you do, please make sure you have lastest git commit from upstream and all CI checks pass.

When ready to publish a new release, we run `nr release`. It prompts a list for the target version you want to release. After selecting the desired one, it bumps your `package.json` and commit the changes with git tags, powered by [`@antfi/bumpp`](https://github.com/antfu/bumpp).
When ready to publish a new release, we run `bun release`. It prompts a list for the target version you want to release. After selecting the desired one, it bumps your `package.json` and commit the changes with git tags, powered by [`@antfi/bumpp`](https://github.com/antfu/bumpp).

There are two kinds of publishing setups, both performed by `nr release`.
There are two kinds of publishing setups, both performed by `bun release`.

<table><tr><td>

Expand All @@ -136,7 +136,7 @@ In `package.json`, we usually have:
```json
{
"scripts": {
"prepublishOnly": "nr build"
"prepublishOnly": "bun build"
}
}
```
Expand Down
Binary file modified bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions db/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Config } from 'drizzle-kit';

export const dbCredentials = {
host: process.env.POSTGRES_HOST || '0.0.0.0',
port: parseInt(process.env.POSTGRES_PORT || '5432'),
Expand Down
2 changes: 1 addition & 1 deletion db/drop.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { db } from '@/database.providers';
import { users } from '@/users/users.schema';
import { users } from '@users/users.model';
import { getTableName } from 'drizzle-orm';
import { exit } from 'process';
// TODO: use react-ink
Expand Down
31 changes: 19 additions & 12 deletions db/seed.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { exit } from 'process';
import { db } from '@/database.providers';
import { users } from '@/users/users.model';
import { users } from '@users/users.model';
import { faker } from '@faker-js/faker';

const data = {
id: 1, //do not use 'users.id.default', seed should be idempotent
email: 'test@email.com',
username: 'test',
password: 'test',
bio: 'test',
image: 'test',
};
console.log('Upserting user: ', data);
await db.insert(users).values(data).onConflictDoNothing();
console.log('User upserted');
console.log('Truncating the user database');
await db.delete(users);
console.log('The database is empty: ', await db.select().from(users));

for (let i = 0; i < 10; i++) {
const data = {
id: faker.datatype.number(),
email: faker.internet.email(),
username: faker.internet.userName(),
password: await Bun.password.hash(faker.internet.password()),
bio: faker.lorem.text(),
image: faker.image.imageUrl(),
};
console.log('Upserting user:', data);

await db.insert(users).values(data);
console.log('User upserted');
}
const userResult = await db.select().from(users);
console.log('User result: ', userResult);

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "elysia-realworld-example-app",
"title": "ElysiaJS RealWorld",
"version": "0.0.1",
"version": "0.1.0",
"description": "A RealWorld example for an app in the backend created with Bun, Elysia, and DrizzleORM.",
"scripts": {
"start": "bun run src/main.ts",
Expand Down Expand Up @@ -32,6 +32,7 @@
},
"devDependencies": {
"@biomejs/biome": "1.2.2",
"@faker-js/faker": "^8.2.0",
"@types/node": "^20.8.3",
"bun-types": "latest",
"drizzle-kit": "^0.19.13",
Expand Down
6 changes: 3 additions & 3 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
AuthorizationError,
BadRequestError,
ERROR_CODE_STATUS_MAP,
} from '@/errors';
import { usersPlugin } from '@/users/users.plugin';
import { profilesPlugin } from '@/profiles/profiles.plugin';
} from '@errors';
import { usersPlugin } from '@users/users.plugin';
import { profilesPlugin } from '@profiles/profiles.plugin';

// the file name is in the spirit of NestJS, where app module is the device in charge of putting together all the pieces of the app
// see: https://docs.nestjs.com/modules
Expand Down
6 changes: 3 additions & 3 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as jose from 'jose';
import { env } from '@/config';
import { UserInDb } from '@/users/users.schema';
import { env } from '@config';
import { UserInDb } from '@users/users.schema';
import { Type } from '@sinclair/typebox';
import { Value } from '@sinclair/typebox/value';
import { AuthenticationError } from '@/errors';
import { AuthenticationError } from '@errors';

export class AuthService {
private readonly ALG = env.JWT_ALGORITHM;
Expand Down
2 changes: 1 addition & 1 deletion src/errors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEFAULT, MapWithDefault } from '@/utils/defaultmap';
import { DEFAULT, MapWithDefault } from '@utils/defaultmap';

export class AuthenticationError extends Error {
public status = 401;
Expand Down
8 changes: 4 additions & 4 deletions src/profiles/profiles.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { AuthService } from '@/auth/auth.service';
import { AuthService } from '@auth/auth.service';
import { db } from '@/database.providers';
import { ProfilesRepository } from '@/profiles/profiles.repository';
import { ProfilesService } from '@/profiles/profiles.service';
import { UsersRepository } from '@/users/users.repository';
import { ProfilesRepository } from '@profiles/profiles.repository';
import { ProfilesService } from '@profiles/profiles.service';
import { UsersRepository } from '@users/users.repository';
import { Elysia } from 'elysia';

export const setupProfiles = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/profiles/profiles.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Elysia } from 'elysia';
import { setupProfiles } from '@/profiles/profiles.module';
import { ReturnedProfileSchema } from '@/profiles/profiles.schema';
import { setupProfiles } from '@profiles/profiles.module';
import { ReturnedProfileSchema } from '@profiles/profiles.schema';

export const profilesPlugin = new Elysia().use(setupProfiles).group(
'/profiles/:username',
Expand Down
2 changes: 1 addition & 1 deletion src/profiles/profiles.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { eq, and, sql } from 'drizzle-orm';
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { users, userFollows } from '@/users/users.model';
import { users, userFollows } from '@users/users.model';

export class ProfilesRepository {
constructor(private readonly db: PostgresJsDatabase) {}
Expand Down
2 changes: 1 addition & 1 deletion src/profiles/profiles.schema.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Type } from '@sinclair/typebox';
import { User, insertUserSchemaRaw } from '@/users/users.schema';
import { User, insertUserSchemaRaw } from '@users/users.schema';

export type Profile = Omit<
User,
Expand Down
6 changes: 3 additions & 3 deletions src/profiles/profiles.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NotFoundError } from 'elysia';
import { ProfilesRepository } from '@/profiles/profiles.repository';
import { Profile } from '@/profiles/profiles.schema';
import { UsersRepository } from '@/users/users.repository';
import { ProfilesRepository } from '@profiles/profiles.repository';
import { Profile } from '@profiles/profiles.schema';
import { UsersRepository } from '@users/users.repository';

export class ProfilesService {
constructor(
Expand Down
6 changes: 3 additions & 3 deletions src/users/users.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AuthService } from '@/auth/auth.service';
import { AuthService } from '@auth/auth.service';
import { db } from '@/database.providers';
import { UsersRepository } from '@/users/users.repository';
import { UsersService } from '@/users/users.service';
import { UsersRepository } from '@users/users.repository';
import { UsersService } from '@users/users.service';
import { Elysia } from 'elysia';

export const setupUsers = () => {
Expand Down
4 changes: 2 additions & 2 deletions src/users/users.plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Elysia } from 'elysia';
import { setupUsers } from '@/users/users.module';
import { setupUsers } from '@users/users.module';
import {
InsertUserSchema,
ReturnedUserSchema,
UserLoginSchema,
UpdateUserSchema,
} from '@/users/users.schema';
} from '@users/users.schema';

export const usersPlugin = new Elysia()
.use(setupUsers)
Expand Down
4 changes: 2 additions & 2 deletions src/users/users.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// in charge of database interactions
import { PostgresJsDatabase } from 'drizzle-orm/postgres-js';
import { eq } from 'drizzle-orm';
import { UserToCreate, UserToUpdate } from '@/users/users.schema';
import { users } from '@/users/users.model';
import { UserToCreate, UserToUpdate } from '@users/users.schema';
import { users } from '@users/users.model';

export class UsersRepository {
constructor(private readonly db: PostgresJsDatabase) {}
Expand Down
2 changes: 1 addition & 1 deletion src/users/users.schema.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Type, Static } from '@sinclair/typebox';
import { createInsertSchema, createSelectSchema } from 'drizzle-typebox';
// Do not use path aliases here (i.e. '@/users/users.model'), as that doesn't work with Drizzle Studio
import { users } from './users.model';
import { users } from '@users/users.model';

// Schema for inserting a user - can be used to validate API requests
export const insertUserSchemaRaw = createInsertSchema(users);
Expand Down
8 changes: 4 additions & 4 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// users.service.ts
// in charge of business logic - generate slug, fetch data from other services, cache something, etc.
import { NotFoundError } from 'elysia';
import { UsersRepository } from '@/users/users.repository';
import { AuthService } from '@/auth/auth.service';
import { UserInDb, UserToCreate, UserToUpdate } from '@/users/users.schema';
import { AuthenticationError, BadRequestError } from '@/errors';
import { UsersRepository } from '@users/users.repository';
import { AuthService } from '@auth/auth.service';
import { UserInDb, UserToCreate, UserToUpdate } from '@users/users.schema';
import { AuthenticationError, BadRequestError } from '@errors';

export class UsersService {
constructor(
Expand Down
7 changes: 6 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,12 @@
/* Specify a set of entries that re-map imports to additional lookup locations. */
"@db/*": ["./db/*"],
"@/*": ["./src/*"],
"@users/*": ["./src/users/*"]
"@users/*": ["./src/users/*"],
"@auth/*": ["./src/auth/*"],
"@profiles/*": ["./src/profiles/*"],
"@utils/*": ["./src/utils/*"],
"@errors": ["./src/errors.ts"],
"@config": ["./src/config.ts"]
},
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
// "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
Expand Down

0 comments on commit 327a915

Please sign in to comment.