@bluedot-innovation/models 6.1.2-alpha.1 → 6.1.2-alpha.3
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 +33 -2
- package/README.MD +14 -1
- package/__tests__/zone/zone.spec.js +3 -3
- package/models/Zone.js +12 -1
- package/package.json +2 -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,27 @@ 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
|
+
- DB_NAME=$DB_NAME MONGO_DB_URI="$DEV_MONGO_DB_URI" node utils/dbInitialiser.js
|
|
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
|
+
|
|
72
|
+
script:
|
|
73
|
+
- DB_NAME=$DB_NAME MONGO_DB_URI="$AU_MONGO_DB_URI" node utils/dbInitialiser.js
|
|
74
|
+
- DB_NAME=$DB_NAME MONGO_DB_URI="$EU_MONGO_DB_URI" node utils/dbInitialiser.js
|
|
75
|
+
- DB_NAME=$DB_NAME MONGO_DB_URI="$US_MONGO_DB_URI" 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.
|
|
@@ -453,7 +453,7 @@ describe('Zone', () => {
|
|
|
453
453
|
})
|
|
454
454
|
})
|
|
455
455
|
|
|
456
|
-
it('should
|
|
456
|
+
it('should throw an error when eta.destinationId is set and there is a single geoline geofeature', (done) => {
|
|
457
457
|
zone.eta = {
|
|
458
458
|
"destinationId": "test-destination-id"
|
|
459
459
|
}
|
|
@@ -466,7 +466,7 @@ describe('Zone', () => {
|
|
|
466
466
|
})
|
|
467
467
|
})
|
|
468
468
|
|
|
469
|
-
it('should
|
|
469
|
+
it('should throw an error when eta.destinationId is set and there is a single beacon geofeature', (done) => {
|
|
470
470
|
zone.eta = {
|
|
471
471
|
"destinationId": "test-destination-id"
|
|
472
472
|
}
|
|
@@ -479,7 +479,7 @@ describe('Zone', () => {
|
|
|
479
479
|
})
|
|
480
480
|
})
|
|
481
481
|
|
|
482
|
-
it('should
|
|
482
|
+
it('should throw an error when eta.destinationId is set and there is more than one geofeature', (done) => {
|
|
483
483
|
zone.eta = {
|
|
484
484
|
"destinationId": "test-destination-id"
|
|
485
485
|
}
|
package/models/Zone.js
CHANGED
|
@@ -121,7 +121,18 @@ let zoneSchema = new Schema({
|
|
|
121
121
|
state: {type: String, default: StateEnum.ACTIVE, select: false},
|
|
122
122
|
accountId: {type: String, required: true, validate: {isAsync: false, validator: helpers.validateUUIDv4, msg: 'default error message'}},
|
|
123
123
|
eta: zoneEtaSchema
|
|
124
|
-
}, {collection: 'zone'})
|
|
124
|
+
}, {collection: 'zone', autoIndex: false})
|
|
125
|
+
|
|
126
|
+
zoneSchema.index(
|
|
127
|
+
{
|
|
128
|
+
projectId: 1,
|
|
129
|
+
'eta.destinationId': 1
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
unique: true,
|
|
133
|
+
partialFilterExpression: { 'eta.destinationId': { $exists:true, $type:"string" } },
|
|
134
|
+
name: "destinationUniqueWithinProject"
|
|
135
|
+
})
|
|
125
136
|
|
|
126
137
|
// Mongoose-json-patch
|
|
127
138
|
// https://www.npmjs.com/package/mongoose-json-patch
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bluedot-innovation/models",
|
|
3
|
-
"version": "6.1.2-alpha.
|
|
3
|
+
"version": "6.1.2-alpha.3",
|
|
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
|
}
|
|
@@ -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
|
+
})
|