@nina-protocol/nina-db 0.0.110 → 0.0.112
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/models/Account.js +3 -4
- package/dist/models/Hub.js +3 -4
- package/dist/redis/subscriptions.js +39 -0
- package/package.json +1 -1
- package/src/models/Account.js +4 -4
- package/src/models/Hub.js +3 -4
- package/src/redis/subscriptions.js +38 -1
package/dist/models/Account.js
CHANGED
|
@@ -4,7 +4,7 @@ import Hub from './Hub.js';
|
|
|
4
4
|
import Post from './Post.js';
|
|
5
5
|
import Release from './Release.js';
|
|
6
6
|
import Verification from './Verification.js';
|
|
7
|
-
import
|
|
7
|
+
import SubscriptionsWithCache from '../redis/subscriptions.js';
|
|
8
8
|
export default class Account extends Model {
|
|
9
9
|
static tableName = 'accounts';
|
|
10
10
|
static idColumn = 'id';
|
|
@@ -37,9 +37,8 @@ export default class Account extends Model {
|
|
|
37
37
|
this.verifications = verifications;
|
|
38
38
|
}
|
|
39
39
|
delete this.id;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
this.followers = parseInt(followersCount.count);
|
|
40
|
+
const followersCount = await SubscriptionsWithCache.getFollowCountForAccountWithCache(this.publicKey, false);
|
|
41
|
+
this.followers = followersCount;
|
|
43
42
|
};
|
|
44
43
|
static relationMappings = () => ({
|
|
45
44
|
published: {
|
package/dist/models/Hub.js
CHANGED
|
@@ -3,7 +3,7 @@ import { stripHtmlIfNeeded } from '../utils/index.js';
|
|
|
3
3
|
import Account from './Account.js';
|
|
4
4
|
import Release from './Release.js';
|
|
5
5
|
import Post from './Post.js';
|
|
6
|
-
import
|
|
6
|
+
import SubscriptionsWithCache from '../redis/subscriptions.js';
|
|
7
7
|
export default class Hub extends Model {
|
|
8
8
|
static get tableName() {
|
|
9
9
|
return 'hubs';
|
|
@@ -31,9 +31,8 @@ export default class Hub extends Model {
|
|
|
31
31
|
delete this.authorityId;
|
|
32
32
|
delete this.id;
|
|
33
33
|
stripHtmlIfNeeded(this.data, 'description');
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
this.followers = parseInt(followersCount.count);
|
|
34
|
+
const followersCount = await SubscriptionsWithCache.getFollowCountForAccountWithCache(this.publicKey, true);
|
|
35
|
+
this.followers = followersCount;
|
|
37
36
|
}
|
|
38
37
|
static relationMappings = () => ({
|
|
39
38
|
authority: {
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import redis from './index.js';
|
|
2
2
|
import Subscription from '../models/Subscription.js';
|
|
3
3
|
import Account from '../models/Account.js';
|
|
4
|
+
import Hub from '../models/Hub.js';
|
|
4
5
|
import { formatColumnForJsonFields, BIG_LIMIT } from '../utils/index.js';
|
|
5
6
|
const SUBSCRIPTION_TO = 'subscription:to';
|
|
6
7
|
const getFollowersForAccountWithCache = async (publicKeyOrHandle, query, override = false) => {
|
|
@@ -98,13 +99,51 @@ const deleteCacheAfterAccountFollow = async (toPublicKey, toHandle, fromPublicKe
|
|
|
98
99
|
await redis.deleteCacheMatchingPattern(`following:${fromPublicKey}:${toHandle}`);
|
|
99
100
|
await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toPublicKey}`);
|
|
100
101
|
await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toHandle}`);
|
|
102
|
+
await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toPublicKey}`);
|
|
103
|
+
await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toHandle}`);
|
|
101
104
|
}
|
|
102
105
|
catch (error) {
|
|
103
106
|
console.log('deleteCacheAfterAccountFollow error: ', error);
|
|
104
107
|
}
|
|
105
108
|
};
|
|
109
|
+
const getFollowCountForAccountWithCache = async (publicKeyOrHandle, isHub = false, override = false) => {
|
|
110
|
+
try {
|
|
111
|
+
let dbRecord = null;
|
|
112
|
+
return redis.withCache(`${SUBSCRIPTION_TO}:count:${publicKeyOrHandle}`, async () => {
|
|
113
|
+
if (isHub) {
|
|
114
|
+
dbRecord = await Hub.query().findOne({ publicKey: publicKeyOrHandle });
|
|
115
|
+
if (!dbRecord) {
|
|
116
|
+
dbRecord = await Hub.query().findOne({ handle: publicKeyOrHandle });
|
|
117
|
+
if (!dbRecord) {
|
|
118
|
+
throw new Error(`Hub not found for publicKeyOrHandle ${publicKeyOrHandle}`);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
dbRecord = await Account.query().findOne({ publicKey: publicKeyOrHandle });
|
|
124
|
+
if (!dbRecord) {
|
|
125
|
+
dbRecord = await Account.query().findOne({ handle: publicKeyOrHandle });
|
|
126
|
+
if (!dbRecord) {
|
|
127
|
+
throw new Error(`Account not found for publicKeyOrHandle ${publicKeyOrHandle}`);
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (dbRecord) {
|
|
132
|
+
const count = await Subscription.query().where('to', dbRecord.publicKey).count('* as count').first();
|
|
133
|
+
return parseInt(count.count);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
return 0;
|
|
137
|
+
}
|
|
138
|
+
}, undefined, override);
|
|
139
|
+
}
|
|
140
|
+
catch (error) {
|
|
141
|
+
console.log('getFollowCountForAccountWithCache error: ', error);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
106
144
|
export default {
|
|
107
145
|
getFollowersForAccountWithCache,
|
|
108
146
|
getUserFollowingAccountWithCache,
|
|
109
147
|
deleteCacheAfterAccountFollow,
|
|
148
|
+
getFollowCountForAccountWithCache,
|
|
110
149
|
};
|
package/package.json
CHANGED
package/src/models/Account.js
CHANGED
|
@@ -4,7 +4,7 @@ import Hub from './Hub.js';
|
|
|
4
4
|
import Post from './Post.js';
|
|
5
5
|
import Release from './Release.js';
|
|
6
6
|
import Verification from './Verification.js';
|
|
7
|
-
import
|
|
7
|
+
import SubscriptionsWithCache from '../redis/subscriptions.js';
|
|
8
8
|
|
|
9
9
|
export default class Account extends Model {
|
|
10
10
|
static tableName= 'accounts';
|
|
@@ -42,9 +42,9 @@ export default class Account extends Model {
|
|
|
42
42
|
this.verifications = verifications;
|
|
43
43
|
}
|
|
44
44
|
delete this.id
|
|
45
|
-
|
|
46
|
-
const followersCount = await
|
|
47
|
-
this.followers =
|
|
45
|
+
|
|
46
|
+
const followersCount = await SubscriptionsWithCache.getFollowCountForAccountWithCache(this.publicKey, false);
|
|
47
|
+
this.followers = followersCount;
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
static relationMappings = () => ({
|
package/src/models/Hub.js
CHANGED
|
@@ -3,7 +3,7 @@ import { stripHtmlIfNeeded } from '../utils/index.js';
|
|
|
3
3
|
import Account from './Account.js';
|
|
4
4
|
import Release from './Release.js';
|
|
5
5
|
import Post from './Post.js';
|
|
6
|
-
import
|
|
6
|
+
import SubscriptionsWithCache from '../redis/subscriptions.js';
|
|
7
7
|
|
|
8
8
|
export default class Hub extends Model {
|
|
9
9
|
static get tableName() {
|
|
@@ -35,9 +35,8 @@ export default class Hub extends Model {
|
|
|
35
35
|
|
|
36
36
|
stripHtmlIfNeeded(this.data, 'description');
|
|
37
37
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
this.followers = parseInt(followersCount.count);
|
|
38
|
+
const followersCount = await SubscriptionsWithCache.getFollowCountForAccountWithCache(this.publicKey, true);
|
|
39
|
+
this.followers = followersCount;
|
|
41
40
|
}
|
|
42
41
|
|
|
43
42
|
static relationMappings = () => ({
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import redis from './index.js';
|
|
2
2
|
import Subscription from '../models/Subscription.js';
|
|
3
3
|
import Account from '../models/Account.js';
|
|
4
|
+
import Hub from '../models/Hub.js';
|
|
4
5
|
import { formatColumnForJsonFields, BIG_LIMIT } from '../utils/index.js';
|
|
5
6
|
|
|
6
7
|
const SUBSCRIPTION_TO = 'subscription:to'
|
|
@@ -119,14 +120,50 @@ const deleteCacheAfterAccountFollow = async(
|
|
|
119
120
|
await redis.deleteCacheMatchingPattern(`following:${fromPublicKey}:${toHandle}`)
|
|
120
121
|
await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toPublicKey}`)
|
|
121
122
|
await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toHandle}`)
|
|
122
|
-
|
|
123
|
+
await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toPublicKey}`)
|
|
124
|
+
await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toHandle}`)
|
|
123
125
|
} catch (error) {
|
|
124
126
|
console.log('deleteCacheAfterAccountFollow error: ', error)
|
|
125
127
|
}
|
|
126
128
|
}
|
|
127
129
|
|
|
130
|
+
const getFollowCountForAccountWithCache = async (publicKeyOrHandle, isHub=false, override=false) => {
|
|
131
|
+
try {
|
|
132
|
+
let dbRecord = null;
|
|
133
|
+
return redis.withCache(`${SUBSCRIPTION_TO}:count:${publicKeyOrHandle}`, async () => {
|
|
134
|
+
if (isHub) {
|
|
135
|
+
dbRecord = await Hub.query().findOne({publicKey: publicKeyOrHandle});
|
|
136
|
+
if (!dbRecord) {
|
|
137
|
+
dbRecord = await Hub.query().findOne({handle: publicKeyOrHandle});
|
|
138
|
+
if (!dbRecord) {
|
|
139
|
+
throw new Error(`Hub not found for publicKeyOrHandle ${publicKeyOrHandle}`)
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
} else {
|
|
143
|
+
dbRecord = await Account.query().findOne({publicKey: publicKeyOrHandle});
|
|
144
|
+
if (!dbRecord) {
|
|
145
|
+
dbRecord = await Account.query().findOne({handle: publicKeyOrHandle});
|
|
146
|
+
if (!dbRecord) {
|
|
147
|
+
throw new Error(`Account not found for publicKeyOrHandle ${publicKeyOrHandle}`)
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
if (dbRecord) {
|
|
153
|
+
const count = await Subscription.query().where('to', dbRecord.publicKey).count('* as count').first();
|
|
154
|
+
return parseInt(count.count);
|
|
155
|
+
} else {
|
|
156
|
+
return 0;
|
|
157
|
+
}
|
|
158
|
+
}, undefined, override)
|
|
159
|
+
} catch (error) {
|
|
160
|
+
console.log('getFollowCountForAccountWithCache error: ', error)
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
128
164
|
export default {
|
|
129
165
|
getFollowersForAccountWithCache,
|
|
130
166
|
getUserFollowingAccountWithCache,
|
|
131
167
|
deleteCacheAfterAccountFollow,
|
|
168
|
+
getFollowCountForAccountWithCache,
|
|
132
169
|
}
|