@deepbrainspace/nx-surrealdb
Version:
NX plugin for SurrealDB migrations with modular architecture
156 lines (152 loc) ⢠5.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = default_1;
const tslib_1 = require("tslib");
const devkit_1 = require("@nx/devkit");
const path = tslib_1.__importStar(require("path"));
async function default_1(tree, options) {
const { name } = options;
// Auto-install required dependencies
const packageJson = tree.read('package.json');
if (packageJson) {
const pkg = JSON.parse(packageJson.toString());
const requiredDeps = {
surrealdb: '^1.3.2',
dotenv: '^16.5.0',
picocolors: '^1.1.1',
};
let depsToAdd = false;
pkg.dependencies = pkg.dependencies || {};
for (const [dep, version] of Object.entries(requiredDeps)) {
if (!pkg.dependencies[dep]) {
pkg.dependencies[dep] = version;
depsToAdd = true;
}
}
if (depsToAdd) {
tree.write('package.json', JSON.stringify(pkg, null, 2));
console.log(`
š¦ Installing required dependencies:
- surrealdb
- dotenv
- picocolors
`);
}
}
// Set defaults using the same pattern as the library
const config = {
name,
url: options.url || 'ws://localhost:8000/rpc',
namespace: options.namespace || 'development',
database: options.database || 'main',
user: options.user || 'root',
pass: options.pass || 'root',
};
// Create the initial module configuration and write it to config.json
const moduleConfig = {
modules: {
'000_admin': {
name: 'System Administration',
description: 'Core database setup and administrative functions',
dependencies: [],
locked: true,
lockReason: 'Critical system module - contains core admin setup and permissions',
},
'010_auth': {
name: 'Authentication & Users',
description: 'User authentication and authorization system',
dependencies: ['000_admin'],
},
'020_schema': {
name: 'Application Schema',
description: 'Core application data models and relationships',
dependencies: ['010_auth'],
},
},
settings: {
configFormat: 'json',
useTransactions: true,
defaultNamespace: config.namespace,
defaultDatabase: config.database,
},
};
// Generate the database project structure
(0, devkit_1.generateFiles)(tree, path.join(__dirname, 'files'), name, {
...config,
template: '',
moduleConfig: JSON.stringify(moduleConfig, null, 2),
});
// Add NX project configuration
const projectConfig = {
name,
root: name,
targets: {
migrate: {
executor: '@deepbrainspace/nx-surrealdb:migrate',
options: {
url: '${SURREALDB_URL}',
user: '${SURREALDB_ROOT_USER}',
pass: '${SURREALDB_ROOT_PASS}',
namespace: '${SURREALDB_NAMESPACE}',
database: '${SURREALDB_DATABASE}',
initPath: name,
},
},
rollback: {
executor: '@deepbrainspace/nx-surrealdb:rollback',
options: {
url: '${SURREALDB_URL}',
user: '${SURREALDB_ROOT_USER}',
pass: '${SURREALDB_ROOT_PASS}',
namespace: '${SURREALDB_NAMESPACE}',
database: '${SURREALDB_DATABASE}',
initPath: name,
},
},
status: {
executor: '@deepbrainspace/nx-surrealdb:status',
options: {
url: '${SURREALDB_URL}',
user: '${SURREALDB_ROOT_USER}',
pass: '${SURREALDB_ROOT_PASS}',
namespace: '${SURREALDB_NAMESPACE}',
database: '${SURREALDB_DATABASE}',
initPath: name,
},
},
reset: {
executor: '@deepbrainspace/nx-surrealdb:reset',
options: {
url: '${SURREALDB_URL}',
user: '${SURREALDB_ROOT_USER}',
pass: '${SURREALDB_ROOT_PASS}',
namespace: '${SURREALDB_NAMESPACE}',
database: '${SURREALDB_DATABASE}',
},
},
},
};
(0, devkit_1.addProjectConfiguration)(tree, name, projectConfig);
await (0, devkit_1.formatFiles)(tree);
// Log helpful next steps
console.log(`
ā
Database project '${name}' created successfully!
š Next steps:
1. Set up your environment variables in .env:
- SURREALDB_URL=ws://localhost:8000/rpc
- SURREALDB_ROOT_USER=root
- SURREALDB_ROOT_PASS=root
- SURREALDB_NAMESPACE=${config.namespace}
- SURREALDB_DATABASE=${config.database}
2. Review and customize the starter migrations:
- ${name}/000_admin/0001_setup_*.surql (System setup)
- ${name}/010_auth/0001_users_*.surql (User authentication)
- ${name}/020_schema/0001_tables_*.surql (Application schema)
3. Uncomment and customize the migration code, then run:
nx run ${name}:migrate
For more info, see ${name}/README.md
`);
// Auto-install packages
return (0, devkit_1.installPackagesTask)(tree);
}
//# sourceMappingURL=generator.js.map