Skip to content

Instance Method

copyFrom(table:columns:format:logger:file:line:writeData:)

Copy data into a table using a COPY <table name> FROM STDIN query.
func copyFrom(table: String, columns: [String] = [], format: PostgresCopyFromFormat = .text(.init()), logger: Logger, file: String = #fileID, line: Int = #line, writeData: nonisolated(nonsending) (PostgresCopyFromWriter) async throws -> Void) async throws

Parameters

table

The name of the table into which to copy the data.

columns

The name of the columns to copy. If an empty array is passed, all columns are assumed to be copied.

format

Options that specify the format of the data that is produced by writeData.

logger

The Logger to log into for the operation.

file

The file the operation was started in. Used for better error reporting.

line

The line the operation was started in. Used for better error reporting.

writeData

Closure that produces the data for the table, to be streamed to the backend. Call write on the writer provided by the closure to send data to the backend and return from the closure once all data is sent. Throw an error from the closure to fail the data transfer. The error thrown by the closure will be rethrown by the copyFrom function.

Discussion

Important

The table and column names are inserted into the COPY FROM query as passed and might thus be susceptible to SQL injection. Ensure no untrusted data is contained in these strings.