"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/index.ts var src_exports = {}; __export(src_exports, { createRepository: () => createRepository, migrateToLatest: () => migrateToLatest, planetscalePlugin: () => planetscalePlugin }); module.exports = __toCommonJS(src_exports); var import_bouncer_core = require("@dotinc/bouncer-core"); var import_kysely_planetscale2 = require("kysely-planetscale"); var import_bouncer_sql = require("@dotinc/bouncer-sql"); var import_date_fns = require("date-fns"); var import_database = require("@planetscale/database"); // src/migration.ts var import_kysely = require("kysely"); var import_kysely_planetscale = require("kysely-planetscale"); var MigrationProvider = class { getMigrations() { return Promise.resolve({ "2022-10-23_base": { up: async (db) => { await db.schema.createTable("publishers").addColumn("id", "varchar(30)", (col) => col.primaryKey()).addColumn("product_name", "varchar(255)", (col) => col.notNull()).addColumn("publisher_name", "varchar(255)", (col) => col.notNull()).addColumn("home_page_url", "varchar(500)").addColumn("contact_page_url", "varchar(500)").addColumn("privacy_notice_page_url", "varchar(500)").addColumn("contact_sales_email", "varchar(255)").addColumn("contact_sales_url", "varchar(500)").addColumn("contact_support_email", "varchar(255)").addColumn("contact_support_url", "varchar(500)").addColumn("mona_base_storage_url", "varchar(500)").addColumn("mona_subscription_state", "varchar(50)").addColumn("mona_subscription_is_being_configured", "boolean").addColumn("is_setup_complete", "boolean").execute(); await db.schema.createTable("seating_config").addColumn( "owner_id", "varchar(30)", (col) => col.unique().notNull().primaryKey() ).addColumn( "defaultLowSeatWarningLevelPercent", "real", (col) => col.notNull() ).addColumn("seating_strategy_name", "varchar(50)").addColumn("low_seat_warning_level_pct", "real").addColumn("limited_overflow_seating_enabled", "boolean").addColumn("seat_reservation_expiry_in_days", "integer").addColumn("default_seat_expiry_in_days", "integer").execute(); await db.schema.createTable("product_config").addColumn( "publisher_id", "varchar(30)", (col) => col.unique().notNull().primaryKey() ).addColumn("on_access_denied_url", "varchar(500)").addColumn("on_access_granted_url", "varchar(500)").addColumn("on_no_seat_available_url", "varchar(500)").addColumn("on_subscription_not_ready_url", "varchar(500)").addColumn("on_subscription_canceled_url", "varchar(500)").addColumn("on_subscription_suspended_url", "varchar(500)").addColumn("on_subscription_not_found_url", "varchar(500)").addColumn("on_no_subscriptions_found_url", "varchar(500)").execute(); await db.schema.createTable("seats").addColumn( "seat_id", "varchar(30)", (col) => col.notNull().primaryKey() ).addColumn("seating_strategy_name", "varchar(50)").addColumn("subscription_id", "varchar(30)").addColumn("created_utc", "datetime").addColumn("seat_type", "varchar(50)").addColumn("expires_utc", "datetime").addColumn("redeemed_utc", "datetime").execute(); await db.schema.createTable("seat_reservations").addColumn( "seat_id", "varchar(30)", (col) => col.primaryKey().notNull() ).addColumn("tenant_id", "varchar(30)").addColumn("user_id", "varchar(30)").addColumn("email", "varchar(255)").addColumn("invite_url", "varchar(500)").execute(); await db.schema.createTable("seat_occupants").addColumn( "seat_id", "varchar(30)", (col) => col.primaryKey().notNull() ).addColumn("user_id", "varchar(30)", (col) => col.notNull()).addColumn("tenant_id", "varchar(30)", (col) => col.notNull()).addColumn("email", "varchar(255)").addColumn("user_name", "varchar(255)").execute(); await db.schema.createTable("subscriptions").addColumn( "subscription_id", "varchar(30)", (col) => col.primaryKey().notNull() ).addColumn("publisher_id", "varchar(30)").addColumn("is_setup_complete", "boolean").addColumn("created_utc", "datetime").addColumn("tenant_id", "varchar(30)").addColumn("subscriber_info", "json").addColumn("source_subscription", "json").addColumn("subscription_name", "varchar(255)").addColumn("tenant_name", "varchar(255)").addColumn("offer_id", "varchar(30)").addColumn("plan_id", "varchar(30)").addColumn("state", "varchar(15)", (col) => col.notNull()).addColumn("admin_role_name", "varchar(50)").addColumn("user_role_name", "varchar(50)").addColumn("management_urls", "json").addColumn("admin_name", "varchar(50)").addColumn("admin_email", "varchar(255)").addColumn("total_seats", "integer").addColumn("is_being_configured", "boolean").addColumn("is_free_trial", "boolean").addColumn("is_test_subscription", "boolean").addColumn("state_last_updated_utc", "datetime").execute(); }, down: async (db) => { await db.schema.dropTable("subscriptions").execute(); await db.schema.dropTable("seat_occupants").execute(); await db.schema.dropTable("seat_reservations").execute(); await db.schema.dropTable("product_config").execute(); await db.schema.dropTable("seating_config").execute(); await db.schema.dropTable("publishers").execute(); } }, "2022-10-29_seat_summary": { up: async (db) => { await db.schema.createTable("seat_summary").addColumn( "subscription_id", "varchar(30)", (col) => col.unique().notNull().primaryKey() ).addColumn("standard_seat_count", "integer", (col) => col.notNull()).addColumn("limited_seat_count", "integer", (col) => col.notNull()).execute(); }, down: async (db) => { await db.schema.dropTable("seat_summary").execute(); } } }); } }; var toLatest = async (dialect, provider) => { const db = new import_kysely.Kysely({ dialect }); const migrator = new import_kysely.Migrator({ db, provider }); const { error, results } = await migrator.migrateToLatest(); results == null ? void 0 : results.forEach((it) => { if (it.status === "Success") { console.log(`migration "${it.migrationName}" was executed successfully`); } else if (it.status === "Error") { console.error(`failed to execute migration "${it.migrationName}"`); } }); if (error) { console.error("failed to migrate"); console.error(error); process.exit(1); } await db.destroy(); }; var migrateToLatest = (config) => { const dialect = new import_kysely_planetscale.PlanetScaleDialect(config); const provider = new MigrationProvider(); toLatest(dialect, provider); }; // src/index.ts var createRepository = (config) => { return (0, import_bouncer_sql.createRepository)({ dialect: new import_kysely_planetscale2.PlanetScaleDialect({ cast: (field, value) => { if (field.type === "DATETIME" && value) return (0, import_bouncer_core.getMysqlFormattedDateTime)((0, import_date_fns.parseJSON)(value)); if (field.type === "TIMESTAMP" && value) return (0, import_bouncer_core.getMysqlFormattedDateTime)((0, import_date_fns.parseJSON)(value)); if (field.type === "INT8" && value !== null && value !== void 0) return parseInt(value) === 1 ? true : false; return (0, import_database.cast)(field, value); }, ...config }), log: ["query", "error"] }); }; var planetscalePlugin = (dbConfig) => { return { name: "planetscale", request: async (_, config) => { return { ...config, repo: createRepository(dbConfig) }; } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createRepository, migrateToLatest, planetscalePlugin });