Skip to content

Commit cc66e47

Browse files
committed
sequelize stuff
1 parent 38eb67a commit cc66e47

File tree

4 files changed

+136
-10
lines changed

4 files changed

+136
-10
lines changed

rootfs_overlay/lkmc/nodejs/sequelize/association_many_to_many_custom_table.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ const sequelize = new Sequelize({
1717
// Create the tables.
1818
const User = sequelize.define('User', {
1919
name: { type: DataTypes.STRING },
20-
}, {});
20+
});
2121
const Post = sequelize.define('Post', {
2222
body: { type: DataTypes.STRING },
23-
}, {});
23+
});
2424
const UserLikesPost = sequelize.define('UserLikesPost', {
2525
UserId: {
2626
type: DataTypes.INTEGER,

rootfs_overlay/lkmc/nodejs/sequelize/association_nested_include.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ await users[0].addFollows([users[1], users[2]])
8080
assert(postsFound.length === 4)
8181
}
8282

83-
// With ordering, offset and limit.
83+
// Similar to the above, but now with ordering, offset and limit.
8484
// The posts are placed inside their respetive authors under .Posts
8585
// The only difference is that posts that we didn't select got removed.
86-
8786
{
8887
const user0Follows = (await User.findByPk(users[0].id, {
8988
offset: 1,
@@ -257,9 +256,10 @@ await users[0].addFollows([users[1], users[2]])
257256
assert(postsFound.length === 4)
258257
}
259258

260-
//// This almost achieves the flat array return. We just have to understand the undocumented custom on:
261-
//// to specify from which side of the UserFollowsUser we are coming. The on:
262-
//// is ignored without super many to many unfortunately, the below just returns all posts.
259+
// This almost achieves the flat array return. We just have to understand the undocumented custom on:
260+
// to specify from which side of the UserFollowsUser we are coming. The on:
261+
// is ignored without super many to many unfortunately, the below just returns all posts.
262+
// We only managed to achieve this with super many to many so far.
263263
{
264264
const postsFound = await Post.findAll({
265265
order: [[
@@ -281,7 +281,7 @@ await users[0].addFollows([users[1], users[2]])
281281
},
282282
],
283283
})
284-
console.error(postsFound.length);
284+
//console.error(postsFound.length);
285285
//assert.strictEqual(postsFound[0].body, 'body6')
286286
//assert.strictEqual(postsFound[1].body, 'body5')
287287
//assert.strictEqual(postsFound[2].body, 'body2')

rootfs_overlay/lkmc/nodejs/sequelize/association_nested_include_super.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ const posts = await Post.bulkCreate([
7272
await users[0].addFollows([users[1], users[2]])
7373

7474
// Get all the posts by authors that user0 follows.
75-
// without post process sorting. We only managed to to this
75+
// without any post process sorting. We only managed to to this
7676
// with a super many to many, because that allows us to specify
7777
// a reversed order in the through table with `on`, since we need to
7878
// match with `FollowId` and not `UserId`.
@@ -100,7 +100,6 @@ await users[0].addFollows([users[1], users[2]])
100100
},
101101
],
102102
})
103-
console.error(postsFound.length);
104103
assert.strictEqual(postsFound[0].body, 'body6')
105104
assert.strictEqual(postsFound[1].body, 'body5')
106105
assert.strictEqual(postsFound[2].body, 'body2')
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#!/usr/bin/env node
2+
3+
// Trying to get everything in the database camel cased, columns lowercase, tables uppercase.
4+
// The defaults documented on getting started documentation do uppercase foreign keys, an
5+
// lowercase non-foreign keys. It's a mess.
6+
7+
const assert = require('assert');
8+
const path = require('path');
9+
10+
const { Sequelize, DataTypes } = require('sequelize');
11+
12+
const sequelize = new Sequelize({
13+
dialect: 'sqlite',
14+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
15+
define: {
16+
timestamps: false
17+
},
18+
});
19+
20+
(async () => {
21+
22+
// Create the tables.
23+
const User = sequelize.define('User', {
24+
name: { type: DataTypes.STRING },
25+
});
26+
const Post = sequelize.define('Post', {
27+
body: { type: DataTypes.STRING },
28+
});
29+
const UserFollowsUser = sequelize.define('UserFollowsUser', {
30+
userId: {
31+
type: DataTypes.INTEGER,
32+
references: {
33+
model: User,
34+
key: 'id'
35+
}
36+
},
37+
followId: {
38+
type: DataTypes.INTEGER,
39+
references: {
40+
model: User,
41+
key: 'id'
42+
}
43+
},
44+
});
45+
User.belongsToMany(User, {
46+
through: UserFollowsUser,
47+
as: 'follows',
48+
foreignKey: 'userId',
49+
otherKey: 'followId',
50+
});
51+
User.hasMany(Post, {foreignKey: 'authorId'});
52+
Post.belongsTo(User, {foreignKey: 'authorId'});
53+
await sequelize.sync({force: true});
54+
55+
// Create data.
56+
const users = await User.bulkCreate([
57+
{name: 'user0'},
58+
{name: 'user1'},
59+
{name: 'user2'},
60+
{name: 'user3'},
61+
])
62+
63+
const posts = await Post.bulkCreate([
64+
{body: 'body00', authorId: users[0].id},
65+
{body: 'body01', authorId: users[0].id},
66+
{body: 'body10', authorId: users[1].id},
67+
{body: 'body11', authorId: users[1].id},
68+
{body: 'body20', authorId: users[2].id},
69+
{body: 'body21', authorId: users[2].id},
70+
{body: 'body30', authorId: users[3].id},
71+
{body: 'body31', authorId: users[3].id},
72+
])
73+
await users[0].addFollows([users[1], users[2]])
74+
75+
// Check case of auto-getters.
76+
77+
const user0Posts = await users[0].getPosts({order: [['body', 'ASC']]})
78+
assert(user0Posts[0].body === 'body00')
79+
assert(user0Posts[1].body === 'body01')
80+
assert(user0Posts.length === 2)
81+
82+
const user1Posts = await users[1].getPosts({order: [['body', 'ASC']]})
83+
assert(user1Posts[0].body === 'body10')
84+
assert(user1Posts[1].body === 'body11')
85+
assert(user1Posts.length === 2)
86+
87+
const post00User = await posts[0].getUser()
88+
assert(post00User.name === 'user0')
89+
90+
const post01User = await posts[1].getUser()
91+
assert(post01User.name === 'user0')
92+
93+
const post10User = await posts[2].getUser()
94+
assert(post10User.name === 'user1')
95+
96+
const post11User = await posts[3].getUser()
97+
assert(post11User.name === 'user1')
98+
99+
// Check case of as;
100+
{
101+
const user0Follows = (await User.findByPk(users[0].id, {
102+
include: [
103+
{
104+
model: User,
105+
as: 'follows',
106+
include: [
107+
{
108+
model: Post,
109+
}
110+
],
111+
},
112+
],
113+
})).follows
114+
const postsFound = []
115+
for (const followedUser of user0Follows) {
116+
postsFound.push(...followedUser.Posts)
117+
}
118+
postsFound.sort((x, y) => { return x.body < y.body ? -1 : x.body > y.body ? 1 : 0 })
119+
assert(postsFound[0].body === 'body10')
120+
assert(postsFound[1].body === 'body11')
121+
assert(postsFound[2].body === 'body20')
122+
assert(postsFound[3].body === 'body21')
123+
assert(postsFound.length === 4)
124+
}
125+
126+
await sequelize.close();
127+
})();

0 commit comments

Comments
 (0)