Skip to content

Adding type mapping #229

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/models/entity-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
const entityType = (sequelize, DataTypes) => {
const EntityType = sequelize.define('EntityType', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING,
required: true
},
description: {
type: DataTypes.STRING
},
questionnaires: {
type: DataTypes.JSON
}
},
{
schema: process.env.DATABASE_SCHEMA
})

EntityType.findById = async (id) => {
const entityType = await EntityType.findOne({
where: { id }
})

return entityType
}

EntityType.findByName = async (name) => {
const entityType = await EntityType.findOne({
where: { name }
})

return entityType
}

return EntityType
}

export default entityType
150 changes: 75 additions & 75 deletions src/models/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,89 +2,89 @@ import { UUIDV4 } from "sequelize"
import models from "./index"

const entity = (sequelize, DataTypes) => {
// Defining our entity table and setting Entity object.
const Entity = sequelize.define('Entity', {
id: {
type: DataTypes.UUID,
unique: true,
primaryKey: true,
defaultValue: UUIDV4
},
name : {
type: DataTypes.STRING,
required: true
},
type : {
type: DataTypes.STRING
},
address: {
type: DataTypes.JSON,
},
phone: {
type: DataTypes.JSON,
},
email: {
type: DataTypes.JSON,
},
checkIn: {
type: DataTypes.JSON,
},
description: {
type: DataTypes.STRING
},
attributes: {
type: DataTypes.JSON,
}
// Defining our entity table and setting Entity object.
const Entity = sequelize.define('Entity', {
id: {
type: DataTypes.UUID,
unique: true,
primaryKey: true,
defaultValue: UUIDV4
},
name: {
type: DataTypes.STRING,
required: true
},
type: {
type: DataTypes.STRING
},
address: {
type: DataTypes.JSON,
},
phone: {
type: DataTypes.JSON,
},
email: {
type: DataTypes.JSON,
},
checkIn: {
type: DataTypes.JSON,
},
description: {
type: DataTypes.STRING
},
attributes: {
type: DataTypes.JSON,
}
},
{
schema: process.env.DATABASE_SCHEMA
schema: process.env.DATABASE_SCHEMA
})

Entity.associate = models => {
Entity.belongsToMany(models.Contact, {
through: "EntityContact",
as: "contacts",
foreignKey: "entityId",
otherKey: "contactId"
})
}
Entity.associate = models => {
Entity.belongsToMany(models.Contact, {
through: "EntityContact",
as: "contacts",
foreignKey: "entityId",
otherKey: "contactId"
})
}

Entity.findById = async (id) => {
const entity = await Entity.findOne({
where: { id }
})

return entity
}
Entity.findById = async (id) => {
const entity = await Entity.findOne({
where: { id }
})

Entity.findByName = async (name) => {
const entity = await Entity.findOne({
where: { name }
})
return entity
}
return entity
}

Entity.findByName = async (name) => {
const entity = await Entity.findOne({
where: { name }
})

Entity.findEntityWithAssociatedContacts = async (entityId) => {
const entityContacts = await Entity.findOne({
where: { id: entityId },
include: [{
model: models.Contact,
as: 'contacts',
required: false,
attributes: ["id", "name", "phone", "email", "attributes", "email"],
through: {
model: models.EntityContact,
as: "entityContacts",
attributes: ["relationshipTitle"]
}
}]
})
return entity
}

return entityContacts
}
Entity.findEntityWithAssociatedContacts = async (entityId) => {
const entityContacts = await Entity.findOne({
where: { id: entityId },
include: [{
model: models.Contact,
as: 'contacts',
required: false,
attributes: ["id", "name", "phone", "email", "attributes", "email"],
through: {
model: models.EntityContact,
as: "entityContacts",
attributes: ["relationshipTitle"]
}
}]
})

return entityContacts
}

return Entity
return Entity
}

export default entity;
export default entity