Last active
March 13, 2020 06:30
-
-
Save motorro/30ff68f0fe0a0abc63b25ecc8ab37516 to your computer and use it in GitHub Desktop.
Database building template function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A template function that initializes and pre-populates a database with Room schema to use in Android application. | |
* @param schemaPath A path to schema being created | |
* @param db A fresh database | |
* @param populate The function that executes database inserts | |
*/ | |
export async function populate(schemaPath: string, db: Database, populate: (this: Database) => Promise<void>) { | |
// Create a context that holds parsed schema definition (Step 1) | |
const creator = new RoomDbCreator(await readSchema(schemaPath), db); | |
db.serialize(); | |
// Step 2. Runs 'setupQueries' | |
await creator.setup(); | |
// Step 3. Creates tables | |
await creator.createTables(); | |
// Step 4. Yeilds database creation to 'populate' function | |
await creator.populate(populate); | |
// Step 5. Creates indicies | |
await creator.createIndices(); | |
// Step 6. Creates views | |
await creator.createViews(); | |
db.parallelize(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment