lewestopher / appointment-api Goto Github PK
View Code? Open in Web Editor NEWThis project forked from boone-studios/typescript-boilerplate
A quick and dirty boilerplate repo for TypeScript related projects
This project forked from boone-studios/typescript-boilerplate
A quick and dirty boilerplate repo for TypeScript related projects
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.
PRECURSOR TO THIS TASK: Config files need to be built out properly for each environment our app will run in.
Create src/Database/index.ts
.
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;
The 'config' module for Node.js does the following:
Checks for the existence of the NODE_ENV
environment variable. This variable will contain either 'localhost', 'testing', or 'production'.
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'.
The module consumes the contents of the file, which typically look like this:
module.exports = {
database: {
host: 'localhost',
port: 3306,
user: 'root',
password: 'root'
}
}
let config = require('config');
let knex = require('knex')({
host: config.database.host,
port: config.database.port,
user: config.database,user,
password: config.database.password
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.