@futura-dev/cosmoprism
Advanced tools
Comparing version
140
dist/cli.js
@@ -36,24 +36,31 @@ #! /usr/bin/env node | ||
const migrate_1 = require("./cmd/migrate/migrate"); | ||
const functions_1 = require("./utils/functions"); | ||
// TODO: add custom options processing | ||
// program definition | ||
commander_1.program | ||
.name('cosmoprism') | ||
.alias('cprism') | ||
.description('Cosmoprism πΊπ§') | ||
.version(process.env.npm_package_version ?? '-'); | ||
.name("cosmoprism") | ||
.alias("cprism") | ||
.description("Cosmoprism πΊπ§") | ||
.version(process.env.npm_package_version ?? "-") | ||
.hook("preAction", functions_1.loadExternalEnv); | ||
// --------------- | ||
// command: INIT | ||
// --------------- | ||
commander_1.program.command('init').action(async () => await (0, init_1.init)()); | ||
commander_1.program.command("init").action(async () => await (0, init_1.init)()); | ||
// --------------- | ||
// command: GENERATE | ||
// --------------- | ||
commander_1.program.command('generate') | ||
.option('-t, --tenant [tenant-id]', 'Apply generate command only for tenant.') | ||
.option('-c, --central', 'Apply generate command only for central.') | ||
commander_1.program | ||
.command("generate") | ||
.option("-t, --tenant [tenant-id]", "Apply generate command only for tenant.") | ||
.option("-c, --central", "Apply generate command only for central.") | ||
.action(async (...args) => { | ||
const { tenant, central } = args[0]; | ||
if (!!tenant && !!central) | ||
throw new Error('ERR: Only one of tenant and central can by passed !!'); | ||
await (0, generate_1.generate)(!central && !tenant ? { mode: 'all' } : !!central ? { mode: 'central' } : { mode: 'tenant', tenant }); | ||
throw new Error("ERR: Only one of tenant and central can by passed !!"); | ||
await (0, generate_1.generate)(!central && !tenant | ||
? { mode: "all" } | ||
: central | ||
? { mode: "central" } | ||
: { mode: "tenant", tenant }); | ||
}); | ||
@@ -63,4 +70,5 @@ // --------------- | ||
// --------------- | ||
commander_1.program.command('validate') | ||
.option('--schema [file]', 'Schema to validate.') | ||
commander_1.program | ||
.command("validate") | ||
.option("--schema [file]", "Schema to validate.") | ||
.action(async (...args) => { | ||
@@ -73,5 +81,6 @@ const { schema } = args[0]; | ||
// --------------- | ||
commander_1.program.command('format') | ||
.option('--schema [file]', 'Schema to validate.') | ||
.option('--check', ' Fails if any files are unformatted. This can be used in CI to detect if the schema is formatted correctly.') | ||
commander_1.program | ||
.command("format") | ||
.option("--schema [file]", "Schema to validate.") | ||
.option("--check", " Fails if any files are unformatted. This can be used in CI to detect if the schema is formatted correctly.") | ||
.action(async (...args) => { | ||
@@ -85,14 +94,19 @@ const { schema, check } = args[0]; | ||
// --------------- | ||
const dbCommand = commander_1.program.command('db'); | ||
const dbCommand = commander_1.program.command("db"); | ||
// dbCommand.command('pull') // TODO: implement | ||
// dbCommand.command('push') // TODO: implement | ||
// dbCommand.command('execute') // TODO: implement | ||
dbCommand.command('seed') | ||
.option('-t, --tenant [tenant-id]', 'Apply generate command only for tenant.') | ||
.option('-c, --central', 'Apply generate command only for central.') | ||
dbCommand | ||
.command("seed") | ||
.option("-t, --tenant [tenant-id]", "Apply generate command only for tenant.") | ||
.option("-c, --central", "Apply generate command only for central.") | ||
.action(async (...args) => { | ||
const { tenant, central } = args[0]; | ||
if (!!tenant && !!central) | ||
throw new Error('ERR: Only one of tenant and central can by passed !!'); | ||
await db_1.db.seed(!central && !tenant ? { mode: 'all' } : !!central ? { mode: 'central' } : { mode: 'tenant', tenant }); | ||
throw new Error("ERR: Only one of tenant and central can by passed !!"); | ||
await db_1.db.seed(!central && !tenant | ||
? { mode: "all" } | ||
: central | ||
? { mode: "central" } | ||
: { mode: "tenant", tenant }); | ||
}); | ||
@@ -103,3 +117,3 @@ // --------------- | ||
// --------------- | ||
const migrateCommand = commander_1.program.command('migrate'); | ||
const migrateCommand = commander_1.program.command("migrate"); | ||
// dbCommand.command('resolve') // TODO: implement | ||
@@ -109,49 +123,52 @@ // dbCommand.command('status') // TODO: implement | ||
// dev | ||
migrateCommand.command('dev') | ||
.option('-t, --tenant [tenant-id]', 'Apply generate command only for tenant.') | ||
.option('-c, --central', 'Apply generate command only for central.') | ||
.option('-n, --name <name>', 'The name of the migration. If no name is provided, the CLI will prompt you.') | ||
.option('--create-only', 'Creates a new migration based on the changes in the schema but does not apply that migration. Run migrate dev to apply migration.') | ||
.option('--skip-seed', 'Skip triggering seed.') | ||
.option('--skip-generate', 'Skip triggering generators (for example, Prisma Client)') | ||
migrateCommand | ||
.command("dev") | ||
.option("-t, --tenant [tenant-id]", "Apply generate command only for tenant.") | ||
.option("-c, --central", "Apply generate command only for central.") | ||
.option("-n, --name <name>", "The name of the migration. If no name is provided, the CLI will prompt you.") | ||
.option("--create-only", "Creates a new migration based on the changes in the schema but does not apply that migration. Run migrate dev to apply migration.") | ||
.option("--skip-seed", "Skip triggering seed.") | ||
.option("--skip-generate", "Skip triggering generators (for example, Prisma Client)") | ||
.action(async (...args) => { | ||
const { tenant, central, name, createOnly, skipSeed, skipGenerate } = args[0]; | ||
if (!!tenant && !!central) | ||
throw new Error('ERR: Only one of tenant and central can by passed !!'); | ||
throw new Error("ERR: Only one of tenant and central can by passed !!"); | ||
await migrate_1.migrate.dev(!central && !tenant | ||
? { mode: 'all', name, createOnly, skipGenerate, skipSeed } | ||
: !!central | ||
? { mode: 'central', name, createOnly, skipGenerate, skipSeed } | ||
: { mode: 'tenant', tenant, name, createOnly, skipGenerate, skipSeed }); | ||
? { mode: "all", name, createOnly, skipGenerate, skipSeed } | ||
: central | ||
? { mode: "central", name, createOnly, skipGenerate, skipSeed } | ||
: { mode: "tenant", tenant, name, createOnly, skipGenerate, skipSeed }); | ||
}); | ||
// reset | ||
migrateCommand.command('reset') | ||
.option('-t, --tenant [tenant-id]', 'Apply generate command only for tenant.') | ||
.option('-c, --central', 'Apply generate command only for central.') | ||
.option('-f, --force', 'Skip the confirmation prompt.') | ||
.option('--skip-seed', 'Skip triggering seed.') | ||
.option('--skip-generate', 'Skip triggering generators (for example, Prisma Client)') | ||
migrateCommand | ||
.command("reset") | ||
.option("-t, --tenant [tenant-id]", "Apply generate command only for tenant.") | ||
.option("-c, --central", "Apply generate command only for central.") | ||
.option("-f, --force", "Skip the confirmation prompt.") | ||
.option("--skip-seed", "Skip triggering seed.") | ||
.option("--skip-generate", "Skip triggering generators (for example, Prisma Client)") | ||
.action(async (...args) => { | ||
const { tenant, central, force, skipSeed, skipGenerate } = args[0]; | ||
if (!!tenant && !!central) | ||
throw new Error('ERR: Only one of tenant and central can by passed !!'); | ||
throw new Error("ERR: Only one of tenant and central can by passed !!"); | ||
await migrate_1.migrate.reset(!central && !tenant | ||
? { mode: 'all', force, skipGenerate, skipSeed } | ||
: !!central | ||
? { mode: 'central', force, skipGenerate, skipSeed } | ||
: { mode: 'tenant', tenant, force, skipGenerate, skipSeed }); | ||
? { mode: "all", force, skipGenerate, skipSeed } | ||
: central | ||
? { mode: "central", force, skipGenerate, skipSeed } | ||
: { mode: "tenant", tenant, force, skipGenerate, skipSeed }); | ||
}); | ||
// deploy | ||
migrateCommand.command('deploy') | ||
.option('-t, --tenant [tenant-id]', 'Apply generate command only for tenant.') | ||
.option('-c, --central', 'Apply generate command only for central.') | ||
migrateCommand | ||
.command("deploy") | ||
.option("-t, --tenant [tenant-id]", "Apply generate command only for tenant.") | ||
.option("-c, --central", "Apply generate command only for central.") | ||
.action(async (...args) => { | ||
const { tenant, central } = args[0]; | ||
if (!!tenant && !!central) | ||
throw new Error('ERR: Only one of tenant and central can by passed !!'); | ||
throw new Error("ERR: Only one of tenant and central can by passed !!"); | ||
await migrate_1.migrate.deploy(!central && !tenant | ||
? { mode: 'all' } | ||
: !!central | ||
? { mode: 'central' } | ||
: { mode: 'tenant', tenant }); | ||
? { mode: "all" } | ||
: central | ||
? { mode: "central" } | ||
: { mode: "tenant", tenant }); | ||
}); | ||
@@ -161,14 +178,17 @@ // --------------- | ||
// --------------- | ||
commander_1.program.command('studio') | ||
.option('-t, --tenant [tenant-id]', 'Apply generate command only for tenant.') | ||
.option('-c, --central', 'Apply generate command only for central.') | ||
.option('-b, --browser [browser]', 'The browser to auto-open Studio in.') | ||
.option('-p, --port [port]', 'The port number to start Studio on.', '5555') | ||
commander_1.program | ||
.command("studio") | ||
.option("-t, --tenant [tenant-id]", "Apply generate command only for tenant.") | ||
.option("-c, --central", "Apply generate command only for central.") | ||
.option("-b, --browser [browser]", "The browser to auto-open Studio in.") | ||
.option("-p, --port [port]", "The port number to start Studio on.", "5555") | ||
.action(async (...args) => { | ||
const { tenant, central, browser, port } = args[0]; | ||
if (!!tenant && !!central) | ||
throw new Error('ERR: Only one of tenant and central can by passed !!'); | ||
await (0, studio_1.studio)(!!central ? { mode: 'central', browser, port } : { mode: 'tenant', tenant, browser, port }); | ||
throw new Error("ERR: Only one of tenant and central can by passed !!"); | ||
await (0, studio_1.studio)(central | ||
? { mode: "central", browser, port } | ||
: { mode: "tenant", tenant, browser, port }); | ||
}); | ||
// parse program | ||
commander_1.program.parse(process.argv); |
@@ -37,33 +37,33 @@ "use strict"; | ||
console.log(`running db seed in '${command.mode}' mode !`); | ||
if (command.mode === 'central' || command.mode === 'all') { | ||
if (command.mode === "central" || command.mode === "all") { | ||
console.log(`\nrunning db seed for central ...`); | ||
// run 'npx ts-node prisma/central/seed.ts' | ||
const centralSeedPath = path_1.default.join('prisma', 'central', 'seed.ts'); | ||
(0, child_process_1.spawnSync)('npx', ['ts-node', centralSeedPath], { | ||
// run 'npx ts-node prisma/central/seed.ts' | ||
const centralSeedPath = path_1.default.join("prisma", "central", "seed.ts"); | ||
(0, child_process_1.spawnSync)("npx", ["ts-node", centralSeedPath], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(`running db seed for central completed π±\n`); | ||
} | ||
if (command.mode === 'all' || command.mode === 'tenant') { | ||
if (command.mode === "all" || command.mode === "tenant") { | ||
// choose tenant | ||
const tenantUrls = command.mode !== 'all' && typeof command.tenant === 'string' | ||
const tenantUrls = command.mode !== "all" && typeof command.tenant === "string" | ||
? [command.tenant] | ||
: await chooseTenantPrompt(); | ||
console.log(`\nrunning db seed for ${tenantUrls.length} tenants ...\n`); | ||
const tenantSeedPath = path_1.default.join('prisma', 'tenant', 'seed.ts'); | ||
const tenantSeedPath = path_1.default.join("prisma", "tenant", "seed.ts"); | ||
for (const url of tenantUrls) { | ||
// run 'npx ts-node prisma/tenant/seed.ts' with 'DATABASE_TENANT_URL' as current tenant url | ||
(0, child_process_1.spawnSync)('npx', ['ts-node', tenantSeedPath], { | ||
(0, child_process_1.spawnSync)("npx", ["ts-node", tenantSeedPath], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env, DATABASE_TENANT_URL: url }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(url, ' completed π±'); | ||
console.log(url, " completed π±"); | ||
} | ||
} | ||
console.log('\nall done π±π !!'); | ||
console.log("\nall done π±π !!"); | ||
return; | ||
@@ -75,4 +75,4 @@ }; | ||
// TODO: retrieve tenants | ||
const configFilePath = path_1.default.join('.cosmoprism.json'); | ||
const config = JSON.parse(await fs.readFile(configFilePath, 'utf-8')); | ||
const configFilePath = path_1.default.join(".cosmoprism.json"); | ||
const config = JSON.parse(await fs.readFile(configFilePath, "utf-8")); | ||
const sequelize = new sequelize_1.Sequelize(config.centralDatabaseUrl); | ||
@@ -83,7 +83,11 @@ const queryExecution = await sequelize.query(`SELECT * FROM "${config.tenantTable.name}";`, { logging: false }); | ||
tenantIds: { | ||
type: 'checkbox', | ||
type: "checkbox", | ||
required: true, | ||
instructions: true, | ||
message: 'Choose a tenant', | ||
choices: queryResult.map((item) => ({ name: item[config.tenantTable.databaseUrlAttributeName], value: item[config.tenantTable.databaseUrlAttributeName], description: item[config.tenantTable.idAttributeName] })) | ||
message: "Choose a tenant", | ||
choices: queryResult.map((item) => ({ | ||
name: item[config.tenantTable.databaseUrlAttributeName], | ||
value: item[config.tenantTable.databaseUrlAttributeName], | ||
description: item[config.tenantTable.idAttributeName] | ||
})) | ||
} | ||
@@ -90,0 +94,0 @@ }); |
@@ -10,4 +10,4 @@ "use strict"; | ||
const format = async (command) => { | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
const schemasToValidate = command.schema | ||
@@ -19,8 +19,8 @@ ? [command.schema] | ||
if (command.check) | ||
commandArgs.push('--check'); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'format', ...commandArgs], { | ||
commandArgs.push("--check"); | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "format", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
@@ -27,0 +27,0 @@ console.log(`schema: ${schema} formatted ποΈ\n`); |
@@ -38,33 +38,33 @@ "use strict"; | ||
console.log(`running generate in '${command.mode}' mode !`); | ||
if (command.mode === 'central' || command.mode === 'all') { | ||
if (command.mode === "central" || command.mode === "all") { | ||
console.log(`\nrunning generate for central ...`); | ||
// run 'npx prisma generate --schema=./prisma/central/schema.prisma' | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'generate', `--schema=${centralSchemaPath}`], { | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "generate", `--schema=${centralSchemaPath}`], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(`running generate for central completed π\n`); | ||
} | ||
if (command.mode === 'all' || command.mode === 'tenant') { | ||
if (command.mode === "all" || command.mode === "tenant") { | ||
// choose tenant | ||
const tenantUrls = command.mode !== 'all' && typeof command.tenant === 'string' | ||
const tenantUrls = command.mode !== "all" && typeof command.tenant === "string" | ||
? [command.tenant] | ||
: await chooseTenantPrompt(); | ||
console.log(`\nrunning generate for ${tenantUrls.length} tenants ...\n`); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
for (const url of tenantUrls) { | ||
// run 'npx prisma generate --schema=./prisma/tenant/schema.prisma' with 'DATABASE_TENANT_URL' as current tenant url | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'generate', `--schema=${tenantSchemaPath}`], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "generate", `--schema=${tenantSchemaPath}`], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env, DATABASE_TENANT_URL: url }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(url, ' completed π'); | ||
console.log(url, " completed π"); | ||
} | ||
} | ||
console.log('\nall done π !!'); | ||
console.log("\nall done π !!"); | ||
}; | ||
@@ -75,4 +75,4 @@ exports.generate = generate; | ||
// TODO: retrieve tenants | ||
const configFilePath = path_1.default.join('.cosmoprism.json'); | ||
const config = JSON.parse(await fs.readFile(configFilePath, 'utf-8')); | ||
const configFilePath = path_1.default.join(".cosmoprism.json"); | ||
const config = JSON.parse(await fs.readFile(configFilePath, "utf-8")); | ||
const sequelize = new sequelize_1.Sequelize(config.centralDatabaseUrl); | ||
@@ -83,7 +83,11 @@ const queryExecution = await sequelize.query(`SELECT * FROM "${config.tenantTable.name}";`, { logging: false }); | ||
tenantIds: { | ||
type: 'checkbox', | ||
type: "checkbox", | ||
required: true, | ||
instructions: true, | ||
message: 'Choose a tenant', | ||
choices: queryResult.map((item) => ({ name: item[config.tenantTable.databaseUrlAttributeName], value: item[config.tenantTable.databaseUrlAttributeName], description: item[config.tenantTable.idAttributeName] })) | ||
message: "Choose a tenant", | ||
choices: queryResult.map((item) => ({ | ||
name: item[config.tenantTable.databaseUrlAttributeName], | ||
value: item[config.tenantTable.databaseUrlAttributeName], | ||
description: item[config.tenantTable.idAttributeName] | ||
})) | ||
} | ||
@@ -90,0 +94,0 @@ }); |
@@ -39,23 +39,23 @@ "use strict"; | ||
// 1. | ||
const configContent = await fs.readFile(path.join(here, 'templates', '.cosmoprism.json.template')); | ||
await fs.writeFile('.cosmoprism.json', configContent); | ||
const configContent = await fs.readFile(path.join(here, "templates", ".cosmoprism.json.template")); | ||
await fs.writeFile(".cosmoprism.json", configContent); | ||
// 2. | ||
if (!fsSync.existsSync('prisma')) { | ||
await fs.mkdir('prisma'); | ||
if (!fsSync.existsSync("prisma")) { | ||
await fs.mkdir("prisma"); | ||
} | ||
// 3. | ||
if (!fsSync.existsSync('prisma/central')) { | ||
await fs.mkdir('prisma/central'); | ||
if (!fsSync.existsSync("prisma/central")) { | ||
await fs.mkdir("prisma/central"); | ||
} | ||
if (!fsSync.existsSync('prisma/tenant')) { | ||
await fs.mkdir('prisma/tenant'); | ||
if (!fsSync.existsSync("prisma/tenant")) { | ||
await fs.mkdir("prisma/tenant"); | ||
} | ||
// 4. | ||
const centralContent = await fs.readFile(path.join(here, 'templates', 'central.template')); | ||
const tenantContent = await fs.readFile(path.join(here, 'templates', 'tenant.template')); | ||
const centralSchemaPath = path.join('prisma', 'central', 'schema.prisma'); | ||
const centralContent = await fs.readFile(path.join(here, "templates", "central.template")); | ||
const tenantContent = await fs.readFile(path.join(here, "templates", "tenant.template")); | ||
const centralSchemaPath = path.join("prisma", "central", "schema.prisma"); | ||
if (!fsSync.existsSync(centralSchemaPath)) { | ||
await fs.writeFile(centralSchemaPath, centralContent); | ||
} | ||
const tenantSchemaPath = path.join('prisma', 'tenant', 'schema.prisma'); | ||
const tenantSchemaPath = path.join("prisma", "tenant", "schema.prisma"); | ||
if (!fsSync.existsSync(tenantSchemaPath)) { | ||
@@ -65,7 +65,7 @@ await fs.writeFile(tenantSchemaPath, tenantContent); | ||
// 5. | ||
const centralSeedPath = path.join('prisma', 'central', 'seed.ts'); | ||
const centralSeedPath = path.join("prisma", "central", "seed.ts"); | ||
if (!fsSync.existsSync(centralSeedPath)) { | ||
await fs.writeFile(centralSeedPath, seedContent); | ||
} | ||
const tenantSeedPath = path.join('prisma', 'tenant', 'seed.ts'); | ||
const tenantSeedPath = path.join("prisma", "tenant", "seed.ts"); | ||
if (!fsSync.existsSync(tenantSeedPath)) { | ||
@@ -72,0 +72,0 @@ await fs.writeFile(tenantSeedPath, seedContent); |
@@ -37,23 +37,23 @@ "use strict"; | ||
console.log(`running 'migrate deploy' in '${command.mode}' mode !`); | ||
if (command.mode === 'central' || command.mode === 'all') { | ||
if (command.mode === "central" || command.mode === "all") { | ||
console.log(`\nrunning 'migrate deploy' for central ...`); | ||
// run 'npx prisma migrate deploy --schema=./prisma/central/schema.prisma' | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
const commandArgs = []; | ||
commandArgs.push(`--schema=${centralSchemaPath}`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'migrate', 'deploy', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "migrate", "deploy", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(`running 'migrate deploy' for central completed π\n`); | ||
} | ||
if (command.mode === 'all' || command.mode === 'tenant') { | ||
if (command.mode === "all" || command.mode === "tenant") { | ||
// choose tenant | ||
const tenantUrls = command.mode !== 'all' && typeof command.tenant === 'string' | ||
const tenantUrls = command.mode !== "all" && typeof command.tenant === "string" | ||
? [command.tenant] | ||
: await chooseTenantPrompt(); | ||
console.log(`\nrunning 'migrate deploy' for ${tenantUrls.length} tenants ...\n`); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
for (const url of tenantUrls) { | ||
@@ -63,12 +63,12 @@ // run 'npx prisma migrate deploy --schema=./prisma/tenant/schema.prisma' with 'DATABASE_TENANT_URL' as current tenant url | ||
commandArgs.push(`--schema=${tenantSchemaPath}`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'migrate', 'deploy', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "migrate", "deploy", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env, DATABASE_TENANT_URL: url }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(url, ' completed π'); | ||
console.log(url, " completed π"); | ||
} | ||
} | ||
console.log('\nall done π !!'); | ||
console.log("\nall done π !!"); | ||
}; | ||
@@ -79,4 +79,4 @@ exports.deploy = deploy; | ||
// TODO: retrieve tenants | ||
const configFilePath = path_1.default.join('.cosmoprism.json'); | ||
const config = JSON.parse(await fs.readFile(configFilePath, 'utf-8')); | ||
const configFilePath = path_1.default.join(".cosmoprism.json"); | ||
const config = JSON.parse(await fs.readFile(configFilePath, "utf-8")); | ||
const sequelize = new sequelize_1.Sequelize(config.centralDatabaseUrl); | ||
@@ -87,7 +87,11 @@ const queryExecution = await sequelize.query(`SELECT * FROM ${config.tenantTable.name};`, { logging: false }); | ||
tenantIds: { | ||
type: 'checkbox', | ||
type: "checkbox", | ||
required: true, | ||
instructions: true, | ||
message: 'Choose a tenant', | ||
choices: queryResult.map((item) => ({ name: item[config.tenantTable.databaseUrlAttributeName], value: item[config.tenantTable.databaseUrlAttributeName], description: item[config.tenantTable.idAttributeName] })) | ||
message: "Choose a tenant", | ||
choices: queryResult.map((item) => ({ | ||
name: item[config.tenantTable.databaseUrlAttributeName], | ||
value: item[config.tenantTable.databaseUrlAttributeName], | ||
description: item[config.tenantTable.idAttributeName] | ||
})) | ||
} | ||
@@ -94,0 +98,0 @@ }); |
@@ -37,6 +37,6 @@ "use strict"; | ||
console.log(`running 'migrate dev' in '${command.mode}' mode !`); | ||
if (command.mode === 'central' || command.mode === 'all') { | ||
if (command.mode === "central" || command.mode === "all") { | ||
console.log(`\nrunning 'migrate dev' for central ...`); | ||
// run 'npx prisma migrate dev --schema=./prisma/central/schema.prisma' | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
const commandArgs = []; | ||
@@ -52,17 +52,17 @@ commandArgs.push(`--schema=${centralSchemaPath}`); | ||
commandArgs.push(`--skip-seed`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'migrate', 'dev', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "migrate", "dev", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(`running 'migrate dev' for central completed π\n`); | ||
} | ||
if (command.mode === 'all' || command.mode === 'tenant') { | ||
if (command.mode === "all" || command.mode === "tenant") { | ||
// choose tenant | ||
const tenantUrls = command.mode !== 'all' && typeof command.tenant === 'string' | ||
const tenantUrls = command.mode !== "all" && typeof command.tenant === "string" | ||
? [command.tenant] | ||
: await chooseTenantPrompt(); | ||
console.log(`\nrunning 'migrate dev' for ${tenantUrls.length} tenants ...\n`); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
for (const url of tenantUrls) { | ||
@@ -80,12 +80,12 @@ // run 'npx prisma migrate dev --schema=./prisma/tenant/schema.prisma' with 'DATABASE_TENANT_URL' as current tenant url | ||
commandArgs.push(`--skip-seed`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'migrate', 'dev', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "migrate", "dev", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env, DATABASE_TENANT_URL: url }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(url, ' completed π'); | ||
console.log(url, " completed π"); | ||
} | ||
} | ||
console.log('\nall done π !!'); | ||
console.log("\nall done π !!"); | ||
}; | ||
@@ -96,4 +96,4 @@ exports.dev = dev; | ||
// TODO: retrieve tenants | ||
const configFilePath = path_1.default.join('.cosmoprism.json'); | ||
const config = JSON.parse(await fs.readFile(configFilePath, 'utf-8')); | ||
const configFilePath = path_1.default.join(".cosmoprism.json"); | ||
const config = JSON.parse(await fs.readFile(configFilePath, "utf-8")); | ||
const sequelize = new sequelize_1.Sequelize(config.centralDatabaseUrl); | ||
@@ -104,7 +104,11 @@ const queryExecution = await sequelize.query(`SELECT * FROM ${config.tenantTable.name};`, { logging: false }); | ||
tenantIds: { | ||
type: 'checkbox', | ||
type: "checkbox", | ||
required: true, | ||
instructions: true, | ||
message: 'Choose a tenant', | ||
choices: queryResult.map((item) => ({ name: item[config.tenantTable.databaseUrlAttributeName], value: item[config.tenantTable.databaseUrlAttributeName], description: item[config.tenantTable.idAttributeName] })) | ||
message: "Choose a tenant", | ||
choices: queryResult.map((item) => ({ | ||
name: item[config.tenantTable.databaseUrlAttributeName], | ||
value: item[config.tenantTable.databaseUrlAttributeName], | ||
description: item[config.tenantTable.idAttributeName] | ||
})) | ||
} | ||
@@ -111,0 +115,0 @@ }); |
@@ -37,6 +37,6 @@ "use strict"; | ||
console.log(`running 'migrate reset' in '${command.mode}' mode !`); | ||
if (command.mode === 'central' || command.mode === 'all') { | ||
if (command.mode === "central" || command.mode === "all") { | ||
console.log(`\nrunning 'migrate reset' for central ...`); | ||
// run 'npx prisma migrate reset --schema=./prisma/central/schema.prisma' | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
const commandArgs = []; | ||
@@ -50,17 +50,17 @@ commandArgs.push(`--schema=${centralSchemaPath}`); | ||
commandArgs.push(`--skip-seed`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'migrate', 'reset', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "migrate", "reset", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(`running 'migrate reset' for central completed π\n`); | ||
} | ||
if (command.mode === 'all' || command.mode === 'tenant') { | ||
if (command.mode === "all" || command.mode === "tenant") { | ||
// choose tenant | ||
const tenantUrls = command.mode !== 'all' && typeof command.tenant === 'string' | ||
const tenantUrls = command.mode !== "all" && typeof command.tenant === "string" | ||
? [command.tenant] | ||
: await chooseTenantPrompt(); | ||
console.log(`\nrunning 'migrate reset' for ${tenantUrls.length} tenants ...\n`); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
for (const url of tenantUrls) { | ||
@@ -76,12 +76,12 @@ // run 'npx prisma migrate reset --schema=./prisma/tenant/schema.prisma' with 'DATABASE_TENANT_URL' as current tenant url | ||
commandArgs.push(`--skip-seed`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'migrate', 'reset', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "migrate", "reset", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env, DATABASE_TENANT_URL: url }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
console.log(url, ' completed π'); | ||
console.log(url, " completed π"); | ||
} | ||
} | ||
console.log('\nall done π !!'); | ||
console.log("\nall done π !!"); | ||
}; | ||
@@ -92,4 +92,4 @@ exports.reset = reset; | ||
// TODO: retrieve tenants | ||
const configFilePath = path_1.default.join('.cosmoprism.json'); | ||
const config = JSON.parse(await fs.readFile(configFilePath, 'utf-8')); | ||
const configFilePath = path_1.default.join(".cosmoprism.json"); | ||
const config = JSON.parse(await fs.readFile(configFilePath, "utf-8")); | ||
const sequelize = new sequelize_1.Sequelize(config.centralDatabaseUrl); | ||
@@ -100,7 +100,11 @@ const queryExecution = await sequelize.query(`SELECT * FROM "${config.tenantTable.name}";`, { logging: false }); | ||
tenantIds: { | ||
type: 'checkbox', | ||
type: "checkbox", | ||
required: true, | ||
instructions: true, | ||
message: 'Choose a tenant', | ||
choices: queryResult.map((item) => ({ name: item[config.tenantTable.databaseUrlAttributeName], value: item[config.tenantTable.databaseUrlAttributeName], description: item[config.tenantTable.idAttributeName] })) | ||
message: "Choose a tenant", | ||
choices: queryResult.map((item) => ({ | ||
name: item[config.tenantTable.databaseUrlAttributeName], | ||
value: item[config.tenantTable.databaseUrlAttributeName], | ||
description: item[config.tenantTable.idAttributeName] | ||
})) | ||
} | ||
@@ -107,0 +111,0 @@ }); |
@@ -36,6 +36,6 @@ "use strict"; | ||
const studio = async (command) => { | ||
if (command.mode === 'central') { | ||
if (command.mode === "central") { | ||
console.log(`\nrunning studio for 'central' ...`); | ||
// run 'npx prisma studio --schema=./prisma/central/schema.prisma' | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
const commandArgs = []; | ||
@@ -47,15 +47,15 @@ commandArgs.push(`--schema=${centralSchemaPath}`); | ||
commandArgs.push(`--port ${command.port}`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'studio', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "studio", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
} | ||
if (command.mode === 'tenant') { | ||
if (command.mode === "tenant") { | ||
// choose tenant | ||
const tenantUrl = typeof command.tenant === 'string' | ||
const tenantUrl = typeof command.tenant === "string" | ||
? command.tenant | ||
: await chooseTenantPrompt(); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
console.log(`\nrunning studio for 'tenant' ...`); | ||
@@ -69,7 +69,7 @@ // run 'npx prisma studio --schema=./prisma/tenant/schema.prisma' with 'DATABASE_TENANT_URL' as current tenant url | ||
commandArgs.push(`--port ${command.port}`); | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'studio', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "studio", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env, DATABASE_TENANT_URL: tenantUrl }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
@@ -83,4 +83,4 @@ } | ||
// TODO: retrieve tenants | ||
const configFilePath = path_1.default.join('.cosmoprism.json'); | ||
const config = JSON.parse(await fs.readFile(configFilePath, 'utf-8')); | ||
const configFilePath = path_1.default.join(".cosmoprism.json"); | ||
const config = JSON.parse(await fs.readFile(configFilePath, "utf-8")); | ||
const sequelize = new sequelize_1.Sequelize(config.centralDatabaseUrl); | ||
@@ -90,8 +90,12 @@ const queryExecution = await sequelize.query(`SELECT * FROM "${config.tenantTable.name}";`, { logging: false }); | ||
const res = await inquirer_1.default.prompt({ | ||
name: 'tenantUrl', | ||
type: 'select', | ||
message: 'Choose a tenant', | ||
choices: queryResult.map((item) => ({ name: item[config.tenantTable.databaseUrlAttributeName], value: item[config.tenantTable.databaseUrlAttributeName], description: item[config.tenantTable.idAttributeName] })) | ||
name: "tenantUrl", | ||
type: "select", | ||
message: "Choose a tenant", | ||
choices: queryResult.map((item) => ({ | ||
name: item[config.tenantTable.databaseUrlAttributeName], | ||
value: item[config.tenantTable.databaseUrlAttributeName], | ||
description: item[config.tenantTable.idAttributeName] | ||
})) | ||
}); | ||
return res.tenantUrl; | ||
}; |
@@ -10,4 +10,4 @@ "use strict"; | ||
const validate = async (command) => { | ||
const centralSchemaPath = path_1.default.join('prisma', 'central', 'schema.prisma'); | ||
const tenantSchemaPath = path_1.default.join('prisma', 'tenant', 'schema.prisma'); | ||
const centralSchemaPath = path_1.default.join("prisma", "central", "schema.prisma"); | ||
const tenantSchemaPath = path_1.default.join("prisma", "tenant", "schema.prisma"); | ||
const schemasToValidate = command.schema | ||
@@ -18,7 +18,7 @@ ? [command.schema] | ||
const commandArgs = [`--schema=${schema}`]; | ||
(0, child_process_1.spawnSync)('npx', ['prisma', 'validate', ...commandArgs], { | ||
(0, child_process_1.spawnSync)("npx", ["prisma", "validate", ...commandArgs], { | ||
shell: true, | ||
stdio: 'inherit', | ||
stdio: "inherit", | ||
env: { ...process.env }, | ||
encoding: 'utf-8', | ||
encoding: "utf-8" | ||
}); | ||
@@ -25,0 +25,0 @@ console.log(`validation passed for schema: ${schema} π\n`); |
{ | ||
"name": "@futura-dev/cosmoprism", | ||
"version": "0.0.1-alpha.1", | ||
"version": "0.0.1-alpha.2", | ||
"private": false, | ||
@@ -35,2 +35,3 @@ "repository": { | ||
"commander": "^12.1.0", | ||
"dotenv": "^16.4.5", | ||
"inquirer": "^12.0.0", | ||
@@ -37,0 +38,0 @@ "mysql2": "^3.11.3", |
{ | ||
"name": "@futura-dev/cosmoprism", | ||
"version": "0.0.1-alpha.1", | ||
"version": "0.0.1-alpha.2", | ||
"private": false, | ||
@@ -35,2 +35,3 @@ "repository": { | ||
"commander": "^12.1.0", | ||
"dotenv": "^16.4.5", | ||
"inquirer": "^12.0.0", | ||
@@ -37,0 +38,0 @@ "mysql2": "^3.11.3", |
52153
3.87%20
5.26%1006
8.17%7
16.67%+ Added
+ Added