Skip to content
Snippets Groups Projects
BundleCode.js 1.15 KiB
Newer Older
const timestamps = require('./properties/timestamps')

module.exports = (sequelize, DataTypes) => {
	const Model = sequelize.define('BundleCode', Object.assign(
		{
			id: {
				type: DataTypes.UUID,
				primaryKey: true,
				defaultValue: DataTypes.UUIDV4,
				validate: {
					isUUID: 4,
				},
			},
			name: {
				type: DataTypes.TEXT,
			},
			description: {
				type: DataTypes.TEXT,
			},
			platforms: {
				type: DataTypes.JSONB,
			},
			meta: {
				type: DataTypes.JSONB,
			},
		},
		timestamps(DataTypes),
	), {
		paranoid: true,
		tableName: 'bundle_codes',
	})

	Model.getPolyIdentifier = () => 'bundle_code'
	Model.getRelationIdentifier = () => 'bundle_codes'

	Model.prototype.toJSON = function userToJSON() {
		return {
			id: this.id,
			name: this.name,
			description: this.description,
			platforms: this.platforms,
			meta: this.meta,
			created_at: this.created_at,
			updated_at: this.updated_at,
		}
	}

	Model.associate = function defineModelAssociations(models) {
		Model.belongsToMany(models.User, { through: 'user_bundle_codes', foreignKey: 'bundle_code_id', otherKey: 'user_id', timestamps: false })
	}

	Model.relations = []

	return Model
}