-
-
Notifications
You must be signed in to change notification settings - Fork 605
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1a12372
commit 0acd34a
Showing
7 changed files
with
285 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
61 changes: 61 additions & 0 deletions
61
rootfs_overlay/lkmc/nodejs/sequelize/before_validate_modify.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
})(); |