-
Notifications
You must be signed in to change notification settings - Fork 184
Expand file tree
/
Copy pathpostgresql.mapping.test.js
More file actions
107 lines (101 loc) · 3.56 KB
/
postgresql.mapping.test.js
File metadata and controls
107 lines (101 loc) · 3.56 KB
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
// Copyright IBM Corp. 2014,2019. All Rights Reserved.
// Node module: loopback-connector-postgresql
// This file is licensed under the Artistic License 2.0.
// License text available at https://opensource.org/licenses/Artistic-2.0
'use strict';
process.env.NODE_ENV = 'test';
require('should');
const async = require('async');
let db;
before(function() {
db = global.getSchema();
});
describe('Mapping models', function() {
it('should honor the postgresql settings for table/column', function(done) {
const schema =
{
'name': 'TestInventory',
'options': {
'idInjection': false,
'postgresql': {
'schema': 'public', 'table': 'inventorytest',
},
},
'properties': {
/*
"id": {
"type": "String", "required": true, "length": 20, "id": 1, "postgresql": {
"columnName": "INVENTORY_ID", "dataType": "VARCHAR", "nullable": "N"
}
},
*/
'productId': {
'type': 'String', 'required': true, 'length': 20, 'id': 1, 'postgresql': {
'columnName': 'product_id', 'dataType': 'VARCHAR', 'nullable': 'NO',
},
},
'locationId': {
'type': 'String', 'required': true, 'length': 20, 'id': 2, 'postgresql': {
'columnName': 'location_id', 'dataType': 'VARCHAR', 'nullable': 'NO',
},
},
'available': {
'type': 'Number', 'required': false, 'postgresql': {
'columnName': 'available', 'dataType': 'INTEGER', 'nullable': 'YES',
},
},
'total': {
'type': 'Number', 'required': false, 'postgresql': {
'columnName': 'total', 'dataType': 'INTEGER', 'nullable': 'YES',
},
},
},
};
const models = db.modelBuilder.buildModels(schema);
// console.log(models);
const Model = models['TestInventory'];
Model.attachTo(db);
db.automigrate(function(err, data) {
async.series([
function(callback) {
Model.destroyAll(callback);
},
function(callback) {
Model.create({productId: 'p001', locationId: 'l001', available: 10, total: 50}, callback);
},
function(callback) {
Model.create({productId: 'p001', locationId: 'l002', available: 30, total: 40}, callback);
},
function(callback) {
Model.create({productId: 'p002', locationId: 'l001', available: 15, total: 30}, callback);
},
function(callback) {
Model.find({fields: ['productId', 'locationId', 'available']}, function(err, results) {
// console.log(results);
results.should.have.lengthOf(3);
results.forEach(function(r) {
r.should.have.property('productId');
r.should.have.property('locationId');
r.should.have.property('available');
r.should.have.property('total', undefined);
});
callback(null, results);
});
},
function(callback) {
Model.find({fields: {'total': false}}, function(err, results) {
// console.log(results);
results.should.have.lengthOf(3);
results.forEach(function(r) {
r.should.have.property('productId');
r.should.have.property('locationId');
r.should.have.property('available');
r.should.have.property('total', undefined);
});
callback(null, results);
});
},
], done);
});
});
});