UNPKG

8.45 kBTypeScriptView Raw
1import { EventEmitter } from 'events';
2import Connection, { type InternalConnectionOptions } from './connection';
3import { Transform } from 'stream';
4import { type DataType, type Parameter } from './data-type';
5import { Collation } from './collation';
6/**
7 * @private
8 */
9interface InternalOptions {
10 checkConstraints: boolean;
11 fireTriggers: boolean;
12 keepNulls: boolean;
13 lockTable: boolean;
14 order: {
15 [columnName: string]: 'ASC' | 'DESC';
16 };
17}
18export interface Options {
19 /**
20 * Honors constraints during bulk load, using T-SQL
21 * [CHECK_CONSTRAINTS](https://technet.microsoft.com/en-us/library/ms186247(v=sql.105).aspx).
22 * (default: `false`)
23 */
24 checkConstraints?: InternalOptions['checkConstraints'] | undefined;
25 /**
26 * Honors insert triggers during bulk load, using the T-SQL [FIRE_TRIGGERS](https://technet.microsoft.com/en-us/library/ms187640(v=sql.105).aspx). (default: `false`)
27 */
28 fireTriggers?: InternalOptions['fireTriggers'] | undefined;
29 /**
30 * Honors null value passed, ignores the default values set on table, using T-SQL [KEEP_NULLS](https://msdn.microsoft.com/en-us/library/ms187887(v=sql.120).aspx). (default: `false`)
31 */
32 keepNulls?: InternalOptions['keepNulls'] | undefined;
33 /**
34 * Places a bulk update(BU) lock on table while performing bulk load, using T-SQL [TABLOCK](https://technet.microsoft.com/en-us/library/ms180876(v=sql.105).aspx). (default: `false`)
35 */
36 lockTable?: InternalOptions['lockTable'] | undefined;
37 /**
38 * Specifies the ordering of the data to possibly increase bulk insert performance, using T-SQL [ORDER](https://docs.microsoft.com/en-us/previous-versions/sql/sql-server-2008-r2/ms177468(v=sql.105)). (default: `{}`)
39 */
40 order?: InternalOptions['order'] | undefined;
41}
42export type Callback =
43/**
44 * A function which will be called after the [[BulkLoad]] finishes executing.
45 *
46 * @param rowCount the number of rows inserted
47 */
48(err: Error | undefined | null, rowCount?: number) => void;
49interface Column extends Parameter {
50 objName: string;
51 collation: Collation | undefined;
52}
53interface ColumnOptions {
54 output?: boolean;
55 /**
56 * For VarChar, NVarChar, VarBinary. Use length as `Infinity` for VarChar(max), NVarChar(max) and VarBinary(max).
57 */
58 length?: number;
59 /**
60 * For Numeric, Decimal.
61 */
62 precision?: number;
63 /**
64 * For Numeric, Decimal, Time, DateTime2, DateTimeOffset.
65 */
66 scale?: number;
67 /**
68 * If the name of the column is different from the name of the property found on `rowObj` arguments passed to [[addRow]], then you can use this option to specify the property name.
69 */
70 objName?: string;
71 /**
72 * Indicates whether the column accepts NULL values.
73 */
74 nullable?: boolean;
75}
76declare class RowTransform extends Transform {
77 /**
78 * @private
79 */
80 columnMetadataWritten: boolean;
81 /**
82 * @private
83 */
84 bulkLoad: BulkLoad;
85 /**
86 * @private
87 */
88 mainOptions: BulkLoad['options'];
89 /**
90 * @private
91 */
92 columns: BulkLoad['columns'];
93 /**
94 * @private
95 */
96 constructor(bulkLoad: BulkLoad);
97 /**
98 * @private
99 */
100 _transform(row: Array<unknown> | {
101 [colName: string]: unknown;
102 }, _encoding: string, callback: (error?: Error) => void): void;
103 /**
104 * @private
105 */
106 _flush(callback: () => void): void;
107}
108/**
109 * A BulkLoad instance is used to perform a bulk insert.
110 *
111 * Use [[Connection.newBulkLoad]] to create a new instance, and [[Connection.execBulkLoad]] to execute it.
112 *
113 * Example of BulkLoad Usages:
114 *
115 * ```js
116 * // optional BulkLoad options
117 * const options = { keepNulls: true };
118 *
119 * // instantiate - provide the table where you'll be inserting to, options and a callback
120 * const bulkLoad = connection.newBulkLoad('MyTable', options, (error, rowCount) => {
121 * console.log('inserted %d rows', rowCount);
122 * });
123 *
124 * // setup your columns - always indicate whether the column is nullable
125 * bulkLoad.addColumn('myInt', TYPES.Int, { nullable: false });
126 * bulkLoad.addColumn('myString', TYPES.NVarChar, { length: 50, nullable: true });
127 *
128 * // execute
129 * connection.execBulkLoad(bulkLoad, [
130 * { myInt: 7, myString: 'hello' },
131 * { myInt: 23, myString: 'world' }
132 * ]);
133 * ```
134 */
135declare class BulkLoad extends EventEmitter {
136 /**
137 * @private
138 */
139 error: Error | undefined;
140 /**
141 * @private
142 */
143 canceled: boolean;
144 /**
145 * @private
146 */
147 executionStarted: boolean;
148 /**
149 * @private
150 */
151 streamingMode: boolean;
152 /**
153 * @private
154 */
155 table: string;
156 /**
157 * @private
158 */
159 timeout: number | undefined;
160 /**
161 * @private
162 */
163 options: InternalConnectionOptions;
164 /**
165 * @private
166 */
167 callback: Callback;
168 /**
169 * @private
170 */
171 columns: Array<Column>;
172 /**
173 * @private
174 */
175 columnsByName: {
176 [name: string]: Column;
177 };
178 /**
179 * @private
180 */
181 firstRowWritten: boolean;
182 /**
183 * @private
184 */
185 rowToPacketTransform: RowTransform;
186 /**
187 * @private
188 */
189 bulkOptions: InternalOptions;
190 /**
191 * @private
192 */
193 connection: Connection | undefined;
194 /**
195 * @private
196 */
197 rows: Array<any> | undefined;
198 /**
199 * @private
200 */
201 rst: Array<any> | undefined;
202 /**
203 * @private
204 */
205 rowCount: number | undefined;
206 collation: Collation | undefined;
207 /**
208 * @private
209 */
210 constructor(table: string, collation: Collation | undefined, connectionOptions: InternalConnectionOptions, { checkConstraints, fireTriggers, keepNulls, lockTable, order, }: Options, callback: Callback);
211 /**
212 * Adds a column to the bulk load.
213 *
214 * The column definitions should match the table you are trying to insert into.
215 * Attempting to call addColumn after the first row has been added will throw an exception.
216 *
217 * ```js
218 * bulkLoad.addColumn('MyIntColumn', TYPES.Int, { nullable: false });
219 * ```
220 *
221 * @param name The name of the column.
222 * @param type One of the supported `data types`.
223 * @param __namedParameters Additional column type information. At a minimum, `nullable` must be set to true or false.
224 * @param length For VarChar, NVarChar, VarBinary. Use length as `Infinity` for VarChar(max), NVarChar(max) and VarBinary(max).
225 * @param nullable Indicates whether the column accepts NULL values.
226 * @param objName If the name of the column is different from the name of the property found on `rowObj` arguments passed to [[addRow]] or [[Connection.execBulkLoad]], then you can use this option to specify the property name.
227 * @param precision For Numeric, Decimal.
228 * @param scale For Numeric, Decimal, Time, DateTime2, DateTimeOffset.
229 */
230 addColumn(name: string, type: DataType, { output, length, precision, scale, objName, nullable }: ColumnOptions): void;
231 /**
232 * @private
233 */
234 getOptionsSql(): string;
235 /**
236 * @private
237 */
238 getBulkInsertSql(): string;
239 /**
240 * This is simply a helper utility function which returns a `CREATE TABLE SQL` statement based on the columns added to the bulkLoad object.
241 * This may be particularly handy when you want to insert into a temporary table (a table which starts with `#`).
242 *
243 * ```js
244 * var sql = bulkLoad.getTableCreationSql();
245 * ```
246 *
247 * A side note on bulk inserting into temporary tables: if you want to access a local temporary table after executing the bulk load,
248 * you'll need to use the same connection and execute your requests using [[Connection.execSqlBatch]] instead of [[Connection.execSql]]
249 */
250 getTableCreationSql(): string;
251 /**
252 * @private
253 */
254 getColMetaData(): Buffer;
255 /**
256 * Sets a timeout for this bulk load.
257 *
258 * ```js
259 * bulkLoad.setTimeout(timeout);
260 * ```
261 *
262 * @param timeout The number of milliseconds before the bulk load is considered failed, or 0 for no timeout.
263 * When no timeout is set for the bulk load, the [[ConnectionOptions.requestTimeout]] of the Connection is used.
264 */
265 setTimeout(timeout?: number): void;
266 /**
267 * @private
268 */
269 createDoneToken(): Buffer;
270 /**
271 * @private
272 */
273 cancel(): void;
274}
275export default BulkLoad;
276
\No newline at end of file