Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/oskang09/fastify-typescript-boilerplate
https://github.com/oskang09/fastify-typescript-boilerplate
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/oskang09/fastify-typescript-boilerplate
- Owner: Oskang09
- Created: 2020-02-03T17:22:02.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-22T14:36:51.000Z (about 2 years ago)
- Last Synced: 2023-03-02T23:22:38.413Z (almost 2 years ago)
- Language: TypeScript
- Size: 354 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 11
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# Tech Stack
* Typescript
# Libraries
* Fastify
* Firebase ( will remove later )
* Sequelize# Explanation
By default plugin folder will have all the tools.
* `endpoint.ts` included all endpoints reading, type interface and annotation definition
* `sequelize.ts` included all model reading, type interface, repository initialization and annotation definition# Model Definition
Will follow `sequelize-typescript` structure.
```ts
import { Model, Table, Column, DataType, CreatedAt, UpdatedAt } from 'sequelize-typescript';@Table({
modelName: 'user',
tableName: 'users',
timestamps: true,
freezeTableName: true,
schema: 'ordorex'
})
class User extends Model {@Column({
type: DataType.INTEGER,
primaryKey: true,
autoIncrement: true,
})
id: number@Column({
type: DataType.STRING,
unique: true,
})
uid: string@Column(DataType.STRING)
name: string@Column({
type: DataType.STRING,
unique: true,
})
email: string@Column(DataType.STRING)
password: string@Column(DataType.STRING)
fcm_token: string@Column(DataType.JSONB)
permissions: string[]@CreatedAt
createdAt: Date@UpdatedAt
updatedAt: DategenerateJwtToken(): object {
return { id: this.id };
}
}export default User;
```# Example Endpoint
`@IRepository(sequelizeModel)` will receive repository instance when construct work like dependency injection. `@Endpoint` will have all definition about endpoint, following `fastify-schema`. `data-mapper` is a extra tool to map data to other layer easy to access supported dot notation definition etc `user.token`.
```ts
import { Repository } from "sequelize-typescript";
import * as jwt from 'jsonwebtoken';
import { Endpoint, IRepository, EndpointAPI, Request, Response } from "../plugin/endpoint";
import User from '../model/user';class UserAPI extends EndpointAPI {
@IRepository(User)
userRepository: Repository@Endpoint({
method: 'PUT',
path: '/user/profile',
})
async update_profile(req: Request, res: Response): Promise {}
@Endpoint({
method: 'POST',
path: "/user/login",
schema: {
body: {
type: 'object',
required: ['email', 'password'],
properties: {
id_token: { type: 'string' },
fcm_token: { type: 'string' },
}
}
},
dataMapper: {
id_token: "body",
fcm_token: "body",
},
})
async login(req: Request, res: Response): Promise {
const { id_token, fcm_token } = req.data;
const decoded = await this.authentication.verifyIdToken(id_token, true);
let user: User = await this.userRepository.findOne({
where: { uid: decoded.uid },
});if (!user) {
throw Error("")
}user = user.update("fcm_token", fcm_token);
const token = await jwt.sign(user.generateJwtToken(), this.jwt_secret)
return { user, token };
}
};export default UserAPI;
```