Skip to content

Commit 8c86e0a

Browse files
committed
more sequelize stuff
1 parent dbf0cbd commit 8c86e0a

File tree

8 files changed

+326
-3
lines changed

8 files changed

+326
-3
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env node
2+
3+
const assert = require('assert');
4+
5+
assert(1 + 1 === 2)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/usr/bin/env node
2+
3+
// https://stackoverflow.com/questions/2257993/how-to-display-all-methods-of-an-object/67975040#67975040
4+
// https://stackoverflow.com/questions/30881632/es6-iterate-over-class-methods/47714550#47714550
5+
6+
const isGetter = (x, name) => (Object.getOwnPropertyDescriptor(x, name) || {}).get
7+
const isFunction = (x, name) => typeof x[name] === "function";
8+
const deepFunctions = x =>
9+
x && x !== Object.prototype &&
10+
Object.getOwnPropertyNames(x)
11+
.filter(name => isGetter(x, name) || isFunction(x, name))
12+
.concat(deepFunctions(Object.getPrototypeOf(x)) || []);
13+
const distinctDeepFunctions = x => Array.from(new Set(deepFunctions(x)));
14+
const getMethods = (obj) => distinctDeepFunctions(obj).filter(
15+
name => name !== "constructor" && !~name.indexOf("__"));
16+
17+
// Example usage.
18+
19+
class BaseClass {
20+
constructor() {
21+
this.baseProp = 1
22+
}
23+
override() { return 1; }
24+
baseMethod() { return 2; }
25+
}
26+
27+
class DerivedClass extends BaseClass {
28+
constructor() {
29+
super()
30+
this.derivedProp = 2
31+
}
32+
override() { return 3; }
33+
get myGetter() { return 4; }
34+
static myStatic() { return 5; }
35+
}
36+
37+
const obj = new DerivedClass();
38+
const methods = getMethods(obj)
39+
methods.sort()
40+
const assert = require('assert')
41+
const util = require('util')
42+
assert(methods[0] === 'baseMethod')
43+
assert(methods[1] === 'myGetter')
44+
assert(methods[2] === 'override')
45+
assert(methods.length === 3)
46+
47+
console.log(getMethods(Math))

rootfs_overlay/lkmc/nodejs/object_to_string.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,19 @@ let my_object = new MyClassUtilInspectCustom(1, 2, new MyClassUtilInspectCustomS
2929
// Affected.
3030
console.log('util.inspect');
3131
console.log(util.inspect(my_object));
32+
console.log();
3233
// Affected.
3334
console.log('console.log');
3435
console.log(my_object);
36+
console.log();
3537
// Not affected.
3638
console.log('toString');
3739
console.log(my_object.toString());
40+
console.log();
3841
// Not affected.
3942
console.log('toString implicit +');
4043
console.log('implicit ' + my_object);
44+
console.log();
4145
// Not affected.
4246
console.log('template string');
4347
console.log(`${my_object}`);
@@ -68,15 +72,20 @@ my_object = new MyClassToString(1, 2, new MyClassToStringSubobject(3, 4));
6872
// Affected.
6973
console.log('util.inspect');
7074
console.log(util.inspect(my_object));
75+
console.log();
7176
// Affected.
7277
console.log('console.log');
7378
console.log(my_object);
79+
console.log();
7480
// Affected.
7581
console.log('toString');
7682
console.log(my_object.toString());
83+
console.log();
7784
// Affected.
7885
console.log('toString implicit +');
7986
console.log('implicit ' + my_object);
87+
console.log();
8088
// Affected.
8189
console.log('template string');
8290
console.log(`${my_object}`);
91+
console.log();

rootfs_overlay/lkmc/nodejs/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
"pg": "8.5.1",
1010
"pg-hstore": "2.3.3",
1111
"sequelize": "6.5.1",
12-
"sqlite3": "^5.0.2",
13-
"ts-node": "^10.0.0"
12+
"sqlite3": "5.0.2",
13+
"ts-node": "10.0.0"
1414
},
1515
"devDependencies": {},
1616
"scripts": {

rootfs_overlay/lkmc/nodejs/sequelize/association.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ await Comment.create({body: 'u1c0', UserId: u1.id});
6565
console.log(Object.getOwnPropertyNames(u0));
6666
const u0Comments = await u0.getComments({
6767
include: [{ model: User }],
68+
order: [['id', 'ASC']],
6869
});
6970
assert(u0Comments[0].body === 'u0c0');
7071
assert(u0Comments[1].body === 'u0c1');
@@ -143,6 +144,5 @@ await Comment.create({body: 'u1c0', UserId: u1.id});
143144
//assert(u0Comments[1].author.name === 'u0');
144145
}
145146
}
146-
147147
await sequelize.close();
148148
})();
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
#!/usr/bin/env node
2+
3+
// https://stackoverflow.com/questions/22958683/how-to-implement-many-to-many-association-in-sequelize/67973948#67973948
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
8+
const { Sequelize, DataTypes } = require('sequelize');
9+
10+
const sequelize = new Sequelize({
11+
dialect: 'sqlite',
12+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
13+
});
14+
15+
(async () => {
16+
17+
// Create the tables.
18+
const User = sequelize.define('User', {
19+
name: { type: DataTypes.STRING },
20+
}, {});
21+
const Post = sequelize.define('Post', {
22+
body: { type: DataTypes.STRING },
23+
}, {});
24+
// UserLikesPost is the name of the relation table.
25+
// Sequelize creates it automatically for us.
26+
// On SQLite that table looks like this:
27+
// CREATE TABLE `UserLikesPost` (
28+
// `createdAt` DATETIME NOT NULL,
29+
// `updatedAt` DATETIME NOT NULL,
30+
// `UserId` INTEGER NOT NULL REFERENCES `Users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
31+
// `PostId` INTEGER NOT NULL REFERENCES `Posts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
32+
// PRIMARY KEY (`UserId`, `PostId`)
33+
// );
34+
User.belongsToMany(Post, {through: 'UserLikesPost'});
35+
Post.belongsToMany(User, {through: 'UserLikesPost'});
36+
await sequelize.sync({force: true});
37+
38+
// Create some users and likes.
39+
40+
const user0 = await User.create({name: 'user0'})
41+
const user1 = await User.create({name: 'user1'})
42+
const user2 = await User.create({name: 'user2'})
43+
44+
const post0 = await Post.create({body: 'post0'});
45+
const post1 = await Post.create({body: 'post1'});
46+
const post2 = await Post.create({body: 'post2'});
47+
48+
// Autogenerated add* methods
49+
50+
// Make user0 like post0
51+
await user0.addPost(post0)
52+
// Make user0 and user2 like post1
53+
await post1.addUsers([user0, user2])
54+
55+
// Autogenerated get* methods
56+
57+
// Get likes by a user.
58+
59+
const user0Likes = await user0.getPosts({order: [['body', 'ASC']]})
60+
assert(user0Likes[0].body === 'post0');
61+
assert(user0Likes[1].body === 'post1');
62+
assert(user0Likes.length === 2);
63+
64+
const user1Likes = await user1.getPosts({order: [['body', 'ASC']]})
65+
assert(user1Likes.length === 0);
66+
67+
const user2Likes = await user2.getPosts({order: [['body', 'ASC']]})
68+
assert(user2Likes[0].body === 'post1');
69+
assert(user2Likes.length === 1);
70+
71+
// Get users that liked a given likes.
72+
73+
const post0Likers = await post0.getUsers({order: [['name', 'ASC']]})
74+
assert(post0Likers[0].name === 'user0');
75+
assert(post0Likers.length === 1);
76+
77+
const post1Likers = await post1.getUsers({order: [['name', 'ASC']]})
78+
assert(post1Likers[0].name === 'user0');
79+
assert(post1Likers[1].name === 'user2');
80+
assert(post1Likers.length === 2);
81+
82+
const post2Likers = await post2.getUsers({order: [['name', 'ASC']]})
83+
assert(post1Likers[0].name === 'user0');
84+
assert(post1Likers[1].name === 'user2');
85+
assert(post1Likers.length === 2);
86+
87+
// Autogenerated has* methods
88+
89+
// Check if user likes post.
90+
assert( await user0.hasPost(post0))
91+
assert( await user0.hasPost(post1))
92+
assert(!await user0.hasPost(post2))
93+
94+
// Check if post is liked by user.
95+
assert( await post0.hasUser(user0))
96+
assert(!await post0.hasUser(user1))
97+
assert(!await post0.hasUser(user2))
98+
99+
// AND of multiple has checks at once.
100+
assert( await user0.hasPosts([post0, post1]))
101+
assert(!await user0.hasPosts([post0, post1, post2]))
102+
103+
// Autogenerated count* methods
104+
assert(await user0.countPosts() === 2)
105+
assert(await post0.countUsers() === 1)
106+
107+
// Autogenerated remove* method
108+
109+
// user0 doesn't like post0 anymore.
110+
await user0.removePost(post0)
111+
// user0 and user 2 don't like post1 anymore.
112+
await post1.removeUsers([user0, user2])
113+
// Check that no-one likes anything anymore.
114+
assert(await user0.countPosts() === 0)
115+
assert(await post0.countUsers() === 0)
116+
117+
// Autogenerated create* method
118+
// Create a new post and automatically make user0 like it.
119+
const post3 = await user0.createPost({'body': 'post3'})
120+
assert(await user0.hasPost(post3))
121+
assert(await post3.hasUser(user0))
122+
123+
// Autogenerated set* method
124+
// Make user0 like exactly these posts. Unlike anything else.
125+
await user0.setPosts([post1, post2])
126+
assert(!await user0.hasPost(post0))
127+
assert( await user0.hasPost(post1))
128+
assert( await user0.hasPost(post2))
129+
assert(!await user0.hasPost(post3))
130+
131+
await sequelize.close();
132+
})();
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
// https://stackoverflow.com/questions/22958683/how-to-implement-many-to-many-association-in-sequelize/67973948#67973948
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
8+
const { Sequelize, DataTypes } = require('sequelize');
9+
10+
const sequelize = new Sequelize({
11+
dialect: 'sqlite',
12+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
13+
});
14+
15+
(async () => {
16+
17+
// Create the tables.
18+
const User = sequelize.define('User', {
19+
name: { type: DataTypes.STRING },
20+
}, {});
21+
const Post = sequelize.define('Post', {
22+
body: { type: DataTypes.STRING },
23+
}, {});
24+
const UserLikesPost = sequelize.define('UserLikesPost', {
25+
UserId: {
26+
type: DataTypes.INTEGER,
27+
references: {
28+
model: User,
29+
key: 'id'
30+
}
31+
},
32+
PostId: {
33+
type: DataTypes.INTEGER,
34+
references: {
35+
model: Post,
36+
key: 'id'
37+
}
38+
},
39+
score: {
40+
type: DataTypes.INTEGER,
41+
},
42+
});
43+
// UserLikesPost is the name of the relation table.
44+
// Sequelize creates it automatically for us.
45+
// On SQLite that table looks like this:
46+
// CREATE TABLE `UserLikesPost` (
47+
// `createdAt` DATETIME NOT NULL,
48+
// `updatedAt` DATETIME NOT NULL,
49+
// `UserId` INTEGER NOT NULL REFERENCES `Users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
50+
// `PostId` INTEGER NOT NULL REFERENCES `Posts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
51+
// `Score` INTEGER,
52+
// PRIMARY KEY (`UserId`, `PostId`)
53+
// );
54+
User.belongsToMany(Post, {through: UserLikesPost});
55+
Post.belongsToMany(User, {through: UserLikesPost});
56+
await sequelize.sync({force: true});
57+
58+
// Create some users and likes.
59+
60+
const user0 = await User.create({name: 'user0'})
61+
const user1 = await User.create({name: 'user1'})
62+
const user2 = await User.create({name: 'user2'})
63+
64+
const post0 = await Post.create({body: 'post0'});
65+
const post1 = await Post.create({body: 'post1'});
66+
const post2 = await Post.create({body: 'post2'});
67+
68+
// Autogenerated add* methods
69+
70+
// Make user0 like post0
71+
await user0.addPost(post0, {through: { score: 1 }})
72+
73+
const user0Likes = await user0.getPosts({order: [['body', 'ASC']]})
74+
assert(user0Likes[0].body === 'post0');
75+
assert(user0Likes[0].UserLikesPost.score === 1);
76+
assert(user0Likes.length === 1);
77+
78+
// TODO: this doesn't work. Possible at all in a single addUsers call?
79+
// Make user0 and user2 like post1
80+
// This method automatically generated.
81+
//await post1.addUsers(
82+
// [user0, user2],
83+
// {through: [
84+
// {score: 2},
85+
// {score: 3},
86+
// ]}
87+
//)
88+
89+
await sequelize.close();
90+
})();
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env node
2+
3+
// https://stackoverflow.com/questions/22958683/how-to-implement-many-to-many-association-in-sequelize/67973948#67973948
4+
5+
const assert = require('assert');
6+
const path = require('path');
7+
8+
const { Sequelize, DataTypes } = require('sequelize');
9+
10+
const sequelize = new Sequelize({
11+
dialect: 'sqlite',
12+
storage: 'tmp.' + path.basename(__filename) + '.sqlite',
13+
});
14+
15+
(async () => {
16+
17+
// Create the tables.
18+
const User = sequelize.define('User', {
19+
name: { type: DataTypes.STRING },
20+
}, {});
21+
User.belongsToMany(User, {through: 'UserFollowsUser', as: 'Follow'});
22+
await sequelize.sync({force: true});
23+
24+
// Create some users.
25+
26+
const user0 = await User.create({name: 'user0'})
27+
const user1 = await User.create({name: 'user1'})
28+
const user2 = await User.create({name: 'user2'})
29+
const user3 = await User.create({name: 'user3'})
30+
31+
// Make user0 follow user1 and user2
32+
await user0.addFollow(user1)
33+
await user0.addFollow(user2)
34+
const user0Follows = await user0.getFollow({order: [['name', 'ASC']]})
35+
assert(user0Follows[0].name === 'user1');
36+
assert(user0Follows[1].name === 'user2');
37+
assert(user0Follows.length === 2);
38+
39+
await sequelize.close();
40+
})();

0 commit comments

Comments
 (0)