-
Notifications
You must be signed in to change notification settings - Fork 47
/
config.ts
89 lines (79 loc) 路 3.02 KB
/
config.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
import * as extend from 'extend';
import { SeederCollection, DeepPartial } from './common';
import { SeederDatabaseConfig, defaultDatabaseConfigObject } from './database';
import { CollectionInsertManyOptions, MongoClientOptions } from 'mongodb';
import { EJSON } from 'bson';
/**
* Defines configuration for database seeding.
*/
export interface SeederConfig {
database: SeederDatabaseConfig; // database connection URI or configuration object
databaseReconnectTimeout: number; // maximum time of waiting for successful MongoDB connection in milliseconds. Ignored when `mongoClientOptions` are passed.
dropDatabase: boolean; // drops entire database before import
dropCollections: boolean; // drops collection before importing it
removeAllDocuments: boolean; // remove all documents from collections before importing it
mongoClientOptions?: MongoClientOptions; // optional MongoDB client options
collectionInsertManyOptions?: CollectionInsertManyOptions; // optional MongoDB collection import options
}
/**
* Stores default configuration for database seeding.
*/
export const defaultSeederConfig: SeederConfig = {
database: defaultDatabaseConfigObject,
databaseReconnectTimeout: 10000,
dropDatabase: false,
dropCollections: false,
removeAllDocuments: false,
};
/**
* Merges configuration for database seeding.
*
* @param partial Partial config object. If not specified, returns a default config object.
* @param previous Previous config object. If not specified, uses a default config object as a base.
*/
export const mergeSeederConfig = (
partial?: DeepPartial<SeederConfig>,
previous?: SeederConfig,
): SeederConfig => {
const source = previous ? previous : defaultSeederConfig;
if (!partial) {
return source;
}
const config = {};
return extend(true, config, source, partial);
};
/**
* Defines collection reading configuration.
*/
export interface SeederCollectionReadingOptions {
extensions: string[]; // files that should be imported
ejsonParseOptions?: EJSON.Options; // options for parsing EJSON files with `.json` extension
transformers: Array<(collection: SeederCollection) => SeederCollection>; // optional transformer functions
}
/**
* Stores default collection reading configuration values.
*/
export const defaultCollectionReadingOptions: SeederCollectionReadingOptions = {
extensions: ['json', 'js', 'cjs'],
ejsonParseOptions: {
relaxed: true,
},
transformers: [],
};
/**
* Merges configuration for collection reading.
*
* @param partial Partial config object. If not specified, returns a default config object.
* @param previous Previous config object. If not specified, uses a default config object as a base.
*/
export const mergeCollectionReadingOptions = (
partial?: DeepPartial<SeederCollectionReadingOptions>,
previous?: SeederCollectionReadingOptions,
): SeederCollectionReadingOptions => {
const source = previous ? previous : defaultCollectionReadingOptions;
if (!partial) {
return source;
}
const config = {};
return extend(true, config, source, partial);
};