@nocobase/plugin-action-import
Version:
Import records using excel templates. You can configure which fields to import and templates will be generated automatically.
80 lines (79 loc) • 3.1 kB
TypeScript
/**
* This file is part of the NocoBase (R) project.
* Copyright (c) 2020-2024 NocoBase Co., Ltd.
* Authors: NocoBase Team.
*
* This project is dual-licensed under AGPL-3.0 and NocoBase Commercial License.
* For more information, please refer to: https://www.nocobase.com/agreement.
*/
/// <reference types="node" />
import { ICollection, ICollectionManager } from '@nocobase/data-source-manager';
import { Model } from '@nocobase/database';
import { Transaction } from 'sequelize';
import EventEmitter from 'events';
import { Context } from '@nocobase/actions';
import { Logger } from '@nocobase/logger';
import { LoggerService } from '../utils';
export type ImportColumn = {
dataIndex: Array<string>;
defaultTitle: string;
title?: string;
description?: string;
};
export type ImporterOptions = {
collectionManager: ICollectionManager;
collection: ICollection;
columns: Array<ImportColumn>;
/** Parsed SheetJS workbook. Used for the synchronous (small-file) import path. */
workbook?: any;
/**
* Absolute path to the XLSX file on disk. When set, the importer uses ExcelJS
* streaming reader so the file is never fully loaded into memory — rows are
* yielded one by one and processed in chunks. This is the preferred path for
* large async imports.
*/
filePath?: string;
chunkSize?: number;
explain?: string;
repository?: any;
logger?: Logger;
rowDefaultValues?: Record<string, any>;
};
export type RunOptions = {
transaction?: Transaction;
context?: any;
};
export declare class XlsxImporter extends EventEmitter {
protected options: ImporterOptions;
private repository;
protected loggerService: LoggerService;
protected logger: Logger;
constructor(options: ImporterOptions);
beforePerformImport(data: string[][], options: RunOptions): Promise<string[][]>;
validateBySpaces(data: string[][], ctx?: Context): Promise<void>;
validate(ctx?: Context): Promise<string[][]>;
run(options?: RunOptions): Promise<any>;
resetSeq(options?: RunOptions): Promise<void>;
private getColumnsByPermission;
protected validateColumns(ctx?: Context): void;
performImport(data: string[][], options?: RunOptions): Promise<any>;
protected getModel(): typeof Model;
handleRowValuesWithColumns(row: any, rowValues: any, options: RunOptions, columns: ImportColumn[], rowIndex?: number): Promise<void>;
handleChuckRows(chunkRows: string[][], runOptions?: RunOptions, options?: {
handingRowIndex: number;
context: any;
}): Promise<void>;
performInsert(insertOptions: {
values: any[];
transaction: Transaction;
context: any;
hooks?: boolean;
}): Promise<Model<{}, {}>[]>;
associateRecords(targets: Model[], options?: any): Promise<void>;
renderErrorMessage(error: any): any;
trimString(str: string): string;
protected getExpectedHeaders(ctx?: Context): string[];
getData(ctx?: Context): Promise<string[][]>;
private alignWithHeaders;
private findAndValidateHeaders;
}