Skip to content

Commit

Permalink
Initialize Survey, Question db models
Browse files Browse the repository at this point in the history
  • Loading branch information
Keskimaki committed Jan 27, 2023
1 parent 1c079a7 commit b6a420d
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/server/db/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const sequelize = new Sequelize({

const runMigrations = async () => {
const migrator = new Umzug({
migrations: { glob: 'src/server/db/migrations/*.ts' },
migrations: { glob: 'src/server/db/migrations/*.cjs' },
context: sequelize.getQueryInterface(),
storage: new SequelizeStorage({ sequelize }),
logger: console,
Expand Down Expand Up @@ -42,7 +42,8 @@ export const connectToDatabase = async (attempt = 0) => {
error: err.stack,
})

// return process.exit(1)
// update once staging has a database
return null // process.exit(1)
}
logger.info(
`Connection to database failed! Attempt ${attempt} of ${DB_CONNECTION_RETRY_LIMIT}`
Expand Down
Empty file removed src/server/db/migrations/.gitkeep
Empty file.
28 changes: 28 additions & 0 deletions src/server/db/migrations/20230127_00_init_surveys.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const { DataTypes } = require('sequelize')

module.exports = {
up: async ({ context: queryInterface }) => {
await queryInterface.createTable('surveys', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
},
})
},
down: async ({ context: queryInterface }) => {
await queryInterface.dropTable('surveys')
},
}
54 changes: 54 additions & 0 deletions src/server/db/migrations/20230127_01_init_questions.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const { DataTypes } = require('sequelize')

module.exports = {
up: async ({ context: queryInterface }) => {
await queryInterface.createTable('questions', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
survey_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
parent_id: {
type: DataTypes.INTEGER,
allowNull: false,
},
priority: {
type: DataTypes.INTEGER,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: true,
},
text: {
type: DataTypes.TEXT,
allowNull: false,
},
option_data: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
},
visibility: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
},
created_at: {
type: DataTypes.DATE,
allowNull: false,
},
updated_at: {
type: DataTypes.DATE,
allowNull: false,
},
})
},
down: async ({ context: queryInterface }) => {
await queryInterface.dropTable('questions')
},
}
Empty file removed src/server/db/models/.gitkeep
Empty file.
85 changes: 85 additions & 0 deletions src/server/db/models/Question.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import {
Model,
InferAttributes,
InferCreationAttributes,
CreationOptional,
DataTypes,
} from 'sequelize'

import { sequelize } from '../connection.js'

interface OptionData {
type: 'singleChoice' | 'multipleChoice' | 'text'
options: Array<any>
}

interface Visibility {
optionIds: Array<string>
}

class Question extends Model<
InferAttributes<Question>,
InferCreationAttributes<Question>
> {
declare id: CreationOptional<number>

declare surveyId: number

declare parentId: number

declare priority: number

declare title: string

declare text: string

declare optionData: OptionData

declare visibility: Visibility
}

Question.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
surveyId: {
type: DataTypes.INTEGER,
allowNull: false,
},
parentId: {
type: DataTypes.INTEGER,
allowNull: false,
},
priority: {
type: DataTypes.INTEGER,
allowNull: false,
},
title: {
type: DataTypes.STRING,
allowNull: true,
},
text: {
type: DataTypes.TEXT,
allowNull: false,
},
optionData: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
},
visibility: {
type: DataTypes.JSONB,
allowNull: false,
defaultValue: {},
},
},
{
underscored: true,
sequelize,
}
)

export default Question
38 changes: 38 additions & 0 deletions src/server/db/models/Survey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
Model,
InferAttributes,
InferCreationAttributes,
CreationOptional,
DataTypes,
} from 'sequelize'

import { sequelize } from '../connection.js'

class Survey extends Model<
InferAttributes<Survey>,
InferCreationAttributes<Survey>
> {
declare id: CreationOptional<number>

declare name: string
}

Survey.init(
{
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
underscored: true,
sequelize,
}
)

export default Survey
8 changes: 8 additions & 0 deletions src/server/db/models/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Survey from './Survey.js'
import Question from './Question.js'

Question.belongsTo(Survey)

Survey.hasMany(Question)

export { Survey, Question }

0 comments on commit b6a420d

Please sign in to comment.