Performance updates

This commit is contained in:
Gerard Lamusse
2025-03-09 20:52:08 +01:00
parent f73c15c196
commit 18bd3d9428
5 changed files with 92 additions and 84 deletions

View File

@@ -1,4 +1,8 @@
# Version 3.0.7 # Version 3.0.8
- **Fixed** Slight performance improvement when importing data.
## Version 3.0.7
- **Fixed** SCHEMA_SYNC_DATA_ONLY being the opposite of what it should be. - **Fixed** SCHEMA_SYNC_DATA_ONLY being the opposite of what it should be.

View File

@@ -82,8 +82,12 @@ class CollectionExporter implements IExporter {
public export = () => this._persistQueue(); public export = () => this._persistQueue();
public async load(merge = false) { public async load(merge = false) {
if (await ExportHelper.fileExists(this.filePath)) { let json;
const json = await readFile(this.filePath, { encoding: 'utf8' }); try {
json = await readFile(this.filePath, { encoding: 'utf8' });
} catch (e) {
return null;
}
if (!json) { if (!json) {
throw new Error(`Collection ${this.name} has invalid content: ${json}`); throw new Error(`Collection ${this.name} has invalid content: ${json}`);
@@ -99,9 +103,6 @@ class CollectionExporter implements IExporter {
return await this.loadGroupedItems(parsedJSON, merge); return await this.loadGroupedItems(parsedJSON, merge);
} }
return null;
}
protected exportCollectionToFile = async () => { protected exportCollectionToFile = async () => {
const items = await this.getItemsForExport(); const items = await this.getItemsForExport();

View File

@@ -1,5 +1,5 @@
import { SchemaOverview } from '@directus/types'; import type { HookConfig } from '@directus/extensions';
import { HookConfig } from '@directus/extensions'; import type { SchemaOverview } from '@directus/types';
import { condenseAction } from './condenseAction'; import { condenseAction } from './condenseAction';
import { copyConfig } from './copyConfig'; import { copyConfig } from './copyConfig';
import { ExportManager } from './exportManager'; import { ExportManager } from './exportManager';

View File

@@ -29,9 +29,7 @@ export class SchemaExporter implements IExporter {
} else { } else {
// Clean up old schema files // Clean up old schema files
const files = await glob(this.schemaFilesPath('*')); const files = await glob(this.schemaFilesPath('*'));
for (const file of files) { await Promise.all(files.map(file => rm(file)));
await rm(file);
}
} }
}; };
@@ -50,8 +48,12 @@ export class SchemaExporter implements IExporter {
*/ */
public load = async () => { public load = async () => {
const svc = this._getSchemaService(); const svc = this._getSchemaService();
if (await ExportHelper.fileExists(this._filePath)) { let json;
const json = await readFile(this._filePath, { encoding: 'utf8' }); try {
json = await readFile(this._filePath, { encoding: 'utf8' });
} catch (e) {
return;
}
if (json) { if (json) {
const schemaParsed = JSON.parse(json); const schemaParsed = JSON.parse(json);
// For older versions, the snapshot was stored under the key `snapshot` // For older versions, the snapshot was stored under the key `snapshot`
@@ -68,7 +70,7 @@ export class SchemaExporter implements IExporter {
let found = 0; let found = 0;
const files = await glob(this.schemaFilesPath('*')); const files = await glob(this.schemaFilesPath('*'));
for (const file of files) { await Promise.all(files.map(async (file) => {
const collectionJson = await readFile(file, { encoding: 'utf8' }); const collectionJson = await readFile(file, { encoding: 'utf8' });
const { fields, relations, ...collectionInfo } = JSON.parse(collectionJson) as Collection & { const { fields, relations, ...collectionInfo } = JSON.parse(collectionJson) as Collection & {
fields: SnapshotField[]; fields: SnapshotField[];
@@ -87,7 +89,7 @@ export class SchemaExporter implements IExporter {
for (const relation of relations) { for (const relation of relations) {
snapshot.relations.push(Object.assign({ collection: collectionInfo.collection }, relation)); snapshot.relations.push(Object.assign({ collection: collectionInfo.collection }, relation));
} }
} }));
if (found === 0) { if (found === 0) {
this.logger.error('No schema files found in schema directory'); this.logger.error('No schema files found in schema directory');
@@ -116,7 +118,6 @@ export class SchemaExporter implements IExporter {
await svc.apply({ diff, hash: currentHash }); await svc.apply({ diff, hash: currentHash });
} }
} }
}
}; };
/** /**
@@ -170,10 +171,11 @@ export class SchemaExporter implements IExporter {
await writeFile(this._filePath, schemaJson); await writeFile(this._filePath, schemaJson);
// Save all collections with fields as individual files // Save all collections with fields as individual files
for (const [collection, item] of Object.entries(map)) { await Promise.all(
const itemJson = JSON.stringify(item, null, 2); Object.entries(map).map(([collection, item]) =>
await writeFile(this.schemaFilesPath(collection), itemJson); writeFile(this.schemaFilesPath(collection), JSON.stringify(item, null, 2))
} )
);
} else { } else {
const schemaJson = JSON.stringify(Object.assign({ hash }, snapshot), null, 2); const schemaJson = JSON.stringify(Object.assign({ hash }, snapshot), null, 2);
await writeFile(this._filePath, schemaJson); await writeFile(this._filePath, schemaJson);

View File

@@ -60,7 +60,7 @@ export class ExportHelper {
} }
static async getExportMeta() { static async getExportMeta() {
if (await this.fileExists(this.hashFile)) { try {
const content = await readFile(this.hashFile, { encoding: 'utf8' }); const content = await readFile(this.hashFile, { encoding: 'utf8' });
const [hash, ts] = content.split('@'); const [hash, ts] = content.split('@');
@@ -70,8 +70,9 @@ export class ExportHelper {
ts, ts,
}; };
} }
} catch {
// ignore
} }
return null; return null;
} }
} }