@villedemontreal/mongo 6.7.1 → 6.7.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/scripts/index.js +16 -0
- package/dist/scripts/index.js.map +1 -0
- package/dist/scripts/lint.js +18 -0
- package/dist/scripts/lint.js.map +1 -0
- package/dist/scripts/lintFix.js +21 -0
- package/dist/scripts/lintFix.js.map +1 -0
- package/dist/scripts/showCoverage.js +40 -0
- package/dist/scripts/showCoverage.js.map +1 -0
- package/dist/scripts/test.js +29 -0
- package/dist/scripts/test.js.map +1 -0
- package/dist/scripts/testUnits.js +95 -0
- package/dist/scripts/testUnits.js.map +1 -0
- package/dist/scripts/watch.js +96 -0
- package/dist/scripts/watch.js.map +1 -0
- package/dist/src/config/configs.js +6 -0
- package/dist/src/config/configs.js.map +1 -1
- package/dist/src/config/constants.js +12 -12
- package/dist/src/config/constants.js.map +1 -1
- package/dist/src/config/init.js.map +1 -1
- package/dist/src/config/mongooseConfigs.js +8 -6
- package/dist/src/config/mongooseConfigs.js.map +1 -1
- package/dist/src/index.js +6 -2
- package/dist/src/index.js.map +1 -1
- package/dist/src/mongoClient.js +64 -59
- package/dist/src/mongoClient.js.map +1 -1
- package/dist/src/mongoUpdater.js +30 -27
- package/dist/src/mongoUpdater.js.map +1 -1
- package/dist/src/mongoUpdater.test.js +11 -11
- package/dist/src/mongoUpdater.test.js.map +1 -1
- package/dist/src/mongoUtils.js +15 -12
- package/dist/src/mongoUtils.js.map +1 -1
- package/dist/src/plugins/pagination/index.js +9 -12
- package/dist/src/plugins/pagination/index.js.map +1 -1
- package/dist/src/plugins/pagination/index.test.js +20 -12
- package/dist/src/plugins/pagination/index.test.js.map +1 -1
- package/dist/src/utils/logger.js +1 -1
- package/dist/src/utils/logger.js.map +1 -1
- package/dist/src/utils/testingConfigurations.js +1 -1
- package/dist/src/utils/testingConfigurations.js.map +1 -1
- package/dist/tests/testingMongoUpdates/1.0.0.js +3 -3
- package/dist/tests/testingMongoUpdates/1.0.1.js +4 -4
- package/dist/tests/testingMongoUpdates/1.0.1.js.map +1 -1
- package/package.json +20 -15
- package/src/config/configs.ts +10 -1
- package/src/config/constants.ts +13 -13
- package/src/config/init.ts +1 -1
- package/src/config/mongooseConfigs.ts +30 -12
- package/src/mongoClient.ts +80 -63
- package/src/mongoUpdater.test.ts +29 -17
- package/src/mongoUpdater.ts +47 -32
- package/src/mongoUtils.ts +20 -14
- package/src/plugins/pagination/index.test.ts +38 -27
- package/src/plugins/pagination/index.ts +19 -14
- package/src/utils/logger.ts +8 -1
- package/dist/src/config/configs.d.ts +0 -16
- package/dist/src/config/constants.d.ts +0 -85
- package/dist/src/config/init.d.ts +0 -9
- package/dist/src/config/mongooseConfigs.d.ts +0 -73
- package/dist/src/index.d.ts +0 -6
- package/dist/src/mongoClient.d.ts +0 -19
- package/dist/src/mongoUpdater.d.ts +0 -103
- package/dist/src/mongoUpdater.test.d.ts +0 -1
- package/dist/src/mongoUtils.d.ts +0 -68
- package/dist/src/mongoUtils.test.d.ts +0 -0
- package/dist/src/plugins/pagination/index.d.ts +0 -11
- package/dist/src/plugins/pagination/index.test.d.ts +0 -1
- package/dist/src/plugins/pagination/specs/IPaginateOptions.d.ts +0 -51
- package/dist/src/utils/logger.d.ts +0 -11
- package/dist/src/utils/testingConfigurations.d.ts +0 -8
- package/dist/tests/testingMongoUpdates/1.0.0.d.ts +0 -5
- package/dist/tests/testingMongoUpdates/1.0.1.d.ts +0 -5
|
@@ -18,34 +18,37 @@ const bookSchema = new mongoose.Schema({
|
|
|
18
18
|
date: Date,
|
|
19
19
|
author: {
|
|
20
20
|
type: String,
|
|
21
|
-
ref: 'Author'
|
|
22
|
-
}
|
|
21
|
+
ref: 'Author',
|
|
22
|
+
},
|
|
23
23
|
});
|
|
24
24
|
|
|
25
25
|
bookSchema.plugin(mongoosePaginate);
|
|
26
26
|
const bookModel: any = mongoose.model('Book', bookSchema);
|
|
27
27
|
|
|
28
28
|
describe('plugin pagination', () => {
|
|
29
|
-
before(async function() {
|
|
29
|
+
before(async function () {
|
|
30
30
|
this.timeout(120000);
|
|
31
31
|
|
|
32
32
|
// Makes sure Mongoose is mocked, but not in Jenkins as we will start a dedicated mongodb container.
|
|
33
|
-
const mockedDb = await mongoUtils.mockMongoose(
|
|
33
|
+
const mockedDb = await mongoUtils.mockMongoose(
|
|
34
|
+
this,
|
|
35
|
+
constants.testsConfig.mockServer.serverVersion
|
|
36
|
+
);
|
|
34
37
|
const connString = mockedDb.getUri();
|
|
35
38
|
await mongoose.connect(connString, { useNewUrlParser: true });
|
|
36
39
|
});
|
|
37
40
|
|
|
38
|
-
before(async function() {
|
|
41
|
+
before(async function () {
|
|
39
42
|
this.timeout(10000);
|
|
40
43
|
let book;
|
|
41
44
|
const books: any = [];
|
|
42
45
|
const date = new Date();
|
|
43
|
-
return authorModel.create({ name: 'Arthur Conan Doyle' }).then(author => {
|
|
46
|
+
return authorModel.create({ name: 'Arthur Conan Doyle' }).then((author) => {
|
|
44
47
|
for (let i = 1; i <= 100; i++) {
|
|
45
48
|
book = new bookModel({
|
|
46
|
-
title:
|
|
49
|
+
title: `Book #${i}`,
|
|
47
50
|
date: new Date(date.getTime() + i),
|
|
48
|
-
author: author._id
|
|
51
|
+
author: author._id,
|
|
49
52
|
});
|
|
50
53
|
books.push(book);
|
|
51
54
|
}
|
|
@@ -58,7 +61,7 @@ describe('plugin pagination', () => {
|
|
|
58
61
|
expect(promise.then).to.be.an.instanceof(Function);
|
|
59
62
|
});
|
|
60
63
|
|
|
61
|
-
it('calls callback', done => {
|
|
64
|
+
it('calls callback', (done) => {
|
|
62
65
|
bookModel.paginate({}, {}, (err: Error, result: IPaginatedResult<any>) => {
|
|
63
66
|
expect(err).to.be.null;
|
|
64
67
|
expect(result).to.be.an.instanceOf(Object);
|
|
@@ -83,12 +86,14 @@ describe('plugin pagination', () => {
|
|
|
83
86
|
});
|
|
84
87
|
});
|
|
85
88
|
it('with offset and limit', () => {
|
|
86
|
-
return bookModel
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
89
|
+
return bookModel
|
|
90
|
+
.paginate({}, { offset: 30, limit: 20 })
|
|
91
|
+
.then((result: IPaginatedResult<any>) => {
|
|
92
|
+
expect(result.items).to.have.length(20);
|
|
93
|
+
expect(result.paging.totalCount).to.equal(100);
|
|
94
|
+
expect(result.paging.limit).to.equal(20);
|
|
95
|
+
expect(result.paging.offset).to.equal(30);
|
|
96
|
+
});
|
|
92
97
|
});
|
|
93
98
|
it('with zero limit', () => {
|
|
94
99
|
return bookModel.paginate({}, { page: 1, limit: 0 }).then((result: IPaginatedResult<any>) => {
|
|
@@ -104,14 +109,18 @@ describe('plugin pagination', () => {
|
|
|
104
109
|
});
|
|
105
110
|
});
|
|
106
111
|
it('with sort', () => {
|
|
107
|
-
return bookModel
|
|
108
|
-
|
|
109
|
-
|
|
112
|
+
return bookModel
|
|
113
|
+
.paginate({}, { sort: { date: -1 } })
|
|
114
|
+
.then((result: IPaginatedResult<any>) => {
|
|
115
|
+
expect(result.items[0].title).to.equal('Book #100');
|
|
116
|
+
});
|
|
110
117
|
});
|
|
111
118
|
it('with populate', () => {
|
|
112
|
-
return bookModel
|
|
113
|
-
|
|
114
|
-
|
|
119
|
+
return bookModel
|
|
120
|
+
.paginate({}, { populate: 'author' })
|
|
121
|
+
.then((result: IPaginatedResult<any>) => {
|
|
122
|
+
expect(result.items[0].author.name).to.equal('Arthur Conan Doyle');
|
|
123
|
+
});
|
|
115
124
|
});
|
|
116
125
|
describe('with lean', () => {
|
|
117
126
|
it('with default leanWithId=true', () => {
|
|
@@ -122,19 +131,21 @@ describe('plugin pagination', () => {
|
|
|
122
131
|
});
|
|
123
132
|
});
|
|
124
133
|
it('with leanWithId=false', () => {
|
|
125
|
-
return bookModel
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
134
|
+
return bookModel
|
|
135
|
+
.paginate({}, { lean: true, leanWithId: false })
|
|
136
|
+
.then((result: IPaginatedResult<any>) => {
|
|
137
|
+
expect(result.items[0]).to.not.be.an.instanceof((mongoose as any).Document);
|
|
138
|
+
expect(result.items[0]).to.not.have.property('id');
|
|
139
|
+
});
|
|
129
140
|
});
|
|
130
141
|
});
|
|
131
142
|
});
|
|
132
143
|
|
|
133
|
-
after(done => {
|
|
144
|
+
after((done) => {
|
|
134
145
|
mongoose.connection.db.dropDatabase(done);
|
|
135
146
|
});
|
|
136
147
|
|
|
137
|
-
after(done => {
|
|
148
|
+
after((done) => {
|
|
138
149
|
mongoose.disconnect(done);
|
|
139
150
|
});
|
|
140
151
|
});
|
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { IPaginateOptions } from './specs/IPaginateOptions';
|
|
2
2
|
|
|
3
|
-
function paginate(
|
|
3
|
+
function paginate(
|
|
4
|
+
q: any,
|
|
5
|
+
options: IPaginateOptions,
|
|
6
|
+
callback: (error: Error, result: any) => void
|
|
7
|
+
) {
|
|
4
8
|
const optionsClean = PaginateBuilder.getOptions((paginate as any).options, options);
|
|
5
9
|
|
|
6
10
|
const query = q || {};
|
|
@@ -22,14 +26,18 @@ export class PaginateBuilder {
|
|
|
22
26
|
lean: false,
|
|
23
27
|
leanWithId: true,
|
|
24
28
|
limit: 10,
|
|
25
|
-
offset: 0
|
|
29
|
+
offset: 0,
|
|
26
30
|
});
|
|
27
31
|
|
|
28
32
|
public static getOptions(...options: IPaginateOptions[]): IPaginateOptions {
|
|
29
33
|
return Object.assign({}, PaginateBuilder.defaultOptions, ...options);
|
|
30
34
|
}
|
|
31
35
|
|
|
32
|
-
public static executeQueries(
|
|
36
|
+
public static executeQueries(
|
|
37
|
+
model: any,
|
|
38
|
+
query: any,
|
|
39
|
+
options: IPaginateOptions
|
|
40
|
+
): [Promise<any[]>, Promise<number>] {
|
|
33
41
|
const { select, sort, populate, lean, leanWithId, limit, offset } = options;
|
|
34
42
|
let itemsQuery: any;
|
|
35
43
|
|
|
@@ -38,20 +46,17 @@ export class PaginateBuilder {
|
|
|
38
46
|
}
|
|
39
47
|
|
|
40
48
|
if (limit > 0) {
|
|
41
|
-
itemsQuery = model
|
|
42
|
-
.find(query)
|
|
43
|
-
.select(select)
|
|
44
|
-
.sort(sort)
|
|
45
|
-
.skip(offset)
|
|
46
|
-
.limit(limit)
|
|
47
|
-
.lean(lean);
|
|
49
|
+
itemsQuery = model.find(query).select(select).sort(sort).skip(offset).limit(limit).lean(lean);
|
|
48
50
|
|
|
49
51
|
if (populate) {
|
|
50
|
-
[].concat(populate).forEach(item => itemsQuery.populate(item));
|
|
52
|
+
[].concat(populate).forEach((item) => itemsQuery.populate(item));
|
|
51
53
|
}
|
|
52
54
|
}
|
|
53
55
|
|
|
54
|
-
return [
|
|
56
|
+
return [
|
|
57
|
+
itemsQuery && limit > 0 ? itemsQuery.exec() : Promise.resolve([]),
|
|
58
|
+
model.countDocuments(query).exec(),
|
|
59
|
+
];
|
|
55
60
|
}
|
|
56
61
|
|
|
57
62
|
public static processResult(
|
|
@@ -62,7 +67,7 @@ export class PaginateBuilder {
|
|
|
62
67
|
const { lean, leanWithId, limit, offset } = options;
|
|
63
68
|
return new Promise((resolve, reject) => {
|
|
64
69
|
Promise.all(promises).then(
|
|
65
|
-
data => {
|
|
70
|
+
(data) => {
|
|
66
71
|
const items = data[0] as any[];
|
|
67
72
|
const count = data[1] as number;
|
|
68
73
|
const result: any = { paging: { limit, offset, totalCount: count } };
|
|
@@ -84,7 +89,7 @@ export class PaginateBuilder {
|
|
|
84
89
|
|
|
85
90
|
resolve(result);
|
|
86
91
|
},
|
|
87
|
-
error => {
|
|
92
|
+
(error) => {
|
|
88
93
|
if (typeof callback === 'function') {
|
|
89
94
|
return callback(error, null);
|
|
90
95
|
}
|
package/src/utils/logger.ts
CHANGED
|
@@ -1,4 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ILogger,
|
|
3
|
+
initLogger,
|
|
4
|
+
LazyLogger,
|
|
5
|
+
Logger,
|
|
6
|
+
LoggerConfigs,
|
|
7
|
+
LogLevel,
|
|
8
|
+
} from '@villedemontreal/logger';
|
|
2
9
|
import { configs } from '../config/configs';
|
|
3
10
|
|
|
4
11
|
let testingLoggerLibInitialised = false;
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { ILogger } from '@villedemontreal/logger';
|
|
2
|
-
/**
|
|
3
|
-
* Lib configs
|
|
4
|
-
*/
|
|
5
|
-
export declare class Configs {
|
|
6
|
-
private _loggerCreator;
|
|
7
|
-
/**
|
|
8
|
-
* The Logger creator
|
|
9
|
-
*/
|
|
10
|
-
get loggerCreator(): (name: string) => ILogger;
|
|
11
|
-
/**
|
|
12
|
-
* Sets the Logger creator.
|
|
13
|
-
*/
|
|
14
|
-
setLoggerCreator(loggerCreator: (name: string) => ILogger): void;
|
|
15
|
-
}
|
|
16
|
-
export declare let configs: Configs;
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { IMongooseConfigs } from './mongooseConfigs';
|
|
2
|
-
/**
|
|
3
|
-
* Library constants
|
|
4
|
-
*/
|
|
5
|
-
export declare class Constants {
|
|
6
|
-
/**
|
|
7
|
-
* The library root. When this library is used
|
|
8
|
-
* as a dependency in a project, the "libRoot"
|
|
9
|
-
* will be the path to the dependency folder,
|
|
10
|
-
* inside the "node_modules".
|
|
11
|
-
*/
|
|
12
|
-
libRoot: string;
|
|
13
|
-
/**
|
|
14
|
-
* The app root. When this library is used
|
|
15
|
-
* as a dependency in a project, the "appRoot"
|
|
16
|
-
* will be the path to the root project!
|
|
17
|
-
*/
|
|
18
|
-
appRoot: string;
|
|
19
|
-
constructor();
|
|
20
|
-
/**
|
|
21
|
-
* Base config to 'mock' a mongo server
|
|
22
|
-
*/
|
|
23
|
-
get testsConfig(): IMongooseConfigs;
|
|
24
|
-
/**
|
|
25
|
-
* Mongo constants
|
|
26
|
-
*/
|
|
27
|
-
get mongo(): {
|
|
28
|
-
testing: {
|
|
29
|
-
/**
|
|
30
|
-
* The "connectionString" to use for a mock
|
|
31
|
-
* Mongo server to be used instead of a real one.
|
|
32
|
-
* This option is only available on the "development"
|
|
33
|
-
* environment, or when tests are ran.
|
|
34
|
-
*/
|
|
35
|
-
MOCK_CONNECTION_STRING: string;
|
|
36
|
-
};
|
|
37
|
-
/**
|
|
38
|
-
* The names of the Mongo collections used in
|
|
39
|
-
* this application.
|
|
40
|
-
*/
|
|
41
|
-
collectionNames: {
|
|
42
|
-
/**
|
|
43
|
-
* Special collection that stores informations about the
|
|
44
|
-
* schema currently installed for the application.
|
|
45
|
-
*/
|
|
46
|
-
APP_SCHEMA: string;
|
|
47
|
-
};
|
|
48
|
-
/**
|
|
49
|
-
* Mongo error codes
|
|
50
|
-
*/
|
|
51
|
-
mongoErrorCodes: {
|
|
52
|
-
/**
|
|
53
|
-
* The code for a Mongo "duplicate key" error.
|
|
54
|
-
*/
|
|
55
|
-
DUPLICATE_KEY: number;
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Mongoose constants
|
|
59
|
-
*/
|
|
60
|
-
mongoose: {
|
|
61
|
-
/**
|
|
62
|
-
* Mongoose error codes
|
|
63
|
-
*/
|
|
64
|
-
errorCodes: {
|
|
65
|
-
/**
|
|
66
|
-
* The code for a Mongoose "required" error.
|
|
67
|
-
*/
|
|
68
|
-
REQUIRED_FIELD: string;
|
|
69
|
-
};
|
|
70
|
-
/**
|
|
71
|
-
* Mongoose error kinds
|
|
72
|
-
*/
|
|
73
|
-
errorKinds: {
|
|
74
|
-
OBJECT_ID: string;
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Mongoose error names
|
|
78
|
-
*/
|
|
79
|
-
errorNames: {
|
|
80
|
-
CAST_ERROR: string;
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
export declare let constants: Constants;
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
import { ILogger } from '@villedemontreal/logger';
|
|
2
|
-
/**
|
|
3
|
-
* Inits the library.
|
|
4
|
-
*/
|
|
5
|
-
export declare function init(loggerCreator: (name: string) => ILogger): void;
|
|
6
|
-
/**
|
|
7
|
-
* checks if the library has been initialized.
|
|
8
|
-
*/
|
|
9
|
-
export declare function isInited(): boolean;
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
export interface IMongooseConfigs {
|
|
2
|
-
/**
|
|
3
|
-
* The updater.mongoSchemaUpdatesDirPath
|
|
4
|
-
* is a required config.
|
|
5
|
-
*/
|
|
6
|
-
updater?: {
|
|
7
|
-
/**
|
|
8
|
-
* Name of the app schema collection name to use.
|
|
9
|
-
* Useful when multiple components use the same database.
|
|
10
|
-
*/
|
|
11
|
-
appSchemaCollectionName?: string;
|
|
12
|
-
/**
|
|
13
|
-
* The path where to find update files.
|
|
14
|
-
*/
|
|
15
|
-
mongoSchemaUpdatesDirPath: string;
|
|
16
|
-
lockMaxAgeSeconds?: number;
|
|
17
|
-
};
|
|
18
|
-
/**
|
|
19
|
-
* @param applyUpdates Should the database be checked for missing
|
|
20
|
-
* updates and have them applied if required?
|
|
21
|
-
* Defaults to true.
|
|
22
|
-
*/
|
|
23
|
-
applyUpdates?: boolean;
|
|
24
|
-
/**
|
|
25
|
-
* If no connectionString is provided, "mock" will be
|
|
26
|
-
* used by default and a temporary Mongo server will
|
|
27
|
-
* be used.
|
|
28
|
-
*/
|
|
29
|
-
connectionString?: string;
|
|
30
|
-
/**
|
|
31
|
-
* The Mongoose connection options.
|
|
32
|
-
*/
|
|
33
|
-
connectionOptions?: any;
|
|
34
|
-
mockServer?: {
|
|
35
|
-
/**
|
|
36
|
-
* @param mongoServerVersion the Mongo version to use.
|
|
37
|
-
*
|
|
38
|
-
* Pass null (or undefined) to use the default version
|
|
39
|
-
* downloaded by mockServer.
|
|
40
|
-
*/
|
|
41
|
-
serverVersion?: string;
|
|
42
|
-
};
|
|
43
|
-
}
|
|
44
|
-
/**
|
|
45
|
-
* Mongoose configs with default values.
|
|
46
|
-
*/
|
|
47
|
-
export declare class MongooseConfigs implements IMongooseConfigs {
|
|
48
|
-
/**
|
|
49
|
-
* @param applyUpdates Should the database be checked for missing
|
|
50
|
-
* updates and have them applied if required?
|
|
51
|
-
*/
|
|
52
|
-
applyUpdates: boolean;
|
|
53
|
-
/**
|
|
54
|
-
* If no connectionString is provided, "mock" will be
|
|
55
|
-
* used by default and a temporary Mongo server will
|
|
56
|
-
* be used.
|
|
57
|
-
*/
|
|
58
|
-
connectionString: string;
|
|
59
|
-
connectionOptions: any;
|
|
60
|
-
updater: {
|
|
61
|
-
lockMaxAgeSeconds: number;
|
|
62
|
-
mongoSchemaUpdatesDirPath: string;
|
|
63
|
-
appSchemaCollectionName: string;
|
|
64
|
-
};
|
|
65
|
-
mockServer: {
|
|
66
|
-
serverVersion: string;
|
|
67
|
-
};
|
|
68
|
-
/**
|
|
69
|
-
* Overrides default configurations using the ones passed
|
|
70
|
-
* as parameters.
|
|
71
|
-
*/
|
|
72
|
-
constructor(overridingConfigs: IMongooseConfigs);
|
|
73
|
-
}
|
package/dist/src/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import * as mongoose from 'mongoose';
|
|
2
|
-
import { IMongooseConfigs } from './config/mongooseConfigs';
|
|
3
|
-
/**
|
|
4
|
-
* This is the entry point to use this library to manage your
|
|
5
|
-
* Mongoose connections.
|
|
6
|
-
*
|
|
7
|
-
* It *must* be called when the application starts, before any
|
|
8
|
-
* connection is made to Mongo.
|
|
9
|
-
*
|
|
10
|
-
* @returns the Mongoose connection to Mongo.
|
|
11
|
-
*/
|
|
12
|
-
export declare function initMongoose(mongooseConfig: IMongooseConfigs): Promise<mongoose.Connection>;
|
|
13
|
-
/**
|
|
14
|
-
* Returns the Mongoose connection.
|
|
15
|
-
*
|
|
16
|
-
* Will throw an error if Mongo haass not been initialized
|
|
17
|
-
* using the "initMongo()" function.
|
|
18
|
-
*/
|
|
19
|
-
export declare function getMongooseConnection(): mongoose.Connection;
|
|
@@ -1,103 +0,0 @@
|
|
|
1
|
-
import * as MongoDb from 'mongodb';
|
|
2
|
-
/**
|
|
3
|
-
* Mongo updater
|
|
4
|
-
* Manages the updates of the mongo schemas
|
|
5
|
-
*/
|
|
6
|
-
export interface IMongoUpdater {
|
|
7
|
-
/**
|
|
8
|
-
* Validates that the application has been installed.
|
|
9
|
-
* This involves creating a special "appSchema" collection
|
|
10
|
-
* and document to track the application version and being able
|
|
11
|
-
* to update its schemas and documents...
|
|
12
|
-
*/
|
|
13
|
-
checkInstallation(): Promise<void>;
|
|
14
|
-
/**
|
|
15
|
-
* Checks if the application needs update or not. Installs the updates
|
|
16
|
-
* if so.
|
|
17
|
-
*/
|
|
18
|
-
checkUpdates(): Promise<void>;
|
|
19
|
-
/**
|
|
20
|
-
* Locks the appSchema document.
|
|
21
|
-
*
|
|
22
|
-
* @returns true if the lock has been acquired succesfully
|
|
23
|
-
* or false if the document was already locked.
|
|
24
|
-
*/
|
|
25
|
-
lockAppSchemaDocument(): Promise<boolean>;
|
|
26
|
-
/**
|
|
27
|
-
* Unlocks the appSchema document.
|
|
28
|
-
*
|
|
29
|
-
* @returns true if the lock has been removed succesfully
|
|
30
|
-
* or false if the document was not locked.
|
|
31
|
-
*/
|
|
32
|
-
unlockAppSchemaDocument(): Promise<boolean>;
|
|
33
|
-
/**
|
|
34
|
-
* Updates the app schema version stored in mongo database
|
|
35
|
-
*/
|
|
36
|
-
updateAppSchemaVersion(currentVersion: string, newVersion: string): Promise<void>;
|
|
37
|
-
/**
|
|
38
|
-
* Installs the appSchema collection.
|
|
39
|
-
*/
|
|
40
|
-
installAppSchemaCollection(): Promise<any>;
|
|
41
|
-
/**
|
|
42
|
-
* Gets a list of available app schema update files.
|
|
43
|
-
*/
|
|
44
|
-
getAppSchemaUpdateFiles(currentVersion: string, newVersion: string): Promise<string[]>;
|
|
45
|
-
/**
|
|
46
|
-
* Updates the app schema
|
|
47
|
-
*/
|
|
48
|
-
applyAppSchemaUpdates(currentVersion: string, newVersion: string): Promise<any>;
|
|
49
|
-
/**
|
|
50
|
-
* Gets the appSchema collection
|
|
51
|
-
*/
|
|
52
|
-
getAppSchemaCollection(): Promise<MongoDb.Collection>;
|
|
53
|
-
/**
|
|
54
|
-
* Gets the current version from the appSchema document.
|
|
55
|
-
*/
|
|
56
|
-
getAppSchemaVersion(): Promise<string>;
|
|
57
|
-
}
|
|
58
|
-
export interface ISchemeInfo {
|
|
59
|
-
version: string;
|
|
60
|
-
lock: boolean;
|
|
61
|
-
lockTimestamp: number;
|
|
62
|
-
}
|
|
63
|
-
export declare class MongoUpdater implements IMongoUpdater {
|
|
64
|
-
private mongoDb;
|
|
65
|
-
/**
|
|
66
|
-
* The *relative* path to the directory where the
|
|
67
|
-
* update files are.
|
|
68
|
-
*/
|
|
69
|
-
private mongoSchemaUpdatesDirPath;
|
|
70
|
-
private lockMaxAgeSeconds;
|
|
71
|
-
private appSchemaCollectionName;
|
|
72
|
-
constructor(mongoDb: MongoDb.Db,
|
|
73
|
-
/**
|
|
74
|
-
* The *relative* path to the directory where the
|
|
75
|
-
* update files are.
|
|
76
|
-
*/
|
|
77
|
-
mongoSchemaUpdatesDirPath: string, lockMaxAgeSeconds: number, appSchemaCollectionName: string);
|
|
78
|
-
installAppSchemaCollection(): Promise<any>;
|
|
79
|
-
updateAppSchemaVersion(currentVersion: string, newVersion: string): Promise<void>;
|
|
80
|
-
getAppSchemaUpdateFiles(currentAppSchemaVersion: string, targetAppSchemaVersion: string): Promise<string[]>;
|
|
81
|
-
applyAppSchemaUpdates(currentVersion: string, newVersion: string): Promise<void>;
|
|
82
|
-
getAppSchemaCollection(): Promise<MongoDb.Collection>;
|
|
83
|
-
getAppSchemaVersion(): Promise<string>;
|
|
84
|
-
/**
|
|
85
|
-
* Tries to get the lock to modify Mongo's schemas.
|
|
86
|
-
*
|
|
87
|
-
* If a lock already exists, checks if it is too old.
|
|
88
|
-
* If too old, will create a new one... This is to prevents
|
|
89
|
-
* situations where a lock would have been taken by an app
|
|
90
|
-
* but that app *crashed* while the lock was on. We don't want
|
|
91
|
-
* suck lock to be active forever...
|
|
92
|
-
*
|
|
93
|
-
*/
|
|
94
|
-
lockAppSchemaDocument(): Promise<boolean>;
|
|
95
|
-
unlockAppSchemaDocument(): Promise<boolean>;
|
|
96
|
-
checkInstallation(): Promise<void>;
|
|
97
|
-
checkUpdates: () => Promise<void>;
|
|
98
|
-
protected getAppSchemaFilesDirPath(): string;
|
|
99
|
-
/**
|
|
100
|
-
* Finds the latest Mongo update file version.
|
|
101
|
-
*/
|
|
102
|
-
protected findMongoAppSchemaTargetVersion(): string;
|
|
103
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/src/mongoUtils.d.ts
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
import { ApiErrorAndInfo } from '@villedemontreal/general-utils';
|
|
2
|
-
import * as mocha from 'mocha';
|
|
3
|
-
import { MongoMemoryReplSet, MongoMemoryServer } from 'mongodb-memory-server-core';
|
|
4
|
-
import * as mongoose from 'mongoose';
|
|
5
|
-
/**
|
|
6
|
-
* Mongo utilities
|
|
7
|
-
*/
|
|
8
|
-
export declare class MongoUtils {
|
|
9
|
-
private mongoMemServer;
|
|
10
|
-
private useReplSet;
|
|
11
|
-
private mockgosseMockedFlag;
|
|
12
|
-
/**
|
|
13
|
-
* Mocks the Mongo databases created through Mongoose.
|
|
14
|
-
*
|
|
15
|
-
* IMPORTANT!!
|
|
16
|
-
* For the "mochaInstance" parameter to be the proper
|
|
17
|
-
* one, this function must be called from a *regular function* in a
|
|
18
|
-
* test file, not from an *arrow function*! For more informations,
|
|
19
|
-
* see : https://github.com/mochajs/mocha/issues/2018
|
|
20
|
-
*
|
|
21
|
-
* Note that, currently, once this mocking is in place,
|
|
22
|
-
* it can't be removed. You should only call this function
|
|
23
|
-
* during testing!
|
|
24
|
-
*
|
|
25
|
-
* If Mongoose is already mocked, the function does nothing.
|
|
26
|
-
*
|
|
27
|
-
* @param mocha from a Mocha test file, pass "this" as the value
|
|
28
|
-
* for this parameter or "null" from elsewhere.
|
|
29
|
-
*
|
|
30
|
-
* @param mongoServerVersion the Mongo version to use.
|
|
31
|
-
* Pass null (or undefined) to use the default version
|
|
32
|
-
* downloaded by mongodb-memory-server.
|
|
33
|
-
*/
|
|
34
|
-
mockMongoose(mochaInstance: mocha.Context, mongoServerVersion: string, useReplSet?: boolean): Promise<MongoMemoryServer | MongoMemoryReplSet>;
|
|
35
|
-
/**
|
|
36
|
-
* Drop all mocked Mongo databases.
|
|
37
|
-
*
|
|
38
|
-
*/
|
|
39
|
-
dropMockedDatabases(): Promise<void>;
|
|
40
|
-
getMockedServerPort(): Promise<number>;
|
|
41
|
-
/**
|
|
42
|
-
* Validates if an object is a Mongoose
|
|
43
|
-
* error.
|
|
44
|
-
*/
|
|
45
|
-
isMongooseError(obj: any): boolean;
|
|
46
|
-
/**
|
|
47
|
-
* Validates if an object is a plain
|
|
48
|
-
* Mongo error.
|
|
49
|
-
*/
|
|
50
|
-
isPlainMongoError(errorObject: any): boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Creates an Api eror from an error thrown
|
|
53
|
-
* by Mongoose.
|
|
54
|
-
*
|
|
55
|
-
* If the specified error is not a Mongo/Mongoose error, it
|
|
56
|
-
* will be returned as is.
|
|
57
|
-
*
|
|
58
|
-
* @param error the Mongo/Mongoose error object
|
|
59
|
-
* @param publicMessage a public message to be used in the
|
|
60
|
-
* generated error. Fopr example : "The user is invalid".
|
|
61
|
-
*/
|
|
62
|
-
convertMongoOrMongooseErrorToApiError(err: any, publicMessage: string): ApiErrorAndInfo | any;
|
|
63
|
-
/**
|
|
64
|
-
* Converts a Mongoose Document to a plain Pojo.
|
|
65
|
-
*/
|
|
66
|
-
convertMongooseDocumentToPlainObject<T>(document: T & mongoose.Document): T;
|
|
67
|
-
}
|
|
68
|
-
export declare let mongoUtils: MongoUtils;
|
|
File without changes
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { IPaginateOptions } from './specs/IPaginateOptions';
|
|
2
|
-
/**
|
|
3
|
-
* @param {Schema} schema
|
|
4
|
-
*/
|
|
5
|
-
export declare function mongoosePaginate(schema: any): void;
|
|
6
|
-
export declare class PaginateBuilder {
|
|
7
|
-
private static readonly defaultOptions;
|
|
8
|
-
static getOptions(...options: IPaginateOptions[]): IPaginateOptions;
|
|
9
|
-
static executeQueries(model: any, query: any, options: IPaginateOptions): [Promise<any[]>, Promise<number>];
|
|
10
|
-
static processResult(promises: any[], options: IPaginateOptions, callback: (error: Error, result: any) => void): Promise<any>;
|
|
11
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
export interface IPaginateOptions {
|
|
2
|
-
/**
|
|
3
|
-
* Fields to return (by default returns all fields)
|
|
4
|
-
* http://mongoosejs.com/docs/api.html#query_Query-select
|
|
5
|
-
* @type {string}
|
|
6
|
-
* @memberof IPaginateOptions
|
|
7
|
-
*/
|
|
8
|
-
select?: string;
|
|
9
|
-
/**
|
|
10
|
-
* Sort order
|
|
11
|
-
* http://mongoosejs.com/docs/api.html#query_Query-sort
|
|
12
|
-
* @type {*}
|
|
13
|
-
* @memberof IPaginateOptions
|
|
14
|
-
*/
|
|
15
|
-
sort?: any;
|
|
16
|
-
/**
|
|
17
|
-
* Paths which should be populated with other documents.
|
|
18
|
-
* http://mongoosejs.com/docs/api.html#query_Query-populate
|
|
19
|
-
* @type {string}
|
|
20
|
-
* @memberof IPaginateOptions
|
|
21
|
-
*/
|
|
22
|
-
populate?: string;
|
|
23
|
-
/**
|
|
24
|
-
* Should return plain javascript objects instead of Mongoose documents?
|
|
25
|
-
* default false
|
|
26
|
-
* @type {boolean}
|
|
27
|
-
* @memberof IPaginateOptions
|
|
28
|
-
*/
|
|
29
|
-
lean?: boolean;
|
|
30
|
-
/**
|
|
31
|
-
* If lean and leanWithId are true, adds id field with string representation of _id to every document
|
|
32
|
-
* default true
|
|
33
|
-
* @type {boolean}
|
|
34
|
-
* @memberof IPaginateOptions
|
|
35
|
-
*/
|
|
36
|
-
leanWithId?: boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Use offset to set skip position
|
|
39
|
-
* default 0
|
|
40
|
-
* @type {number}
|
|
41
|
-
* @memberof IPaginateOptions
|
|
42
|
-
*/
|
|
43
|
-
offset?: number;
|
|
44
|
-
/**
|
|
45
|
-
* limit the items returned
|
|
46
|
-
* default 10
|
|
47
|
-
* @type {number}
|
|
48
|
-
* @memberof IPaginateOptions
|
|
49
|
-
*/
|
|
50
|
-
limit?: number;
|
|
51
|
-
}
|