39937-vm/backend/src/db/api/image_edits.js
2026-05-10 04:12:41 +00:00

1384 lines
35 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 Image_editsDBApi {
static async create(data, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const image_edits = await db.image_edits.create(
{
id: data.id || undefined,
title: data.title
||
null
,
prompt: data.prompt
||
null
,
negative_prompt: data.negative_prompt
||
null
,
edit_mode: data.edit_mode
||
null
,
status: data.status
||
null
,
strength: data.strength
||
null
,
steps: data.steps
||
null
,
guidance_scale: data.guidance_scale
||
null
,
seed: data.seed
||
null
,
random_seed: data.random_seed
||
false
,
output_width: data.output_width
||
null
,
output_height: data.output_height
||
null
,
denoise: data.denoise
||
null
,
scheduler: data.scheduler
||
null
,
preserve_faces: data.preserve_faces
||
false
,
enhance_prompt: data.enhance_prompt
||
false
,
mask_blur: data.mask_blur
||
null
,
inpaint_feather: data.inpaint_feather
||
null
,
output_format: data.output_format
||
null
,
result_file_size_bytes: data.result_file_size_bytes
||
null
,
duration_ms: data.duration_ms
||
null
,
error_message: data.error_message
||
null
,
queued_at: data.queued_at
||
null
,
started_at: data.started_at
||
null
,
finished_at: data.finished_at
||
null
,
is_favorite: data.is_favorite
||
false
,
importHash: data.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
},
{ transaction },
);
await image_edits.setOwner( data.owner || null, {
transaction,
});
await image_edits.setProject( data.project || null, {
transaction,
});
await image_edits.setSource_image( data.source_image || null, {
transaction,
});
await image_edits.setModel( data.model || null, {
transaction,
});
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'mask_image',
belongsToId: image_edits.id,
},
data.mask_image,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'result_image',
belongsToId: image_edits.id,
},
data.result_image,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'result_file',
belongsToId: image_edits.id,
},
data.result_file,
options,
);
return image_edits;
}
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 image_editsData = data.map((item, index) => ({
id: item.id || undefined,
title: item.title
||
null
,
prompt: item.prompt
||
null
,
negative_prompt: item.negative_prompt
||
null
,
edit_mode: item.edit_mode
||
null
,
status: item.status
||
null
,
strength: item.strength
||
null
,
steps: item.steps
||
null
,
guidance_scale: item.guidance_scale
||
null
,
seed: item.seed
||
null
,
random_seed: item.random_seed
||
false
,
output_width: item.output_width
||
null
,
output_height: item.output_height
||
null
,
denoise: item.denoise
||
null
,
scheduler: item.scheduler
||
null
,
preserve_faces: item.preserve_faces
||
false
,
enhance_prompt: item.enhance_prompt
||
false
,
mask_blur: item.mask_blur
||
null
,
inpaint_feather: item.inpaint_feather
||
null
,
output_format: item.output_format
||
null
,
result_file_size_bytes: item.result_file_size_bytes
||
null
,
duration_ms: item.duration_ms
||
null
,
error_message: item.error_message
||
null
,
queued_at: item.queued_at
||
null
,
started_at: item.started_at
||
null
,
finished_at: item.finished_at
||
null
,
is_favorite: item.is_favorite
||
false
,
importHash: item.importHash || null,
createdById: currentUser.id,
updatedById: currentUser.id,
createdAt: new Date(Date.now() + index * 1000),
}));
// Bulk create items
const image_edits = await db.image_edits.bulkCreate(image_editsData, { transaction });
// For each item created, replace relation files
for (let i = 0; i < image_edits.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'mask_image',
belongsToId: image_edits[i].id,
},
data[i].mask_image,
options,
);
}
for (let i = 0; i < image_edits.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'result_image',
belongsToId: image_edits[i].id,
},
data[i].result_image,
options,
);
}
for (let i = 0; i < image_edits.length; i++) {
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'result_file',
belongsToId: image_edits[i].id,
},
data[i].result_file,
options,
);
}
return image_edits;
}
static async update(id, data, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const image_edits = await db.image_edits.findByPk(id, {}, {transaction});
const updatePayload = {};
if (data.title !== undefined) updatePayload.title = data.title;
if (data.prompt !== undefined) updatePayload.prompt = data.prompt;
if (data.negative_prompt !== undefined) updatePayload.negative_prompt = data.negative_prompt;
if (data.edit_mode !== undefined) updatePayload.edit_mode = data.edit_mode;
if (data.status !== undefined) updatePayload.status = data.status;
if (data.strength !== undefined) updatePayload.strength = data.strength;
if (data.steps !== undefined) updatePayload.steps = data.steps;
if (data.guidance_scale !== undefined) updatePayload.guidance_scale = data.guidance_scale;
if (data.seed !== undefined) updatePayload.seed = data.seed;
if (data.random_seed !== undefined) updatePayload.random_seed = data.random_seed;
if (data.output_width !== undefined) updatePayload.output_width = data.output_width;
if (data.output_height !== undefined) updatePayload.output_height = data.output_height;
if (data.denoise !== undefined) updatePayload.denoise = data.denoise;
if (data.scheduler !== undefined) updatePayload.scheduler = data.scheduler;
if (data.preserve_faces !== undefined) updatePayload.preserve_faces = data.preserve_faces;
if (data.enhance_prompt !== undefined) updatePayload.enhance_prompt = data.enhance_prompt;
if (data.mask_blur !== undefined) updatePayload.mask_blur = data.mask_blur;
if (data.inpaint_feather !== undefined) updatePayload.inpaint_feather = data.inpaint_feather;
if (data.output_format !== undefined) updatePayload.output_format = data.output_format;
if (data.result_file_size_bytes !== undefined) updatePayload.result_file_size_bytes = data.result_file_size_bytes;
if (data.duration_ms !== undefined) updatePayload.duration_ms = data.duration_ms;
if (data.error_message !== undefined) updatePayload.error_message = data.error_message;
if (data.queued_at !== undefined) updatePayload.queued_at = data.queued_at;
if (data.started_at !== undefined) updatePayload.started_at = data.started_at;
if (data.finished_at !== undefined) updatePayload.finished_at = data.finished_at;
if (data.is_favorite !== undefined) updatePayload.is_favorite = data.is_favorite;
updatePayload.updatedById = currentUser.id;
await image_edits.update(updatePayload, {transaction});
if (data.owner !== undefined) {
await image_edits.setOwner(
data.owner,
{ transaction }
);
}
if (data.project !== undefined) {
await image_edits.setProject(
data.project,
{ transaction }
);
}
if (data.source_image !== undefined) {
await image_edits.setSource_image(
data.source_image,
{ transaction }
);
}
if (data.model !== undefined) {
await image_edits.setModel(
data.model,
{ transaction }
);
}
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'mask_image',
belongsToId: image_edits.id,
},
data.mask_image,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'result_image',
belongsToId: image_edits.id,
},
data.result_image,
options,
);
await FileDBApi.replaceRelationFiles(
{
belongsTo: db.image_edits.getTableName(),
belongsToColumn: 'result_file',
belongsToId: image_edits.id,
},
data.result_file,
options,
);
return image_edits;
}
static async deleteByIds(ids, options) {
const currentUser = (options && options.currentUser) || { id: null };
const transaction = (options && options.transaction) || undefined;
const image_edits = await db.image_edits.findAll({
where: {
id: {
[Op.in]: ids,
},
},
transaction,
});
await db.sequelize.transaction(async (transaction) => {
for (const record of image_edits) {
await record.update(
{deletedBy: currentUser.id},
{transaction}
);
}
for (const record of image_edits) {
await record.destroy({transaction});
}
});
return image_edits;
}
static async remove(id, options) {
const currentUser = (options && options.currentUser) || {id: null};
const transaction = (options && options.transaction) || undefined;
const image_edits = await db.image_edits.findByPk(id, options);
await image_edits.update({
deletedBy: currentUser.id
}, {
transaction,
});
await image_edits.destroy({
transaction
});
return image_edits;
}
static async findBy(where, options) {
const transaction = (options && options.transaction) || undefined;
const image_edits = await db.image_edits.findOne(
{ where },
{ transaction },
);
if (!image_edits) {
return image_edits;
}
const output = image_edits.get({plain: true});
output.edit_parameters_image_edit = await image_edits.getEdit_parameters_image_edit({
transaction
});
output.jobs_image_edit = await image_edits.getJobs_image_edit({
transaction
});
output.exports_image_edit = await image_edits.getExports_image_edit({
transaction
});
output.owner = await image_edits.getOwner({
transaction
});
output.project = await image_edits.getProject({
transaction
});
output.source_image = await image_edits.getSource_image({
transaction
});
output.model = await image_edits.getModel({
transaction
});
output.mask_image = await image_edits.getMask_image({
transaction
});
output.result_image = await image_edits.getResult_image({
transaction
});
output.result_file = await image_edits.getResult_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.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.projects,
as: 'project',
where: filter.project ? {
[Op.or]: [
{ id: { [Op.in]: filter.project.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.project.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.source_images,
as: 'source_image',
where: filter.source_image ? {
[Op.or]: [
{ id: { [Op.in]: filter.source_image.split('|').map(term => Utils.uuid(term)) } },
{
title: {
[Op.or]: filter.source_image.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.ai_models,
as: 'model',
where: filter.model ? {
[Op.or]: [
{ id: { [Op.in]: filter.model.split('|').map(term => Utils.uuid(term)) } },
{
name: {
[Op.or]: filter.model.split('|').map(term => ({ [Op.iLike]: `%${term}%` }))
}
},
]
} : {},
},
{
model: db.file,
as: 'mask_image',
},
{
model: db.file,
as: 'result_image',
},
{
model: db.file,
as: 'result_file',
},
];
if (filter) {
if (filter.id) {
where = {
...where,
['id']: Utils.uuid(filter.id),
};
}
if (filter.title) {
where = {
...where,
[Op.and]: Utils.ilike(
'image_edits',
'title',
filter.title,
),
};
}
if (filter.prompt) {
where = {
...where,
[Op.and]: Utils.ilike(
'image_edits',
'prompt',
filter.prompt,
),
};
}
if (filter.negative_prompt) {
where = {
...where,
[Op.and]: Utils.ilike(
'image_edits',
'negative_prompt',
filter.negative_prompt,
),
};
}
if (filter.error_message) {
where = {
...where,
[Op.and]: Utils.ilike(
'image_edits',
'error_message',
filter.error_message,
),
};
}
if (filter.strengthRange) {
const [start, end] = filter.strengthRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
strength: {
...where.strength,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
strength: {
...where.strength,
[Op.lte]: end,
},
};
}
}
if (filter.stepsRange) {
const [start, end] = filter.stepsRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
steps: {
...where.steps,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
steps: {
...where.steps,
[Op.lte]: end,
},
};
}
}
if (filter.guidance_scaleRange) {
const [start, end] = filter.guidance_scaleRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
guidance_scale: {
...where.guidance_scale,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
guidance_scale: {
...where.guidance_scale,
[Op.lte]: end,
},
};
}
}
if (filter.seedRange) {
const [start, end] = filter.seedRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
seed: {
...where.seed,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
seed: {
...where.seed,
[Op.lte]: end,
},
};
}
}
if (filter.output_widthRange) {
const [start, end] = filter.output_widthRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
output_width: {
...where.output_width,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
output_width: {
...where.output_width,
[Op.lte]: end,
},
};
}
}
if (filter.output_heightRange) {
const [start, end] = filter.output_heightRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
output_height: {
...where.output_height,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
output_height: {
...where.output_height,
[Op.lte]: end,
},
};
}
}
if (filter.denoiseRange) {
const [start, end] = filter.denoiseRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
denoise: {
...where.denoise,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
denoise: {
...where.denoise,
[Op.lte]: end,
},
};
}
}
if (filter.mask_blurRange) {
const [start, end] = filter.mask_blurRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
mask_blur: {
...where.mask_blur,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
mask_blur: {
...where.mask_blur,
[Op.lte]: end,
},
};
}
}
if (filter.inpaint_featherRange) {
const [start, end] = filter.inpaint_featherRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
inpaint_feather: {
...where.inpaint_feather,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
inpaint_feather: {
...where.inpaint_feather,
[Op.lte]: end,
},
};
}
}
if (filter.result_file_size_bytesRange) {
const [start, end] = filter.result_file_size_bytesRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
result_file_size_bytes: {
...where.result_file_size_bytes,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
result_file_size_bytes: {
...where.result_file_size_bytes,
[Op.lte]: end,
},
};
}
}
if (filter.duration_msRange) {
const [start, end] = filter.duration_msRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
duration_ms: {
...where.duration_ms,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
duration_ms: {
...where.duration_ms,
[Op.lte]: end,
},
};
}
}
if (filter.queued_atRange) {
const [start, end] = filter.queued_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
queued_at: {
...where.queued_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
queued_at: {
...where.queued_at,
[Op.lte]: end,
},
};
}
}
if (filter.started_atRange) {
const [start, end] = filter.started_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
started_at: {
...where.started_at,
[Op.lte]: end,
},
};
}
}
if (filter.finished_atRange) {
const [start, end] = filter.finished_atRange;
if (start !== undefined && start !== null && start !== '') {
where = {
...where,
finished_at: {
...where.finished_at,
[Op.gte]: start,
},
};
}
if (end !== undefined && end !== null && end !== '') {
where = {
...where,
finished_at: {
...where.finished_at,
[Op.lte]: end,
},
};
}
}
if (filter.active !== undefined) {
where = {
...where,
active: filter.active === true || filter.active === 'true'
};
}
if (filter.edit_mode) {
where = {
...where,
edit_mode: filter.edit_mode,
};
}
if (filter.status) {
where = {
...where,
status: filter.status,
};
}
if (filter.random_seed) {
where = {
...where,
random_seed: filter.random_seed,
};
}
if (filter.scheduler) {
where = {
...where,
scheduler: filter.scheduler,
};
}
if (filter.preserve_faces) {
where = {
...where,
preserve_faces: filter.preserve_faces,
};
}
if (filter.enhance_prompt) {
where = {
...where,
enhance_prompt: filter.enhance_prompt,
};
}
if (filter.output_format) {
where = {
...where,
output_format: filter.output_format,
};
}
if (filter.is_favorite) {
where = {
...where,
is_favorite: filter.is_favorite,
};
}
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.image_edits.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(
'image_edits',
'title',
query,
),
],
};
}
const records = await db.image_edits.findAll({
attributes: [ 'id', 'title' ],
where,
limit: limit ? Number(limit) : undefined,
offset: offset ? Number(offset) : undefined,
orderBy: [['title', 'ASC']],
});
return records.map((record) => ({
id: record.id,
label: record.title,
}));
}
};