626 lines
15 KiB
JavaScript
626 lines
15 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 Generated_imagesDBApi {
|
|
|
|
|
|
|
|
static async create(data, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const generated_images = await db.generated_images.create(
|
|
{
|
|
id: data.id || undefined,
|
|
|
|
width: data.width
|
|
||
|
|
null
|
|
,
|
|
|
|
height: data.height
|
|
||
|
|
null
|
|
,
|
|
|
|
score: data.score
|
|
||
|
|
null
|
|
,
|
|
|
|
is_selected: data.is_selected
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
notes: data.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
exported_at: data.exported_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: data.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
},
|
|
{ transaction },
|
|
);
|
|
|
|
|
|
await generated_images.setJob( data.job || null, {
|
|
transaction,
|
|
});
|
|
|
|
await generated_images.setOwner( data.owner || null, {
|
|
transaction,
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.generated_images.getTableName(),
|
|
belongsToColumn: 'image_file',
|
|
belongsToId: generated_images.id,
|
|
},
|
|
data.image_file,
|
|
options,
|
|
);
|
|
|
|
|
|
return generated_images;
|
|
}
|
|
|
|
|
|
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 generated_imagesData = data.map((item, index) => ({
|
|
id: item.id || undefined,
|
|
|
|
width: item.width
|
|
||
|
|
null
|
|
,
|
|
|
|
height: item.height
|
|
||
|
|
null
|
|
,
|
|
|
|
score: item.score
|
|
||
|
|
null
|
|
,
|
|
|
|
is_selected: item.is_selected
|
|
||
|
|
false
|
|
|
|
,
|
|
|
|
notes: item.notes
|
|
||
|
|
null
|
|
,
|
|
|
|
exported_at: item.exported_at
|
|
||
|
|
null
|
|
,
|
|
|
|
importHash: item.importHash || null,
|
|
createdById: currentUser.id,
|
|
updatedById: currentUser.id,
|
|
createdAt: new Date(Date.now() + index * 1000),
|
|
}));
|
|
|
|
// Bulk create items
|
|
const generated_images = await db.generated_images.bulkCreate(generated_imagesData, { transaction });
|
|
|
|
// For each item created, replace relation files
|
|
|
|
for (let i = 0; i < generated_images.length; i++) {
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.generated_images.getTableName(),
|
|
belongsToColumn: 'image_file',
|
|
belongsToId: generated_images[i].id,
|
|
},
|
|
data[i].image_file,
|
|
options,
|
|
);
|
|
}
|
|
|
|
|
|
return generated_images;
|
|
}
|
|
|
|
static async update(id, data, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
|
|
const generated_images = await db.generated_images.findByPk(id, {}, {transaction});
|
|
|
|
|
|
|
|
|
|
const updatePayload = {};
|
|
|
|
if (data.width !== undefined) updatePayload.width = data.width;
|
|
|
|
|
|
if (data.height !== undefined) updatePayload.height = data.height;
|
|
|
|
|
|
if (data.score !== undefined) updatePayload.score = data.score;
|
|
|
|
|
|
if (data.is_selected !== undefined) updatePayload.is_selected = data.is_selected;
|
|
|
|
|
|
if (data.notes !== undefined) updatePayload.notes = data.notes;
|
|
|
|
|
|
if (data.exported_at !== undefined) updatePayload.exported_at = data.exported_at;
|
|
|
|
|
|
updatePayload.updatedById = currentUser.id;
|
|
|
|
await generated_images.update(updatePayload, {transaction});
|
|
|
|
|
|
|
|
if (data.job !== undefined) {
|
|
await generated_images.setJob(
|
|
|
|
data.job,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
if (data.owner !== undefined) {
|
|
await generated_images.setOwner(
|
|
|
|
data.owner,
|
|
|
|
{ transaction }
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await FileDBApi.replaceRelationFiles(
|
|
{
|
|
belongsTo: db.generated_images.getTableName(),
|
|
belongsToColumn: 'image_file',
|
|
belongsToId: generated_images.id,
|
|
},
|
|
data.image_file,
|
|
options,
|
|
);
|
|
|
|
|
|
return generated_images;
|
|
}
|
|
|
|
static async deleteByIds(ids, options) {
|
|
const currentUser = (options && options.currentUser) || { id: null };
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const generated_images = await db.generated_images.findAll({
|
|
where: {
|
|
id: {
|
|
[Op.in]: ids,
|
|
},
|
|
},
|
|
transaction,
|
|
});
|
|
|
|
await db.sequelize.transaction(async (transaction) => {
|
|
for (const record of generated_images) {
|
|
await record.update(
|
|
{deletedBy: currentUser.id},
|
|
{transaction}
|
|
);
|
|
}
|
|
for (const record of generated_images) {
|
|
await record.destroy({transaction});
|
|
}
|
|
});
|
|
|
|
|
|
return generated_images;
|
|
}
|
|
|
|
static async remove(id, options) {
|
|
const currentUser = (options && options.currentUser) || {id: null};
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const generated_images = await db.generated_images.findByPk(id, options);
|
|
|
|
await generated_images.update({
|
|
deletedBy: currentUser.id
|
|
}, {
|
|
transaction,
|
|
});
|
|
|
|
await generated_images.destroy({
|
|
transaction
|
|
});
|
|
|
|
return generated_images;
|
|
}
|
|
|
|
static async findBy(where, options) {
|
|
const transaction = (options && options.transaction) || undefined;
|
|
|
|
const generated_images = await db.generated_images.findOne(
|
|
{ where },
|
|
{ transaction },
|
|
);
|
|
|
|
if (!generated_images) {
|
|
return generated_images;
|
|
}
|
|
|
|
const output = generated_images.get({plain: true});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output.job = await generated_images.getJob({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.owner = await generated_images.getOwner({
|
|
transaction
|
|
});
|
|
|
|
|
|
output.image_file = await generated_images.getImage_file({
|
|
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.generation_jobs,
|
|
as: 'job',
|
|
|
|
where: filter.job ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.job.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
prompt: {
|
|
[Op.or]: filter.job.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
{
|
|
model: db.users,
|
|
as: 'owner',
|
|
|
|
where: filter.owner ? {
|
|
[Op.or]: [
|
|
{ id: { [Op.in]: filter.owner.split('|').map(term => Utils.uuid(term)) } },
|
|
{
|
|
firstName: {
|
|
[Op.or]: filter.owner.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
|
|
}
|
|
},
|
|
]
|
|
} : {},
|
|
|
|
},
|
|
|
|
|
|
|
|
{
|
|
model: db.file,
|
|
as: 'image_file',
|
|
},
|
|
|
|
];
|
|
|
|
if (filter) {
|
|
if (filter.id) {
|
|
where = {
|
|
...where,
|
|
['id']: Utils.uuid(filter.id),
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.notes) {
|
|
where = {
|
|
...where,
|
|
[Op.and]: Utils.ilike(
|
|
'generated_images',
|
|
'notes',
|
|
filter.notes,
|
|
),
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (filter.widthRange) {
|
|
const [start, end] = filter.widthRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
width: {
|
|
...where.width,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
width: {
|
|
...where.width,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.heightRange) {
|
|
const [start, end] = filter.heightRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
height: {
|
|
...where.height,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
height: {
|
|
...where.height,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.scoreRange) {
|
|
const [start, end] = filter.scoreRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
score: {
|
|
...where.score,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
score: {
|
|
...where.score,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
if (filter.exported_atRange) {
|
|
const [start, end] = filter.exported_atRange;
|
|
|
|
if (start !== undefined && start !== null && start !== '') {
|
|
where = {
|
|
...where,
|
|
exported_at: {
|
|
...where.exported_at,
|
|
[Op.gte]: start,
|
|
},
|
|
};
|
|
}
|
|
|
|
if (end !== undefined && end !== null && end !== '') {
|
|
where = {
|
|
...where,
|
|
exported_at: {
|
|
...where.exported_at,
|
|
[Op.lte]: end,
|
|
},
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
if (filter.active !== undefined) {
|
|
where = {
|
|
...where,
|
|
active: filter.active === true || filter.active === 'true'
|
|
};
|
|
}
|
|
|
|
|
|
if (filter.is_selected) {
|
|
where = {
|
|
...where,
|
|
is_selected: filter.is_selected,
|
|
};
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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.generated_images.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(
|
|
'generated_images',
|
|
'notes',
|
|
query,
|
|
),
|
|
],
|
|
};
|
|
}
|
|
|
|
const records = await db.generated_images.findAll({
|
|
attributes: [ 'id', 'notes' ],
|
|
where,
|
|
limit: limit ? Number(limit) : undefined,
|
|
offset: offset ? Number(offset) : undefined,
|
|
orderBy: [['notes', 'ASC']],
|
|
});
|
|
|
|
return records.map((record) => ({
|
|
id: record.id,
|
|
label: record.notes,
|
|
}));
|
|
}
|
|
|
|
|
|
};
|
|
|