Giter VIP home page Giter VIP logo

appointment-api's People

Contributors

lewestopher avatar yabadee avatar

Watchers

 avatar  avatar

appointment-api's Issues

appointmentValidator.js

We need to develop a class that will take the data to be saved as an appointment in the database and "validates" it to make sure that it meets our requirements. Create this in the appointments module folder with the router, model, and controller.

PRECURSOR TO THIS TASK: Database schema needs to be created before the validator can be made.

Example:

Say we have a User object that needs to be saved to the database. The database schema might look something like this:

{
    "id": "integer | length: 11 | autoincrement | unique",
    "email": "varchar | length: 255 | unique | required",
    "first_name": "varchar | length: 255",
    "last_name": "varchar | length: 255",
    "password": "varchar | length: 255 | required",
}

We can tell that when creating a new User, there are two fields we absolutely need according to the database requirements: "email", and "password", so we might build a validator that looks like this:

class UserValidator {
    constructor(raw_user) {
        this.validated = false;
        this.raw_user = raw_user;
    }

    validate() {
        if (!this.raw_user.email) {
            throw new Exception('ValidationError: User model is missing email.')
        }

        if (!this.raw_user.password) {
            throw new Exception('ValidationError: User model is missing password.')
        }

        return true;
    }
}

Now we can get the raw user data that our user sent us and check to see if it is validated:

let passingUser = {
    email: '[email protected]',
    password: '123456'
};

let validation = new UserValidator(passingUser);
console.log(validation.validate()); // Consoles to 'true'

let failingUser = {
    email: '[email protected]'
}
let validation = new UserValidator(failingUser);
console.log(validation.validate()); // Throws a ValidationError and stops the program

This prevents us from saving incomplete data to the database and makes sure that our saved data has the highest of integrity.

Create src/Database/index.ts. Instantiate the Knex object there with our local DB connection settings. Export the Knex object so we can access the database.

PRECURSOR TO THIS TASK: Config files need to be built out properly for each environment our app will run in.

  1. Create src/Database/index.ts.

  2. Build the Knex object and export it like so:

let config = require('config');
let knex = require('knex')({
    host: config.database.host,
    port: config.database.port,
    user: config.database.user,
    password: config.database.password
});
export default knex;

Create the following config files in src/config: default.ts, localhost.ts, testing.ts, production.ts

The 'config' module for Node.js does the following:

  1. Checks for the existence of the NODE_ENV environment variable. This variable will contain either 'localhost', 'testing', or 'production'.

  2. Once we know the value of NODE_ENV, the module then checks for the associated file located in {project root}/config. EG - 'localhost.js', or 'production.js'.

  3. The module consumes the contents of the file, which typically look like this:

module.exports = {
    database: {
        host: 'localhost',
        port: 3306,
        user: 'root',
        password: 'root'
    }
}
  1. After the module reads the config information, we can read our config information at anytime like so:
let config = require('config');
let knex = require('knex')({
    host: config.database.host,
    port: config.database.port,
    user: config.database,user,
    password: config.database.password
});

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.