Skip to content

Commit

Permalink
Merge pull request #821 from undb-xyz/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
nichenqin committed Apr 8, 2023
2 parents 0a79290 + b89a6a6 commit 887cbe2
Show file tree
Hide file tree
Showing 787 changed files with 10,994 additions and 4,239 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ node_modules
*.log
docker
out
.ego
.undb
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
root: true,
// This tells ESLint to load the config from the package `@egodb/eslint-config`
extends: ['@egodb/eslint-config'],
// This tells ESLint to load the config from the package `@undb/eslint-config`
extends: ['@undb/eslint-config'],
settings: {
next: {
rootDir: ['apps/frontend/'],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/build-and-push-to-registry.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
images: |
ghcr.io/${{ github.repository }}
- name: Build egodb and push
- name: Build undb and push
uses: docker/build-push-action@v4
with:
push: true
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ yarn-error.log*

.pnpm-store/

.ego/
.undb/
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
{
"previewLimit": 50,
"driver": "SQLite",
"name": "ego",
"database": "${workspaceFolder:egodb}/.ego/data/ego.development.sqlite"
"name": "undb",
"database": "${workspaceFolder:undb}/.undb/data/undb.development.sqlite"
}
],
"sqltools.useNodeRuntime": true
Expand Down
22 changes: 11 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# builder
FROM node:18-bullseye-slim as builder

WORKDIR /egodb
WORKDIR /undb

RUN npm install -g turbo
COPY . .
RUN turbo prune --scope=@egodb/backend --scope=@egodb/frontend
RUN turbo prune --scope=@undb/backend --scope=@undb/frontend

# installer
FROM node:18-bullseye AS installer

RUN npm install -g pnpm

WORKDIR /egodb
WORKDIR /undb

COPY --from=builder /egodb/out/pnpm-lock.yaml ./pnpm-lock.yaml
COPY --from=builder /undb/out/pnpm-lock.yaml ./pnpm-lock.yaml
RUN pnpm fetch

COPY --from=builder /egodb/out/ .
COPY --from=builder /undb/out/ .

RUN pnpm install -r --offline

Expand All @@ -28,14 +28,14 @@ RUN pnpm prune --prod --config.ignore-scripts=true
# runner
FROM gcr.io/distroless/nodejs18-debian11 as runner

WORKDIR /egodb
WORKDIR /undb

ENV NODE_ENV production
ENV EGODB_DATABASE_SQLITE_DATA /var/opt/.ego
ENV UNDB_DATABASE_SQLITE_DATA /var/opt/.undb

COPY --from=installer /egodb/node_modules ./node_modules
COPY --from=installer /egodb/packages ./packages
COPY --from=installer /egodb/apps/backend ./apps/backend
COPY --from=installer /egodb/apps/frontend/dist ./out
COPY --from=installer /undb/node_modules ./node_modules
COPY --from=installer /undb/packages ./packages
COPY --from=installer /undb/apps/backend ./apps/backend
COPY --from=installer /undb/apps/frontend/dist ./out

CMD ["apps/backend/dist/main.js"]
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# egodb
# undb

<img height="50px" src="./docs/logo.png" alt="egodb" align="right" />
<img height="50px" src="./docs/logo.png" alt="undb" align="right" />

Private first, self-hosted no code database.
Private first, unified, self-hosted no code database.

<a href="https://demo.egodb.io/">Live Demo</a>
<a href="https://demo.undb.xyz/">Live Demo</a>

![ego](./docs/ego.png)
> use email `test@example.com` and password `123456` to login
![undb](./docs/undb.png)

## Features

Expand All @@ -22,19 +24,19 @@ Private first, self-hosted no code database.
### Deploy with docker

```
docker run -d --name egodb -p 4000:4000 --platform linux/x86_64 -v ~/.egodb/:/var/opts/.ego ghcr.io/ego-io/egodb:latest
docker run -d --name undb -p 4000:4000 --platform linux/x86_64 -v ~/.undb/:/var/opts/.undb ghcr.io/undb-xyz/undb:latest
```

> And then you can visit http://localhost:4000 and get started
### Deploy with Render.com

<a href="https://render.com/deploy?repo=https://github.com/ego-io/egodb">
<a href="https://render.com/deploy?repo=https://github.com/undb-xyz/undb">
<img src="https://render.com/images/deploy-to-render-button.svg" alt="Deploy to Render">
</a>

> You'll need a render account to deploy your own egodb instance
> You'll need a render account to deploy your own undb instance
## License

egodb is open-source under the GNU Affero General Public License Version 3 (AGPLv3). You can find it [here](./LICENSE).
undb is open-source under the GNU Affero General Public License Version 3 (AGPLv3). You can find it [here](./LICENSE).
4 changes: 2 additions & 2 deletions apps/backend/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module.exports = {
root: true,
extends: ["@egodb/eslint-config"],
extends: ['@undb/eslint-config'],
parserOptions: {
emitDecoratorMetadata: true,
},
};
}
24 changes: 17 additions & 7 deletions apps/backend/package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "@egodb/backend",
"name": "@undb/backend",
"version": "0.0.0",
"dependencies": {
"@egodb/core": "^0.0.0",
"@egodb/cqrs": "^0.0.0",
"@egodb/domain": "^0.0.0",
"@egodb/sqlite": "^0.0.0",
"@egodb/trpc": "^0.0.0",
"@undb/core": "^0.0.0",
"@undb/cqrs": "^0.0.0",
"@undb/domain": "^0.0.0",
"@undb/sqlite": "^0.0.0",
"@undb/trpc": "^0.0.0",
"@mikro-orm/better-sqlite": "^5.6.15",
"@mikro-orm/core": "^5.6.15",
"@mikro-orm/migrations": "^5.6.15",
Expand All @@ -15,10 +15,13 @@
"@nestjs/config": "^2.3.1",
"@nestjs/core": "^9.3.12",
"@nestjs/cqrs": "^9.0.3",
"@nestjs/jwt": "^10.0.3",
"@nestjs/passport": "^9.0.3",
"@nestjs/platform-express": "^9.3.12",
"@nestjs/serve-static": "^3.0.1",
"@nestjs/terminus": "^9.2.2",
"@trpc/server": "^10.18.0",
"bcrypt": "^5.1.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"compression": "^1.7.4",
Expand All @@ -28,6 +31,9 @@
"nestjs-cls": "^3.3.1",
"nestjs-pino": "^3.1.2",
"oxide.ts": "^1.1.0",
"passport": "^0.6.0",
"passport-jwt": "^4.0.1",
"passport-local": "^1.0.0",
"pino-http": "^8.3.3",
"pino-pretty": "^10.0.0",
"reflect-metadata": "^0.1.13",
Expand All @@ -36,17 +42,21 @@
"uuid": "^9.0.0"
},
"devDependencies": {
"@egodb/eslint-config": "^0.0.0",
"@undb/eslint-config": "^0.0.0",
"@nestjs/cli": "^9.3.0",
"@nestjs/schematics": "^9.1.0",
"@nestjs/testing": "^9.3.12",
"@swc/cli": "^0.1.62",
"@swc/core": "^1.3.44",
"@types/bcrypt": "^5.0.0",
"@types/compression": "^1.7.2",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.0",
"@types/multer": "^1.4.7",
"@types/node": "^18.15.11",
"@types/passport": "^1.0.12",
"@types/passport-jwt": "^3.0.8",
"@types/passport-local": "^1.0.35",
"@types/supertest": "^2.0.12",
"@types/uuid": "^9.0.1",
"eslint": "^8.37.0",
Expand Down
10 changes: 7 additions & 3 deletions apps/backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { createConfig } from '@egodb/sqlite'
import { MikroORM } from '@mikro-orm/core'
import { MikroOrmModule } from '@mikro-orm/nestjs'
import type { OnModuleInit } from '@nestjs/common'
import { Module } from '@nestjs/common'
import { ConfigType } from '@nestjs/config'
import { ServeStaticModule } from '@nestjs/serve-static'
import { createConfig } from '@undb/sqlite'
import { ClsModule } from 'nestjs-cls'
import { LoggerModule } from 'nestjs-pino'
import path from 'path'
import { AttachmentModule } from './attachment/attachment.module.js'
import { AuthModule } from './auth/auth.module.js'
import { BaseConfigService } from './configs/base-config.service.js'
import { ConfigModule } from './configs/config.module.js'
import { sqliteConfig } from './configs/sqlite.js'
import { sqliteConfig } from './configs/sqlite.config.js'
import { HealthModule } from './health/health.module.js'
import { modules } from './modules/index.js'
import { UserService } from './modules/user/user.service.js'
import { TrpcModule } from './trpc/trpc.module.js'

@Module({
Expand Down Expand Up @@ -42,12 +44,14 @@ import { TrpcModule } from './trpc/trpc.module.js'
ServeStaticModule.forRoot({
rootPath: path.resolve(process.cwd(), './out'),
}),
AuthModule,
],
})
export class AppModule implements OnModuleInit {
constructor(private readonly orm: MikroORM) {}
constructor(private readonly orm: MikroORM, private readonly userService: UserService) {}

async onModuleInit() {
await this.orm.getMigrator().up()
await this.userService.createAdmin()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { ConfigType } from '@nestjs/config'
import fs from 'node:fs'
import path from 'node:path'
import { v4 } from 'uuid'
import { InjectObjectStorageConfig, objectStorageConfig } from '../../configs/object-storage.js'
import { InjectObjectStorageConfig, objectStorageConfig } from '../../configs/object-storage.config.js'
import { IObjectStorage } from './object-storage.js'

@Injectable()
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/attachment/attachment.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from '@nestjs/common'
import { ConfigType } from '@nestjs/config'
import { ServeStaticModule } from '@nestjs/serve-static'
import { objectStorageConfig } from '../configs/object-storage.js'
import { objectStorageConfig } from '../configs/object-storage.config.js'
import { objectStorage } from './adapters/provider.js'
import { AttachmentController } from './attachment.controller.js'
import { AttachmentService } from './attachment.service.js'
Expand Down
31 changes: 31 additions & 0 deletions apps/backend/src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Body, Controller, Get, HttpCode, HttpStatus, Post, Request, UseGuards } from '@nestjs/common'
import { JwtService } from '@nestjs/jwt'
import { AuthService } from './auth.service.js'
import { JwtAuthGuard } from './jwt-auth.guard.js'
import { LocalAuthGuard } from './local-auth.guard.js'

@Controller('auth')
export class AuthController {
constructor(private authService: AuthService, private jwtService: JwtService) {}

@HttpCode(HttpStatus.OK)
@Post('register')
async register(@Body() body: { password: string; email: string }) {
return this.authService.register(body.email, body.password)
}

@HttpCode(HttpStatus.OK)
@UseGuards(LocalAuthGuard)
@Post('login')
async login(@Request() req: Express.Request) {
const payload = await this.authService.login(req.user as any)
return { access_token: this.jwtService.sign(payload) }
}

@HttpCode(HttpStatus.OK)
@UseGuards(JwtAuthGuard)
@Get('me')
getProfile(@Request() req: Express.Request) {
return this.authService.me(req.user as any)
}
}
36 changes: 36 additions & 0 deletions apps/backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Module } from '@nestjs/common'
import { ConfigType } from '@nestjs/config'
import { CqrsModule } from '@nestjs/cqrs'
import { JwtModule } from '@nestjs/jwt'
import { PassportModule } from '@nestjs/passport'
import { authConfig } from '../configs/auth.config.js'
import { UserModule } from '../modules/user/user.module.js'
import { AuthController } from './auth.controller.js'
import { AuthService } from './auth.service.js'
import { NestLgoinCommandHandler, NestRegisterCommandHandler } from './commands/index.js'
import { JwtStrategy } from './jwt.strategy.js'
import { LocalStrategy } from './local.strategy.js'
import { NestGetMeQueryHandler } from './queries/index.js'

const CommandHandlers = [NestLgoinCommandHandler, NestRegisterCommandHandler]
const QueryHandlers = [NestGetMeQueryHandler]

@Module({
imports: [
CqrsModule,
UserModule,
PassportModule,
JwtModule.registerAsync({
useFactory: (config: ConfigType<typeof authConfig>) => ({
secret: config.jwt.secret,
signOptions: {
expiresIn: '20d',
},
}),
inject: [authConfig.KEY],
}),
],
providers: [AuthService, LocalStrategy, JwtStrategy, ...CommandHandlers, ...QueryHandlers],
controllers: [AuthController],
})
export class AuthModule {}
37 changes: 37 additions & 0 deletions apps/backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Injectable } from '@nestjs/common'
import { CommandBus, QueryBus } from '@nestjs/cqrs'
import { IQueryUser, WithUserEmail } from '@undb/core'
import { GetMeQuery, LoginCommand, RegisterCommand } from '@undb/cqrs'
import * as bcrypt from 'bcrypt'
import { UserService } from '../modules/user/user.service.js'

@Injectable()
export class AuthService {
constructor(
private usersService: UserService,
private readonly commandBus: CommandBus,
private readonly queryBus: QueryBus,
) {}

async validateUser(email: string, pass: string): Promise<IQueryUser | null> {
const user = await this.usersService.findOne(WithUserEmail.fromString(email))
if (!user) return null

const isPasswordMatch = await bcrypt.compare(pass, user.password)
if (isPasswordMatch) return user.toQuery()
return null
}

async register(email: string, password: string) {
const hashedPassword = await bcrypt.hash(password, 10)
return this.commandBus.execute(new RegisterCommand({ email, password: hashedPassword }))
}

async login(user: IQueryUser) {
return this.commandBus.execute(new LoginCommand({ user }))
}

async me(user: IQueryUser) {
return this.queryBus.execute(new GetMeQuery({ me: user }))
}
}
2 changes: 2 additions & 0 deletions apps/backend/src/auth/commands/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './login.command.handler.js'
export * from './register.command.handler.js'
5 changes: 5 additions & 0 deletions apps/backend/src/auth/commands/login.command.handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { CommandHandler } from '@nestjs/cqrs'
import { LoginCommand, LoginCommandHandler } from '@undb/cqrs'

@CommandHandler(LoginCommand)
export class NestLgoinCommandHandler extends LoginCommandHandler {}
Loading

0 comments on commit 887cbe2

Please sign in to comment.