-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize Survey, Question db models
- Loading branch information
Showing
8 changed files
with
216 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } |