mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-22 17:55:57 +01:00
more sequelize as usual
This commit is contained in:
@@ -2,10 +2,31 @@
|
||||
|
||||
const express = require('express')
|
||||
|
||||
function check_helper(req, res) {
|
||||
if (req.params.param.length > 2) {
|
||||
res.status(404)
|
||||
res.send('ko')
|
||||
} else {
|
||||
return req.params.param + 'ok'
|
||||
}
|
||||
}
|
||||
|
||||
const app = express()
|
||||
app.get('/', (req, res) => {
|
||||
res.send('hello world')
|
||||
})
|
||||
app.get('/check-helper-1/:param', (req, res) => {
|
||||
const ret = check_helper(req, res)
|
||||
if (ret) {
|
||||
res.send(ret)
|
||||
}
|
||||
})
|
||||
app.get('/check-helper-2/:param', (req, res) => {
|
||||
const ret = check_helper(req, res)
|
||||
if (ret) {
|
||||
res.send(ret)
|
||||
}
|
||||
})
|
||||
app.get('/error', async (req, res, next) => {
|
||||
try {
|
||||
throw 'my error'
|
||||
@@ -14,6 +35,12 @@ app.get('/error', async (req, res, next) => {
|
||||
next(error);
|
||||
}
|
||||
})
|
||||
app.get('/query', (req, res) => {
|
||||
res.send(`aa: ${req.query.aa} bb: ${req.query.bb}`)
|
||||
})
|
||||
app.get('/splat/:splat(*)', (req, res) => {
|
||||
res.send('splat ' + req.params.splat)
|
||||
})
|
||||
const server = app.listen(3000, () => {
|
||||
console.log(`listening: http://localhost:${server.address().port}`)
|
||||
|
||||
@@ -28,11 +55,10 @@ const server = app.listen(3000, () => {
|
||||
method: method,
|
||||
}
|
||||
http.request(options, res => {
|
||||
console.error(res.statusCode);
|
||||
assert(res.statusCode === status);
|
||||
assert.strictEqual(res.statusCode, status);
|
||||
res.on('data', d => {
|
||||
if (body !== undefined) {
|
||||
assert(d.toString() === body);
|
||||
assert.strictEqual(d.toString(), body);
|
||||
}
|
||||
})
|
||||
}).end()
|
||||
@@ -40,6 +66,21 @@ const server = app.listen(3000, () => {
|
||||
test('/', 'GET', 200, 'hello world')
|
||||
test('/', 'POST', 404)
|
||||
test('/dontexist', 'GET', 404)
|
||||
|
||||
// Shows 'my error' on terminal, without stack trace.
|
||||
test('/error', 'GET', 500)
|
||||
|
||||
test('/query?aa=000&bb=111', 'GET', 200, 'aa: 000 bb: 111')
|
||||
|
||||
// https://stackoverflow.com/questions/10020099/express-js-routing-optional-splat-param
|
||||
// https://stackoverflow.com/questions/16829803/express-js-route-parameter-with-slashes
|
||||
// https://stackoverflow.com/questions/34571784/how-to-use-parameters-containing-a-slash-character
|
||||
test('/splat/aaa', 'GET', 200, 'splat aaa')
|
||||
test('/splat/aaa/bbb', 'GET', 200, 'splat aaa/bbb')
|
||||
test('/splat/aaa/bbb/ccc', 'GET', 200, 'splat aaa/bbb/ccc')
|
||||
|
||||
test('/check-helper-1/aa', 'GET', 200, 'aaok')
|
||||
test('/check-helper-2/bb', 'GET', 200, 'bbok')
|
||||
test('/check-helper-1/ccc', 'GET', 404, 'ko')
|
||||
test('/check-helper-2/ddd', 'GET', 404, 'ko')
|
||||
})
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const assert = require('assert')
|
||||
const http = require('http')
|
||||
|
||||
const express = require('express')
|
||||
const app = express()
|
||||
const port = 3000
|
||||
|
||||
app.get('/error', async (req, res) => {
|
||||
throw 'my error'
|
||||
})
|
||||
|
||||
const server = app.listen(port, () => {
|
||||
// Test it.
|
||||
function test(path, method, status, body) {
|
||||
const options = {
|
||||
hostname: 'localhost',
|
||||
port: server.address().port,
|
||||
path: path,
|
||||
method: method,
|
||||
}
|
||||
http.request(options, res => {
|
||||
console.error(res.statusCode);
|
||||
assert(res.statusCode === status);
|
||||
}).end()
|
||||
}
|
||||
test('/error', 'GET', 500)
|
||||
})
|
||||
61
rootfs_overlay/lkmc/nodejs/sequelize/before_validate_modify.js
Executable file
61
rootfs_overlay/lkmc/nodejs/sequelize/before_validate_modify.js
Executable file
@@ -0,0 +1,61 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// https://github.com/sequelize/sequelize/issues/3534
|
||||
// https://github.com/sequelize/sequelize/issues/8586
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
|
||||
const { Sequelize, DataTypes } = require('sequelize');
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
|
||||
});
|
||||
|
||||
(async () => {
|
||||
|
||||
const IntegerNames = sequelize.define('IntegerNames',
|
||||
{
|
||||
value: {
|
||||
type: DataTypes.INTEGER,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
name2: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
|
||||
},
|
||||
{
|
||||
hooks: {
|
||||
beforeValidate: (integerName, options) => {
|
||||
integerName.name2 = integerName.name + 'asdf'
|
||||
// This fixes the failure.
|
||||
//options.fields.push('name2');
|
||||
}
|
||||
,}
|
||||
},
|
||||
);
|
||||
await IntegerNames.sync({force: true})
|
||||
await IntegerNames.create({value: 2, name: 'two'});
|
||||
await IntegerNames.create({value: 3, name: 'three'});
|
||||
await IntegerNames.create({value: 5, name: 'five'});
|
||||
|
||||
const integerName = await IntegerNames.findOne({ where: { value: 2 } });
|
||||
assert.strictEqual(integerName.name, 'two');
|
||||
assert.strictEqual(integerName.name2, 'twoasdf');
|
||||
integerName.name = 'TWO'
|
||||
integerName.save();
|
||||
|
||||
const integerName2 = await IntegerNames.findOne({ where: { value: 2 } });
|
||||
assert.strictEqual(integerName2.name, 'TWO');
|
||||
// Fails.
|
||||
//assert.strictEqual(integerName2.name2, 'TWOasdf');
|
||||
|
||||
await sequelize.close();
|
||||
|
||||
})();
|
||||
33
rootfs_overlay/lkmc/nodejs/sequelize/inc.js
Executable file
33
rootfs_overlay/lkmc/nodejs/sequelize/inc.js
Executable file
@@ -0,0 +1,33 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
|
||||
const { Sequelize, DataTypes } = require('sequelize');
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
|
||||
});
|
||||
|
||||
(async () => {
|
||||
await sequelize.authenticate();
|
||||
const IntegerNames = sequelize.define('IntegerNames', {
|
||||
value: {
|
||||
type: DataTypes.INTEGER,
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
});
|
||||
await IntegerNames.sync({force: true})
|
||||
await IntegerNames.create({value: 2, name: 'two'});
|
||||
await IntegerNames.create({value: 3, name: 'three'});
|
||||
await IntegerNames.create({value: 5, name: 'five'});
|
||||
const integerName5 = await IntegerNames.findOne({ where: { value: 5 } });
|
||||
integerName5.increment('value')
|
||||
// Sequelize updates, but others don't...
|
||||
console.error(integerName5.value);
|
||||
const integerName6 = await IntegerNames.findOne({ where: { value: 6 } });
|
||||
assert.strictEqual(integerName6.name, 'five')
|
||||
await sequelize.close();
|
||||
})();
|
||||
@@ -101,12 +101,12 @@ const integerNames = await IntegerNames.findAll({
|
||||
value: 2
|
||||
}
|
||||
});
|
||||
assert(integerNames[0].name === 'two');
|
||||
assert.strictEqual(integerNames[0].name, 'two');
|
||||
|
||||
// Truncate all tables.
|
||||
// https://stackoverflow.com/questions/47816162/wipe-all-tables-in-a-schema-sequelize-nodejs/66985334#66985334
|
||||
await sequelize.truncate();
|
||||
assert((await IntegerNames.findAll()).length === 0);
|
||||
assert.strictEqual((await IntegerNames.findAll()).length, 0);
|
||||
|
||||
// Datetime. Automatically converts to/from date objects.
|
||||
const Dates = sequelize.define('Dates', {
|
||||
@@ -123,8 +123,8 @@ let date = await Dates.findOne({
|
||||
['date', 'ASC'],
|
||||
],
|
||||
});
|
||||
assert(date.date.getTime() === new Date(2000, 0, 1, 2, 3, 4, 5).getTime());
|
||||
assert(date.date.getTime() === dateCreate.date.getTime());
|
||||
assert.strictEqual(date.date.getTime(), new Date(2000, 0, 1, 2, 3, 4, 5).getTime());
|
||||
assert.strictEqual(date.date.getTime(), dateCreate.date.getTime());
|
||||
|
||||
// Otherwise it hangs for 10 seconds, it seems that it keeps the connection alive.
|
||||
// https://stackoverflow.com/questions/28253831/recreating-database-sequelizejs-is-slow
|
||||
|
||||
83
rootfs_overlay/lkmc/nodejs/sequelize/update.js
Executable file
83
rootfs_overlay/lkmc/nodejs/sequelize/update.js
Executable file
@@ -0,0 +1,83 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// https://stackoverflow.com/questions/54898994/bulkupdate-in-sequelize-orm/69044138#69044138
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
|
||||
const { Sequelize, DataTypes, Op } = require('sequelize');
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
|
||||
});
|
||||
|
||||
(async () => {
|
||||
const Inverses = sequelize.define('Inverses',
|
||||
{
|
||||
value: {
|
||||
type: DataTypes.INTEGER,
|
||||
primaryKey: true,
|
||||
},
|
||||
inverse: {
|
||||
type: DataTypes.INTEGER,
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
},
|
||||
{ timestamps: false }
|
||||
);
|
||||
await Inverses.sync({force: true})
|
||||
await Inverses.create({value: 2, inverse: -2, name: 'two'});
|
||||
await Inverses.create({value: 3, inverse: -3, name: 'three'});
|
||||
await Inverses.create({value: 5, inverse: -5, name: 'five'});
|
||||
|
||||
// Initial state.
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, -3);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, -5);
|
||||
assert.strictEqual(await Inverses.count(), 3);
|
||||
|
||||
// Update to fixed value.
|
||||
await Inverses.update(
|
||||
{ inverse: 0, },
|
||||
{ where: { value: { [Op.gt]: 2 } } },
|
||||
);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, 0);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, 0);
|
||||
assert.strictEqual(await Inverses.count(), 3);
|
||||
|
||||
// Update to match another column.
|
||||
await Inverses.update(
|
||||
{ inverse: sequelize.col('value'), },
|
||||
{ where: { value: { [Op.gt]: 2 } } },
|
||||
);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, 3);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, 5);
|
||||
assert.strictEqual(await Inverses.count(), 3);
|
||||
|
||||
// Update to match another column with modification.
|
||||
await Inverses.update(
|
||||
{ inverse: sequelize.fn('1 + ', sequelize.col('value')), },
|
||||
{ where: { value: { [Op.gt]: 2 } } },
|
||||
);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).inverse, -2);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).inverse, 4);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).inverse, 6);
|
||||
assert.strictEqual(await Inverses.count(), 3);
|
||||
|
||||
// A string function test.
|
||||
await Inverses.update(
|
||||
{ name: sequelize.fn('upper', sequelize.col('name')), },
|
||||
{ where: { value: { [Op.gt]: 2 } } },
|
||||
);
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 2 } })).name, 'two');
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 3 } })).name, 'THREE');
|
||||
assert.strictEqual((await Inverses.findOne({ where: { value: 5 } })).name, 'FIVE');
|
||||
assert.strictEqual(await Inverses.count(), 3);
|
||||
|
||||
await sequelize.close();
|
||||
})();
|
||||
60
rootfs_overlay/lkmc/nodejs/sequelize/updateOnDuplicate.js
Normal file
60
rootfs_overlay/lkmc/nodejs/sequelize/updateOnDuplicate.js
Normal file
@@ -0,0 +1,60 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
// https://stackoverflow.com/questions/54898994/bulkupdate-in-sequelize-orm/69044138#69044138
|
||||
|
||||
const assert = require('assert');
|
||||
const path = require('path');
|
||||
|
||||
const { Sequelize, DataTypes } = require('sequelize');
|
||||
|
||||
const sequelize = new Sequelize({
|
||||
dialect: 'sqlite',
|
||||
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
|
||||
});
|
||||
|
||||
(async () => {
|
||||
const IntegerNames = sequelize.define('IntegerNames',
|
||||
{
|
||||
value: {
|
||||
type: DataTypes.INTEGER,
|
||||
unique: true, // mandatory
|
||||
primaryKey: true,
|
||||
},
|
||||
name: {
|
||||
type: DataTypes.STRING,
|
||||
},
|
||||
},
|
||||
{
|
||||
timestamps: false,
|
||||
}
|
||||
);
|
||||
await IntegerNames.sync({force: true})
|
||||
await IntegerNames.create({value: 2, name: 'two'});
|
||||
await IntegerNames.create({value: 3, name: 'three'});
|
||||
await IntegerNames.create({value: 5, name: 'five'});
|
||||
|
||||
// Initial state.
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 2 } })).name, 'two');
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 3 } })).name, 'three');
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 5 } })).name, 'five');
|
||||
assert.strictEqual(await IntegerNames.count(), 3);
|
||||
|
||||
// Update.
|
||||
await IntegerNames.bulkCreate(
|
||||
[
|
||||
{value: 2, name: 'TWO'},
|
||||
{value: 3, name: 'THREE'},
|
||||
{value: 7, name: 'SEVEN'},
|
||||
],
|
||||
{ updateOnDuplicate: ["name"] }
|
||||
);
|
||||
|
||||
// Final state.
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 2 } })).name, 'TWO');
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 3 } })).name, 'THREE');
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 5 } })).name, 'five');
|
||||
assert.strictEqual((await IntegerNames.findOne({ where: { value: 7 } })).name, 'SEVEN');
|
||||
assert.strictEqual(await IntegerNames.count(), 4);
|
||||
|
||||
await sequelize.close();
|
||||
})();
|
||||
Reference in New Issue
Block a user