This repository has been archived by the owner on Aug 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
types.d.ts
270 lines (258 loc) · 9.3 KB
/
types.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* An instance of a single fixture which represents a single
* database record.
* @param name - Name of the fixture.
* @param data - Fixture data.
*/
declare class Fixture {
constructor(name: string, data: any);
/**
* Resolves all the relations on the fixture.
* @param relationMap - Collection of all unresolved relations.
* @param insertMap - Collection of all insertable fixtures.
*/
resolve(relationMap: Map<string, object>, insertMap: Map<string, object>): void;
/**
* Sets data onto the related columns.
* @param model - Instance of the current model.
* @param relations - Collection of relations to resolve.
* @param insertMap - Collection of all insertable fixtures.
*/
set(model: Model, relations: any, insertMap: Map<string, object>): void;
}
/**
* An instance of a single model which represents an instance
* of a Fixture, which in turn represents a record in the
* database.
* @param options - Configuration for Factory Zero.
* @param table - Table instance defined for this Model.
* @param data - Data object to bind to this instance.
*/
declare class Model {
constructor(options: ZeroOptions, table: Table, data: any);
}
/**
* Fixture resolver for resolving table columns and configured relationships.
* This supports polymorphism which is configurable via [ZeroOptions].
* @param options - Configuration for Factory Zero.
*/
declare class Resolver {
constructor(options: ZeroOptions);
/**
* Resolves relations and returns a map of all insertable relations.
* @returns - All fixtures with relations resolved.
*/
fixtures(): Map<string, object>;
/**
* Fetches a new instance of Table for each fixture file.
* @returns - Unresolved instances of Fixture.
*/
fixtureTables(): Fixture[];
/**
* Fetches a new instance of Model for each instance of Table.
* @param table - Table instance to resolve relations against.
* @returns - Unresolved instances of Fixture.
*/
fixtureModels(table: Table): Fixture[];
/**
* Searches the given fixture model for possible relatable columns.
* @param model - Model instance to search for relations on.
* @returns - Collection of relations to resolve later.
*/
relations(model: Model): any;
/**
* Applies polymorphism to columns that can be polymorphically related.
* @param model - Model instance to search for polymorphic relations on.
* @param relations - Collection of relations to check for polymorphism on.
*/
applyPolymorphism(model: Model, relations: any): void;
}
/**
* An instance of an individual database table.
* @param options - Configuration for Factory Zero.
* @param tableName - Name of the table on this instance.
* @param fixture - Fixture instance to bind onto this table instance.
*/
declare class Table {
constructor(options: ZeroOptions, tableName: string, fixture: Fixture);
/**
* Creates an options object with default values.
* @param model - Model instance to build the options against.
* @param options - Configuration for Factory Zero.
* @returns - Defaulted options.
*/
configure(model: Model, options: ZeroOptions): any;
/**
* Primary key configuration for this Table.
*/
pk: any;
/**
* All columns defined on the database table this instance refers to.
*/
columns: any;
/**
* A collection of Fixture instances relating to this table instance.
*/
fixtures: Fixture[];
}
declare module "Loader" {
/**
* Loads all fixture files into memory based on configuration defined
* in Zero's options.
* @param options - Zero options.
* @returns - All fixtures loaded from disk.
*/
function loadFixtures(options: any): any;
/**
* Retrieves the fixture file names from the configured directory.
* @param options - Zero options.
* @returns - Collection of fixture file names.
*/
function getFixtureFiles(options: any): string[];
/**
* Maps fixture files into an object.
* @param options - Zero options.
* @param files - Retrieved fixture files.
* @returns - Mapped fixtures into an object.
*/
function mapFixtures(options: any, files: object[]): any;
/**
* Imports an individual fixture file.
* @param options - Zero options.
* @param file - Individual file to import and name,
* @returns - Imported fixture file.
*/
function createFixtureMapping(options: any, file: any): (string | object)[];
/**
* Imports the fixture file via import or require, depending on the type.
* @param path - Directory path to the fixtures directory.
* @param ext - Fixture file type extension.
* @returns - Imported/required fixture file.
*/
function importFixture(path: string, ext: string): Promise | any;
}
declare module "SchemaInfo" {
/**
* Fetches all information schema objects related to all fixture files.
* @param db - Knex DB instance.
* @param fixtures - All fixtures loaded from files.
* @returns - Information schemas related to all fixture files.
*/
function getTablesInfo(db: Knex, fixtures: object[]): object[];
/**
* Fetches a single information schema table object.
* @param db - Knex DB instance.
* @param name - Information schema table name to fetch.
* @returns - Key/value of the fetched information schema.
*/
function queryInfoSchema(db: Knex, name: stirng): (string | object)[];
/**
* Mapping method for converting information schema query results to an array.
* @param columns - Information schema table columns.
* @returns - Key/value of the fetched information schema.
*/
function mapColumns(columns: any): (string | object)[];
}
/**
* Converts a Model instance to a plain object.
* @param instance - Instance of a Model.
* @returns - Plain object of the given Model instance.
*/
declare function toJson(instance: Model): any;
/**
* Clones an object, including getters/setters and class prototypes.
* @param object - Data object to clone.
* @returns - Cloned object.
*/
declare function clone(object: any): any;
/**
* Filters an object via a predicate over the object's keys.
* @param object - Object data to filter over.
* @param predicate - Test function.
* @returns - Filtered object.
*/
declare function filterKeys(object: any, predicate: (...params: any[]) => any): any;
/**
* Filters an object via a predicate over the object's values.
* @param object - Object data to filter over.
* @param predicate - Test function.
* @returns - Filtered object.
*/
declare function filterValues(object: any, predicate: (...params: any[]) => any): any;
/**
* Factory Zero options schema.
* @property extensions - Fixture file extensions to search for.
* @property snaked - Should file names be serialised in snake case?
* @property pk - Default primary key for all models.
* @property [col] - Primary key column name.
* @property [type] - Primary key column type.
* @property directory - Path to fixture files.
* @property keys - Configurable keys for storing options relating to this program.
* @property keys.model - Key to use for model options within the fixtures.
* @property suffixes - Polymorphic column suffixes.
* @property suffixes.type - Suffix used for the polymorphic type column.
* @property suffixes.id - Suffix used for the polymorphic id column.
*/
declare type ZeroOptions = {
extensions: string[];
snaked: boolean;
pk: string | any;
col?: string;
type?: string;
directory: string;
keys: {
model: string;
};
suffixes: {
type: string;
id: string;
};
};
/**
* Entry class instance for Factory Zero.
* @param connection - Database connection options for this Knex instance.
* @param options - Configuration for Factory Zero.
*/
declare class Zero {
constructor(connection: any | Knex, options: ZeroOptions);
/**
* Seeding method that runs all the necessary queries to seed all available fixtures
* into the configured database.
* @returns - A chained Promise instance of all queries on this instance.
*/
seed(): Promise;
/**
* Sets up Factory Zero for seeding by loading fixtures and fetching schema
* information via information_schema.
*/
setup(): void;
/**
* Clears data from all tables found in the fixtures directory.
* @returns - A collection of all DELETE queries.
*/
clear(): Promise;
/**
* Insert all fixtures into the configured database with all fixtures
* found in the fixtures directory.
* @param fixtures - All fixtures to insert into the database.
* @returns - A collection of all INSERT queries.
*/
insert(fixtures: Map<string, object>): Promise;
/**
* Fetches all fixtures with all relations resolved via lib/Resolver.
* @returns - A collection of all fixtures ready to insert.
*/
fixtures(): Map<string, object>;
/**
* Creates an individual Knex database instance.
* @param kx - Knex connection instance.
* @returns - Knex database instance.
*/
createKnex(kx: any | Knex): Knex;
/**
* Checks if [kx] is an instance of Knex.
* @param kx - Any value that might be an instance of Knex.
* @returns - Is [kx] an instance of Knex?
*/
isKnex(kx: any): boolean;
}