Skip to content

Instantly share code, notes, and snippets.

View motorro's full-sized avatar

Nikolai Kotchetkov motorro

View GitHub Profile
@motorro
motorro / prepopulate-City.kt
Last active March 11, 2020 18:52
City entity definition
/**
* City entity
*/
@Entity(
tableName = "cities",
indices = [
Index(name = "city_name", value = ["name"])
]
)
data class City(
@motorro
motorro / prepopulate-enable-schema.groovy
Last active January 11, 2021 11:26
Enabling Room schema export
android {
defaultConfig {
javaCompileOptions {
annotationProcessorOptions {
arguments += [
"room.schemaLocation": "$projectDir/schemas".toString()
]
}
}
}
@motorro
motorro / prepopulate-db-schema.json
Created March 12, 2020 10:04
Exported Room schema
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "24b13f284eb850e47c2ef1e44a4d559c",
"entities": [
{
"tableName": "cities",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}`...",
"fields": [
@motorro
motorro / prepopulate-populate.ts
Last active March 13, 2020 06:30
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);
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();
@motorro
motorro / prepopulate-MainActivityModel.kt
Created March 12, 2020 15:40
New database instance
val db = Room
.databaseBuilder(getApplication(), CitiesDb::class.java, "cities.db")
.createFromAsset("databases/cities.db")
.fallbackToDestructiveMigration() // On version update - just copy over
.build()
@motorro
motorro / prepopulate-task.ts
Created March 12, 2020 16:12
Pre-populating gulp task
/**
* City population
*/
exports.cities = async () => {
const options: Args = minimist<Args>(
process.argv.slice(2),
{
string: ["dataDir"],
default: { dataDir: "_data"}
}
@motorro
motorro / prepopulate-cities.ts
Created March 12, 2020 16:23
Cities database creation script
/**
* 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();
@motorro
motorro / prepopulate-CitiesDb.kt
Created March 13, 2020 07:51
Database definition
@Database(entities = [City::class], version = BuildConfig.CITIES_DB_VERSION)
@TypeConverters(DataConverters::class)
abstract class CitiesDb: RoomDatabase() {
/**
* Cities DAO
*/
abstract fun citiesDao(): CitiesDao
}
@motorro
motorro / prepopulate-database-version.groovy
Created March 13, 2020 07:57
Database version constant
android {
defaultConfig {
// Database version that gets updated with import script
buildConfigField "int", "CITIES_DB_VERSION", CITIES_DB_VERSION
}
}