All files / postcardotp-backend/src/models apiKey.js

100% Statements 7/7
100% Branches 0/0
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60  1x 1x 1x                                                                                   1x 1x         1x           1x    
// models/apiKey.js
const { DataTypes } = require("sequelize");
module.exports = (sequelize) => {
  const ApiKey = sequelize.define(
    "ApiKey",
    {
      id: {
        type: DataTypes.UUID,
        defaultValue: DataTypes.UUIDV4,
        primaryKey: true,
      },
      user_id: {
        type: DataTypes.UUID,
        allowNull: false,
      },
      key: {
        type: DataTypes.TEXT,
        allowNull: false,
        comment: "store encrypted/hashed value",
      },
      type: {
        type: DataTypes.STRING(50),
        defaultValue: "live",
        comment: "allowed: live, test",
      },
      status: {
        type: DataTypes.STRING(20),
        defaultValue: "active",
        comment: "allowed: active, revoked",
      },
      created_at: {
        type: DataTypes.DATE,
        defaultValue: sequelize.literal("CURRENT_TIMESTAMP"),
      },
      updated_at: {
        type: DataTypes.DATE,
        defaultValue: sequelize.literal("CURRENT_TIMESTAMP"),
      },
    },
    {
      tableName: "tbl_api_keys",
      timestamps: false,
    },
  );
 
  ApiKey.associate = (models) => {
    ApiKey.belongsTo(models.User, {
      foreignKey: "user_id",
      as: "user",
    });
 
    ApiKey.hasMany(models.ApiKeyUsage, {
      foreignKey: "api_key_id",
      as: "usageLogs",
    });
  };
 
  return ApiKey;
};