@nina-protocol/nina-db 0.0.111 → 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.
@@ -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 Subscription from './Subscription.js';
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
- // Use count() instead of range(0,0) to avoid temporary object creation
41
- const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
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: {
@@ -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 Subscription from './Subscription.js';
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
- // Use count() instead of range(0,0) to avoid temporary object creation
35
- const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
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: {
@@ -99,13 +99,51 @@ const deleteCacheAfterAccountFollow = async (toPublicKey, toHandle, fromPublicKe
99
99
  await redis.deleteCacheMatchingPattern(`following:${fromPublicKey}:${toHandle}`);
100
100
  await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toPublicKey}`);
101
101
  await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toHandle}`);
102
+ await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toPublicKey}`);
103
+ await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toHandle}`);
102
104
  }
103
105
  catch (error) {
104
106
  console.log('deleteCacheAfterAccountFollow error: ', error);
105
107
  }
106
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
+ };
107
144
  export default {
108
145
  getFollowersForAccountWithCache,
109
146
  getUserFollowingAccountWithCache,
110
147
  deleteCacheAfterAccountFollow,
148
+ getFollowCountForAccountWithCache,
111
149
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nina-protocol/nina-db",
3
- "version": "0.0.111",
3
+ "version": "0.0.112",
4
4
  "description": "",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -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 Subscription from './Subscription.js';
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
- // Use count() instead of range(0,0) to avoid temporary object creation
46
- const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
47
- this.followers = parseInt(followersCount.count);
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 Subscription from './Subscription.js';
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
- // Use count() instead of range(0,0) to avoid temporary object creation
39
- const followersCount = await Subscription.query().where('to', this.publicKey).count('* as count').first();
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 = () => ({
@@ -120,14 +120,50 @@ const deleteCacheAfterAccountFollow = async(
120
120
  await redis.deleteCacheMatchingPattern(`following:${fromPublicKey}:${toHandle}`)
121
121
  await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toPublicKey}`)
122
122
  await redis.deleteCacheMatchingPattern(`following:${fromHandle}:${toHandle}`)
123
-
123
+ await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toPublicKey}`)
124
+ await redis.deleteCacheMatchingPattern(`${SUBSCRIPTION_TO}:count:${toHandle}`)
124
125
  } catch (error) {
125
126
  console.log('deleteCacheAfterAccountFollow error: ', error)
126
127
  }
127
128
  }
128
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
+
129
164
  export default {
130
165
  getFollowersForAccountWithCache,
131
166
  getUserFollowingAccountWithCache,
132
167
  deleteCacheAfterAccountFollow,
168
+ getFollowCountForAccountWithCache,
133
169
  }