sequelize stuff

This commit is contained in:
Ciro Santilli
2021-07-27 08:58:44 +01:00
parent 38eb67ad2e
commit cc66e47dd0
4 changed files with 136 additions and 10 deletions

View File

@@ -17,10 +17,10 @@ const sequelize = new Sequelize({
// Create the tables.
const User = sequelize.define('User', {
name: { type: DataTypes.STRING },
}, {});
});
const Post = sequelize.define('Post', {
body: { type: DataTypes.STRING },
}, {});
});
const UserLikesPost = sequelize.define('UserLikesPost', {
UserId: {
type: DataTypes.INTEGER,

View File

@@ -80,10 +80,9 @@ await users[0].addFollows([users[1], users[2]])
assert(postsFound.length === 4)
}
// With ordering, offset and limit.
// Similar to the above, but now with ordering, offset and limit.
// The posts are placed inside their respetive authors under .Posts
// The only difference is that posts that we didn't select got removed.
{
const user0Follows = (await User.findByPk(users[0].id, {
offset: 1,
@@ -257,9 +256,10 @@ await users[0].addFollows([users[1], users[2]])
assert(postsFound.length === 4)
}
//// This almost achieves the flat array return. We just have to understand the undocumented custom on:
//// to specify from which side of the UserFollowsUser we are coming. The on:
//// is ignored without super many to many unfortunately, the below just returns all posts.
// This almost achieves the flat array return. We just have to understand the undocumented custom on:
// to specify from which side of the UserFollowsUser we are coming. The on:
// is ignored without super many to many unfortunately, the below just returns all posts.
// We only managed to achieve this with super many to many so far.
{
const postsFound = await Post.findAll({
order: [[
@@ -281,7 +281,7 @@ await users[0].addFollows([users[1], users[2]])
},
],
})
console.error(postsFound.length);
//console.error(postsFound.length);
//assert.strictEqual(postsFound[0].body, 'body6')
//assert.strictEqual(postsFound[1].body, 'body5')
//assert.strictEqual(postsFound[2].body, 'body2')

View File

@@ -72,7 +72,7 @@ const posts = await Post.bulkCreate([
await users[0].addFollows([users[1], users[2]])
// Get all the posts by authors that user0 follows.
// without post process sorting. We only managed to to this
// without any post process sorting. We only managed to to this
// with a super many to many, because that allows us to specify
// a reversed order in the through table with `on`, since we need to
// match with `FollowId` and not `UserId`.
@@ -100,7 +100,6 @@ await users[0].addFollows([users[1], users[2]])
},
],
})
console.error(postsFound.length);
assert.strictEqual(postsFound[0].body, 'body6')
assert.strictEqual(postsFound[1].body, 'body5')
assert.strictEqual(postsFound[2].body, 'body2')

View File

@@ -0,0 +1,127 @@
#!/usr/bin/env node
// Trying to get everything in the database camel cased, columns lowercase, tables uppercase.
// The defaults documented on getting started documentation do uppercase foreign keys, an
// lowercase non-foreign keys. It's a mess.
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',
define: {
timestamps: false
},
});
(async () => {
// Create the tables.
const User = sequelize.define('User', {
name: { type: DataTypes.STRING },
});
const Post = sequelize.define('Post', {
body: { type: DataTypes.STRING },
});
const UserFollowsUser = sequelize.define('UserFollowsUser', {
userId: {
type: DataTypes.INTEGER,
references: {
model: User,
key: 'id'
}
},
followId: {
type: DataTypes.INTEGER,
references: {
model: User,
key: 'id'
}
},
});
User.belongsToMany(User, {
through: UserFollowsUser,
as: 'follows',
foreignKey: 'userId',
otherKey: 'followId',
});
User.hasMany(Post, {foreignKey: 'authorId'});
Post.belongsTo(User, {foreignKey: 'authorId'});
await sequelize.sync({force: true});
// Create data.
const users = await User.bulkCreate([
{name: 'user0'},
{name: 'user1'},
{name: 'user2'},
{name: 'user3'},
])
const posts = await Post.bulkCreate([
{body: 'body00', authorId: users[0].id},
{body: 'body01', authorId: users[0].id},
{body: 'body10', authorId: users[1].id},
{body: 'body11', authorId: users[1].id},
{body: 'body20', authorId: users[2].id},
{body: 'body21', authorId: users[2].id},
{body: 'body30', authorId: users[3].id},
{body: 'body31', authorId: users[3].id},
])
await users[0].addFollows([users[1], users[2]])
// Check case of auto-getters.
const user0Posts = await users[0].getPosts({order: [['body', 'ASC']]})
assert(user0Posts[0].body === 'body00')
assert(user0Posts[1].body === 'body01')
assert(user0Posts.length === 2)
const user1Posts = await users[1].getPosts({order: [['body', 'ASC']]})
assert(user1Posts[0].body === 'body10')
assert(user1Posts[1].body === 'body11')
assert(user1Posts.length === 2)
const post00User = await posts[0].getUser()
assert(post00User.name === 'user0')
const post01User = await posts[1].getUser()
assert(post01User.name === 'user0')
const post10User = await posts[2].getUser()
assert(post10User.name === 'user1')
const post11User = await posts[3].getUser()
assert(post11User.name === 'user1')
// Check case of as;
{
const user0Follows = (await User.findByPk(users[0].id, {
include: [
{
model: User,
as: 'follows',
include: [
{
model: Post,
}
],
},
],
})).follows
const postsFound = []
for (const followedUser of user0Follows) {
postsFound.push(...followedUser.Posts)
}
postsFound.sort((x, y) => { return x.body < y.body ? -1 : x.body > y.body ? 1 : 0 })
assert(postsFound[0].body === 'body10')
assert(postsFound[1].body === 'body11')
assert(postsFound[2].body === 'body20')
assert(postsFound[3].body === 'body21')
assert(postsFound.length === 4)
}
await sequelize.close();
})();