@nina-protocol/nina-db 0.0.30 → 0.0.32

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
+ };
@@ -29,6 +29,7 @@ class Post extends Model {
29
29
  this.publishedThroughHub = publishedThroughHub.publicKey;
30
30
  this.hub = publishedThroughHub;
31
31
  delete this.hub.id;
32
+ delete this.hub.authorityId;
32
33
  }
33
34
  delete this.publisherId;
34
35
  delete this.id;
@@ -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
+ import { customAlphabet } from 'nanoid';
11
+ const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
12
+ const randomStringGenerator = customAlphabet(alphabet, 12);
10
13
  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,12 +60,14 @@ 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
  });
@@ -96,6 +102,14 @@ class Release extends Model {
96
102
  }
97
103
  }
98
104
  };
105
+ static generateSlug = async (metadata) => {
106
+ const slug = metadata.name.toLowerCase().replace(/ /g, '-').replace(/[^a-z0-9-]/g, '');
107
+ const existingRelease = await Release.query().findOne({ slug });
108
+ if (existingRelease) {
109
+ return `${slug}-${randomStringGenerator()}`;
110
+ }
111
+ return slug;
112
+ };
99
113
  format = async () => {
100
114
  const publisher = await this.$relatedQuery('publisher').select('publicKey');
101
115
  const publishedThroughHub = await this.$relatedQuery('publishedThroughHub');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nina-protocol/nina-db",
3
- "version": "0.0.30",
3
+ "version": "0.0.32",
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
+ };
@@ -32,6 +32,7 @@ class Post extends Model {
32
32
  this.publishedThroughHub = publishedThroughHub.publicKey;
33
33
  this.hub = publishedThroughHub;
34
34
  delete this.hub.id;
35
+ delete this.hub.authorityId;
35
36
  }
36
37
 
37
38
  delete this.publisherId
@@ -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
  });
@@ -105,6 +112,16 @@ export default class Release extends Model {
105
112
  }
106
113
  }
107
114
 
115
+ static generateSlug = async (metadata) => {
116
+ const slug = metadata.name.toLowerCase().replace(/ /g, '-').replace(/[^a-z0-9-]/g, '');
117
+ const existingRelease = await Release.query().findOne({ slug });
118
+ if (existingRelease) {
119
+ return `${slug}-${randomStringGenerator()}`;
120
+ }
121
+ return slug;
122
+
123
+ }
124
+
108
125
  format = async () => {
109
126
  const publisher = await this.$relatedQuery('publisher').select('publicKey');
110
127
  const publishedThroughHub = await this.$relatedQuery('publishedThroughHub');