Skip to content

Commit 0acd34a

Browse files
committed
more sequelize as usual
1 parent 1a12372 commit 0acd34a

File tree

7 files changed

+285
-36
lines changed

7 files changed

+285
-36
lines changed

rootfs_overlay/lkmc/nodejs/express.js

+44-3
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,31 @@
22

33
const express = require('express')
44

5+
function check_helper(req, res) {
6+
if (req.params.param.length > 2) {
7+
res.status(404)
8+
res.send('ko')
9+
} else {
10+
return req.params.param + 'ok'
11+
}
12+
}
13+
514
const app = express()
615
app.get('/', (req, res) => {
716
res.send('hello world')
817
})
18+
app.get('/check-helper-1/:param', (req, res) => {
19+
const ret = check_helper(req, res)
20+
if (ret) {
21+
res.send(ret)
22+
}
23+
})
24+
app.get('/check-helper-2/:param', (req, res) => {
25+
const ret = check_helper(req, res)
26+
if (ret) {
27+
res.send(ret)
28+
}
29+
})
930
app.get('/error', async (req, res, next) => {
1031
try {
1132
throw 'my error'
@@ -14,6 +35,12 @@ app.get('/error', async (req, res, next) => {
1435
next(error);
1536
}
1637
})
38+
app.get('/query', (req, res) => {
39+
res.send(`aa: ${req.query.aa} bb: ${req.query.bb}`)
40+
})
41+
app.get('/splat/:splat(*)', (req, res) => {
42+
res.send('splat ' + req.params.splat)
43+
})
1744
const server = app.listen(3000, () => {
1845
console.log(`listening: http://localhost:${server.address().port}`)
1946

@@ -28,18 +55,32 @@ const server = app.listen(3000, () => {
2855
method: method,
2956
}
3057
http.request(options, res => {
31-
console.error(res.statusCode);
32-
assert(res.statusCode === status);
58+
assert.strictEqual(res.statusCode, status);
3359
res.on('data', d => {
3460
if (body !== undefined) {
35-
assert(d.toString() === body);
61+
assert.strictEqual(d.toString(), body);
3662
}
3763
})
3864
}).end()
3965
}
4066
test('/', 'GET', 200, 'hello world')
4167
test('/', 'POST', 404)
4268
test('/dontexist', 'GET', 404)
69+
4370
// Shows 'my error' on terminal, without stack trace.
4471
test('/error', 'GET', 500)
72+
73+
test('/query?aa=000&bb=111', 'GET', 200, 'aa: 000 bb: 111')
74+
75+
// https://stackoverflow.com/questions/10020099/express-js-routing-optional-splat-param
76+
// https://stackoverflow.com/questions/16829803/express-js-route-parameter-with-slashes
77+
// https://stackoverflow.com/questions/34571784/how-to-use-parameters-containing-a-slash-character
78+
test('/splat/aaa', 'GET', 200, 'splat aaa')
79+
test('/splat/aaa/bbb', 'GET', 200, 'splat aaa/bbb')
80+
test('/splat/aaa/bbb/ccc', 'GET', 200, 'splat aaa/bbb/ccc')
81+
82+
test('/check-helper-1/aa', 'GET', 200, 'aaok')
83+
test('/check-helper-2/bb', 'GET', 200, 'bbok')
84+
test('/check-helper-1/ccc', 'GET', 404, 'ko')
85+
test('/check-helper-2/ddd', 'GET', 404, 'ko')
4586
})

rootfs_overlay/lkmc/nodejs/express2.js

-29
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env node
2+
3+
// https://github.com/sequelize/sequelize/issues/3534
4+
// https://github.com/sequelize/sequelize/issues/8586
5+
6+
const assert = require('assert');
7+
const path = require('path');
8+
9+
const { Sequelize, DataTypes } = require('sequelize');
10+
11+
const sequelize = new Sequelize({
12+
dialect: 'sqlite',
13+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
14+
});
15+
16+
(async () => {
17+
18+
const IntegerNames = sequelize.define('IntegerNames',
19+
{
20+
value: {
21+
type: DataTypes.INTEGER,
22+
allowNull: false,
23+
unique: true,
24+
},
25+
name: {
26+
type: DataTypes.STRING,
27+
},
28+
name2: {
29+
type: DataTypes.STRING,
30+
},
31+
32+
},
33+
{
34+
hooks: {
35+
beforeValidate: (integerName, options) => {
36+
integerName.name2 = integerName.name + 'asdf'
37+
// This fixes the failure.
38+
//options.fields.push('name2');
39+
}
40+
,}
41+
},
42+
);
43+
await IntegerNames.sync({force: true})
44+
await IntegerNames.create({value: 2, name: 'two'});
45+
await IntegerNames.create({value: 3, name: 'three'});
46+
await IntegerNames.create({value: 5, name: 'five'});
47+
48+
const integerName = await IntegerNames.findOne({ where: { value: 2 } });
49+
assert.strictEqual(integerName.name, 'two');
50+
assert.strictEqual(integerName.name2, 'twoasdf');
51+
integerName.name = 'TWO'
52+
integerName.save();
53+
54+
const integerName2 = await IntegerNames.findOne({ where: { value: 2 } });
55+
assert.strictEqual(integerName2.name, 'TWO');
56+
// Fails.
57+
//assert.strictEqual(integerName2.name2, 'TWOasdf');
58+
59+
await sequelize.close();
60+
61+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env node
2+
3+
const assert = require('assert');
4+
const path = require('path');
5+
6+
const { Sequelize, DataTypes } = require('sequelize');
7+
const sequelize = new Sequelize({
8+
dialect: 'sqlite',
9+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
10+
});
11+
12+
(async () => {
13+
await sequelize.authenticate();
14+
const IntegerNames = sequelize.define('IntegerNames', {
15+
value: {
16+
type: DataTypes.INTEGER,
17+
},
18+
name: {
19+
type: DataTypes.STRING,
20+
},
21+
});
22+
await IntegerNames.sync({force: true})
23+
await IntegerNames.create({value: 2, name: 'two'});
24+
await IntegerNames.create({value: 3, name: 'three'});
25+
await IntegerNames.create({value: 5, name: 'five'});
26+
const integerName5 = await IntegerNames.findOne({ where: { value: 5 } });
27+
integerName5.increment('value')
28+
// Sequelize updates, but others don't...
29+
console.error(integerName5.value);
30+
const integerName6 = await IntegerNames.findOne({ where: { value: 6 } });
31+
assert.strictEqual(integerName6.name, 'five')
32+
await sequelize.close();
33+
})();

rootfs_overlay/lkmc/nodejs/sequelize/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,12 @@ const integerNames = await IntegerNames.findAll({
101101
value: 2
102102
}
103103
});
104-
assert(integerNames[0].name === 'two');
104+
assert.strictEqual(integerNames[0].name, 'two');
105105

106106
// Truncate all tables.
107107
// https://stackoverflow.com/questions/47816162/wipe-all-tables-in-a-schema-sequelize-nodejs/66985334#66985334
108108
await sequelize.truncate();
109-
assert((await IntegerNames.findAll()).length === 0);
109+
assert.strictEqual((await IntegerNames.findAll()).length, 0);
110110

111111
// Datetime. Automatically converts to/from date objects.
112112
const Dates = sequelize.define('Dates', {
@@ -123,8 +123,8 @@ let date = await Dates.findOne({
123123
['date', 'ASC'],
124124
],
125125
});
126-
assert(date.date.getTime() === new Date(2000, 0, 1, 2, 3, 4, 5).getTime());
127-
assert(date.date.getTime() === dateCreate.date.getTime());
126+
assert.strictEqual(date.date.getTime(), new Date(2000, 0, 1, 2, 3, 4, 5).getTime());
127+
assert.strictEqual(date.date.getTime(), dateCreate.date.getTime());
128128

129129
// Otherwise it hangs for 10 seconds, it seems that it keeps the connection alive.
130130
// https://stackoverflow.com/questions/28253831/recreating-database-sequelizejs-is-slow
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env node
2+
3+
// https://stackoverflow.com/questions/54898994/bulkupdate-in-sequelize-orm/69044138#69044138
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
8+
const { Sequelize, DataTypes, Op } = require('sequelize');
9+
10+
const sequelize = new Sequelize({
11+
dialect: 'sqlite',
12+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
13+
});
14+
15+
(async () => {
16+
const Inverses = sequelize.define('Inverses',
17+
{
18+
value: {
19+
type: DataTypes.INTEGER,
20+
primaryKey: true,
21+
},
22+
inverse: {
23+
type: DataTypes.INTEGER,
24+
},
25+
name: {
26+
type: DataTypes.STRING,
27+
},
28+
},
29+
{ timestamps: false }
30+
);
31+
await Inverses.sync({force: true})
32+
await Inverses.create({value: 2, inverse: -2, name: 'two'});
33+
await Inverses.create({value: 3, inverse: -3, name: 'three'});
34+
await Inverses.create({value: 5, inverse: -5, name: 'five'});
35+
36+
// Initial state.
37+
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
38+
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, -3);
39+
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, -5);
40+
assert.strictEqual(await Inverses.count(), 3);
41+
42+
// Update to fixed value.
43+
await Inverses.update(
44+
{ inverse: 0, },
45+
{ where: { value: { [Op.gt]: 2 } } },
46+
);
47+
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
48+
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, 0);
49+
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, 0);
50+
assert.strictEqual(await Inverses.count(), 3);
51+
52+
// Update to match another column.
53+
await Inverses.update(
54+
{ inverse: sequelize.col('value'), },
55+
{ where: { value: { [Op.gt]: 2 } } },
56+
);
57+
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
58+
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, 3);
59+
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, 5);
60+
assert.strictEqual(await Inverses.count(), 3);
61+
62+
// Update to match another column with modification.
63+
await Inverses.update(
64+
{ inverse: sequelize.fn('1 + ', sequelize.col('value')), },
65+
{ where: { value: { [Op.gt]: 2 } } },
66+
);
67+
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
68+
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, 4);
69+
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, 6);
70+
assert.strictEqual(await Inverses.count(), 3);
71+
72+
// A string function test.
73+
await Inverses.update(
74+
{ name: sequelize.fn('upper', sequelize.col('name')), },
75+
{ where: { value: { [Op.gt]: 2 } } },
76+
);
77+
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).name, 'two');
78+
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).name, 'THREE');
79+
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).name, 'FIVE');
80+
assert.strictEqual(await Inverses.count(), 3);
81+
82+
await sequelize.close();
83+
})();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env node
2+
3+
// https://stackoverflow.com/questions/54898994/bulkupdate-in-sequelize-orm/69044138#69044138
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
8+
const { Sequelize, DataTypes } = require('sequelize');
9+
10+
const sequelize = new Sequelize({
11+
dialect: 'sqlite',
12+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
13+
});
14+
15+
(async () => {
16+
const IntegerNames = sequelize.define('IntegerNames',
17+
{
18+
value: {
19+
type: DataTypes.INTEGER,
20+
unique: true, // mandatory
21+
primaryKey: true,
22+
},
23+
name: {
24+
type: DataTypes.STRING,
25+
},
26+
},
27+
{
28+
timestamps: false,
29+
}
30+
);
31+
await IntegerNames.sync({force: true})
32+
await IntegerNames.create({value: 2, name: 'two'});
33+
await IntegerNames.create({value: 3, name: 'three'});
34+
await IntegerNames.create({value: 5, name: 'five'});
35+
36+
// Initial state.
37+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 2 } })).name, 'two');
38+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 3 } })).name, 'three');
39+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 5 } })).name, 'five');
40+
assert.strictEqual(await IntegerNames.count(), 3);
41+
42+
// Update.
43+
await IntegerNames.bulkCreate(
44+
[
45+
{value: 2, name: 'TWO'},
46+
{value: 3, name: 'THREE'},
47+
{value: 7, name: 'SEVEN'},
48+
],
49+
{ updateOnDuplicate: ["name"] }
50+
);
51+
52+
// Final state.
53+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 2 } })).name, 'TWO');
54+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 3 } })).name, 'THREE');
55+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 5 } })).name, 'five');
56+
assert.strictEqual((await IntegerNames.findOne({ where: { value: 7 } })).name, 'SEVEN');
57+
assert.strictEqual(await IntegerNames.count(), 4);
58+
59+
await sequelize.close();
60+
})();

0 commit comments

Comments
 (0)