UNPKG

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