aboutsummaryrefslogtreecommitdiff
path: root/src/api/database
diff options
context:
space:
mode:
Diffstat (limited to 'src/api/database')
-rw-r--r--src/api/database/migrations/20190221225812_initialMigration.js93
-rw-r--r--src/api/database/migrations/20201227023216_addUniques.js33
-rw-r--r--src/api/database/seeds/initial.js30
3 files changed, 156 insertions, 0 deletions
diff --git a/src/api/database/migrations/20190221225812_initialMigration.js b/src/api/database/migrations/20190221225812_initialMigration.js
new file mode 100644
index 0000000..a27a08a
--- /dev/null
+++ b/src/api/database/migrations/20190221225812_initialMigration.js
@@ -0,0 +1,93 @@
+exports.up = async knex => {
+ await knex.schema.createTable('users', table => {
+ table.increments();
+ table.string('username');
+ table.text('password');
+ table.boolean('enabled');
+ table.boolean('isAdmin');
+ table.string('apiKey');
+ table.timestamp('passwordEditedAt');
+ table.timestamp('apiKeyEditedAt');
+ table.timestamp('createdAt');
+ table.timestamp('editedAt');
+ });
+
+ await knex.schema.createTable('albums', table => {
+ table.increments();
+ table.integer('userId');
+ table.string('name');
+ table.timestamp('zippedAt');
+ table.timestamp('createdAt');
+ table.timestamp('editedAt');
+ });
+
+ await knex.schema.createTable('files', table => {
+ table.increments();
+ table.integer('userId');
+ table.string('name');
+ table.string('original');
+ table.string('type');
+ table.integer('size');
+ table.string('hash');
+ table.string('ip');
+ table.timestamp('createdAt');
+ table.timestamp('editedAt');
+ });
+
+ await knex.schema.createTable('links', table => {
+ table.increments();
+ table.integer('userId');
+ table.integer('albumId');
+ table.string('identifier');
+ table.integer('views');
+ table.boolean('enabled');
+ table.boolean('enableDownload');
+ table.timestamp('expiresAt');
+ table.timestamp('createdAt');
+ table.timestamp('editedAt');
+ });
+
+ await knex.schema.createTable('albumsFiles', table => {
+ table.increments();
+ table.integer('albumId');
+ table.integer('fileId');
+ });
+
+ await knex.schema.createTable('albumsLinks', table => {
+ table.increments();
+ table.integer('albumId');
+ table.integer('linkId');
+ });
+
+ await knex.schema.createTable('tags', table => {
+ table.increments();
+ table.string('uuid');
+ table.integer('userId');
+ table.string('name');
+ table.timestamp('createdAt');
+ table.timestamp('editedAt');
+ });
+
+ await knex.schema.createTable('fileTags', table => {
+ table.increments();
+ table.integer('fileId');
+ table.integer('tagId');
+ });
+
+ await knex.schema.createTable('bans', table => {
+ table.increments();
+ table.string('ip');
+ table.timestamp('createdAt');
+ });
+};
+exports.down = async knex => {
+ await knex.schema.dropTableIfExists('users');
+ await knex.schema.dropTableIfExists('albums');
+ await knex.schema.dropTableIfExists('files');
+ await knex.schema.dropTableIfExists('links');
+ await knex.schema.dropTableIfExists('albumsFiles');
+ await knex.schema.dropTableIfExists('albumsLinks');
+ await knex.schema.dropTableIfExists('tags');
+ await knex.schema.dropTableIfExists('fileTags');
+ await knex.schema.dropTableIfExists('bans');
+};
diff --git a/src/api/database/migrations/20201227023216_addUniques.js b/src/api/database/migrations/20201227023216_addUniques.js
new file mode 100644
index 0000000..14f9e7f
--- /dev/null
+++ b/src/api/database/migrations/20201227023216_addUniques.js
@@ -0,0 +1,33 @@
+exports.up = async knex => {
+ await knex.schema.alterTable('users', table => {
+ table.unique(['username', 'apiKey']);
+ });
+
+ await knex.schema.alterTable('albums', table => {
+ table.boolean('nsfw').defaultTo(false);
+ table.unique(['userId', 'name']);
+ });
+
+ await knex.schema.alterTable('links', table => {
+ table.unique(['userId', 'albumId', 'identifier']);
+ });
+
+ await knex.schema.alterTable('albumsFiles', table => {
+ table.unique(['albumId', 'fileId']);
+ });
+
+ await knex.schema.alterTable('albumsLinks', table => {
+ table.unique(['linkId']);
+ });
+
+ await knex.schema.alterTable('tags', table => {
+ table.unique(['userId', 'name']);
+ });
+
+ await knex.schema.alterTable('fileTags', table => {
+ table.unique(['fileId', 'tagId']);
+ });
+};
+exports.down = async knex => {
+ // Nothing
+};
diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js
new file mode 100644
index 0000000..edc1949
--- /dev/null
+++ b/src/api/database/seeds/initial.js
@@ -0,0 +1,30 @@
+/* eslint-disable no-console */
+const bcrypt = require('bcrypt');
+const moment = require('moment');
+
+exports.seed = async db => {
+ const now = moment.utc().toDate();
+ const user = await db.table('users').where({ username: process.env.ADMIN_ACCOUNT }).first();
+ if (user) return;
+ try {
+ const hash = await bcrypt.hash(process.env.ADMIN_PASSWORD, 10);
+ await db.table('users').insert({
+ username: process.env.ADMIN_ACCOUNT,
+ password: hash,
+ passwordEditedAt: now,
+ createdAt: now,
+ editedAt: now,
+ enabled: true,
+ isAdmin: true
+ });
+ console.log();
+ console.log('=========================================================');
+ console.log('== Successfully created the admin account. ==');
+ console.log('=========================================================');
+ console.log('== Run `pm2 start pm2.json` to start the service ==');
+ console.log('=========================================================');
+ console.log();
+ } catch (error) {
+ console.error(error);
+ }
+};