diff --git a/src/database/config.js b/src/database/config.js index a13f133..2800b22 100644 --- a/src/database/config.js +++ b/src/database/config.js @@ -10,6 +10,7 @@ module.exports = { database: process.env.POSTGRES_DB, username: process.env.POSTGRES_USER, password: process.env.POSTGRES_PASSWORD, + logging: console.log, define: { timestamps: false, freezeTableName: true, diff --git a/src/database/migrations/20250308000000-first-schema.js b/src/database/migrations/20250308000000-first-schema-tables.js similarity index 95% rename from src/database/migrations/20250308000000-first-schema.js rename to src/database/migrations/20250308000000-first-schema-tables.js index 8e7bfe9..3407606 100644 --- a/src/database/migrations/20250308000000-first-schema.js +++ b/src/database/migrations/20250308000000-first-schema-tables.js @@ -48,7 +48,7 @@ module.exports = { { transaction: t } ), queryInterface.createTable( - 'login_challenge_completed', + 'nostr_challenge_created', { uuid: { type: Sequelize.UUID, @@ -56,13 +56,14 @@ module.exports = { unique: true, primaryKey: true, }, - nostr_challenge_completed_uuid: { - type: Sequelize.UUID, - allowNull: false, - }, - public_key: { + challenge: { type: Sequelize.STRING, allowNull: false, + unique: true, + }, + expires_at: { + type: Sequelize.DATE, + allowNull: false, }, created_at: { type: Sequelize.DATE, @@ -91,32 +92,6 @@ module.exports = { }, { transaction: t } ), - - queryInterface.createTable( - 'nostr_challenge_created', - { - uuid: { - type: Sequelize.UUID, - allowNull: false, - unique: true, - primaryKey: true, - }, - challenge: { - type: Sequelize.STRING, - allowNull: false, - unique: true, - }, - expires_at: { - type: Sequelize.DATE, - allowNull: false, - }, - created_at: { - type: Sequelize.DATE, - allowNull: false, - }, - }, - { transaction: t } - ), queryInterface.createTable( 'nostr_challenge_completed', { @@ -130,12 +105,6 @@ module.exports = { type: Sequelize.STRING, allowNull: false, unique: true, - references: { - model: { - tableName: 'nostr_challenge_created', - }, - key: 'challenge', - }, }, signed_event: { type: Sequelize.JSONB, @@ -152,6 +121,30 @@ module.exports = { }, { transaction: t } ), + queryInterface.createTable( + 'login_challenge_completed', + { + uuid: { + type: Sequelize.UUID, + allowNull: false, + unique: true, + primaryKey: true, + }, + nostr_challenge_completed_uuid: { + type: Sequelize.UUID, + allowNull: false, + }, + public_key: { + type: Sequelize.STRING, + allowNull: false, + }, + created_at: { + type: Sequelize.DATE, + allowNull: false, + }, + }, + { transaction: t } + ), ]); }); }, diff --git a/src/database/migrations/20250308000001-first-schema-fks.js b/src/database/migrations/20250308000001-first-schema-fks.js new file mode 100644 index 0000000..7ede52a --- /dev/null +++ b/src/database/migrations/20250308000001-first-schema-fks.js @@ -0,0 +1,42 @@ +'use strict'; +module.exports = { + up: (queryInterface, Sequelize) => { + return queryInterface.sequelize.transaction((t) => { + return Promise.all([ + queryInterface.addConstraint('login_challenge_created', { + fields: ['nostr_challenge_uuid'], + type: 'foreign key', + references: { + table: 'nostr_challenge_created', + field: 'uuid', + }, + onDelete: 'cascade', + onUpdate: 'cascade', + }), + queryInterface.addConstraint('nostr_challenge_completed', { + fields: ['challenge'], + type: 'foreign key', + references: { + table: 'nostr_challenge_created', + field: 'challenge', + }, + onDelete: 'cascade', + onUpdate: 'cascade', + }), + queryInterface.addConstraint('login_challenge_completed', { + fields: ['nostr_challenge_completed_uuid'], + type: 'foreign key', + references: { + table: 'nostr_challenge_completed', + field: 'uuid', + }, + onDelete: 'cascade', + onUpdate: 'cascade', + }), + ]); + }); + }, + down: (queryInterface, Sequelize) => { + return queryInterface.dropTable('Users'); + }, +};