Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 | 1x 1x 23x 7x 7x 2x 5x 8x 5x 2x 2x 2x 7x 33x 1x 33x 9x 9x 3x 6x 6x 2x 4x 5x 2x 3x 3x 1x 2x 2x 10x 3x 14x 7x 7x 7x | /**
* Driver Registry
*
* Manages multiple driver delegates for Rebase backend.
* Allows different databases for different collections.
*
* Usage:
* - Single DB: Pass a single DataDriver → maps to "(default)"
* - Multiple DBs: Pass a map of { dbId: DataDriver }
* - Collections use `databaseId` property to specify which driver to use
* - Collections without `databaseId` fallback to "(default)"
*/
import { DataDriver } from "@rebasepro/types";
/**
* The default driver identifier used when:
* - A single driver is provided (not a map)
* - A collection doesn't specify a databaseId
*/
export const DEFAULT_DRIVER_ID = "(default)";
/**
* Registry for managing multiple driver delegates
*/
export interface DriverRegistry {
/**
* Register a driver delegate with an ID
* @param id - Unique identifier for this driver (e.g., "analytics", "users")
* @param delegate - The DataDriver instance
*/
register(id: string, delegate: DataDriver): void;
/**
* Get the default driver delegate (id = "(default)")
* @throws Error if no default driver is registered
*/
getDefault(): DataDriver;
/**
* Get a driver delegate by ID
* @param id - Driver identifier, or undefined/null for default
* @returns The DataDriver, or undefined if not found
*/
get(id: string | undefined | null): DataDriver | undefined;
/**
* Get a driver delegate by ID, with fallback to default
* @param id - Driver identifier, or undefined/null for default
* @returns The DataDriver (falls back to default if id not found)
* @throws Error if neither the specified nor default driver exists
*/
getOrDefault(id: string | undefined | null): DataDriver;
/**
* Check if a driver with the given ID exists
*/
has(id: string): boolean;
/**
* List all registered driver IDs
*/
list(): string[];
/**
* Get the number of registered drivers
*/
size(): number;
}
/**
* Default implementation of DriverRegistry
*/
export class DefaultDriverRegistry implements DriverRegistry {
private delegates = new Map<string, DataDriver>();
/**
* Create a DriverRegistry from either a single delegate or a map
* @param input - Single DataDriver (maps to "(default)") or Record<string, DataDriver>
*/
static create(
input: DataDriver | Record<string, DataDriver>
): DefaultDriverRegistry {
const registry = new DefaultDriverRegistry();
if (isDataDriverDelegate(input)) {
// Single delegate → register as "(default)"
registry.register(DEFAULT_DRIVER_ID, input);
} else {
// Map of delegates → register each
for (const [id, delegate] of Object.entries(input)) {
registry.register(id, delegate);
}
// Ensure there's a default if not explicitly provided
if (!registry.has(DEFAULT_DRIVER_ID) && registry.size() > 0) {
// If no explicit "(default)", use the first one as default
const firstId = Object.keys(input)[0];
console.warn(
`[DriverRegistry] No "${DEFAULT_DRIVER_ID}" driver provided. ` +
`Using "${firstId}" as the default.`
);
registry.register(DEFAULT_DRIVER_ID, input[firstId]);
}
}
return registry;
}
register(id: string, delegate: DataDriver): void {
if (this.delegates.has(id)) {
console.warn(`[DriverRegistry] Overwriting driver with id "${id}"`);
}
this.delegates.set(id, delegate);
}
getDefault(): DataDriver {
const delegate = this.delegates.get(DEFAULT_DRIVER_ID);
if (!delegate) {
throw new Error(
`[DriverRegistry] No default driver registered. ` +
`Register one with id "${DEFAULT_DRIVER_ID}" or pass a single DataDriver.`
);
}
return delegate;
}
get(id: string | undefined | null): DataDriver | undefined {
if (id === undefined || id === null) {
return this.delegates.get(DEFAULT_DRIVER_ID);
}
return this.delegates.get(id);
}
getOrDefault(id: string | undefined | null): DataDriver {
// If no ID specified, return default
if (id === undefined || id === null) {
return this.getDefault();
}
// Try to get by ID
const delegate = this.delegates.get(id);
if (delegate) {
return delegate;
}
// Fallback to default with warning
console.warn(
`[DriverRegistry] Driver "${id}" not found, falling back to "${DEFAULT_DRIVER_ID}"`
);
return this.getDefault();
}
has(id: string): boolean {
return this.delegates.has(id);
}
list(): string[] {
return Array.from(this.delegates.keys());
}
size(): number {
return this.delegates.size;
}
}
/**
* Type guard to check if an object is a DataDriver
*/
function isDataDriverDelegate(obj: unknown): obj is DataDriver {
Iif (typeof obj !== "object" || obj === null) {
return false;
}
const delegate = obj as DataDriver;
// Check for required DataDriver properties
return (
typeof delegate.key === "string" &&
typeof delegate.fetchCollection === "function" &&
typeof delegate.fetchEntity === "function" &&
typeof delegate.saveEntity === "function" &&
typeof delegate.deleteEntity === "function"
);
}
|