UNPKG

11.4 kBTypeScriptView Raw
1
2import { TableColumn } from "../schema-builder/table/TableColumn";
3import { Table } from "../schema-builder/table/Table";
4import { TableForeignKey } from "../schema-builder/table/TableForeignKey";
5import { TableIndex } from "../schema-builder/table/TableIndex";
6import { Connection } from "../connection/Connection";
7import { ReadStream } from "../platform/PlatformTools";
8import { EntityManager } from "../entity-manager/EntityManager";
9import { ObjectLiteral } from "../common/ObjectLiteral";
10import { SqlInMemory } from "../driver/SqlInMemory";
11import { TableUnique } from "../schema-builder/table/TableUnique";
12import { View } from "../schema-builder/view/View";
13import { Broadcaster } from "../subscriber/Broadcaster";
14import { TableCheck } from "../schema-builder/table/TableCheck";
15import { IsolationLevel } from "../driver/types/IsolationLevel";
16import { TableExclusion } from "../schema-builder/table/TableExclusion";
17/**
18 * Runs queries on a single database connection.
19 */
20export interface QueryRunner {
21 /**
22 * Connection used by this query runner.
23 */
24 readonly connection: Connection;
25 /**
26 * Broadcaster used on this query runner to broadcast entity events.
27 */
28 readonly broadcaster: Broadcaster;
29 /**
30 * Entity manager working only with this query runner.
31 */
32 readonly manager: EntityManager;
33 /**
34 * Indicates if connection for this query runner is released.
35 * Once its released, query runner cannot run queries anymore.
36 */
37 readonly isReleased: boolean;
38 /**
39 * Indicates if transaction is in progress.
40 */
41 readonly isTransactionActive: boolean;
42 /**
43 * Stores temporarily user data.
44 * Useful for sharing data with subscribers.
45 */
46 data: ObjectLiteral;
47 /**
48 * All synchronized tables in the database.
49 */
50 loadedTables: Table[];
51 /**
52 * All synchronized views in the database.
53 */
54 loadedViews: View[];
55 /**
56 * Creates/uses database connection from the connection pool to perform further operations.
57 * Returns obtained database connection.
58 */
59 connect(): Promise<any>;
60 /**
61 * Releases used database connection.
62 * You cannot use query runner methods after connection is released.
63 */
64 release(): Promise<void>;
65 /**
66 * Removes all tables from the currently connected database.
67 * Be careful with using this method and avoid using it in production or migrations
68 * (because it can clear all your database).
69 */
70 clearDatabase(database?: string): Promise<void>;
71 /**
72 * Starts transaction.
73 */
74 startTransaction(isolationLevel?: IsolationLevel): Promise<void>;
75 /**
76 * Commits transaction.
77 * Error will be thrown if transaction was not started.
78 */
79 commitTransaction(): Promise<void>;
80 /**
81 * Ends transaction.
82 * Error will be thrown if transaction was not started.
83 */
84 rollbackTransaction(): Promise<void>;
85 /**
86 * Executes a given SQL query and returns raw database results.
87 */
88 query(query: string, parameters?: any[]): Promise<any>;
89 /**
90 * Returns raw data stream.
91 */
92 stream(query: string, parameters?: any[], onEnd?: Function, onError?: Function): Promise<ReadStream>;
93 /**
94 * Returns all available database names including system databases.
95 */
96 getDatabases(): Promise<string[]>;
97 /**
98 * Returns all available schema names including system schemas.
99 * If database parameter specified, returns schemas of that database.
100 * Useful for SQLServer and Postgres only.
101 */
102 getSchemas(database?: string): Promise<string[]>;
103 /**
104 * Loads a table by a given name from the database.
105 */
106 getTable(tablePath: string): Promise<Table | undefined>;
107 /**
108 * Loads all tables from the database and returns them.
109 *
110 * todo: make tablePaths optional
111 */
112 getTables(tablePaths: string[]): Promise<Table[]>;
113 /**
114 * Loads a view by a given name from the database.
115 */
116 getView(viewPath: string): Promise<View | undefined>;
117 /**
118 * Loads all views from the database and returns them.
119 */
120 getViews(viewPaths: string[]): Promise<View[]>;
121 /**
122 * Checks if a database with the given name exist.
123 */
124 hasDatabase(database: string): Promise<boolean>;
125 /**
126 * Checks if a schema with the given name exist.
127 */
128 hasSchema(schema: string): Promise<boolean>;
129 /**
130 * Checks if a table with the given name exist.
131 */
132 hasTable(table: Table | string): Promise<boolean>;
133 /**
134 * Checks if a column exist in the table.
135 */
136 hasColumn(table: Table | string, columnName: string): Promise<boolean>;
137 /**
138 * Creates a new database.
139 */
140 createDatabase(database: string, ifNotExist?: boolean): Promise<void>;
141 /**
142 * Drops database.
143 */
144 dropDatabase(database: string, ifExist?: boolean): Promise<void>;
145 /**
146 * Creates a new table schema.
147 */
148 createSchema(schemaPath: string, ifNotExist?: boolean): Promise<void>;
149 /**
150 * Drops table schema.
151 * For SqlServer can accept schema path (e.g. 'dbName.schemaName') as parameter.
152 * If schema path passed, it will drop schema in specified database.
153 */
154 dropSchema(schemaPath: string, ifExist?: boolean, isCascade?: boolean): Promise<void>;
155 /**
156 * Creates a new table.
157 */
158 createTable(table: Table, ifNotExist?: boolean, createForeignKeys?: boolean, createIndices?: boolean): Promise<void>;
159 /**
160 * Drops a table.
161 */
162 dropTable(table: Table | string, ifExist?: boolean, dropForeignKeys?: boolean, dropIndices?: boolean): Promise<void>;
163 /**
164 * Creates a new view.
165 */
166 createView(view: View, oldView?: View): Promise<void>;
167 /**
168 * Drops a view.
169 */
170 dropView(view: View | string): Promise<void>;
171 /**
172 * Renames a table.
173 */
174 renameTable(oldTableOrName: Table | string, newTableName: string): Promise<void>;
175 /**
176 * Adds a new column.
177 */
178 addColumn(table: Table | string, column: TableColumn): Promise<void>;
179 /**
180 * Adds new columns.
181 */
182 addColumns(table: Table | string, columns: TableColumn[]): Promise<void>;
183 /**
184 * Renames a column.
185 */
186 renameColumn(table: Table | string, oldColumnOrName: TableColumn | string, newColumnOrName: TableColumn | string): Promise<void>;
187 /**
188 * Changes a column in the table.
189 */
190 changeColumn(table: Table | string, oldColumn: TableColumn | string, newColumn: TableColumn): Promise<void>;
191 /**
192 * Changes columns in the table.
193 */
194 changeColumns(table: Table | string, changedColumns: {
195 oldColumn: TableColumn;
196 newColumn: TableColumn;
197 }[]): Promise<void>;
198 /**
199 * Drops a column in the table.
200 */
201 dropColumn(table: Table | string, column: TableColumn | string): Promise<void>;
202 /**
203 * Drops columns in the table.
204 */
205 dropColumns(table: Table | string, columns: TableColumn[]): Promise<void>;
206 /**
207 * Creates a new primary key.
208 */
209 createPrimaryKey(table: Table | string, columnNames: string[]): Promise<void>;
210 /**
211 * Updates composite primary keys.
212 */
213 updatePrimaryKeys(table: Table | string, columns: TableColumn[]): Promise<void>;
214 /**
215 * Drops a primary key.
216 */
217 dropPrimaryKey(table: Table | string): Promise<void>;
218 /**
219 * Creates a new unique constraint.
220 */
221 createUniqueConstraint(table: Table | string, uniqueConstraint: TableUnique): Promise<void>;
222 /**
223 * Creates new unique constraints.
224 */
225 createUniqueConstraints(table: Table | string, uniqueConstraints: TableUnique[]): Promise<void>;
226 /**
227 * Drops an unique constraint.
228 */
229 dropUniqueConstraint(table: Table | string, uniqueOrName: TableUnique | string): Promise<void>;
230 /**
231 * Drops unique constraints.
232 */
233 dropUniqueConstraints(table: Table | string, uniqueConstraints: TableUnique[]): Promise<void>;
234 /**
235 * Creates a new check constraint.
236 */
237 createCheckConstraint(table: Table | string, checkConstraint: TableCheck): Promise<void>;
238 /**
239 * Creates new check constraints.
240 */
241 createCheckConstraints(table: Table | string, checkConstraints: TableCheck[]): Promise<void>;
242 /**
243 * Drops a check constraint.
244 */
245 dropCheckConstraint(table: Table | string, checkOrName: TableCheck | string): Promise<void>;
246 /**
247 * Drops check constraints.
248 */
249 dropCheckConstraints(table: Table | string, checkConstraints: TableCheck[]): Promise<void>;
250 /**
251 * Creates a new exclusion constraint.
252 */
253 createExclusionConstraint(table: Table | string, exclusionConstraint: TableExclusion): Promise<void>;
254 /**
255 * Creates new exclusion constraints.
256 */
257 createExclusionConstraints(table: Table | string, exclusionConstraints: TableExclusion[]): Promise<void>;
258 /**
259 * Drops a exclusion constraint.
260 */
261 dropExclusionConstraint(table: Table | string, exclusionOrName: TableExclusion | string): Promise<void>;
262 /**
263 * Drops exclusion constraints.
264 */
265 dropExclusionConstraints(table: Table | string, exclusionConstraints: TableExclusion[]): Promise<void>;
266 /**
267 * Creates a new foreign key.
268 */
269 createForeignKey(table: Table | string, foreignKey: TableForeignKey): Promise<void>;
270 /**
271 * Creates new foreign keys.
272 */
273 createForeignKeys(table: Table | string, foreignKeys: TableForeignKey[]): Promise<void>;
274 /**
275 * Drops a foreign key.
276 */
277 dropForeignKey(table: Table | string, foreignKeyOrName: TableForeignKey | string): Promise<void>;
278 /**
279 * Drops foreign keys.
280 */
281 dropForeignKeys(table: Table | string, foreignKeys: TableForeignKey[]): Promise<void>;
282 /**
283 * Creates a new index.
284 */
285 createIndex(table: Table | string, index: TableIndex): Promise<void>;
286 /**
287 * Creates new indices.
288 */
289 createIndices(table: Table | string, indices: TableIndex[]): Promise<void>;
290 /**
291 * Drops an index.
292 */
293 dropIndex(table: Table | string, index: TableIndex | string): Promise<void>;
294 /**
295 * Drops indices.
296 */
297 dropIndices(table: Table | string, indices: TableIndex[]): Promise<void>;
298 /**
299 * Clears all table contents.
300 * Note: this operation uses SQL's TRUNCATE query which cannot be reverted in transactions.
301 */
302 clearTable(tableName: string): Promise<void>;
303 /**
304 * Enables special query runner mode in which sql queries won't be executed,
305 * instead they will be memorized into a special variable inside query runner.
306 * You can get memorized sql using getMemorySql() method.
307 */
308 enableSqlMemory(): void;
309 /**
310 * Disables special query runner mode in which sql queries won't be executed
311 * started by calling enableSqlMemory() method.
312 *
313 * Previously memorized sql will be flushed.
314 */
315 disableSqlMemory(): void;
316 /**
317 * Flushes all memorized sqls.
318 */
319 clearSqlMemory(): void;
320 /**
321 * Gets sql stored in the memory. Parameters in the sql are already replaced.
322 */
323 getMemorySql(): SqlInMemory;
324 /**
325 * Executes up sql queries.
326 */
327 executeMemoryUpSql(): Promise<void>;
328 /**
329 * Executes down sql queries.
330 */
331 executeMemoryDownSql(): Promise<void>;
332}