@nina-protocol/nina-db 0.0.67 → 0.0.68

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.
@@ -37,5 +37,4 @@ export const up = function (knex) {
37
37
  */
38
38
  export const down = function (knex) {
39
39
  return knex.schema.dropTableIfExists('tags');
40
- return knex.schema.dropTableIfExists('tags_content');
41
40
  };
@@ -0,0 +1,32 @@
1
+ /**
2
+ * @param { import("knex").Knex } knex
3
+ * @returns { Promise<void> }
4
+ */
5
+ export const up = function (knex) {
6
+ return Promise.all([
7
+ knex.schema.createTable('tags_releases', table => {
8
+ table.primary(['tagId', 'releaseId']);
9
+ table.integer('releaseId')
10
+ .unsigned()
11
+ .references('id')
12
+ .inTable('releases')
13
+ .onDelete('CASCADE')
14
+ .index();
15
+ table.integer('tagId')
16
+ .unsigned()
17
+ .references('id')
18
+ .inTable('tags')
19
+ .onDelete('CASCADE')
20
+ .index();
21
+ table.timestamp('created_at').defaultTo(knex.fn.now());
22
+ table.timestamp('updated_at').defaultTo(knex.fn.now());
23
+ }),
24
+ ]);
25
+ };
26
+ /**
27
+ * @param { import("knex").Knex } knex
28
+ * @returns { Promise<void> }
29
+ */
30
+ export const down = function (knex) {
31
+ return knex.schema.dropTableIfExists('tags_releases');
32
+ };
@@ -10,10 +10,12 @@ export default class Tag extends Model {
10
10
  value: { type: 'string' },
11
11
  },
12
12
  };
13
+ static sanitizeValue = (value) => value.toLowerCase().replace('#', '').replace(',', '');
13
14
  static findOrCreate = async (value) => {
14
- let tag = await Tag.query().where('value', value).first();
15
+ const sanitizedValue = this.sanitizeValue(value);
16
+ let tag = await Tag.query().where('value', sanitizedValue).first();
15
17
  if (!tag) {
16
- tag = await Tag.query().insert({ value });
18
+ tag = await Tag.query().insert({ value: sanitizedValue });
17
19
  }
18
20
  return tag;
19
21
  };
@@ -0,0 +1,17 @@
1
+ import { Model } from 'objection';
2
+ export default class TagsRelease extends Model {
3
+ static tableName = 'tags_releases';
4
+ $beforeUpdate() {
5
+ this.updatedAt = new Date().toISOString();
6
+ }
7
+ static jsonSchema = {
8
+ type: 'object',
9
+ required: ['tagId', 'releaseId', 'createdAt', 'updatedAt'],
10
+ properties: {
11
+ tagId: { type: 'integer' },
12
+ releaseId: { type: 'integer' },
13
+ createdAt: { type: 'string' },
14
+ updatedAt: { type: 'string' },
15
+ },
16
+ };
17
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nina-protocol/nina-db",
3
- "version": "0.0.67",
3
+ "version": "0.0.68",
4
4
  "description": "",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -37,5 +37,4 @@ export const up = function (knex) {
37
37
  */
38
38
  export const down = function (knex) {
39
39
  return knex.schema.dropTableIfExists('tags');
40
- return knex.schema.dropTableIfExists('tags_content');
41
40
  };
@@ -18,6 +18,8 @@ export const up = function (knex) {
18
18
  .inTable('tags')
19
19
  .onDelete('CASCADE')
20
20
  .index();
21
+ table.timestamp('created_at').defaultTo(knex.fn.now());
22
+ table.timestamp('updated_at').defaultTo(knex.fn.now());
21
23
  }),
22
24
  ]);
23
25
  };
package/src/models/Tag.js CHANGED
@@ -12,10 +12,13 @@ export default class Tag extends Model {
12
12
  },
13
13
  }
14
14
 
15
+ static sanitizeValue = (value) => value.toLowerCase().replace('#', '').replace(',', '');
16
+
15
17
  static findOrCreate = async (value) => {
16
- let tag = await Tag.query().where('value', value).first();
18
+ const sanitizedValue = this.sanitizeValue(value);
19
+ let tag = await Tag.query().where('value', sanitizedValue).first();
17
20
  if (!tag) {
18
- tag = await Tag.query().insert({ value });
21
+ tag = await Tag.query().insert({ value: sanitizedValue });
19
22
  }
20
23
  return tag;
21
24
  }
@@ -0,0 +1,20 @@
1
+ import { Model } from 'objection';
2
+
3
+ export default class TagsRelease extends Model {
4
+ static tableName = 'tags_releases';
5
+
6
+ $beforeUpdate() {
7
+ this.updatedAt = new Date().toISOString();
8
+ }
9
+
10
+ static jsonSchema = {
11
+ type: 'object',
12
+ required: ['tagId', 'releaseId', 'createdAt', 'updatedAt'],
13
+ properties: {
14
+ tagId: { type: 'integer' },
15
+ releaseId: { type: 'integer' },
16
+ createdAt: { type: 'string' },
17
+ updatedAt: { type: 'string' },
18
+ },
19
+ }
20
+ }