agrs-sequelize-sdk 1.1.19 → 1.1.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -64,44 +64,23 @@ module.exports = (sequelize, DataTypes) => {
64
64
  hooks: {
65
65
  beforeCreate: async (record, options) => {
66
66
  try {
67
- // Find the earliest date for this keyword using raw query for better performance
68
- const [result] = await sequelize.query(
69
- `
70
- SELECT MIN("Date") as first_date
71
- FROM "KeywordPerformance"
72
- WHERE LOWER("Keyword") = LOWER(:keyword)
73
- `,
74
- {
75
- replacements: { keyword: record.Keyword },
76
- type: sequelize.QueryTypes.SELECT,
67
+ // Find existing record for this keyword
68
+ const existingKeyword =
69
+ await sequelize.models.KeywordPerformance.findOne({
70
+ where: {
71
+ Keyword: sequelize.where(
72
+ sequelize.fn("LOWER", sequelize.col("Keyword")),
73
+ sequelize.fn("LOWER", record.Keyword)
74
+ ),
75
+ },
76
+ attributes: ["FirstAppearanceDate"],
77
77
  transaction: options.transaction,
78
- }
79
- );
78
+ });
80
79
 
81
- // If no existing record found, use current record's date
82
- // Otherwise, use the earlier date between existing and current
83
- const existingFirstDate = result?.first_date;
84
- record.FirstAppearanceDate = existingFirstDate
85
- ? Math.min(new Date(existingFirstDate), new Date(record.Date))
86
- : record.Date;
87
-
88
- // If we found an earlier date, update all existing records for this keyword
89
- if (existingFirstDate && record.Date < existingFirstDate) {
90
- await sequelize.query(
91
- `
92
- UPDATE "KeywordPerformance"
93
- SET "FirstAppearanceDate" = :newDate
94
- WHERE LOWER("Keyword") = LOWER(:keyword)
95
- `,
96
- {
97
- replacements: {
98
- newDate: record.Date,
99
- keyword: record.Keyword,
100
- },
101
- transaction: options.transaction,
102
- }
103
- );
104
- }
80
+ // If keyword exists, use its FirstAppearanceDate
81
+ // If not, use the current record's Date
82
+ record.FirstAppearanceDate =
83
+ existingKeyword?.FirstAppearanceDate || record.Date;
105
84
  } catch (error) {
106
85
  console.error("Error in FirstAppearanceDate hook:", error);
107
86
  throw error;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agrs-sequelize-sdk",
3
- "version": "1.1.19",
3
+ "version": "1.1.20",
4
4
  "main": "index.js",
5
5
  "scripts": {
6
6
  "start": "node index.js",