2020-07-17 09:05:58 +03:00
|
|
|
import configs from "~/knexfile";
|
|
|
|
import knex from "knex";
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
import * as Logging from "~/common/logging";
|
|
|
|
|
2020-07-17 09:05:58 +03:00
|
|
|
const envConfig = configs["development"];
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`SETUP: database`, envConfig);
|
2020-07-17 09:05:58 +03:00
|
|
|
|
|
|
|
const db = knex(envConfig);
|
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`RUNNING: seed-database.js`);
|
2020-07-17 09:05:58 +03:00
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// SCRIPTS
|
|
|
|
// --------------------------
|
|
|
|
|
2021-03-07 23:53:54 +03:00
|
|
|
//replace createdat, updatedat, ownerid, owneruserid
|
|
|
|
|
2020-10-19 02:26:14 +03:00
|
|
|
const createDealsTable = db.schema.createTable("deals", function (table) {
|
2021-09-28 23:52:21 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.string("textileBucketCID").notNullable();
|
|
|
|
table.string("pinCID").notNullable();
|
|
|
|
table.string("requestId").notNullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-10-19 02:26:14 +03:00
|
|
|
});
|
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
const createUsersTable = db.schema.createTable("users", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2021-03-07 23:53:54 +03:00
|
|
|
table.string("email").unique().nullable();
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
table.timestamp("lastActive").notNullable().defaultTo(db.raw("now()"));
|
2020-09-21 06:58:15 +03:00
|
|
|
table.string("username").unique().notNullable();
|
2020-07-17 09:05:58 +03:00
|
|
|
table.string("password").nullable();
|
|
|
|
table.string("salt").nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("name").nullable();
|
2021-09-10 22:09:36 +03:00
|
|
|
table.string("body", 2000).nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("photo").nullable();
|
|
|
|
table.string("twitterId").unique().nullable();
|
|
|
|
table.string("twitterUsername").nullable();
|
|
|
|
table.boolean("twitterVerified").notNullable().defaultTo(false);
|
2021-09-25 04:20:28 +03:00
|
|
|
table.string("textileKey").nullable();
|
|
|
|
table.string("textileToken", 400).nullable();
|
|
|
|
table.string("textileThreadID").nullable();
|
|
|
|
table.string("textileBucketCID").nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.jsonb("onboarding").nullable();
|
2021-05-25 01:19:48 +03:00
|
|
|
table.integer("followerCount").notNullable().defaultTo(0);
|
|
|
|
table.integer("slateCount").notNullable().defaultTo(0);
|
2021-06-09 01:53:30 +03:00
|
|
|
table.integer("authVersion").notNullable().defaultTo(2);
|
2020-07-17 09:05:58 +03:00
|
|
|
});
|
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
const createSlatesTable = db.schema.createTable("slates", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
table.timestamp("updatedAt").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
table.string("slatename").notNullable();
|
2021-09-10 22:09:36 +03:00
|
|
|
table.string("body", 2000).nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("name").nullable();
|
|
|
|
table.string("preview").nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.boolean("isPublic").notNullable().defaultTo(false);
|
2021-05-25 01:19:48 +03:00
|
|
|
table.integer("subscriberCount").notNullable().defaultTo(0);
|
|
|
|
table.integer("fileCount").notNullable().defaultTo(0);
|
2020-07-17 09:05:58 +03:00
|
|
|
});
|
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
const createKeysTable = db.schema.createTable("keys", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2020-09-21 06:58:15 +03:00
|
|
|
table.string("key").unique().nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("ownerId").notNullable();
|
2020-07-28 09:54:15 +03:00
|
|
|
table.integer("level").defaultTo(0);
|
2021-03-07 23:53:54 +03:00
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-07-28 09:54:15 +03:00
|
|
|
});
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
const createFilesTable = db.schema.createTable("files", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
table.string("cid").notNullable();
|
|
|
|
table.string("filename").nullable();
|
|
|
|
table.boolean("isPublic").notNullable().defaultTo(false);
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("name").nullable();
|
|
|
|
table.integer("size").notNullable().defaultTo(0);
|
|
|
|
table.string("type").notNullable().defaultTo("link");
|
|
|
|
table.string("blurhash").nullable();
|
|
|
|
table.string("source").nullable();
|
2021-09-10 22:09:36 +03:00
|
|
|
table.string("body", 2000).nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("author").nullable();
|
|
|
|
table.jsonb("coverImage").nullable();
|
2020-10-19 02:26:14 +03:00
|
|
|
table.jsonb("data").nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("linkName").nullable();
|
2021-09-10 22:09:36 +03:00
|
|
|
table.string("linkBody", 2000).nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.string("linkAuthor").nullable();
|
|
|
|
table.string("linkSource").nullable();
|
|
|
|
table.string("linkDomain").nullable();
|
|
|
|
table.string("linkImage").nullable();
|
|
|
|
table.string("linkFavicon").nullable();
|
2021-09-10 22:09:36 +03:00
|
|
|
table.text("linkHtml").nullable();
|
2021-09-04 01:36:02 +03:00
|
|
|
table.boolean("linkIFrameAllowed").nullable().defaultTo(false);
|
|
|
|
table.jsonb("tags").nullable();
|
2021-05-25 01:19:48 +03:00
|
|
|
table.integer("downloadCount").notNullable().defaultTo(0);
|
2021-09-04 01:36:02 +03:00
|
|
|
table.integer("saveCount").notNullable().defaultTo(0);
|
2021-07-07 23:50:57 +03:00
|
|
|
table.string("url").nullable();
|
|
|
|
table.boolean("isLink").notNullable().defaultTo(false);
|
2020-10-19 02:26:14 +03:00
|
|
|
});
|
2020-09-21 06:58:15 +03:00
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
const createSlateFilesTable = db.schema.createTable("slate_files", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("fileId").references("id").inTable("files");
|
|
|
|
table.uuid("slateId").references("id").inTable("slates");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-08-25 09:08:13 +03:00
|
|
|
});
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
const createSubscriptionsTable = db.schema.createTable("subscriptions", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.uuid("slateId").references("id").inTable("slates");
|
|
|
|
table.uuid("userId").references("id").inTable("users");
|
2020-08-25 09:08:13 +03:00
|
|
|
table.jsonb("data").nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-08-25 09:08:13 +03:00
|
|
|
});
|
|
|
|
|
2021-05-25 01:19:48 +03:00
|
|
|
const createActivityTable = db.schema.createTable("activity", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2021-03-07 23:53:54 +03:00
|
|
|
table.uuid("ownerId").references("id").inTable("users");
|
|
|
|
table.uuid("userId").references("id").inTable("users");
|
|
|
|
table.uuid("slateId").references("id").inTable("slates");
|
|
|
|
table.uuid("fileId").references("id").inTable("files");
|
|
|
|
table.string("type");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2021-05-25 01:19:48 +03:00
|
|
|
table.boolean("ignore").notNullable().defaultTo(false);
|
|
|
|
});
|
|
|
|
|
2020-11-17 10:44:47 +03:00
|
|
|
const createStatsTable = db.schema.createTable("stats", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.jsonb("data").nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-11-17 10:44:47 +03:00
|
|
|
});
|
|
|
|
|
2020-11-17 10:48:23 +03:00
|
|
|
const createOrphansTable = db.schema.createTable("orphans", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.jsonb("data").nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-11-17 10:48:23 +03:00
|
|
|
});
|
|
|
|
|
2020-12-02 10:07:49 +03:00
|
|
|
const createGlobalTable = db.schema.createTable("global", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.jsonb("data").nullable();
|
2021-03-07 23:53:54 +03:00
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
2020-12-02 10:07:49 +03:00
|
|
|
});
|
2020-11-17 10:48:23 +03:00
|
|
|
|
2021-05-19 05:58:14 +03:00
|
|
|
const createUsageTable = db.schema.createTable("usage", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.uuid("userId").references("id").inTable("users");
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
});
|
2021-06-09 01:53:30 +03:00
|
|
|
//NOTE(toast): making sid pkey and letting emails dupe allows for multiple keys per user,
|
|
|
|
//stops people from getting dos'd on verification
|
|
|
|
const createVerificationsTable = db.schema.createTable("verifications", function (table) {
|
|
|
|
table.uuid("sid").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.string("email").nullable();
|
|
|
|
table.string("twitterToken").unique().nullable();
|
|
|
|
table.string("pin", 6).unique().notNullable();
|
|
|
|
table.timestamp("createdAt").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
table.boolean("isVerified").notNullable().defaultTo(false);
|
|
|
|
table
|
|
|
|
.enu("type", [
|
|
|
|
"email_verification",
|
|
|
|
"email_twitter_verification",
|
|
|
|
"password_reset",
|
|
|
|
"user_migration",
|
|
|
|
])
|
|
|
|
.defaultTo("email_verification");
|
|
|
|
table.string("username").nullable();
|
|
|
|
table.boolean("passwordChanged").nullable();
|
|
|
|
});
|
|
|
|
|
|
|
|
const createTwitterTokensTable = db.schema.createTable("twitterTokens", function (table) {
|
|
|
|
table.string("token").primary().unique().notNullable();
|
|
|
|
table.string("tokenSecret").notNullable();
|
|
|
|
table.string("email").nullable();
|
|
|
|
table.string("id_str").nullable();
|
|
|
|
table.string("screen_name").nullable();
|
|
|
|
table.string("verified").nullable();
|
|
|
|
});
|
2021-05-19 05:58:14 +03:00
|
|
|
|
2020-07-17 09:05:58 +03:00
|
|
|
// --------------------------
|
|
|
|
// RUN
|
|
|
|
// --------------------------
|
|
|
|
|
2020-08-25 09:08:13 +03:00
|
|
|
Promise.all([
|
2020-10-19 02:26:14 +03:00
|
|
|
createDealsTable,
|
2020-08-25 09:08:13 +03:00
|
|
|
createUsersTable,
|
|
|
|
createSlatesTable,
|
|
|
|
createKeysTable,
|
2021-03-07 23:53:54 +03:00
|
|
|
createFilesTable,
|
|
|
|
createSlateFilesTable,
|
|
|
|
createSubscriptionsTable,
|
2020-08-25 09:08:13 +03:00
|
|
|
createActivityTable,
|
2020-11-17 10:44:47 +03:00
|
|
|
createStatsTable,
|
2020-12-02 10:07:49 +03:00
|
|
|
createOrphansTable,
|
2021-06-09 01:53:30 +03:00
|
|
|
createVerificationsTable,
|
2020-12-02 10:07:49 +03:00
|
|
|
createGlobalTable,
|
2021-05-19 05:58:14 +03:00
|
|
|
createUsageTable,
|
2021-06-09 01:53:30 +03:00
|
|
|
createTwitterTokensTable,
|
2020-08-25 09:08:13 +03:00
|
|
|
]);
|
2020-07-17 09:05:58 +03:00
|
|
|
|
2021-06-11 22:25:58 +03:00
|
|
|
Logging.log(`FINISHED: seed-database.js`);
|
|
|
|
Logging.log(` CTRL +C to return to terminal.`);
|