Skip to content

Instantly share code, notes, and snippets.

@ericpkatz
ericpkatz / index.html
Created January 2, 2024 15:40
React Starting Point
<!DOCTYPE html>
<html>
<body>
<div id="root"></div>
</body>
<!-- This setup is not suitable for production. -->
<!-- Only use it in development! -->
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script async src="https://ga.jspm.io/npm:es-module-shims@1.7.0/dist/es-module-shims.js"></script>
require('./env');
const ngrok = require('ngrok');
const init = async()=> {
if(process.env.ngrok){
const url = await ngrok.connect(
{
port: process.env.PORT, authtoken: process.env.ngrok
}
);
console.log(url);
@ericpkatz
ericpkatz / webpack.config.js
Created August 17, 2022 11:23
Simple Webpack Config
module.exports = {
devtool: 'source-map',
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: ['@babel/preset-react']
@ericpkatz
ericpkatz / server.js
Created August 17, 2022 11:17
Backend For Facet Search
const Sequelize = require('sequelize');
const conn = new Sequelize(process.env.DATABASE_URL || 'postgres://localhost/acme_db');
const express = require('express');
const app = express();
const path = require('path');
app.use('/dist', express.static('dist'));
app.get('/', (req, res, next)=> res.sendFile(path.join(__dirname, 'index.html')));
const pg = require('pg');
try {
//in secret.js add the following
//process.env.DATABASE_URL = 'YOUR_SECURE_REMOTE_CONNECTION_STRING';
//ADD secret.js to your .gitignore for security
require('./secret');
}
catch(ex){
console.log(ex);
}

Sequelize Tips

Schema Design

  • Plan out your schema without thinking about Sequelize. This can be done using pen (or pencil) and paper.
    • Start with your entities.
    • Determine how the entities relate to each other.

Sequelize

  • Using Sequelize and pg create your Sequelize object
  • Each of your entities will be a Sequelize model
DROP TABLE IF EXISTS ownership;
DROP TABLE IF EXISTS sneakers;
DROP TABLE IF EXISTS brands;
DROP TABLE IF EXISTS collectors;
CREATE TABLE brands(
id INTEGER PRIMARY KEY,
name VARCHAR(100)
);
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<style id="jsbin-css">
body {
color: red;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<h1>Hello World</h1>
<script id="jsbin-javascript">

Issue - the "swallowing" of errors

  • a swallowed error in a thunk will prevent a component from detecting the error

  • a swallowed error in a Sequelize method will prevent an express route from detecting the error

  • both of these often result in difficult to detect bugs

  • I think this is a valid example with Sequelize. You can imagine the createByName method being called in an express route, but I'm just using it to seed the database.

const Sequelize = require('sequelize');