Skip to content

Commit

Permalink
Add object hash (md5) to schema
Browse files Browse the repository at this point in the history
  • Loading branch information
barbarah committed Sep 21, 2023
1 parent bdb6b18 commit adc0058
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/database/migrations/0001_create_object_item.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CREATE TABLE `object_item` (
`id` serial AUTO_INCREMENT NOT NULL,
`object_hash` varchar(32) NOT NULL,
`object_id` text NOT NULL,
`object_list_id` int,
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` varchar(50) NOT NULL,
CONSTRAINT `object_item_id` PRIMARY KEY(`id`),
CONSTRAINT `object_item_object_id_object_list_id_unique` UNIQUE(`object_id`,`object_list_id`)
CONSTRAINT `object_item_object_hash` PRIMARY KEY(`object_hash`),
CONSTRAINT `object_item_object_hash_object_list_id_unique` UNIQUE(`object_hash`,`object_list_id`)
);
--> statement-breakpoint
CREATE INDEX `object_id` ON `object_item` (`object_id`);--> statement-breakpoint
CREATE INDEX `object_hash` ON `object_item` (`object_hash`);--> statement-breakpoint
CREATE INDEX `object_list_id` ON `object_item` (`object_list_id`);
2 changes: 1 addition & 1 deletion packages/database/migrations/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"version": "5",
"dialect": "mysql",
"id": "4ce42ab4-fdff-449e-9757-1afe2d24c8ff",
"id": "fa9b049d-c092-42f8-a57e-8cae800db6dd",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"object_list": {
Expand Down
30 changes: 15 additions & 15 deletions packages/database/migrations/meta/0001_snapshot.json
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
{
"version": "5",
"dialect": "mysql",
"id": "bb5484fc-0270-4cab-be00-c4879c8e2539",
"prevId": "4ce42ab4-fdff-449e-9757-1afe2d24c8ff",
"id": "cf9627d1-a3e1-4768-8539-90a87f3e0a97",
"prevId": "fa9b049d-c092-42f8-a57e-8cae800db6dd",
"tables": {
"object_item": {
"name": "object_item",
"columns": {
"id": {
"name": "id",
"type": "serial",
"object_hash": {
"name": "object_hash",
"type": "varchar(32)",
"primaryKey": false,
"notNull": true,
"autoincrement": true
"autoincrement": false
},
"object_id": {
"name": "object_id",
Expand Down Expand Up @@ -45,10 +45,10 @@
}
},
"indexes": {
"object_id": {
"name": "object_id",
"object_hash": {
"name": "object_hash",
"columns": [
"object_id"
"object_hash"
],
"isUnique": false
},
Expand All @@ -62,18 +62,18 @@
},
"foreignKeys": {},
"compositePrimaryKeys": {
"object_item_id": {
"name": "object_item_id",
"object_item_object_hash": {
"name": "object_item_object_hash",
"columns": [
"id"
"object_hash"
]
}
},
"uniqueConstraints": {
"object_item_object_id_object_list_id_unique": {
"name": "object_item_object_id_object_list_id_unique",
"object_item_object_hash_object_list_id_unique": {
"name": "object_item_object_hash_object_list_id_unique",
"columns": [
"object_id",
"object_hash",
"object_list_id"
]
}
Expand Down
4 changes: 2 additions & 2 deletions packages/database/migrations/meta/_journal.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
{
"idx": 0,
"version": "5",
"when": 1695113713084,
"when": 1695286867071,
"tag": "0000_create_object_list",
"breakpoints": true
},
{
"idx": 1,
"version": "5",
"when": 1695113728187,
"when": 1695286872562,
"tag": "0001_create_object_item",
"breakpoints": true
}
Expand Down
12 changes: 11 additions & 1 deletion packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
"db:migrate": "dotenv -c -- ts-node ./scripts/migrate.ts",
"db:drop": "dotenv -c -- drizzle-kit drop",
"db:studio": "dotenv -c -- drizzle-kit studio",
"db:create": "bash ./scripts/create-database.sh"
"db:create": "bash ./scripts/create-database.sh",
"test": "jest",
"test:ci": "npm test -- --ci",
"test:watch": "npm test -- --watchAll"
},
"devDependencies": {
"dotenv-cli": "7.3.0",
Expand All @@ -22,5 +25,12 @@
"drizzle-orm": "0.28.5",
"drizzle-zod": "0.5.1",
"mysql2": "3.6.0"
},
"jest": {
"preset": "ts-jest",
"testTimeout": 60000,
"testMatch": ["<rootDir>/src/**/*.test.ts"],
"collectCoverage": true,
"collectCoverageFrom": ["<rootDir>/src/**/*.ts"]
}
}
7 changes: 3 additions & 4 deletions packages/database/src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ export const objectListsRelations = relations(objectLists, ({many}) => ({
export const objectItems = mysqlTable(
'object_item',
{
id: serial('id').primaryKey(),
// The objectId is a uri, so it can be long.
objectHash: varchar('object_hash', {length: 32}).primaryKey(),
objectId: text('object_id').notNull(),
objectListId: int('object_list_id'),
createdAt: timestamp('created_at')
Expand All @@ -52,9 +51,9 @@ export const objectItems = mysqlTable(
createdBy: varchar('created_by', {length: 50}).notNull(),
},
item => ({
objectId: index('object_id').on(item.objectId),
objectHash: index('object_hash').on(item.objectHash),
objectListId: index('object_list_id').on(item.objectListId),
unique: unique().on(item.objectId, item.objectListId),
unique: unique().on(item.objectHash, item.objectListId),
})
);

Expand Down
15 changes: 15 additions & 0 deletions packages/database/src/iriToHash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {iriToHash} from './iriToHash';

describe('iriToHash', () => {
it('returns a hash', () => {
const hash = iriToHash('https://example.com');
expect(hash).toBe('c984d06aafbecf6bc55569f964148ea3');
});

it('returns a hash of the same length', () => {
const hash = iriToHash(
'https://example.com/1234567890123456789012345678901234567890'
);
expect(hash.length).toBe(32);
});
});
7 changes: 7 additions & 0 deletions packages/database/src/iriToHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import crypto from 'crypto';

export function iriToHash(iri: string) {
const hash = crypto.createHash('md5');
hash.update(iri);
return hash.digest('hex');
}
2 changes: 2 additions & 0 deletions packages/database/src/object-list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {objectLists, insertObjectItemSchema, objectItems} from './db/schema';
import db from './db/connection';
import {iriToHash} from './iriToHash';

async function getListsByCommunityId(communityId: string) {
return db.query.objectLists.findMany({
Expand Down Expand Up @@ -54,6 +55,7 @@ async function addObjectToList({
listId,
objectId,
createdBy: userId,
objectHash: iriToHash(objectId),
});

return db.insert(objectItems).values(objectItem);
Expand Down

0 comments on commit adc0058

Please sign in to comment.