add in sequelize, setup apis

This commit is contained in:
iamBadgers
2024-03-06 23:15:47 -08:00
parent f346bcafc0
commit 05e31f5986
3 changed files with 1528 additions and 30 deletions

1449
backend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@@ -6,13 +6,16 @@
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint": "eslint . --ext .js,.jsx,.cjs,.mjs,.ts,.tsx,.cts,.mts --fix --ignore-path .gitignore",
"format": "prettier --write src/"
"format": "prettier --write src/",
"dev": "npx tsc && node dist/app.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.21",
"@types/sequelize": "^4.28.20",
"@types/sqlite3": "^3.1.11",
"@typescript-eslint/eslint-plugin": "^7.1.1",
"@typescript-eslint/parser": "^7.1.1",
"eslint": "^8.57.0",
@@ -20,6 +23,10 @@
"typescript": "^5.3.3"
},
"dependencies": {
"express": "^4.18.3"
"@types/body-parser": "^1.19.5",
"body-parser": "^1.20.2",
"express": "^4.18.3",
"sequelize": "^6.37.1",
"sqlite3": "^5.1.7"
}
}

View File

@@ -1,11 +1,105 @@
import express from 'express'
// import sqlite3 from 'sqlite3'
import { Sequelize, Model, DataTypes } from 'sequelize'
import { json } from 'body-parser'
const databasePath = './testdb.db'
let database
const sequelize = new Sequelize({
dialect: 'sqlite',
storage: databasePath
})
const Character = sequelize.define(
'Characters',
{
id: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT },
playerName: { type: DataTypes.TEXT },
role: { type: DataTypes.TEXT },
creationDate: { type: DataTypes.INTEGER }
},
{ timestamps: false }
)
const Game = sequelize.define(
'Games',
{
id: { type: DataTypes.INTEGER, primaryKey: true },
title: { type: DataTypes.TEXT },
status: { type: DataTypes.TEXT },
fix: { type: DataTypes.BOOLEAN },
postdate: { type: DataTypes.INTEGER },
gamemaster: { type: DataTypes.TEXT },
payoutEB: { type: DataTypes.INTEGER },
payoutIP: { type: DataTypes.INTEGER },
payputLoot: { type: DataTypes.INTEGER }
},
{ timestamps: false }
)
const Pick = sequelize.define(
'Picks',
{
gameId: { type: DataTypes.INTEGER, primaryKey: true },
gameTitle: { type: DataTypes.TEXT },
character: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
const App = sequelize.define(
'Apps',
{
gameId: { type: DataTypes.INTEGER, primaryKey: true },
gameTitle: { type: DataTypes.TEXT },
characterId: { type: DataTypes.INTEGER, primaryKey: true },
characterName: { type: DataTypes.TEXT }
},
{ timestamps: false }
)
const app = express()
const port = 3000
const jsonParser = json()
const port = 3001
app.get('/', (req, res) => {
res.send('Hello World!')
})
app.listen(port, () => {
app.get('/api/character/:characterId', async (req, res) => {
res.send(await Character.findOne({ where: { id: req.params['characterId'] } }))
})
app.post('/api/character', jsonParser, async (req, res) => {
const page = req.body.page
const orderBy = req.body.orderBy
const count = req.body.count
res.setHeader('Content-Type', 'application/json')
res.send(await Character.findAll({offset: page * count, limit: count}))
})
app.get('/api/game/:characterId', async (req, res) => {
res.send(await Game.findOne({ where: { id: req.params['characterId'] } }))
})
app.post('/api/game', jsonParser, async (req, res) => {
const page = req.body.page
const orderBy = req.body.orderBy
const count = req.body.count
res.setHeader('Content-Type', 'application/json')
res.send(await Game.findAll({offset: page * count, limit: count}))
})
app.get('/api/game/:gameId', async (req, res) => {
res.send(await Game.findOne({ where: { id: req.params['gameId'] } }))
})
app.listen(port, async () => {
await sequelize.authenticate()
return console.log(`Express is listening at http://localhost:${port}`)
})