import { AbstractPowerSyncDatabase, SqliteBucketStorage } from '@powersync/common';
import { NodeRemote } from '../sync/stream/NodeRemote.js';
import { NodeStreamingSyncImplementation } from '../sync/stream/NodeStreamingSyncImplementation.js';
import { WorkerConnectionPool } from './WorkerConnectionPool.js';
/**
 * A PowerSync database which provides SQLite functionality
 * which is automatically synced.
 *
 * @example
 * ```typescript
 * export const db = new PowerSyncDatabase({
 *  schema: AppSchema,
 *  database: {
 *    dbFilename: 'example.db'
 *  }
 * });
 * ```
 */
export class PowerSyncDatabase extends AbstractPowerSyncDatabase {
    constructor(options) {
        super(options);
    }
    async _initialize() {
        await this.database.initialize();
    }
    /**
     * Opens a DBAdapter using better-sqlite3 as the default SQLite open factory.
     */
    openDBAdapter(options) {
        return new WorkerConnectionPool(options.database);
    }
    generateBucketStorageAdapter() {
        return new SqliteBucketStorage(this.database, this.logger);
    }
    connect(connector, options) {
        return super.connect(connector, options);
    }
    generateSyncStreamImplementation(connector, options) {
        const logger = this.logger;
        const remote = new NodeRemote(connector, logger, {
            dispatcher: options.dispatcher,
            ...this.options.remoteOptions
        });
        return new NodeStreamingSyncImplementation({
            adapter: this.bucketStorageAdapter,
            remote,
            uploadCrud: async () => {
                await this.waitForReady();
                await connector.uploadData(this);
            },
            ...options,
            identifier: this.database.name,
            logger
        });
    }
}
//# sourceMappingURL=PowerSyncDatabase.js.map