From 46ab41688918b9778131015da64a8ac62e4ffe29 Mon Sep 17 00:00:00 2001 From: Ciro Santilli Date: Sun, 31 Oct 2021 12:27:29 +0000 Subject: [PATCH] more sequelize --- .../sequelize/association_many_to_many.js | 1 + rootfs_overlay/lkmc/nodejs/sequelize/index.js | 19 ++++++++++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/rootfs_overlay/lkmc/nodejs/sequelize/association_many_to_many.js b/rootfs_overlay/lkmc/nodejs/sequelize/association_many_to_many.js index 2cb4d39..93d327f 100755 --- a/rootfs_overlay/lkmc/nodejs/sequelize/association_many_to_many.js +++ b/rootfs_overlay/lkmc/nodejs/sequelize/association_many_to_many.js @@ -62,6 +62,7 @@ const user0Likes = await user0.getPosts({order: [['body', 'ASC']]}) assert(user0Likes[0].body === 'post0'); assert(user0Likes[1].body === 'post1'); assert(user0Likes.length === 2); +assert.strictEqual(await user0.countPosts(), 2) const user1Likes = await user1.getPosts({order: [['body', 'ASC']]}) assert(user1Likes.length === 0); diff --git a/rootfs_overlay/lkmc/nodejs/sequelize/index.js b/rootfs_overlay/lkmc/nodejs/sequelize/index.js index 2536190..2cf071d 100755 --- a/rootfs_overlay/lkmc/nodejs/sequelize/index.js +++ b/rootfs_overlay/lkmc/nodejs/sequelize/index.js @@ -13,7 +13,7 @@ const assert = require('assert'); const path = require('path'); -const { Sequelize, DataTypes } = require('sequelize'); +const { Sequelize, DataTypes, Op } = require('sequelize'); // To use the URI syntax, we need an explcit username and password. // But the second constructor works with peer authentication. @@ -96,17 +96,30 @@ await IntegerNames.create({value: 5, name: 'five'}); // 3 | 5 | five | 2021-03-19 19:12:08.437+00 | 2021-03-19 19:12:08.437+00 // (3 rows) -const integerNames = await IntegerNames.findAll({ +let integerNames = await IntegerNames.findAll({ where: { value: 2 } }); assert.strictEqual(integerNames[0].name, 'two'); +assert.strictEqual(integerNames.length, 1); + +// SELECT and destroy: https://stackoverflow.com/questions/8402597/sequelize-js-delete-query +await IntegerNames.destroy({ + where: { + value: { [Op.gt]: 2 }, + }, + limit: 1, +}); +integerNames = await IntegerNames.findAll({order: [['value', 'ASC']]}) +assert.strictEqual(integerNames[0].name, 'two'); +assert.strictEqual(integerNames[1].name, 'five'); +assert.strictEqual(integerNames.length, 2); // Truncate all tables. // https://stackoverflow.com/questions/47816162/wipe-all-tables-in-a-schema-sequelize-nodejs/66985334#66985334 await sequelize.truncate(); -assert.strictEqual((await IntegerNames.findAll()).length, 0); +assert.strictEqual(await IntegerNames.count(), 0); // Datetime. Automatically converts to/from date objects. const Dates = sequelize.define('Dates', {