Links

Instantiate PowerSync Database

You need to instantiate the PowerSync database — this is the core managed database.
Its primary functions are to record all changes in the local database, whether online or offline. In addition, it automatically uploads changes to your app backend when connected.

Flutter

To instantiate PowerSyncDatabase, inject the Schema you defined in the previous step and a file path — it's important to only instantiate one instance of PowerSyncDatabase per file.
// Open the local database
db = PowerSyncDatabase(
schema: schema,
path: join(getApplicationSupportDirectory().path, 'powersync-demo.db')
);
await db.initialize();
Once you've instantiated your PowerSyncDatabase, you will need to call the connect() method in order to activate it. This method requires the backend connector that was created in Integrating With Your Backend as a parameter.
db.connect(connector: currentConnector);

React Native & Expo

To instantiate the PowerSync database using RNQSPowerSyncDatabaseOpenFactory, inject the schema you defined in the previous step and a file path — it's important to only instantiate one instance per file.
import { AbstractPowerSyncDatabase, RNQSPowerSyncDatabaseOpenFactory } from '@journeyapps/powersync-sdk-react-native';
const factory = new RNQSPowerSyncDatabaseOpenFactory({
schema: AppSchema, // Which was created earlier
dbFilename: 'test.sqlite'
});
export const PowerSync = factory.getInstance();
Once you've instantiated your PowerSync database, you will need to call the connect() method in order to activate it. This method requires the backend connector that was created in Integrating With Your Backend as a parameter.
export const setupPowerSync = async () => {
const connector = new Connector();
await PowerSync.init();
await PowerSync.connect(connector);
}

JS Web

To instantiate PowerSyncDatabase using WASQLitePowerSyncDatabaseOpenFactory, inject the schema you defined in the previous step and a file path — it's important to only instantiate one instance per file.
import { WASQLitePowerSyncDatabaseOpenFactory } from '@journeyapps/powersync-sdk-web'
const factory = new WASQLitePowerSyncDatabaseOpenFactory({
schema: AppSchema, // Which was created earlier
dbFilename: 'test.sqlite'
});
export const PowerSync = factory.getInstance();
Once you've instantiated your PowerSync database, you will need to call the connect() method in order to activate it. This method requires the backend connector that was created in Integrating With Your Backend as a parameter.
export const setupPowerSync = async () => {
const connector = new Connector();
await PowerSync.init();
await PowerSync.connect(connector);
}

More Examples

For additional implementation examples, see Example / Demo Apps.