π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
fix: improve performance for attachment processing
mertmit213c4c7c5745
diff --git a/packages/nocodb/src/db/BaseModelSqlv2.ts b/packages/nocodb/src/db/BaseModelSqlv2.tsindex cce1bdf2ff..c410d357a9 100644--- a/packages/nocodb/src/db/BaseModelSqlv2.ts+++ b/packages/nocodb/src/db/BaseModelSqlv2.ts@@ -5582,191 +5582,164 @@ class BaseModelSqlv2 implements IBaseModelSqlV2 {d: Record<string, any>,) {try {- if (d) {- const promises = [];- for (const col of attachmentColumns) {- if (d[col.id] && typeof d[col.id] === 'string') {+ if (!d || !attachmentColumns.length) {+ return d;+ }++ const allAttachments = [];+ const allThumbnails = [];++ // First pass: parse JSON and collect all attachment instances (no deduplication)+ for (const col of attachmentColumns) {+ if (!d[col.id]) continue;++ // Parse JSON if needed+ if (typeof d[col.id] === 'string') {+ try {d[col.id] = JSON.parse(d[col.id]);+ } catch {+ continue;}+ }- if (d[col.id]?.length) {- d[col.id] = d[col.id].filter(- (attr) => !attr.id?.startsWith('temp_'),- );- for (let i = 0; i < d[col.id].length; i++) {- if (typeof d[col.id][i] === 'string') {- d[col.id][i] = JSON.parse(d[col.id][i]);- }+ if (!Array.isArray(d[col.id]) || !d[col.id].length) continue;- const attachment = d[col.id][i];-- // we expect array of array of attachments in case of lookup- if (Array.isArray(attachment)) {- for (const lookedUpAttachment of attachment) {- if (lookedUpAttachment?.path) {- promises.push(- PresignedUrl.signAttachment({- attachment: lookedUpAttachment,- filename: lookedUpAttachment.title,- }),- );+ // Process each attachment instance individually+ for (let i = 0; i < d[col.id].length; i++) {+ const item = d[col.id][i];- if (!supportsThumbnails(lookedUpAttachment)) {- continue;- }+ if (typeof item === 'string') {+ try {+ d[col.id][i] = JSON.parse(item);+ } catch {+ continue;+ }+ }- lookedUpAttachment.thumbnails = {- tiny: {},- small: {},- card_cover: {},- };-- const thumbnailPath = `thumbnails/${lookedUpAttachment.path.replace(- /^download[/\\]/i,- '',- )}`;-- for (const key of Object.keys(- lookedUpAttachment.thumbnails,- )) {- promises.push(- PresignedUrl.signAttachment({- attachment: {- ...lookedUpAttachment,- path: `${thumbnailPath}/${key}.jpg`,- },- filename: lookedUpAttachment.title,- mimetype: 'image/jpeg',- nestedKeys: ['thumbnails', key],- }),- );- }- } else if (lookedUpAttachment?.url) {- if (lookedUpAttachment?.url.startsWith('data:')) {- continue;- }+ const attachment = d[col.id][i];- promises.push(- PresignedUrl.signAttachment({- attachment: lookedUpAttachment,- filename: lookedUpAttachment.title,- }),- );+ if (attachment.id?.startsWith('temp_')) {+ // Skip temporary attachments+ continue;+ }- if (!supportsThumbnails(lookedUpAttachment)) {- continue;- }+ // Handle array of arrays (lookup case)+ if (Array.isArray(attachment)) {+ for (const lookedUpAttachment of attachment) {+ const thumbnails =+ this.prepareAttachmentForSigning(lookedUpAttachment);+ if (+ lookedUpAttachment &&+ (lookedUpAttachment.path || lookedUpAttachment.url)+ ) {+ allAttachments.push(lookedUpAttachment);+ allThumbnails.push(...thumbnails);+ }+ }+ } else {+ const thumbnails = this.prepareAttachmentForSigning(attachment);+ if (attachment && (attachment.path || attachment.url)) {+ allAttachments.push(attachment);+ allThumbnails.push(...thumbnails);+ }+ }+ }+ }- const thumbnailUrl = lookedUpAttachment.url.replace(- 'nc/uploads',- 'nc/thumbnails',- );+ // Batch process all attachment instances+ const batchSize = 50;+ const promises = [];++ // Process main attachments in batches+ for (let i = 0; i < allAttachments.length; i += batchSize) {+ const batch = allAttachments.slice(i, i + batchSize);+ const batchPromises = batch.map(+ (attachment) =>+ PresignedUrl.signAttachment({+ attachment,+ filename: attachment.title,+ }).catch(() => {}), // Ignore individual failures+ );+ promises.push(...batchPromises);+ }- lookedUpAttachment.thumbnails = {- tiny: {},- small: {},- card_cover: {},- };-- for (const key of Object.keys(- lookedUpAttachment.thumbnails,- )) {- promises.push(- PresignedUrl.signAttachment({- attachment: {- ...lookedUpAttachment,- url: `${thumbnailUrl}/${key}.jpg`,- },- filename: lookedUpAttachment.title,- mimetype: 'image/jpeg',- nestedKeys: ['thumbnails', key],- }),- );- }- }- }- } else {- if (attachment?.path) {- promises.push(- PresignedUrl.signAttachment({- attachment,- filename: attachment.title,- }),- );+ // Process thumbnails in batches+ for (let i = 0; i < allThumbnails.length; i += batchSize) {+ const batch = allThumbnails.slice(i, i + batchSize);+ const batchPromises = batch.map(+ ({ attachment, thumbnailKey, thumbnailPath }) =>+ PresignedUrl.signAttachment({+ attachment: {+ ...attachment,+ ...(attachment.path+ ? { path: thumbnailPath }+ : { url: thumbnailPath }),+ },+ filename: attachment.title,+ mimetype: 'image/jpeg',+ nestedKeys: ['thumbnails', thumbnailKey],+ }).catch(() => {}), // Ignore individual failures+ );+ promises.push(...batchPromises);+ }- if (!supportsThumbnails(attachment)) {- continue;- }+ // Wait for all batches to complete+ await Promise.all(promises);+ } catch (error) {+ // Log error but don't throw to avoid breaking the entire response+ console.warn('Error in _convertAttachmentType:', error.message);+ }- const thumbnailPath = `thumbnails/${attachment.path.replace(- /^download[/\\]/i,- '',- )}`;+ return d;+ }- attachment.thumbnails = {- tiny: {},- small: {},- card_cover: {},- };+ private prepareAttachmentForSigning(attachment: any) {+ const thumbnails = [];- for (const key of Object.keys(attachment.thumbnails)) {- promises.push(- PresignedUrl.signAttachment({- attachment: {- ...attachment,- path: `${thumbnailPath}/${key}.jpg`,- },- filename: attachment.title,- mimetype: 'image/jpeg',- nestedKeys: ['thumbnails', key],- }),- );- }- } else if (attachment?.url) {- if (attachment?.url.startsWith('data:')) {- continue;- }+ if (!attachment || (!attachment.path && !attachment.url)) {+ return thumbnails;+ }- promises.push(- PresignedUrl.signAttachment({- attachment,- filename: attachment.title,- }),- );+ // Skip data URLs+ if (attachment.url?.startsWith('data:')) {+ return thumbnails;+ }- const thumbhailUrl = attachment.url.replace(- 'nc/uploads',- 'nc/thumbnails',- );+ // Process thumbnails for images+ if (supportsThumbnails(attachment)) {+ attachment.thumbnails = {+ tiny: {},+ small: {},+ card_cover: {},+ };- attachment.thumbnails = {- tiny: {},- small: {},- card_cover: {},- };+ const thumbnailKeys = Object.keys(attachment.thumbnails);- for (const key of Object.keys(attachment.thumbnails)) {- promises.push(- PresignedUrl.signAttachment({- attachment: {- ...attachment,- url: `${thumbhailUrl}/${key}.jpg`,- },- filename: attachment.title,- mimetype: 'image/jpeg',- nestedKeys: ['thumbnails', key],- }),- );- }- }- }- }- }+ for (const key of thumbnailKeys) {+ let thumbnailPath: string;++ if (attachment.path) {+ const cleanPath = attachment.path.replace(/^download[/\\]/i, '');+ thumbnailPath = `thumbnails/${cleanPath}/${key}.jpg`;+ } else if (attachment.url) {+ const thumbnailUrl = attachment.url.replace(+ 'nc/uploads',+ 'nc/thumbnails',+ );+ thumbnailPath = `${thumbnailUrl}/${key}.jpg`;+ }++ if (thumbnailPath) {+ thumbnails.push({+ attachment,+ thumbnailKey: key,+ thumbnailPath,+ });}- await Promise.all(promises);}- } catch {}- return d;+ }++ return thumbnails;}protected async _convertJsonType(