Code Monkey home page Code Monkey logo

superxpress's Introduction

Welcome to Super Xpress

This template provide you a ready to use express REST APIs and Graphql.

Features

  • Graphql
  • Rest APIs
  • TypeOrm
  • JWT Authentications
  • Joi Validator
  • CLI
  • Basic Middleware (logger, cors, jwt, file uploads )
  • Lodash for basic developer function, and basic Cryptography function

Instalations

Steps to run this project:

  1. run git clone https://github.com/hikaaam/SuperXpress.git
  2. Run yarn install command
  3. Setup database settings inside ormconfig.json file
  4. Copy .envexample to .env
  5. Run yarn start command

CLI

Cli provided for you to generate controller, entity, validator, and router

yarn run make

This command will show you an options what you want to generate

If you generate Entity, the GraphqlResolver will also generated but you need to register your resolver on the GraphqlRouter/index.ts

import YourResolver from "./YourResolver";
/* ... */
export default buildSchema({
  resolvers: [
    UserResolver,
    YourSesolver, //put it here
  ],
  /* ... */
});

and if you generate a Router you also need to register on Route/intex/ts

const yourRouter: Router = require("./Your");
/*...*/
router.use("/", yourRouter);

Entity

Entity is defined database model that automatically synced everytime you made a change

Setting up Entity

Example of entity

import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  BaseEntity,
} from "typeorm";
import { ObjectType, Field, Int } from 'type-graphql';

@Entity("Users")
@ObjectType()
export class User extends BaseEntity {

@PrimaryGeneratedColumn() //your column type (a primary key id)
@Field(() => Int) //graphql field return as integer
  id: number; //your column name
}

@Column({ //to register it as a column in the database
  length: 100,
})
@Field() //graphql field return as a string (default is a string)
  name: string; //your column name

}

Entity('name')

This one is what your table name gonna be

ObjectType()

This one is a graphql function to translate your typeorm schema into readable graphql schema

Column()

To define a column in the database and make typeorm understand that this is a column

Field()

Field is to make graphql understand what you written below its a registered column on the database

Entity UseCase

  • find()
User.find({
  where: [
    {
      name: "john doe",
    },
  ],
});
//is the same as SELECT * FROM USER WHERE name = 'john doe'
  • findOne();
User.findOne(1);
//is the same as SELECT * FROM USER WHERE id = 1;

Other usecase you can find the example on Typeorm Docs

Relations

  • Entity

User Entity

import { Book } from "./Book";
import { OneToMany, OneToOne, JoinColumn } from "typeorm";
import { Field } from "type-graphql";
/* ... */
@Entity("Users")
@ObjectType()
export class User extends BaseEntity {
  /* ... */
  //Example of one to many relation author to books
  @OneToMany(() => Book, books => books.User)
  @JoinColumn()
  @Field(() => [Book]) //this one to tell grapqhl a books field return array of book 
  // (you need FieldResolver in order for it to work)
  books: Book[];
}

Book Entity

import { User } from "./User";
import { OneToMany, OneToOne, JoinColumn } from "typeorm";
import { Field } from "type-graphql";
/* ... */
@Entity("Books")
@ObjectType()
export class Book extends BaseEntity {
  /* ... */
  //Example of many to one relation books to author
  @ManyToOne(() => User, author => author.Books)
  @JoinColumn()
  @Field(() => User) //this one to tell grapqhl an author field return User
  author: User;
}

Typeorm Usage

const users = await User.find({ relations: ["books"] });

Author FieldResolver on BookResolver

import {FieldResolver, Root } from 'type-graphql';
import {User} from './User';
/*...*/
@Resolver(of => Book)
class BookResolver {
/*...*/

@FieldResolver()
author(@Root() book: Book) {
  return User.findOne({
    cache: 1000,
    where: { author: { id: book.author.id } },
  });
}

Books FieldResolver on UserResolver

import {FieldResolver, Root } from 'type-graphql';
import {Book} from './Book';
/*...*/
@Resolver(of => User)
class UserResolver {
/*...*/

@FieldResolver()
books(@Root() user: User) {
  return Book.find({
    cache: 1000,
    where: { author: { id: author.id } },
  });
}

field

for other example on relation you can find it on Typeorm/Relations

Others

import {Crypt} from '../../../Vendor';

//a hash function (sha256)
const hashedText = Crypt.hash('plaintext');
//an encrypt functions (AES)
const encryptedText = Crypt.encrypt('plaintext','yoursuperstrongkey');
const decryptedText = Crypt.decrypt('plaintext','yoursuperstrongkey');

superxpress's People

Contributors

hikaaam avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

beckgis7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.