38743-vm/backend/src/db/api/dictionary_words.js
2026-02-24 17:57:27 +00:00

482 lines
12 KiB
JavaScript

const db = require('../models');
const FileDBApi = require('./file');
const crypto = require('crypto');
const Utils = require('../utils');
const Sequelize = db.Sequelize;
const Op = Sequelize.Op;
module.exports = class Dictionary_wordsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const dictionary_words = await db.dictionary_words.create(
{
id: data.id || undefined,
word: data.word
||
null
,
normalized_word: data.normalized_word
||
null
,
length: data.length
||
null
,
part_of_speech: data.part_of_speech
||
null
,
is_active: data.is_active
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await dictionary_words.setDictionary_source( data.dictionary_source || null, {
transaction,
});
return dictionary_words;
}
static async bulkImport(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
// Prepare data - wrapping individual data transformations in a map() method
const dictionary_wordsData = data.map((item, index) => ({
id: item.id || undefined,
word: item.word
||
null
,
normalized_word: item.normalized_word
||
null
,
length: item.length
||
null
,
part_of_speech: item.part_of_speech
||
null
,
is_active: item.is_active
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const dictionary_words = await db.dictionary_words.bulkCreate(dictionary_wordsData, { transaction });
// For each item created, replace relation files
return dictionary_words;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const dictionary_words = await db.dictionary_words.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.word !== undefined) updatePayload.word = data.word;
if (data.normalized_word !== undefined) updatePayload.normalized_word = data.normalized_word;
if (data.length !== undefined) updatePayload.length = data.length;
if (data.part_of_speech !== undefined) updatePayload.part_of_speech = data.part_of_speech;
if (data.is_active !== undefined) updatePayload.is_active = data.is_active;
updatePayload.updatedById = currentUser.id;
await dictionary_words.update(updatePayload, {transaction});
if (data.dictionary_source !== undefined) {
await dictionary_words.setDictionary_source(
data.dictionary_source,
{ transaction }
);
}
return dictionary_words;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const dictionary_words = await db.dictionary_words.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of dictionary_words) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of dictionary_words) {
await record.destroy({transaction});
}
});
return dictionary_words;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const dictionary_words = await db.dictionary_words.findByPk(id, options);
await dictionary_words.update({
deletedBy: currentUser.id
}, {
transaction,
});
await dictionary_words.destroy({
transaction
});
return dictionary_words;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const dictionary_words = await db.dictionary_words.findOne(
{ where },
{ transaction },
);
if (!dictionary_words) {
return dictionary_words;
}
const output = dictionary_words.get({plain: true});
output.dictionary_source = await dictionary_words.getDictionary_source({
transaction
});
return output;
}
static async findAll(
filter,
options
) {
const limit = filter.limit || 0;
let offset = 0;
let where = {};
const currentPage = +filter.page;
offset = currentPage * limit;
const orderBy = null;
const transaction = (options && options.transaction) || undefined;
let include = [
{
model: db.dictionary_sources,
as: 'dictionary_source',
where: filter.dictionary_source ? {
[Op.or]: [
{ id: { [Op.in]: filter.dictionary_source.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.dictionary_source.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.word) {
where = {
...where,
[Op.and]: Utils.ilike(
'dictionary_words',
'word',
filter.word,
),
};
}
if (filter.normalized_word) {
where = {
...where,
[Op.and]: Utils.ilike(
'dictionary_words',
'normalized_word',
filter.normalized_word,
),
};
}
if (filter.part_of_speech) {
where = {
...where,
[Op.and]: Utils.ilike(
'dictionary_words',
'part_of_speech',
filter.part_of_speech,
),
};
}
if (filter.lengthRange) {
const [start, end] = filter.lengthRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
length: {
...where.length,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
length: {
...where.length,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.is_active) {
where = {
...where,
is_active: filter.is_active,
};
}
if (filter.createdAtRange) {
const [start, end] = filter.createdAtRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
['createdAt']: {
...where.createdAt,
[Op.lte]: end,
},
};
}
}
}
const queryOptions = {
where,
include,
distinct: true,
order: filter.field && filter.sort
? [[filter.field, filter.sort]]
: [['createdAt', 'desc']],
transaction: options?.transaction,
logging: console.log
};
if (!options?.countOnly) {
queryOptions.limit = limit ? Number(limit) : undefined;
queryOptions.offset = offset ? Number(offset) : undefined;
}
try {
const { rows, count } = await db.dictionary_words.findAndCountAll(queryOptions);
return {
rows: options?.countOnly ? [] : rows,
count: count
};
} catch (error) {
console.error('Error executing query:', error);
throw error;
}
}
static async findAllAutocomplete(query, limit, offset, ) {
let where = {};
if (query) {
where = {
[Op.or]: [
{ ['id']: Utils.uuid(query) },
Utils.ilike(
'dictionary_words',
'word',
query,
),
],
};
}
const records = await db.dictionary_words.findAll({
attributes: [ 'id', 'word' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['word', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.word,
}));
}
};