Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saas Boilerplate (book #2) - postgresql? PostgreSQL sequelize #45

Closed
drewdt1 opened this issue Oct 18, 2019 · 6 comments
Closed

Saas Boilerplate (book #2) - postgresql? PostgreSQL sequelize #45

drewdt1 opened this issue Oct 18, 2019 · 6 comments
Assignees
Labels
Ready to test - Async Issue is implemented and ready for testing. PR is merged.

Comments

@drewdt1
Copy link

drewdt1 commented Oct 18, 2019

Love the premise. Do your books provide instruction for setting up the boilerplate code with a postgresql db (in opposition to mongodb)?


Click to see Hill for issue #45
Single issue hill
Click to see Hill for all issues
All issue hill

created by Async

@tima101
Copy link
Member

tima101 commented Oct 20, 2019

@drewdt1 Thanks for feedback. No plans for now.


EDIT: No PostgreSQL in the book, only MongoDB.

@tima101 tima101 self-assigned this Oct 20, 2019
@tima101 tima101 added Assigned - Async Issue is assigned to at least one person. PR is assigned to at least one person. Ready to test - Async Issue is implemented and ready for testing. PR is merged. and removed Assigned - Async Issue is assigned to at least one person. PR is assigned to at least one person. labels Oct 20, 2019
@mmoyles87
Copy link

mmoyles87 commented Oct 21, 2019

It's a pretty trivial change. I've done it on quite a few projects using this boilerplate.

I used sequelize, replaced the mongodb models with sequelize models, and then you need to configure express-session with the sequelize plugin.

Something like this in app.ts:

import * as SequelizeSessionInit from 'connect-session-sequelize';

const SequelizeStore = SequelizeSessionInit(session.Store);
const sessionStore = new SequelizeStore({
  db: sequelize,
  table: 'Session',
  checkExpirationInterval: 10 * 60 * 1000,
  expiration: 240 * 60 * 1000,
});

const sessionOptions = {
  name: SESSION_NAME,
  secret: SESSION_SECRET,
  store: sessionStore,
  resave: false,
  saveUninitialized: false,
  cookie: {
    httpOnly: true,
    maxAge: 240 * 60 * 1000, // expires in 240 minutes
    domain: COOKIE_DOMAIN,
  } as any,
};

and a models/Session.ts file would like something like this

import { DataTypes, Model } from 'sequelize';

import sequelize from '../db';

class Session extends Model {
  // Note that the `null assertion` `!` is required in strict mode.
  public sid!: string;
  public expires!: Date;
  public data!: string;
}

Session.init(
  {
    sid: {
      type: DataTypes.STRING(36),
      primaryKey: true,
    },
    expires: {
      type: DataTypes.DATE(),
      allowNull: false,
    },
    data: DataTypes.TEXT(),
  },
  {
    sequelize, // this bit is important,
    // schema: 'cmlp',
    timestamps: false, // don't add the timestamp attributes (updatedAt, createdAt)
    indexes: [
      {
        name: 'session_sid_index',
        using: 'BTREE',
        fields: ['sid'],
      },
    ],
  },
);

export default Session;
import { Sequelize } from 'sequelize';
// import logger from './logs';

const { PGHOST, PGUSER, PGDATABASE, PGPASSWORD, PGPORT } = process.env;

// Option 1: Passing parameters separately
export default new Sequelize(PGDATABASE, PGUSER, PGPASSWORD, {
  host: PGHOST,
  port: parseInt(PGPORT, 10),
  dialect: 'postgres',
  // logging: str => logger.debug(str),
  logging: false,
});

You'll also need to update consts.ts to parse the new envars.

There are no changed required in the app app just api app.

@tima101
Copy link
Member

tima101 commented Oct 21, 2019

@dubvfan87 Nice.

Would you like to update current README with all details? You are welcome to create a new section PostgreSQL instead of MongoDB.

@drewdt1
Copy link
Author

drewdt1 commented Oct 23, 2019

@dubvfan87 good stuff. thank you very much!

@mmoyles87
Copy link

@tima101 Do you think it makes sense to add? This doesn't cover converting things like creating new Sequelize models, because I stripped the repo down to a barebones state before I did the conversion. It may help folks get on the right track though.

@tima101
Copy link
Member

tima101 commented Oct 25, 2019

@dubvfan87 Provides direction. It could be very detailed if you think it saves people time.

@tima101 tima101 closed this as completed Jun 1, 2021
@tima101 tima101 changed the title Saas Boilerplate (book #2) - postgresql? Saas Boilerplate (book #2) - postgresql? PostgreSQL sequelize Jun 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Ready to test - Async Issue is implemented and ready for testing. PR is merged.
Projects
None yet
Development

No branches or pull requests

3 participants