Skip to content

Commit efdd0b0

Browse files
committed
updating utils files: Organizing them and also adding a redis client/store, to use with express-session
1 parent aee99b6 commit efdd0b0

9 files changed

Lines changed: 175 additions & 58 deletions
File renamed without changes.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const {DataTypes} = require("sequelize");
2+
3+
module.exports = {
4+
up: async ({context: queryInterface}) => {
5+
await queryInterface.createTable("user_posts", {
6+
id: {
7+
type: DataTypes.INTEGER,
8+
primaryKey: true,
9+
autoIncrement: true,
10+
},
11+
user_id: {
12+
type: DataTypes.INTEGER,
13+
allowNull: false,
14+
references: {model: "users", key: "id"},
15+
},
16+
post_id: {
17+
type: DataTypes.INTEGER,
18+
allowNull: false,
19+
references: {model: "posts", key: "id"},
20+
},
21+
read: {
22+
type: DataTypes.BOOLEAN,
23+
defaultValue: false,
24+
},
25+
});
26+
},
27+
down: async ({context: queryInterface}) => {
28+
await queryInterface.dropTable("user_posts");
29+
},
30+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const {DataTypes} = require("sequelize");
2+
3+
module.exports = {
4+
up: async ({context: queryInterface}) => {
5+
await queryInterface.addColumn("posts", "content", {
6+
type: DataTypes.STRING,
7+
defaultValue: "",
8+
});
9+
},
10+
down: async ({context: queryInterface}) => {
11+
await queryInterface.removeColumn("posts", "content");
12+
},
13+
};

utils/db.js

Lines changed: 0 additions & 55 deletions
This file was deleted.

utils/migrationDown.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {migrationDown} = require("./sequelize");
2+
3+
migrationDown();

utils/migrationUp.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const {migrationUp} = require("./sequelize");
2+
3+
migrationUp();

utils/redisStore.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const RedisStore = require("connect-redis").default;
2+
const redis = require("redis");
3+
4+
// Method 1
5+
// const redisClient1 = redis.createClient({
6+
// host: process.env.REDIS_HOST,
7+
// port: process.env.REDIS_PORT,
8+
// password: process.env.REDIS_PASSWORD,
9+
// });
10+
// // Method 2
11+
// const redisClient2 = redis.createClient({
12+
// url: process.env.REDIS_URI,
13+
// });
14+
15+
const redisClient = redis.createClient();
16+
redisClient.connect().catch(console.error);
17+
18+
// const redisClient = createClient({
19+
// username: "default", // use your Redis user. More info https://redis.io/docs/management/security/acl/
20+
// password: "secret", // use your password here
21+
// socket: {
22+
// host: "my-redis.cloud.redislabs.com",
23+
// port: 6379,
24+
// tls: true,
25+
// key: readFileSync("./redis_user_private.key"),
26+
// cert: readFileSync("./redis_user.crt"),
27+
// ca: [readFileSync("./redis_ca.pem")],
28+
// },
29+
// });
30+
// redisClient.on("error", (err) => console.log("Redis Client Error", err));
31+
// await redisClient.connect();
32+
// await redisClient.disconnect();
33+
34+
// {
35+
// secret: process.env.session_secret,
36+
// name: process.env.session_name,
37+
// store: new RedisSession({
38+
// client
39+
// }),
40+
// rolling: true,
41+
// saveUninitialized: true,
42+
// unset: 'destroy',
43+
// resave: true,
44+
// proxy: true,
45+
// logErrors: process.env.debug === 'true',
46+
// cookie: {
47+
// path: '/',
48+
// domain: '.' + process.env.app_domain,
49+
// secure: true,
50+
// sameSite: true,
51+
// httpOnly: true,
52+
// expires: false,
53+
// maxAge: 60000 * process.env.session_exp_mins,
54+
// }
55+
// }
56+
57+
const redisStore = new RedisStore({
58+
client: redisClient,
59+
prefix: "myapp:",
60+
});
61+
62+
module.exports = redisStore;

utils/rollback.js

Lines changed: 0 additions & 3 deletions
This file was deleted.

utils/sequelize.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
const Sequelize = require("sequelize");
2+
const {Umzug, SequelizeStorage} = require("umzug");
3+
const config = require("../config/config");
4+
const logger = require("../logger/logger");
5+
6+
const sequelize = new Sequelize(config.DB, {
7+
dialectOptions: {
8+
ssl: {
9+
require: true,
10+
rejectUnauthorized: false,
11+
},
12+
},
13+
dialect: "postgres",
14+
});
15+
16+
const migrationConf = {
17+
migrations: {glob: "migrations/*.js"},
18+
context: sequelize.getQueryInterface(),
19+
storage: new SequelizeStorage({sequelize, tableName: "migrations"}),
20+
};
21+
22+
const migrationUp = async () => {
23+
try {
24+
await sequelize.authenticate();
25+
const migrator = new Umzug(migrationConf);
26+
const migrations = await migrator.up();
27+
logger.info("Migrations up to date", {
28+
files: migrations.map((m) => m.name),
29+
});
30+
} catch (error) {
31+
logger.error(`Error running migration: ${error}`);
32+
}
33+
};
34+
35+
const migrationDown = async () => {
36+
try {
37+
await sequelize.authenticate();
38+
const migrator = new Umzug(migrationConf);
39+
const migrations = await migrator.down();
40+
logger.info("Migrations removed", {
41+
files: migrations.map((m) => m.name),
42+
});
43+
} catch (error) {
44+
logger.error(`Error rolling back migration: ${error}`);
45+
}
46+
};
47+
48+
const connectToDatabase = async () => {
49+
try {
50+
await sequelize.authenticate();
51+
logger.info(`connected to the database`);
52+
} catch (err) {
53+
logger.error(`failed to connect to the database: ${err}`);
54+
process.exitCode = 1;
55+
}
56+
return null;
57+
};
58+
59+
module.exports = {
60+
connectToDatabase,
61+
sequelize,
62+
migrationUp,
63+
migrationDown,
64+
};

0 commit comments

Comments
 (0)