@nina-protocol/nina-db 0.0.31 → 0.0.33

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.
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @param { import("knex").Knex } knex
3
+ * @returns { Promise<void> }
4
+ */
5
+ export const up = function (knex) {
6
+ return knex.schema.table('releases', table => {
7
+ table.string('slug');
8
+ });
9
+ };
10
+ /**
11
+ * @param { import("knex").Knex } knex
12
+ * @returns { Promise<void> }
13
+ */
14
+ export const down = function (knex) {
15
+ };
@@ -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
- class Account extends Model {
7
+ export default class Account extends Model {
8
8
  static tableName = 'accounts';
9
9
  static idColumn = 'id';
10
10
  static jsonSchema = {
@@ -113,4 +113,3 @@ class Account extends Model {
113
113
  },
114
114
  });
115
115
  }
116
- export default Account;
@@ -1,7 +1,7 @@
1
1
  import { Model } from 'objection';
2
2
  import Account from './Account.js';
3
3
  import Release from './Release.js';
4
- class Exchange extends Model {
4
+ export default class Exchange extends Model {
5
5
  static tableName = 'exchanges';
6
6
  static idColum = 'id';
7
7
  static jsonSchema = {
@@ -60,4 +60,3 @@ class Exchange extends Model {
60
60
  },
61
61
  });
62
62
  }
63
- export default Exchange;
@@ -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
- class Hub extends Model {
6
+ export default class Hub extends Model {
7
7
  static get tableName() {
8
8
  return 'hubs';
9
9
  }
@@ -81,4 +81,3 @@ class Hub extends Model {
81
81
  },
82
82
  });
83
83
  }
84
- export default Hub;
@@ -7,18 +7,22 @@ import Exchange from './Exchange.js';
7
7
  import Hub from './Hub.js';
8
8
  import Post from './Post.js';
9
9
  import axios from 'axios';
10
- class Release extends Model {
10
+ import { customAlphabet } from 'nanoid';
11
+ const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
12
+ const randomStringGenerator = customAlphabet(alphabet, 12);
13
+ export default class Release extends Model {
11
14
  static tableName = 'releases';
12
15
  static idColumn = 'id';
13
16
  static jsonSchema = {
14
17
  type: 'object',
15
- required: ['publicKey', 'mint', 'metadata', 'datetime'],
18
+ required: ['publicKey', 'mint', 'metadata', 'datetime', 'slug'],
16
19
  properties: {
17
20
  publicKey: { type: 'string' },
18
21
  mint: { type: 'string' },
22
+ slug: { type: 'string' },
19
23
  metadata: {
20
24
  type: 'object',
21
- required: ['name', 'symbol', 'description', 'image', 'properties'],
25
+ required: ['name', 'symbol', 'description', 'image', 'properties',],
22
26
  properties: {
23
27
  name: { type: 'string' },
24
28
  symbol: { type: 'string' },
@@ -56,22 +60,26 @@ class Release extends Model {
56
60
  catch (error) {
57
61
  json = (await axios.get(metadataAccount.uri.replace('arweave.net', 'ar-io.net'))).data;
58
62
  }
63
+ const slug = await this.generateSlug(json);
59
64
  let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
60
65
  release = await this.createRelease({
61
66
  publicKey,
62
67
  mint: releaseAccount.releaseMint.toBase58(),
63
68
  metadata: json,
64
69
  datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
70
+ slug,
65
71
  publisherId: publisher.id,
66
72
  releaseAccount
67
73
  });
68
74
  return release;
69
75
  };
70
76
  static createRelease = async ({ publicKey, mint, metadata, datetime, publisherId, releaseAccount }) => {
77
+ const slug = await this.generateSlug(metadata);
71
78
  const release = await Release.query().insertGraph({
72
79
  publicKey,
73
80
  mint,
74
81
  metadata,
82
+ slug,
75
83
  datetime,
76
84
  publisherId,
77
85
  });
@@ -96,6 +104,14 @@ class Release extends Model {
96
104
  }
97
105
  }
98
106
  };
107
+ static generateSlug = async (metadata) => {
108
+ const slug = metadata.name.toLowerCase().replace(/ /g, '-').replace(/[^a-z0-9-]/g, '');
109
+ const existingRelease = await Release.query().findOne({ slug });
110
+ if (existingRelease) {
111
+ return `${slug}-${randomStringGenerator()}`;
112
+ }
113
+ return slug;
114
+ };
99
115
  format = async () => {
100
116
  const publisher = await this.$relatedQuery('publisher').select('publicKey');
101
117
  const publishedThroughHub = await this.$relatedQuery('publishedThroughHub');
@@ -189,4 +205,3 @@ class Release extends Model {
189
205
  }
190
206
  });
191
207
  }
192
- export default Release;
@@ -1,6 +1,6 @@
1
1
  import { Model } from 'objection';
2
2
  import Account from './Account.js';
3
- class Verification extends Model {
3
+ export default class Verification extends Model {
4
4
  static get tableName() {
5
5
  return 'verifications';
6
6
  }
@@ -48,4 +48,3 @@ class Verification extends Model {
48
48
  },
49
49
  });
50
50
  }
51
- export default Verification;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nina-protocol/nina-db",
3
- "version": "0.0.31",
3
+ "version": "0.0.33",
4
4
  "description": "",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @param { import("knex").Knex } knex
3
+ * @returns { Promise<void> }
4
+ */
5
+ export const up = function(knex) {
6
+ return knex.schema.table('releases' , table => {
7
+ table.string('slug');
8
+ });
9
+ };
10
+
11
+ /**
12
+ * @param { import("knex").Knex } knex
13
+ * @returns { Promise<void> }
14
+ */
15
+ export const down = function(knex) {
16
+
17
+ };
@@ -7,6 +7,10 @@ import Exchange from './Exchange.js';
7
7
  import Hub from './Hub.js';
8
8
  import Post from './Post.js';
9
9
  import axios from 'axios';
10
+ import { customAlphabet } from 'nanoid';
11
+
12
+ const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
13
+ const randomStringGenerator = customAlphabet(alphabet, 12);
10
14
 
11
15
  export default class Release extends Model {
12
16
  static tableName = 'releases';
@@ -14,13 +18,14 @@ export default class Release extends Model {
14
18
  static idColumn = 'id';
15
19
  static jsonSchema = {
16
20
  type: 'object',
17
- required: ['publicKey', 'mint', 'metadata', 'datetime'],
21
+ required: ['publicKey', 'mint', 'metadata', 'datetime', 'slug'],
18
22
  properties: {
19
23
  publicKey: { type: 'string' },
20
24
  mint: { type: 'string' },
25
+ slug: { type: 'string' },
21
26
  metadata: {
22
27
  type: 'object',
23
- required: ['name', 'symbol', 'description', 'image', 'properties'],
28
+ required: ['name', 'symbol', 'description', 'image', 'properties',],
24
29
  properties: {
25
30
  name: { type: 'string' },
26
31
  symbol: { type: 'string' },
@@ -63,12 +68,14 @@ export default class Release extends Model {
63
68
  json = (await axios.get(metadataAccount.uri.replace('arweave.net', 'ar-io.net'))).data
64
69
  }
65
70
 
71
+ const slug = await this.generateSlug(json);
66
72
  let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
67
73
  release = await this.createRelease({
68
74
  publicKey,
69
75
  mint: releaseAccount.releaseMint.toBase58(),
70
76
  metadata: json,
71
77
  datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
78
+ slug,
72
79
  publisherId: publisher.id,
73
80
  releaseAccount
74
81
  });
@@ -76,10 +83,13 @@ export default class Release extends Model {
76
83
  }
77
84
 
78
85
  static createRelease = async ({publicKey, mint, metadata, datetime, publisherId, releaseAccount}) => {
86
+ const slug = await this.generateSlug(metadata);
87
+
79
88
  const release = await Release.query().insertGraph({
80
89
  publicKey,
81
90
  mint,
82
91
  metadata,
92
+ slug,
83
93
  datetime,
84
94
  publisherId,
85
95
  })
@@ -105,6 +115,16 @@ export default class Release extends Model {
105
115
  }
106
116
  }
107
117
 
118
+ static generateSlug = async (metadata) => {
119
+ const slug = metadata.name.toLowerCase().replace(/ /g, '-').replace(/[^a-z0-9-]/g, '');
120
+ const existingRelease = await Release.query().findOne({ slug });
121
+ if (existingRelease) {
122
+ return `${slug}-${randomStringGenerator()}`;
123
+ }
124
+ return slug;
125
+
126
+ }
127
+
108
128
  format = async () => {
109
129
  const publisher = await this.$relatedQuery('publisher').select('publicKey');
110
130
  const publishedThroughHub = await this.$relatedQuery('publishedThroughHub');