@bluedot-innovation/models 6.1.2-alpha.2 → 6.1.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/.gitlab-ci.yml +35 -2
- package/Makefile +21 -0
- package/README.MD +14 -1
- package/__tests__/zone/zone.spec.js +28 -59
- package/models/Zone.js +0 -1
- package/package.json +2 -1
- package/schemas/ZoneEta.js +1 -1
- package/utils/dbInitialiser.js +98 -0
package/.gitlab-ci.yml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Official framework image. Look for the different tagged releases at:
|
|
2
2
|
# https://hub.docker.com/r/library/node/tags/
|
|
3
|
-
image: node:
|
|
3
|
+
image: node:12
|
|
4
4
|
|
|
5
5
|
before_script:
|
|
6
6
|
- npm install
|
|
@@ -11,7 +11,13 @@ cache:
|
|
|
11
11
|
paths:
|
|
12
12
|
- node_modules/
|
|
13
13
|
|
|
14
|
+
stages:
|
|
15
|
+
- test
|
|
16
|
+
- deploy
|
|
17
|
+
- release
|
|
18
|
+
|
|
14
19
|
test:lint:
|
|
20
|
+
stage: test
|
|
15
21
|
tags:
|
|
16
22
|
- infrastructure
|
|
17
23
|
script:
|
|
@@ -21,7 +27,8 @@ test:lint:
|
|
|
21
27
|
# https://github.com/nodejs/LTS#lts_schedule
|
|
22
28
|
# jest does not support node < 4
|
|
23
29
|
# https://facebook.github.io/jest/docs/troubleshooting.html#compatibility-issues
|
|
24
|
-
test:node:
|
|
30
|
+
test:node:12:
|
|
31
|
+
stage: test
|
|
25
32
|
tags:
|
|
26
33
|
- infrastructure
|
|
27
34
|
script:
|
|
@@ -42,3 +49,29 @@ publish:
|
|
|
42
49
|
script:
|
|
43
50
|
- echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}'>.npmrc
|
|
44
51
|
- npm publish
|
|
52
|
+
|
|
53
|
+
# Create any newly required indexes in the dev db (on merge to master)
|
|
54
|
+
create_dev_indexes:
|
|
55
|
+
stage: deploy
|
|
56
|
+
tags:
|
|
57
|
+
- infrastructure
|
|
58
|
+
only:
|
|
59
|
+
- master
|
|
60
|
+
|
|
61
|
+
script:
|
|
62
|
+
- MONGO_DB_URI=$DEV_MONGO_DB_URI make deploy-db-changes
|
|
63
|
+
|
|
64
|
+
# Create any newly required indexes in the production dbs
|
|
65
|
+
create_production_indexes:
|
|
66
|
+
stage: release
|
|
67
|
+
tags:
|
|
68
|
+
- infrastructure
|
|
69
|
+
when: manual
|
|
70
|
+
allow_failure: false
|
|
71
|
+
only:
|
|
72
|
+
- master
|
|
73
|
+
|
|
74
|
+
script:
|
|
75
|
+
- MONGO_DB_URI="$AU_MONGO_DB_URI" make deploy-db-changes
|
|
76
|
+
- MONGO_DB_URI="$EU_MONGO_DB_URI" make deploy-db-changes
|
|
77
|
+
- MONGO_DB_URI="$US_MONGO_DB_URI" make deploy-db-changes
|
package/Makefile
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
pre:
|
|
2
|
+
rm -rf node_modules
|
|
3
|
+
npm ci
|
|
4
|
+
|
|
5
|
+
test: pre
|
|
6
|
+
npm test
|
|
7
|
+
|
|
8
|
+
test-coverage: pre
|
|
9
|
+
npm run test:coverage
|
|
10
|
+
|
|
11
|
+
deploy-db-changes:
|
|
12
|
+
|
|
13
|
+
ifndef DB_NAME
|
|
14
|
+
$(error 'DB_NAME is not set')
|
|
15
|
+
endif
|
|
16
|
+
|
|
17
|
+
ifndef MONGO_DB_URI
|
|
18
|
+
$(error 'MONGO_DB_URI is not set')
|
|
19
|
+
endif
|
|
20
|
+
|
|
21
|
+
node utils/dbInitialiser.js
|
package/README.MD
CHANGED
|
@@ -33,4 +33,17 @@ aws apigateway put-integration \
|
|
|
33
33
|
--uri arn:aws:apigateway:aws-region:lambda:path/2015-03-31/functions/arn:aws:lambda:aws-region:aws-acct-id:function:your-lambda-function-name/invocations
|
|
34
34
|
To publish:
|
|
35
35
|
|
|
36
|
-
run `npm version x.x.x`
|
|
36
|
+
run `npm version x.x.x`
|
|
37
|
+
|
|
38
|
+
##### On Adding New Indexes
|
|
39
|
+
|
|
40
|
+
When adding indexes to node_modules, they will not actually be created in the db, due to the flag auto_index: false. This is the recommended set up for
|
|
41
|
+
production usage (see https://mongoosejs.com/docs/guide.html#autoIndex) as creating indexes with mongoose can lead to performance issues if done at the wrong
|
|
42
|
+
time.
|
|
43
|
+
|
|
44
|
+
To resolve this the file utils/dbInitialiser is used to perform any required db updates such as index creation. This creates the indexes
|
|
45
|
+
once and can be triggered at a time when load is not expected to be high in production. This has been added as a manual release stage as part of the
|
|
46
|
+
CI/CD pipeline. If you need to add a new index, just add it to the indexes object in utils/dbInitialiser.
|
|
47
|
+
|
|
48
|
+
Indexes should still be added to the mongoose schemas as well, as these can be used in tests by triggering index creation with
|
|
49
|
+
mockgoose using model.ensureIndexes(), and it provides another level of documentation.
|
|
@@ -106,42 +106,20 @@ describe('Zone', () => {
|
|
|
106
106
|
})
|
|
107
107
|
})
|
|
108
108
|
|
|
109
|
-
|
|
110
|
-
zone.eta = {
|
|
111
|
-
"destinationId": "test-destination-id"
|
|
112
|
-
}
|
|
113
|
-
zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^polygon$/))
|
|
114
|
-
let Zone = new ZoneModel(zone)
|
|
115
|
-
Zone.validate((err, res) => {
|
|
116
|
-
assert.notExists(res)
|
|
117
|
-
assert.notExists(err)
|
|
118
|
-
done()
|
|
119
|
-
})
|
|
120
|
-
})
|
|
109
|
+
const validEtaGeofeatureTypes = ['boundingBox', 'circle', 'polygon']
|
|
121
110
|
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
it('should save without errors when eta.destinationId is set and there is a single circle geofeature', (done) => {
|
|
135
|
-
|
|
136
|
-
zone.eta = {
|
|
137
|
-
"destinationId": "test-destination-id"
|
|
138
|
-
}
|
|
139
|
-
zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^circle$/))
|
|
140
|
-
let Zone = new ZoneModel(zone)
|
|
141
|
-
Zone.validate((err, res) => {
|
|
142
|
-
assert.notExists(res)
|
|
143
|
-
assert.notExists(err)
|
|
144
|
-
done()
|
|
111
|
+
validEtaGeofeatureTypes.map(type => {
|
|
112
|
+
it(`should save without errors when eta.destinationId is set and there is a single ${type} geofeature`, (done) => {
|
|
113
|
+
zone.eta = {
|
|
114
|
+
"destinationId": "test-destination-id"
|
|
115
|
+
}
|
|
116
|
+
zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(new RegExp(type)))
|
|
117
|
+
let Zone = new ZoneModel(zone)
|
|
118
|
+
Zone.validate((err, res) => {
|
|
119
|
+
assert.notExists(res)
|
|
120
|
+
assert.notExists(err)
|
|
121
|
+
done()
|
|
122
|
+
})
|
|
145
123
|
})
|
|
146
124
|
})
|
|
147
125
|
})
|
|
@@ -448,38 +426,29 @@ describe('Zone', () => {
|
|
|
448
426
|
let Zone = new ZoneModel(zone)
|
|
449
427
|
Zone.validate((err, res) => {
|
|
450
428
|
assert.exists(err, 'err object should exist')
|
|
451
|
-
assert.match(err.message, /(destinationId must be
|
|
429
|
+
assert.match(err.message, /(destinationId must be less than 129 characters long)/i)
|
|
452
430
|
done()
|
|
453
431
|
})
|
|
454
432
|
})
|
|
455
433
|
|
|
456
|
-
|
|
457
|
-
zone.eta = {
|
|
458
|
-
"destinationId": "test-destination-id"
|
|
459
|
-
}
|
|
460
|
-
zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(/^geoline$/))
|
|
461
|
-
let Zone = new ZoneModel(zone)
|
|
462
|
-
Zone.validate((err, res) => {
|
|
463
|
-
assert.exists(err, 'err object should exist')
|
|
464
|
-
assert.match(err.message, /(destinationId cannot be set on zones with a geoline or beacon)/i)
|
|
465
|
-
done()
|
|
466
|
-
})
|
|
467
|
-
})
|
|
434
|
+
const invalidEtaGeofeatures = ['geoline', 'beacon']
|
|
468
435
|
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
436
|
+
invalidEtaGeofeatures.map(type => {
|
|
437
|
+
it(`should throw an error when eta.destinationId is set and there is a single ${type} geofeature`, (done) => {
|
|
438
|
+
zone.eta = {
|
|
439
|
+
"destinationId": "test-destination-id"
|
|
440
|
+
}
|
|
441
|
+
zone.geofeatures = zone.geofeatures.filter(gf => gf.type.match(new RegExp(type)))
|
|
442
|
+
let Zone = new ZoneModel(zone)
|
|
443
|
+
Zone.validate((err, res) => {
|
|
444
|
+
assert.exists(err, 'err object should exist')
|
|
445
|
+
assert.match(err.message, /(destinationId cannot be set on zones with a geoline or beacon)/i)
|
|
446
|
+
done()
|
|
447
|
+
})
|
|
479
448
|
})
|
|
480
449
|
})
|
|
481
450
|
|
|
482
|
-
it('should
|
|
451
|
+
it('should throw an error when eta.destinationId is set and there is more than one geofeature', (done) => {
|
|
483
452
|
zone.eta = {
|
|
484
453
|
"destinationId": "test-destination-id"
|
|
485
454
|
}
|
package/models/Zone.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bluedot-innovation/models",
|
|
3
|
-
"version": "6.1.2
|
|
3
|
+
"version": "6.1.2",
|
|
4
4
|
"main": "Models.js",
|
|
5
5
|
"scripts": {
|
|
6
6
|
"lint": "./node_modules/.bin/eslint -c .eslintrc.js ./models/*.js ./__tests__/**/*.js ./__tests__/**/**/*.js ./schemas/*.js",
|
|
@@ -36,6 +36,7 @@
|
|
|
36
36
|
"deep-equal": "^1.0.1",
|
|
37
37
|
"fast-deep-equal": "^2.0.1",
|
|
38
38
|
"lodash.clonedeep": "4.5.0",
|
|
39
|
+
"mongodb": "^3.5.7",
|
|
39
40
|
"mongoose": "^5.7.13",
|
|
40
41
|
"uuid": "^3.1.0"
|
|
41
42
|
}
|
package/schemas/ZoneEta.js
CHANGED
|
@@ -3,7 +3,7 @@ let helpers = require('../helpers')
|
|
|
3
3
|
|
|
4
4
|
let ZoneEtaSchema = new mongoose.Schema({
|
|
5
5
|
destinationId: {type: String, validate: [
|
|
6
|
-
{ validator: helpers.validateEtaDestinationIdLength, msg: 'destinationId must be
|
|
6
|
+
{ validator: helpers.validateEtaDestinationIdLength, msg: 'destinationId must be less than 129 characters long' },
|
|
7
7
|
{ validator: helpers.validateEtaDestinationSingleGeofeature, msg: 'destinationId can only be set on zones with a single geofeature' },
|
|
8
8
|
{ validator: helpers.validateEtaDestinationIdGeofeatureType, msg: 'destinationId cannot be set on zones with a geoline or beacon' }
|
|
9
9
|
]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
const MongoClient = require('mongodb').MongoClient
|
|
2
|
+
|
|
3
|
+
/*
|
|
4
|
+
Add indexes here, rest of the code should not require updating
|
|
5
|
+
For adding indexes to a new collection create a new array with key=collectionName
|
|
6
|
+
Fields are the first parameter of createIndex, the fields in the collection and their sort order
|
|
7
|
+
Options are the optional second parameter of create index and include name, unique and others. These can be left out if not required
|
|
8
|
+
https://docs.mongodb.com/manual/reference/method/db.collection.createIndex/
|
|
9
|
+
*/
|
|
10
|
+
const indexes = {
|
|
11
|
+
zone: [
|
|
12
|
+
{
|
|
13
|
+
fields: {
|
|
14
|
+
projectId: 1,
|
|
15
|
+
'eta.destinationId': 1
|
|
16
|
+
},
|
|
17
|
+
options: {
|
|
18
|
+
unique: true,
|
|
19
|
+
partialFilterExpression: { 'eta.destinationId': { $exists:true, $type:"string" } },
|
|
20
|
+
name: "destinationUniqueWithinProject"
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
]
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const createIndexIfNotExists = async (collection, fields, options) => {
|
|
27
|
+
let indexName
|
|
28
|
+
if(options && options.name){
|
|
29
|
+
indexName = options.name
|
|
30
|
+
}else {
|
|
31
|
+
// Attempt to work out the default name mongo would generate. Better to provide a name in options so don't have to rely on this being correct
|
|
32
|
+
// If this is incorrect it will just fail to create the index as you can't have duplicate indexes with the same fields anyway
|
|
33
|
+
const fieldArray = Object.entries(fields)
|
|
34
|
+
indexName = fieldArray.reduce((accumulator, [fieldName, sortDirection]) => {
|
|
35
|
+
let newValue = accumulator;
|
|
36
|
+
if (accumulator) {
|
|
37
|
+
newValue = `${newValue}_`
|
|
38
|
+
}
|
|
39
|
+
return `${newValue}${fieldName}_${sortDirection}`
|
|
40
|
+
}, "")
|
|
41
|
+
}
|
|
42
|
+
const alreadyPresent = await collection.indexExists(indexName)
|
|
43
|
+
if(alreadyPresent){
|
|
44
|
+
console.log(`Index ${indexName} is already present in the database`)
|
|
45
|
+
return
|
|
46
|
+
}
|
|
47
|
+
try{
|
|
48
|
+
const createdIndex = await collection.createIndex(fields, options)
|
|
49
|
+
console.log(`Index ${createdIndex} successfully created. Name should match ${indexName}`)
|
|
50
|
+
}catch(error){
|
|
51
|
+
console.log(`An error occurred creating index ${indexName}`, error)
|
|
52
|
+
throw error
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const createAllIndexesIfNotExists = db => {
|
|
57
|
+
const createIndexPromises = []
|
|
58
|
+
for(const collectionName in indexes){
|
|
59
|
+
const collection = db.collection(collectionName);
|
|
60
|
+
for(const index of indexes[collectionName]){
|
|
61
|
+
createIndexPromises.push(createIndexIfNotExists(collection, index.fields, index.options))
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// We want to execute all index creation promises, then if there are any errors throw an error
|
|
66
|
+
return Promise.allSettled(createIndexPromises).then(results => {
|
|
67
|
+
const hasErrors = results.find(result => result.status === "rejected")
|
|
68
|
+
if(hasErrors){
|
|
69
|
+
throw new Error("An error occurred creating indexes")
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const MONGO_DB_URI = process.env.MONGO_DB_URI
|
|
75
|
+
const DB_NAME = process.env.DB_NAME
|
|
76
|
+
const client = new MongoClient(MONGO_DB_URI, { useUnifiedTopology: true })
|
|
77
|
+
|
|
78
|
+
console.log(`Using DB_NAME:${DB_NAME}`)
|
|
79
|
+
|
|
80
|
+
client.connect(async error => {
|
|
81
|
+
if(error){
|
|
82
|
+
console.log("Failed to connect to Mongo DB", error)
|
|
83
|
+
process.exit(1)
|
|
84
|
+
}
|
|
85
|
+
console.log("Connected to Mongo DB")
|
|
86
|
+
|
|
87
|
+
const db = client.db(DB_NAME)
|
|
88
|
+
|
|
89
|
+
try{
|
|
90
|
+
await createAllIndexesIfNotExists(db)
|
|
91
|
+
}catch(error){
|
|
92
|
+
console.log(error)
|
|
93
|
+
await client.close()
|
|
94
|
+
process.exit(1)
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
await client.close()
|
|
98
|
+
})
|