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
{ | |
"formatVersion": 1, | |
"database": { | |
"version": 1, | |
"identityHash": "24b13f284eb850e47c2ef1e44a4d559c", | |
"entities": [ | |
{ | |
"tableName": "cities", | |
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}`...", | |
"fields": [ |
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
async function createDb(): Promise<void> { | |
const db: Database = new Database("db.db"); | |
await populate("app/scr/schemas/1.json", db, function(this: Database): Promise<void> { | |
return new Promise<void>(function(resolve, reject) { | |
this.exec("INSERT INTO `playlists` (id, title, genre) VALUES (1, 'SAMPLE', 'JAZZ')", (err) => { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(); |
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
/** | |
* City population | |
*/ | |
exports.cities = async () => { | |
const options: Args = minimist<Args>( | |
process.argv.slice(2), | |
{ | |
string: ["dataDir"], | |
default: { dataDir: "_data"} | |
} |
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
/** | |
* Takes cities source files and populates SQLite database | |
* @param schemaPath Where the Room schemas are put | |
* @param dataDir Where the data is put | |
* @return A promise for created database | |
*/ | |
export async function cities(schemaPath: string, dataDir: string): Promise<string> { | |
const schemaFile = await findLatestSchema(schemaPath); | |
const timeStarted = Date.now(); |
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); |
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
@Database(entities = [City::class], version = BuildConfig.CITIES_DB_VERSION) | |
@TypeConverters(DataConverters::class) | |
abstract class CitiesDb: RoomDatabase() { | |
/** | |
* Cities DAO | |
*/ | |
abstract fun citiesDao(): CitiesDao | |
} |
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
android { | |
defaultConfig { | |
// Database version that gets updated with import script | |
buildConfigField "int", "CITIES_DB_VERSION", CITIES_DB_VERSION | |
} | |
} |
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
{ | |
"id": 1, | |
"name": "Kitezh", | |
"state": "", | |
"country": "RU", | |
"coord": { | |
"lon": 44.676201, | |
"lat": 56.957775 | |
} | |
} |
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
$ npx gulp cities | |
[11:52:21] Requiring external module ts-node/register | |
[11:52:23] Using gulpfile ~\Work\Projects\room-populate-demo\script\gulpfile.ts | |
[11:52:23] Starting 'cities'... | |
Getting latest schema... | |
Found latest version: 1 | |
Populating Cities database... | |
Populating Cities table... | |
Invalid city: 6255146 | |
Invalid city: 6255147 |
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
/** | |
* Populates Cities | |
*/ | |
async function populateCities(db: Database, cities: Array<City>) { | |
db.serialize(); | |
db.run("begin transaction"); | |
const stmt = db.prepare("REPLACE INTO cities VALUES (?,?,?,?,?,?)"); | |
for (const city of cities) { | |
stmt.run( |