Skip to content

Commit

Permalink
many changes, see message
Browse files Browse the repository at this point in the history
- moves sequelize DB stuff and express routes to /lib commonJS modules
- adds env use to docker-compose and node.js
- node server will now connect to the DB, create the .env database if it doesn't exist, then populate it with the appropriate tables etc if not already set up
- node server will also (wipe and) populate the markers found in /public/markers into the DB for api return to frontend
- implements PostGIS (spatially-aware PostgresDB) in docker compose w persistent volume la3map_pg_data
- docker compose file has been updated to support healthchecks for both services & app dependency on DB running
- docker compose will send env variable IS_DOCKER if running via docker to adjust for service hostname rather than assuming the service is running on localhost, or whatever is provided as POSTGRES_HOST in .env
- saves marker geo data as geometry columns instead of raw json
  • Loading branch information
indig0fox committed Jul 5, 2023
1 parent f523a3c commit dea4ff1
Show file tree
Hide file tree
Showing 9 changed files with 480 additions and 359 deletions.
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
APP_PORT=3192
POSTGRES_HOST=localhost
POSTGRES_PORT=5432
POSTGRES_EXT_PORT=3193
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_DB=la3map
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules
package-lock.json
app.db
app.db
.env
50 changes: 50 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: "3"

name: la3map
services:
node:
build: ./
working_dir: /usr/app
environment:
- NODE_ENV=production
- IS_DOCKER=true
ports:
- ${APP_PORT}:3000
depends_on:
postgres:
condition: service_healthy
healthcheck:
test:
[
"CMD",
"curl",
"-f",
"http://localhost:3000/healthcheck"
]
interval: 30s
timeout: 10s
retries: 5
command: "npm start"
restart: unless-stopped
volumes:
- ./:/usr/app
- /usr/app/node_modules
postgres:
env_file: .env
image: postgis/postgis:12-3.3
healthcheck:
test: [ "CMD", "pg_isready", "-U", "${POSTGRES_USER}" ]
interval: 5s
timeout: 5s
retries: 5
environment:
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
- POSTGRES_DB=${POSTGRES_DB}
ports:
- ${POSTGRES_EXT_PORT}:5432
volumes:
- pg_data:/var/lib/postgresql/data

volumes:
pg_data: {}
13 changes: 0 additions & 13 deletions docker-compose.yml

This file was deleted.

98 changes: 98 additions & 0 deletions lib/db.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// database connection and model definitions

const Sequelize = require('sequelize');

// create/connect database (SQLite)
// const sequelize = new Sequelize({
// dialect: 'sqlite',
// storage: dbFile,
// // logging: false,
// // transactionType: 'IMMEDIATE',
// retry: {
// max: 10
// }
// });


// create/connect database (PostgreSQL)
const sequelize = new Sequelize(`postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}:${process.env.POSTGRES_PORT}/${process.env.POSTGRES_DB}`, {
dialect: 'postgres',
application_name: 'la3map',
keepAlive: true,
logging: false
});

// define models
const Drawing = sequelize.define('Drawing', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true,
},
// data is json
data: Sequelize.DataTypes.GEOMETRY,
description: Sequelize.TEXT,
color: Sequelize.TEXT,
imageUrl: Sequelize.TEXT,
});

const Session = sequelize.define('Session', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true,
},
worldname: Sequelize.TEXT,
});

const User = sequelize.define('User', {
username: {
type: Sequelize.TEXT,
unique: true,
},
password: Sequelize.TEXT,
role: Sequelize.TEXT,
});


const Marker = sequelize.define('Marker', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
primaryKey: true,
},
addon: {
type: Sequelize.TEXT,
},
marker_name: {
type: Sequelize.TEXT,
},
url: Sequelize.TEXT
},
{
indexes: [
{
unique: true,
fields: ['addon', 'marker_name']
}
]
}
);

// define relationships
Drawing.Session = Drawing.belongsTo(Session, {
foreignKey: 'session_id',
onDelete: 'CASCADE',
});
Session.Drawings = Session.hasMany(Drawing, {
foreignKey: 'session_id',
onDelete: 'CASCADE',
})

module.exports = {
sequelize,
Drawing,
Session,
User,
Marker,
};
Loading

0 comments on commit dea4ff1

Please sign in to comment.