Skip to content

Commit

Permalink
more sequelize as usual
Browse files Browse the repository at this point in the history
  • Loading branch information
cirosantilli committed Sep 3, 2021
1 parent 1a12372 commit 0acd34a
Show file tree
Hide file tree
Showing 7 changed files with 285 additions and 36 deletions.
47 changes: 44 additions & 3 deletions rootfs_overlay/lkmc/nodejs/express.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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}`)

Expand All @@ -28,18 +55,32 @@ 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()
}
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')
})
29 changes: 0 additions & 29 deletions rootfs_overlay/lkmc/nodejs/express2.js

This file was deleted.

61 changes: 61 additions & 0 deletions rootfs_overlay/lkmc/nodejs/sequelize/before_validate_modify.js
Original file line number Diff line number Diff line change
@@ -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 changes: 33 additions & 0 deletions rootfs_overlay/lkmc/nodejs/sequelize/inc.js
Original file line number Diff line number Diff line change
@@ -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();
})();
8 changes: 4 additions & 4 deletions rootfs_overlay/lkmc/nodejs/sequelize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand All @@ -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
Expand Down
83 changes: 83 additions & 0 deletions rootfs_overlay/lkmc/nodejs/sequelize/update.js
Original file line number Diff line number Diff line change
@@ -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 changes: 60 additions & 0 deletions rootfs_overlay/lkmc/nodejs/sequelize/updateOnDuplicate.js
Original file line number Diff line number Diff line change
@@ -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();
})();

0 comments on commit 0acd34a

Please sign in to comment.