Git Product home page Git Product logo

kittens-inn's Introduction

Lebo's Kittens inn

Lebo is running a local cattery and people can book their kittens in for up to 10 days at a time while they are going on holiday.

She wants a system where people can make bookings specifying the name of the kitten, the day of the week the kitten will be booked in and how long the kitten will be staying.

She enlisted our help to create this system for her.

Setup

Clone this repo from GitHub then run npm install.

Run the app using nodemon.

Fix the tests

This is a quick introduction to SQL using PostgreSQL.

Fix the tests to learn more about SQL

To run the tests use:

npm test

Database setup

Ensure you have PostgreSQL installed on your PC.

If you don't have it installed you can install it on Ubuntu using this command.

sudo apt-get update
sudo apt-get install postgresql postgresql-contrib

You will need to create a user for PostgreSQL that match the username you are loggin into Ubuntu.

Create a username - coder with a password of pg123. Enter the password when prompted after executing the createuser command.

sudo -u postgres createuser coder -P;

Create the database

Create the database using this command.

sudo -u postgres createdb kitten_inn;

Grant access to the database

Now run psql as the postgres user:

sudo -u postgres psql;

Grant the coder user (or your specific user) access to the kitten_inn database by running this command:

grant all privileges on database kitten_inn to coder;

Type in \q to exit psql as the postgres user.

Connect to your database using: psql -d kitten_inn

Database tables

Use the scripts below in psql to create the booking table.

Create the table we need

create table booking(
	id serial not null primary key,
	name text,
	staying_for int,
	arriving_on text
);

Create the SQL we need

To create booking use:

insert into booking (name, staying_for, arriving_on)
	values ("Snowy", 3, "Tuesday");

All the bookings:

select * from booking

All the bookings for 3 days or less:

select * from booking where days > 3

All the bookings longer than 3 days:

select * from booking where days > 3

Link the database to our application

To use PostgreSQL in NodeJS you need to install the pg module from npm.

npm install --save pg

Import the module and get Pool reference to use.

const pg = require("pg");
const Pool = pg.Pool;

Create Pool instance by using database connection string.

const connectionString = process.env.DATABASE_URL || 'postgresql://coder:pg123@localhost:5432/kitten_inn';

const pool = new Pool({
    connectionString
  });

Use the SQL in our application

Use SQL in your application like this.

async function queryDB() {
	
	const param1 = "value";

	let results = await pool.query('select field1, field2 from your_table where id = $1', [param1]);
	
	return results;

}

kittens-inn's People

Contributors

codex-avee avatar

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.