2020-07-17 09:05:58 +03:00
|
|
|
import configs from "~/knexfile";
|
|
|
|
import knex from "knex";
|
|
|
|
|
|
|
|
const envConfig = configs["development"];
|
|
|
|
|
|
|
|
console.log(`SETUP: database`, envConfig);
|
|
|
|
|
|
|
|
const db = knex(envConfig);
|
|
|
|
|
2020-07-28 09:54:15 +03:00
|
|
|
console.log(`RUNNING: seed-database.js`);
|
2020-07-17 09:05:58 +03:00
|
|
|
|
|
|
|
// --------------------------
|
|
|
|
// SCRIPTS
|
|
|
|
// --------------------------
|
|
|
|
|
2020-10-19 02:26:14 +03:00
|
|
|
const createDealsTable = db.schema.createTable("deals", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.string("owner_user_id").nullable();
|
|
|
|
table.jsonb("data").nullable();
|
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
table.timestamp("updated_at").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
});
|
|
|
|
|
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()"));
|
2020-07-17 09:05:58 +03:00
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
2020-07-17 09:05:58 +03:00
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("updated_at").notNullable().defaultTo(db.raw("now()"));
|
2020-07-17 09:05:58 +03:00
|
|
|
|
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();
|
|
|
|
table.jsonb("data").nullable();
|
|
|
|
});
|
|
|
|
|
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()"));
|
2020-07-17 09:05:58 +03:00
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
2020-07-17 09:05:58 +03:00
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("updated_at").notNullable().defaultTo(db.raw("now()"));
|
2020-07-17 09:05:58 +03:00
|
|
|
|
|
|
|
table.timestamp("published_at").nullable();
|
|
|
|
|
2020-09-28 23:12:43 +03:00
|
|
|
table.string("slatename").nullable();
|
2020-07-25 03:16:35 +03:00
|
|
|
|
2020-07-17 09:05:58 +03:00
|
|
|
table.jsonb("data").nullable();
|
|
|
|
});
|
|
|
|
|
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();
|
2020-07-28 09:54:15 +03:00
|
|
|
table.uuid("owner_id").notNullable();
|
|
|
|
table.integer("level").defaultTo(0);
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
2020-07-28 09:54:15 +03:00
|
|
|
});
|
|
|
|
|
2020-10-19 02:26:14 +03:00
|
|
|
const createSubscriptionTable = db.schema.createTable("subscriptions", function (table) {
|
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
|
|
|
table.string("owner_user_id").nullable();
|
|
|
|
table.string("target_slate_id").nullable();
|
|
|
|
table.string("target_user_id").nullable();
|
|
|
|
table.jsonb("data").nullable();
|
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
});
|
2020-09-21 06:58:15 +03:00
|
|
|
|
|
|
|
const createTrustedTable = db.schema.createTable("trusted", function (table) {
|
2020-10-19 02:26:14 +03:00
|
|
|
table.uuid("id").primary().unique().notNullable().defaultTo(db.raw("uuid_generate_v4()"));
|
2020-08-25 09:08:13 +03:00
|
|
|
table.string("owner_user_id").nullable();
|
|
|
|
table.string("target_user_id").nullable();
|
|
|
|
table.jsonb("data").nullable();
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
2020-08-25 09:08:13 +03:00
|
|
|
});
|
|
|
|
|
2020-09-21 06:58:15 +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()"));
|
2020-09-21 06:58:15 +03:00
|
|
|
table.string("owner_slate_id").nullable();
|
2020-08-25 09:08:13 +03:00
|
|
|
table.string("owner_user_id").nullable();
|
|
|
|
table.jsonb("data").nullable();
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
2020-08-25 09:08:13 +03:00
|
|
|
});
|
|
|
|
|
2020-09-21 06:58:15 +03:00
|
|
|
const createPendingTable = db.schema.createTable("pending", 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("owner_user_id").notNullable();
|
2020-08-25 09:08:13 +03:00
|
|
|
table.jsonb("data").nullable();
|
2020-09-21 06:58:15 +03:00
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
2020-08-25 09:08:13 +03:00
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
});
|
|
|
|
|
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();
|
|
|
|
table.timestamp("created_at").notNullable().defaultTo(db.raw("now()"));
|
|
|
|
});
|
2020-11-17 10:48:23 +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,
|
|
|
|
createSubscriptionTable,
|
|
|
|
createActivityTable,
|
|
|
|
createTrustedTable,
|
2020-09-21 06:58:15 +03:00
|
|
|
createPendingTable,
|
2020-11-17 10:44:47 +03:00
|
|
|
createStatsTable,
|
2020-12-02 10:07:49 +03:00
|
|
|
createOrphansTable,
|
|
|
|
createGlobalTable,
|
2020-08-25 09:08:13 +03:00
|
|
|
]);
|
2020-07-17 09:05:58 +03:00
|
|
|
|
|
|
|
console.log(`FINISHED: seed-database.js`);
|
|
|
|
console.log(` CTRL +C to return to terminal.`);
|