more sequelize as usual

This commit is contained in:
Ciro Santilli
2021-09-03 13:49:06 +01:00
parent 1a1237218e
commit 0acd34a1d4
7 changed files with 285 additions and 36 deletions

View 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();
})();

View 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();
})();

View File

@@ -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

View 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();
})();

View 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();
})();