feat(server): add tenant management, health checks, and database export/import APIs
This commit is contained in:
@@ -8,6 +8,15 @@ import type {
|
||||
ICollectionInfo,
|
||||
IDocumentsResult,
|
||||
ISmartDbMetrics,
|
||||
ISmartDbHealth,
|
||||
ISmartDbDatabaseTenantInput,
|
||||
ISmartDbDeleteDatabaseTenantInput,
|
||||
ISmartDbRotateDatabaseTenantPasswordInput,
|
||||
ISmartDbDatabaseTenantDescriptor,
|
||||
ISmartDbDeleteDatabaseTenantResult,
|
||||
ISmartDbDatabaseExport,
|
||||
ISmartDbImportDatabaseInput,
|
||||
ISmartDbImportDatabaseResult,
|
||||
} from '../rust-db-bridge.js';
|
||||
|
||||
/**
|
||||
@@ -204,6 +213,85 @@ export class SmartdbServer {
|
||||
return this.options.host ?? '127.0.0.1';
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an isolated database/user pair for an application tenant.
|
||||
*/
|
||||
async createDatabaseTenant(
|
||||
params: ISmartDbDatabaseTenantInput,
|
||||
): Promise<ISmartDbDatabaseTenantDescriptor> {
|
||||
const descriptor = await this.bridge.createDatabaseTenant(params);
|
||||
return this.withTenantMongoUri(descriptor, params.password);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a tenant database and its tenant user(s).
|
||||
*/
|
||||
async deleteDatabaseTenant(
|
||||
params: ISmartDbDeleteDatabaseTenantInput,
|
||||
): Promise<ISmartDbDeleteDatabaseTenantResult> {
|
||||
return this.bridge.deleteDatabaseTenant(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Rotate a tenant user's password without restarting the server.
|
||||
*/
|
||||
async rotateDatabaseTenantPassword(
|
||||
params: ISmartDbRotateDatabaseTenantPasswordInput,
|
||||
): Promise<ISmartDbDatabaseTenantDescriptor> {
|
||||
const descriptor = await this.bridge.rotateDatabaseTenantPassword(params);
|
||||
return this.withTenantMongoUri(descriptor, params.password);
|
||||
}
|
||||
|
||||
/**
|
||||
* List known database tenants.
|
||||
*/
|
||||
async listDatabaseTenants(): Promise<ISmartDbDatabaseTenantDescriptor[]> {
|
||||
return this.bridge.listDatabaseTenants();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a tenant descriptor without exposing a password.
|
||||
*/
|
||||
async getDatabaseTenantDescriptor(params: {
|
||||
databaseName: string;
|
||||
username: string;
|
||||
}): Promise<ISmartDbDatabaseTenantDescriptor> {
|
||||
return this.bridge.getDatabaseTenantDescriptor(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export one database as an Extended JSON snapshot.
|
||||
*/
|
||||
async exportDatabase(params: { databaseName: string }): Promise<ISmartDbDatabaseExport> {
|
||||
return this.bridge.exportDatabase(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Replace one database with a previously exported snapshot.
|
||||
*/
|
||||
async importDatabase(params: ISmartDbImportDatabaseInput): Promise<ISmartDbImportDatabaseResult> {
|
||||
return this.bridge.importDatabase(params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get readiness/health details for long-running service use.
|
||||
*/
|
||||
async getHealth(): Promise<ISmartDbHealth> {
|
||||
if (!this.isRunning) {
|
||||
return {
|
||||
running: false,
|
||||
storage: this.options.storage,
|
||||
storagePath: this.options.storage === 'file' ? this.options.storagePath : this.options.persistPath,
|
||||
authEnabled: Boolean(this.options.auth?.enabled),
|
||||
authUsers: this.options.auth?.users?.length ?? 0,
|
||||
usersPathConfigured: Boolean(this.options.auth?.usersPath),
|
||||
databaseCount: 0,
|
||||
collectionCount: 0,
|
||||
};
|
||||
}
|
||||
return this.bridge.getHealth();
|
||||
}
|
||||
|
||||
// --- OpLog / Debug API ---
|
||||
|
||||
/**
|
||||
@@ -258,4 +346,26 @@ export class SmartdbServer {
|
||||
async getMetrics(): Promise<ISmartDbMetrics> {
|
||||
return this.bridge.getMetrics();
|
||||
}
|
||||
|
||||
private withTenantMongoUri(
|
||||
descriptor: ISmartDbDatabaseTenantDescriptor,
|
||||
password: string,
|
||||
): ISmartDbDatabaseTenantDescriptor {
|
||||
return {
|
||||
...descriptor,
|
||||
mongodbUri: this.buildTenantMongoUri(descriptor.databaseName, descriptor.username, password),
|
||||
};
|
||||
}
|
||||
|
||||
private buildTenantMongoUri(databaseName: string, username: string, password: string): string {
|
||||
const host = this.options.socketPath
|
||||
? encodeURIComponent(this.options.socketPath)
|
||||
: `${this.options.host ?? '127.0.0.1'}:${this.options.port ?? 27017}`;
|
||||
const auth = `${encodeURIComponent(username)}:${encodeURIComponent(password)}@`;
|
||||
const query = new URLSearchParams({ authSource: databaseName });
|
||||
if (this.options.tls?.enabled) {
|
||||
query.set('tls', 'true');
|
||||
}
|
||||
return `mongodb://${auth}${host}/${encodeURIComponent(databaseName)}?${query.toString()}`;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user