2026-03-26 10:27:05 +00:00
|
|
|
import { DataSource } from 'typeorm';
|
|
|
|
|
import { ormconfig } from '../ormconfig';
|
|
|
|
|
import { User } from '../src/entities/user.entity';
|
|
|
|
|
import { Organization } from '../src/entities/organization.entity';
|
|
|
|
|
import { OrganizationUser } from '../src/entities/organization_user.entity';
|
|
|
|
|
import { SSOType, ConfigScope } from '../src/entities/sso_config.entity';
|
|
|
|
|
import { AppEnvironment } from '../src/entities/app_environments.entity';
|
|
|
|
|
import { GroupPermissions } from '../src/entities/group_permissions.entity';
|
|
|
|
|
import { GroupUsers } from '../src/entities/group_users.entity';
|
|
|
|
|
import { Metadata } from '../src/entities/metadata.entity';
|
|
|
|
|
import { defaultAppEnvironments } from '../src/helpers/utils.helper';
|
|
|
|
|
import { OnboardingStatus } from '../src/modules/onboarding/constants';
|
2026-03-26 10:46:09 +00:00
|
|
|
import { DEFAULT_GROUP_PERMISSIONS } from '../src/modules/group-permissions/constants';
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
|
|
|
|
|
const SEED_DEFAULTS = {
|
2026-03-26 10:27:05 +00:00
|
|
|
email: 'dev@tooljet.io',
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
password: 'password',
|
2026-03-26 10:27:05 +00:00
|
|
|
firstName: 'The',
|
|
|
|
|
lastName: 'Developer',
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
workspaceName: 'My workspace',
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function getSeedConfig() {
|
|
|
|
|
return {
|
|
|
|
|
email: process.env.SEED_EMAIL || SEED_DEFAULTS.email,
|
|
|
|
|
password: process.env.SEED_PASSWORD || SEED_DEFAULTS.password,
|
2026-03-26 10:27:05 +00:00
|
|
|
firstName: process.env.SEED_FIRST_NAME || SEED_DEFAULTS.firstName,
|
|
|
|
|
lastName: process.env.SEED_LAST_NAME || SEED_DEFAULTS.lastName,
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
workspaceName: process.env.SEED_WORKSPACE || SEED_DEFAULTS.workspaceName,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function bootstrap() {
|
|
|
|
|
const config = getSeedConfig();
|
|
|
|
|
|
2026-03-26 10:27:05 +00:00
|
|
|
const dataSource = new DataSource({
|
|
|
|
|
...(ormconfig as any),
|
|
|
|
|
entities: [__dirname + '/../src/**/*.entity{.js,.ts}', __dirname + '/../ee/**/*.entity{.js,.ts}'],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await dataSource.initialize();
|
|
|
|
|
console.log('Database connected.');
|
|
|
|
|
|
|
|
|
|
const manager = dataSource.manager;
|
|
|
|
|
|
|
|
|
|
// Check if already seeded
|
|
|
|
|
const existingUser = await manager.findOne(User, { where: { email: config.email } });
|
|
|
|
|
if (existingUser) {
|
|
|
|
|
console.log('Database already seeded. Skipping.');
|
|
|
|
|
await dataSource.destroy();
|
|
|
|
|
process.exit(0);
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 10:27:05 +00:00
|
|
|
await manager.transaction(async (txManager) => {
|
|
|
|
|
// 1. Create organization with SSO config
|
|
|
|
|
const organization = txManager.create(Organization, {
|
|
|
|
|
name: config.workspaceName,
|
|
|
|
|
slug: config.workspaceName.toLowerCase().replace(/\s+/g, '-'),
|
|
|
|
|
isDefault: true,
|
|
|
|
|
ssoConfigs: [
|
|
|
|
|
{
|
|
|
|
|
enabled: true,
|
|
|
|
|
sso: SSOType.FORM,
|
|
|
|
|
configScope: ConfigScope.ORGANIZATION,
|
|
|
|
|
},
|
|
|
|
|
],
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
});
|
2026-03-26 10:27:05 +00:00
|
|
|
await txManager.save(organization);
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
|
2026-03-26 10:27:05 +00:00
|
|
|
// 2. Create super admin user
|
|
|
|
|
const user = txManager.create(User, {
|
|
|
|
|
firstName: config.firstName,
|
|
|
|
|
lastName: config.lastName,
|
|
|
|
|
email: config.email,
|
|
|
|
|
password: config.password,
|
|
|
|
|
defaultOrganizationId: organization.id,
|
|
|
|
|
status: 'active',
|
|
|
|
|
source: 'signup',
|
|
|
|
|
userType: 'instance',
|
|
|
|
|
onboardingStatus: OnboardingStatus.ONBOARDING_COMPLETED,
|
|
|
|
|
});
|
|
|
|
|
await txManager.save(user);
|
|
|
|
|
|
|
|
|
|
// 3. Create organization-user mapping
|
|
|
|
|
const organizationUser = txManager.create(OrganizationUser, {
|
|
|
|
|
organizationId: organization.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
role: 'all_users',
|
|
|
|
|
status: 'active',
|
|
|
|
|
source: 'signup',
|
|
|
|
|
});
|
|
|
|
|
await txManager.save(organizationUser);
|
|
|
|
|
|
|
|
|
|
// 4. Create default app environments
|
|
|
|
|
for (const env of defaultAppEnvironments) {
|
|
|
|
|
const appEnv = txManager.create(AppEnvironment, {
|
|
|
|
|
organizationId: organization.id,
|
|
|
|
|
name: env.name,
|
|
|
|
|
isDefault: env.isDefault,
|
|
|
|
|
priority: env.priority,
|
|
|
|
|
});
|
|
|
|
|
await txManager.save(appEnv);
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
2026-03-26 10:27:05 +00:00
|
|
|
// 5. Create default permission groups (admin, builder, end_user)
|
|
|
|
|
for (const groupKey of Object.keys(DEFAULT_GROUP_PERMISSIONS)) {
|
|
|
|
|
const groupDef = DEFAULT_GROUP_PERMISSIONS[groupKey];
|
|
|
|
|
const group = txManager.create(GroupPermissions, {
|
2026-03-26 10:46:09 +00:00
|
|
|
...(groupDef as any),
|
2026-03-26 10:27:05 +00:00
|
|
|
organizationId: organization.id,
|
|
|
|
|
});
|
|
|
|
|
await txManager.save(group);
|
|
|
|
|
|
|
|
|
|
// Add user to admin group
|
|
|
|
|
if (groupDef.name === 'admin') {
|
|
|
|
|
const groupUser = txManager.create(GroupUsers, {
|
|
|
|
|
groupId: group.id,
|
|
|
|
|
userId: user.id,
|
|
|
|
|
});
|
|
|
|
|
await txManager.save(groupUser);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6. Mark metadata as onboarded so frontend skips /setup entirely
|
|
|
|
|
const [metadata] = await txManager.find(Metadata);
|
|
|
|
|
if (metadata) {
|
|
|
|
|
metadata.data = { ...metadata.data, onboarded: true };
|
|
|
|
|
await txManager.save(metadata);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(
|
|
|
|
|
'Seeding complete. Use default credentials to login.\n' + `email: ${config.email}\n` + `password: ${config.password}`
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await dataSource.destroy();
|
|
|
|
|
process.exit(0);
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bootstrap().catch((error) => {
|
2026-03-26 10:27:05 +00:00
|
|
|
console.error('Seeding failed:', error);
|
Chore: db:seed script for first user setup (#15684)
* Chore: db:seed script for first user setup
Replaces the stale commented-out seed script with a working HTTP-based
implementation that calls POST /api/onboarding/setup-super-admin — the
exact same endpoint QA uses via curl. This ensures the seed creates all
required database records (org, user, roles, environments, groups,
themes, default data sources, instance settings) without duplicating
any business logic.
Supports env var overrides (SEED_EMAIL, SEED_PASSWORD, SEED_NAME,
SEED_WORKSPACE) for CI/preview deployments. Idempotent — skips if
users already exist.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: use got instead of raw http, improve re-seed message
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: trim verbose logs from seed script
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
* Chore: remove redundant comment
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
---------
2026-03-25 11:46:18 +00:00
|
|
|
process.exit(1);
|
|
|
|
|
});
|