Initial commit

This commit is contained in:
2025-12-07 14:32:46 +00:00
commit 0a0969b8af
4726 changed files with 536089 additions and 0 deletions

View File

@@ -0,0 +1,74 @@
// migrations/20250619_create_tables.js
exports.up = async function(knex) {
// Create 'users' table
await knex.schema.createTable('users', (table) => {
table.increments('id').primary();
table.string('username', 255).notNullable().unique();
table.string('password', 255).notNullable();
table.timestamp('created_at').defaultTo(knex.fn.now());
});
// Create 'chargers' table with new fields
await knex.schema.createTable('chargers', (table) => {
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'));
table.integer('user_id').unsigned().notNullable()
.references('id').inTable('users').onDelete('CASCADE');
table.string('location', 255).notNullable();
table.string('status', 50).notNullable().defaultTo('offline');
table.integer('charging_current').notNullable().defaultTo(32);
table.timestamp('updated_at').notNullable().defaultTo(knex.fn.now());
table.string('mqtt_user', 255).notNullable();
table.string('mqtt_pass', 255).notNullable();
table.string('mqtt_topic', 255).notNullable().unique();
table.timestamp('created_at').defaultTo(knex.fn.now());
// Add power and current for 3 phases, voltage and other new fields
table.integer('charging_time').notNullable().defaultTo(0); // Total charging time
table.decimal('consumption', 8, 2).notNullable().defaultTo(0); // Consumption (kWh)
// Power for 3 phases (L1, L2, L3)
table.decimal('power_l1', 8, 2).notNullable().defaultTo(0);
table.decimal('power_l2', 8, 2).notNullable().defaultTo(0);
table.decimal('power_l3', 8, 2).notNullable().defaultTo(0);
// Voltage for 3 phases (L1, L2, L3)
table.decimal('voltage_l1', 8, 2).notNullable().defaultTo(0);
table.decimal('voltage_l2', 8, 2).notNullable().defaultTo(0);
table.decimal('voltage_l3', 8, 2).notNullable().defaultTo(0);
// Current for 3 phases (L1, L2, L3)
table.decimal('current_l1', 8, 2).notNullable().defaultTo(0);
table.decimal('current_l2', 8, 2).notNullable().defaultTo(0);
table.decimal('current_l3', 8, 2).notNullable().defaultTo(0);
});
// Create 'charger_configs' table
await knex.schema.createTable('charger_configs', (table) => {
table.uuid('charger_id').primary()
.references('id').inTable('chargers').onDelete('CASCADE');
table.integer('max_charging_current').notNullable().defaultTo(32);
table.boolean('require_auth').notNullable().defaultTo(false);
table.boolean('rcm_enabled').notNullable().defaultTo(false);
table.integer('temperature_limit').notNullable().defaultTo(60);
table.timestamp('config_received_at').notNullable().defaultTo(knex.fn.now());
});
// Create 'charger_sessions' table
await knex.schema.createTable('charger_sessions', (table) => {
table.increments('id').primary();
table.uuid('charger_id').notNullable()
.references('id').inTable('chargers').onDelete('CASCADE');
table.timestamp('started_at').notNullable();
table.timestamp('ended_at');
table.decimal('kwh', 8, 2).notNullable().defaultTo(0);
table.decimal('cost', 10, 2);
table.timestamp('created_at').defaultTo(knex.fn.now());
});
};
exports.down = async function(knex) {
await knex.schema.dropTableIfExists('charger_sessions');
await knex.schema.dropTableIfExists('charger_configs');
await knex.schema.dropTableIfExists('chargers');
await knex.schema.dropTableIfExists('users');
};

View File

@@ -0,0 +1,2 @@
// shim para compatibilidade com o nome antigo registado no knex_migrations
module.exports = require('./20251123_create_charger_schedules');

View File

@@ -0,0 +1,29 @@
exports.up = async function (knex) {
const exists = await knex.schema.hasTable('charger_schedules');
if (exists) return;
return knex.schema.createTable('charger_schedules', (t) => {
t.uuid('id')
.primary()
.defaultTo(knex.raw('gen_random_uuid()'));
t.uuid('charger_id')
.notNullable()
.references('id')
.inTable('chargers')
.onDelete('CASCADE');
t.string('start', 5).notNullable();
t.string('end', 5).notNullable();
t.enu('repeat', ['everyday', 'weekdays', 'weekends'])
.notNullable()
.defaultTo('everyday');
t.timestamp('created_at').defaultTo(knex.fn.now());
});
};
exports.down = function (knex) {
return knex.schema.dropTableIfExists('charger_schedules');
};

View File

@@ -0,0 +1,25 @@
exports.up = function (knex) {
return knex.schema.createTable('push_subscriptions', (t) => {
t.uuid('id')
.primary()
.defaultTo(knex.raw('gen_random_uuid()'));
// ✅ users.id é integer no teu caso
t.integer('user_id')
.notNullable()
.references('id')
.inTable('users')
.onDelete('CASCADE');
t.text('endpoint').notNullable().unique();
t.text('p256dh').notNullable();
t.text('auth').notNullable();
t.text('user_agent');
t.timestamp('created_at').defaultTo(knex.fn.now());
});
};
exports.down = function (knex) {
return knex.schema.dropTableIfExists('push_subscriptions');
};