-
Notifications
You must be signed in to change notification settings - Fork 70
/
postgrator.d.ts
200 lines (176 loc) · 4.86 KB
/
postgrator.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import { ConnectionOptions } from "tls";
declare namespace Postgrator {
/**
* A common query result
*/
export interface QueryResult {
rows: any[];
fields: any;
}
/**
* A migration object
*/
export interface Migration {
version: number;
action: "do" | "undo";
filename: string;
name: string;
md5: string;
getSql: () => string;
}
export interface BaseOptions {
schemaTable?: string;
validateChecksums?: boolean;
migrationPattern?: string;
newline?: string;
execQuery?: (query: string) => Promise<{ rows: any[] }>;
execSqlScript?: (sqlScript: string) => Promise<void>;
}
/**
* Configuration options for MySQL connections
*/
export interface MySQLOptions extends BaseOptions {
driver: "mysql";
database?: string;
}
/**
* Configuration options for PostgreSQL connections
*/
export interface PostgreSQLOptions extends BaseOptions {
driver: "pg";
database?: string;
currentSchema?: string;
}
/**
* Configuration options for Microsoft SQL Server connections
*/
export interface MsSQLOptions extends BaseOptions {
driver: "mssql";
database: string;
currentSchema?: string;
}
/**
* Configuration options for Sqlite3
*/
export interface Sqlite3Options extends BaseOptions {
driver: "sqlite3";
currentSchema?: string;
}
type Options =
| PostgreSQLOptions
| MySQLOptions
| MsSQLOptions
| Sqlite3Options;
/**
* A migration event handler
*
* @param migration the migration object representing this event
*/
export type MigrationEventCallback = (
migration: Postgrator.Migration
) => void;
}
declare class Postgrator {
/**
* Creates an instance of the postgrator class
* @param options Configuration options
*/
constructor(options: Postgrator.Options);
/**
* Reads all migrations from directory
*
* @returns array of migration objects
*/
getMigrations(): Promise<Postgrator.Migration[]>;
/**
* Executes sql query using the common client and ends connection afterwards
*
* @returns result of query
* @param query sql query to execute
*/
runQuery(query: string): Promise<Postgrator.QueryResult>;
/**
* Executes user-supplied db migration script consisting of multiple SQL statements
*
* @param sqlScript sql queries to execute
*/
runSqlScript(sqlScript: string): Promise<void>;
/**
* Gets the database version of the schema from the database.
* Otherwise 0 if no version has been run
*
* @returns database schema version
*/
getDatabaseVersion(): Promise<number>;
/**
* Returns an object with max version of migration available
*
* @returns
*/
getMaxVersion(): Promise<number>;
/**
* Validate md5 checksums for applied migrations
*
* @returns
* @param databaseVersion
*/
validateMigrations(databaseVersion: number): Promise<undefined>;
/**
* Runs the migrations in the order to reach target version
*
* @returns Array of migration objects to appled to database
* @param migrations Array of migration objects to apply to database
*/
runMigrations(
migrations?: Postgrator.Migration[]
): Promise<Postgrator.Migration[]>;
/**
* returns an array of relevant migrations based on the target and database version passed.
* returned array is sorted in the order it needs to be run
*
* @returns Sorted array of relevant migration objects
* @param databaseVersion
* @param targetVersion
*/
getRunnableMigrations(
databaseVersion: number,
targetVersion: number
): Postgrator.Migration[];
/**
* Main method to move a schema to a particular version.
* A target must be specified, otherwise nothing is run.
*
* @returns
* @param target version to migrate as string or number (handled as numbers internally)
*/
migrate(target?: string): Promise<Postgrator.Migration[]>;
/**
* Registers an event lister for the `validation-started` event
*
* @param event the event name
* @param cb the callback to handle the event
*/
on(event: "validation-started", cb: Postgrator.MigrationEventCallback): void;
/**
* Registers an event lister for the `validation-finished` event
*
* @param event the event name
* @param cb the callback to handle the event
*/
on(event: "validation-finished", cb: Postgrator.MigrationEventCallback): void;
/**
* Registers an event lister for the `migration-started` event
*
* @param event the event name
* @param cb the callback to handle the event
*/
on(event: "migration-started", cb: Postgrator.MigrationEventCallback): void;
/**
* Registers an event lister for the `migration-finished` event
*
* @param event the event name
* @param cb the callback to handle the event
*/
on(event: "migration-finished", cb: Postgrator.MigrationEventCallback): void;
}
export default Postgrator;