Can you run a custom script that affects SQLite storage in react-native

153 views Asked by At

I am creating an app that will save data on the local phone memory with react-native-sqlite-storage.

I want to make a script that will create a database (if it does not exist), create the necessary tables, and then populate those tables with mock data (basically, a seed script). I plan on adding a script to my package.json to seed this data, something like

{
    ....
    "scripts": {
        ....
        "seed": "ts-node index.ts"
    }
}

Where index.ts will contain the necessary seeding script, something like:

// import react-native-sqlite-storage
const seedDatabase = () => {
    // Populate the data ...
}
seedDatabase();

But I am uncertain if this would work. Will it work? If not, is there a way to seed data?

1

There are 1 answers

0
Артем Мартьянов On
  1. You can use this package to create and populate your database https://www.npmjs.com/package/sqlite3
  2. then just run this to copy it to your device
adb push your_db.db /storage/emulated/0/your_db.db
  1. In your react native code just use that file https://github.com/andpor/react-native-sqlite-storage#importing-a-pre-populated-database