0.9.2 • Published 2 months ago

ngulf v0.9.2

Weekly downloads
-
License
MIT
Repository
github
Last release
2 months ago

ngulf

Based on the Fastify webframework. Integrate typeorm, ioredis, and class-validator

Quick Start

npx ngulf init

Controller

// src/controller/DemoController.ts
import {Controller} from "ngulf";

@Controller("/demo")
export default class DemoController {

  @Get("/hello")
  async hello() {
    return "hello ngulf";
  }
}

Listen

// src/app.ts
import Ngulf from "ngulf";
import * as path from "path"

const app = Ngulf.create({
  routePrefix: "/api",
  controllers: path.join(__dirname, "controller"),
});
app.listen({ port: 3737 }).then(() => {
  console.log("Ngulf listen on 3737");
});

Request http://localhost:3737/api/demo/hello

Params

// src/controller/DemoController.ts
import {Controller} from "ngulf";

@Controller("/demo")
export default class DemoController {

  @Get("/hello")
  async hello() {
    return "hello ngulf";
  }

  @Get("/query")
  async query(@Query("name") name: string) {
    return `hello ${name}`;
  }

  @Post("/body")
  async body(@Body() data:any) {
    console.log(data)
    return data
  }

  @Post("/header")
  async header(@Headers("user-agent") userAgent?:string) {
    return userAgent
  }
}

Zod

import { z } from "zod";

export const ZodUser = z.object({
  username: z.string(),
  password: z
    .string({ required_error: "password is Required" })
    .nonempty("password is empty"),
  email: z.string().email().nullish(),
});

export type AddZodUser = z.infer<typeof ZodUser>;

Controller

@Post("/api")
  async testZod(@Body(ZodUser) data: AddZodUser) {
    console.log(data);
    return data;
  }

Validator

// src/dto/UserDto.ts
import { IsNotEmpty } from "ngulf/class-validator";

export default class UserDto {
  id?:string

  @IsNotEmpty({ groups: ["login", "add"] })
  name!: string;

  @IsNotEmpty({ groups: ["login"] })
  password!: string;
}

Controller

// src/controller/DemoController.ts
import {Controller} from "ngulf";

@Controller("/demo")
export default class DemoController {

  @Post("/login")
  async login(@Body(new Validation({ groups: ["login"] })) dto: UserDto){
    if(dto.name === "admin" && dto.password === "123456"){
      return true;
    }
    return false
  }

  @Post("/add")
  async add(@Body(new Validation({ groups: ["add"] })) dto: UserDto){
    // add user...
    return true
  }
}

ORM

Add configuration

// src/app.ts
import Ngulf from "ngulf";
import * as path from "path"

const app = Ngulf.create({
  routePrefix: "/api",
  controllers: path.join(__dirname, "controller"),
  orm: {
      type: "mysql",
      port: 3306,
      host: "localhost",
      username: "root",
      password: "",
      database: "test",
      entityPrefix: "ng_",
      entities: [path.join(__dirname, "entity/*{.ts,.js}")],
      // The production environment must be false, otherwise data may be lost
      synchronize: true, 
    },
});
app.listen({ port: 3737 }).then(() => {
  console.log("Ngulf listen on 3737");
});

Create entity

// src/entity/UserEntity.ts
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
} from "ngulf/typeorm";

@Entity({ name: "user" })
export default class UserEntity {
  @PrimaryGeneratedColumn("uuid")
  id!: string;

  @Column()
  name!: string;

  @Column()
  password!: string;
}

Create servcie

// src/service/UserService.ts
import { Injectable, OrmModel, OrmModelType } from "ngulf";
import UserEntity from "../entity/UserEntity";

@Injectable()
export default class UserService {
  @OrmModel(UserEntity)
  private model!: OrmModelType<UserEntity>;

  async login(name: string, password:string) {
    return await this.model.findOneBy({ name, password });
  }

  async add(name: string, password?:string) {
    const data = this.model.create();
    data.name = name;
    data.password = password || "123456";
    return await this.model.save(data);
  }
}

Use servcie

// src/controller/DemoController.ts
import {Controller} from "ngulf";

@Controller("/demo")
export default class DemoController {

  constructor(private readonly service: UserService){}

  @Post("/login")
  async login(@Body(new Validation({ groups: ["login"] })) dto: UserDto){
    const rel = await this.service.login(dto.name, dto.password!)
    return rel ? "Login success" : "Login fail"
  }

  @Post("/add")
  async add(@Body(new Validation({ groups: ["add"] })) dto: UserDto){
    return this.service.add(dto.name, dto.password)
  }
}

Demo

https://github.com/drinkjs/ngulf-demo

0.9.2

2 months ago

0.9.1

4 months ago

0.9.0

4 months ago

0.8.2

4 months ago

0.8.1

4 months ago

0.8.0

4 months ago

0.7.4

7 months ago

0.7.3

7 months ago

0.7.9

6 months ago

0.7.6

7 months ago

0.7.5

7 months ago

0.7.8

6 months ago

0.7.7

7 months ago

0.5.10

9 months ago

0.6.7

8 months ago

0.6.6

9 months ago

0.6.9

8 months ago

0.6.8

8 months ago

0.7.2

8 months ago

0.7.1

8 months ago

0.5.6

9 months ago

0.7.0

8 months ago

0.5.8

9 months ago

0.5.7

9 months ago

0.5.9

9 months ago

0.6.3

9 months ago

0.6.2

9 months ago

0.6.5

9 months ago

0.6.4

9 months ago

0.6.1

9 months ago

0.6.0

9 months ago

0.5.4

10 months ago

0.5.3

10 months ago

0.5.0

10 months ago

0.3.2

10 months ago

0.5.2

10 months ago

0.5.1

10 months ago

0.3.3

10 months ago

0.4.0

10 months ago

0.3.0

1 year ago

0.3.1

1 year ago

0.2.1

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.1.0

2 years ago

0.1.2

2 years ago

0.2.0

2 years ago

0.1.1

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.9

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.0.24

2 years ago

0.0.23

2 years ago

0.0.22

2 years ago

0.0.21

2 years ago

0.0.20

2 years ago

0.0.19

2 years ago

0.0.18

2 years ago

0.0.17

2 years ago

0.0.16

2 years ago

0.0.15

2 years ago

0.0.14

2 years ago

0.0.12

2 years ago

0.0.11

2 years ago

0.0.10

2 years ago

0.0.9

2 years ago

0.0.8

2 years ago

0.0.7

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago