Skip to content

Instantly share code, notes, and snippets.

@motorro
Last active March 13, 2020 06:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motorro/30ff68f0fe0a0abc63b25ecc8ab37516 to your computer and use it in GitHub Desktop.
Save motorro/30ff68f0fe0a0abc63b25ecc8ab37516 to your computer and use it in GitHub Desktop.
Database building template function
/**
* 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