Skip to content

Commit 46ab416

Browse files
committed
more sequelize
1 parent 6f496cc commit 46ab416

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

rootfs_overlay/lkmc/nodejs/sequelize/association_many_to_many.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const user0Likes = await user0.getPosts({order: [['body', 'ASC']]})
6262
assert(user0Likes[0].body === 'post0');
6363
assert(user0Likes[1].body === 'post1');
6464
assert(user0Likes.length === 2);
65+
assert.strictEqual(await user0.countPosts(), 2)
6566

6667
const user1Likes = await user1.getPosts({order: [['body', 'ASC']]})
6768
assert(user1Likes.length === 0);

rootfs_overlay/lkmc/nodejs/sequelize/index.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
const assert = require('assert');
1414
const path = require('path');
1515

16-
const { Sequelize, DataTypes } = require('sequelize');
16+
const { Sequelize, DataTypes, Op } = require('sequelize');
1717

1818
// To use the URI syntax, we need an explcit username and password.
1919
// But the second constructor works with peer authentication.
@@ -96,17 +96,30 @@ await IntegerNames.create({value: 5, name: 'five'});
9696
// 3 | 5 | five | 2021-03-19 19:12:08.437+00 | 2021-03-19 19:12:08.437+00
9797
// (3 rows)
9898

99-
const integerNames = await IntegerNames.findAll({
99+
let integerNames = await IntegerNames.findAll({
100100
where: {
101101
value: 2
102102
}
103103
});
104104
assert.strictEqual(integerNames[0].name, 'two');
105+
assert.strictEqual(integerNames.length, 1);
106+
107+
// SELECT and destroy: https://stackoverflow.com/questions/8402597/sequelize-js-delete-query
108+
await IntegerNames.destroy({
109+
where: {
110+
value: { [Op.gt]: 2 },
111+
},
112+
limit: 1,
113+
});
114+
integerNames = await IntegerNames.findAll({order: [['value', 'ASC']]})
115+
assert.strictEqual(integerNames[0].name, 'two');
116+
assert.strictEqual(integerNames[1].name, 'five');
117+
assert.strictEqual(integerNames.length, 2);
105118

106119
// Truncate all tables.
107120
// https://stackoverflow.com/questions/47816162/wipe-all-tables-in-a-schema-sequelize-nodejs/66985334#66985334
108121
await sequelize.truncate();
109-
assert.strictEqual((await IntegerNames.findAll()).length, 0);
122+
assert.strictEqual(await IntegerNames.count(), 0);
110123

111124
// Datetime. Automatically converts to/from date objects.
112125
const Dates = sequelize.define('Dates', {

0 commit comments

Comments
 (0)