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.
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 — however, this method requires a backend connector parameter. See the next section, Integrating With Your Backend for details on that.db.connect(connector: currentConnector);
To instantiate
PowerSyncDatabase
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 { PowerSyncDatabase } from '@journeyapps/powersync-sdk-react-native';
import { RNQSPowerSyncDatabaseOpenFactory } from '@journeyapps/powersync-react-native-quick-sqlite-adapter';
const factory = new RNQSPowerSyncDatabaseOpenFactory({
schema: AppSchema, // Which was created earlier
dbFilename: 'test.sqlite'
});
export const PowerSync = factory.getInstance();
Once you've instantiated your
PowerSyncDatabase
, you will need to call the connect()
method in order to activate it — however, this method requires a backend connector parameter. See the next section, Integrating With Your Backend for details on that.export const setupPowerSync = async () => {
const connector = new Connector();
await PowerSync.init();
await PowerSync.connect(connector);
}
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
PowerSyncDatabase
, you will need to call the connect()
method in order to activate it — however, this method requires a backend connector parameter. See the next section, Integrating With Your Backend for details on that.export const setupPowerSync = async () => {
const connector = new Connector();
await PowerSync.init();
await PowerSync.connect(connector);
}
Last modified 13d ago