/
DatabaseInitialization.ts
127 lines (112 loc) · 4.19 KB
/
DatabaseInitialization.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/**
* React Native SQLite Demo
* Copyright (c) 2018 Bruce Lefebvre <bruce@brucelefebvre.com>
* https://github.com/blefebvre/react-native-sqlite-demo/blob/master/LICENSE
*/
import SQLite from "react-native-sqlite-storage";
export class DatabaseInitialization {
// Perform any updates to the database schema. These can occur during initial configuration, or after an app store update.
// This should be called each time the database is opened.
public updateDatabaseTables(database: SQLite.SQLiteDatabase): Promise<void> {
let dbVersion: number = 0;
console.log("Beginning database updates...");
// First: create tables if they do not already exist
return database
.transaction(this.createTables)
.then(() => {
// Get the current database version
return this.getDatabaseVersion(database);
})
.then(version => {
dbVersion = version;
console.log("Current database version is: " + dbVersion);
// Perform DB updates based on this version
// This is included as an example of how you make database schema changes once the app has been shipped
if (dbVersion < 1) {
// Uncomment the next line, and the referenced function below, to enable this
// return database.transaction(this.preVersion1Inserts);
}
// otherwise,
return;
})
.then(() => {
if (dbVersion < 2) {
// Uncomment the next line, and the referenced function below, to enable this
// return database.transaction(this.preVersion2Inserts);
}
// otherwise,
return;
});
}
// Perform initial setup of the database tables
private createTables(transaction: SQLite.Transaction) {
// DANGER! For dev only
const dropAllTables = false;
if (dropAllTables) {
transaction.executeSql("DROP TABLE IF EXISTS List;");
transaction.executeSql("DROP TABLE IF EXISTS ListItem;");
transaction.executeSql("DROP TABLE IF EXISTS Version;");
}
// List table
transaction.executeSql(`
CREATE TABLE IF NOT EXISTS List(
list_id INTEGER PRIMARY KEY NOT NULL,
title TEXT
);
`);
// ListItem table
transaction.executeSql(`
CREATE TABLE IF NOT EXISTS ListItem(
item_id INTEGER PRIMARY KEY NOT NULL,
list_id INTEGER,
text TEXT,
done INTEGER DEFAULT 0,
FOREIGN KEY ( list_id ) REFERENCES List ( list_id )
);
`);
// Version table
transaction.executeSql(`
CREATE TABLE IF NOT EXISTS Version(
version_id INTEGER PRIMARY KEY NOT NULL,
version INTEGER
);
`);
}
// Get the version of the database, as specified in the Version table
private getDatabaseVersion(database: SQLite.SQLiteDatabase): Promise<number> {
// Select the highest version number from the version table
return database
.executeSql("SELECT version FROM Version ORDER BY version DESC LIMIT 1;")
.then(([results]) => {
if (results.rows && results.rows.length > 0) {
const version = results.rows.item(0).version;
return version;
} else {
return 0;
}
})
.catch(error => {
console.log(`No version set. Returning 0. Details: ${error}`);
return 0;
});
}
// Once the app has shipped, use the following functions as a template for updating the database:
/*
// This function should be called when the version of the db is < 1
private preVersion1Inserts(transaction: SQLite.Transaction) {
console.log("Running pre-version 1 DB inserts");
// Make schema changes
transaction.executeSql("ALTER TABLE ...");
// Lastly, update the database version
transaction.executeSql("INSERT INTO Version (version) VALUES (1);");
}
// This function should be called when the version of the db is < 2
private preVersion2Inserts(transaction: SQLite.Transaction) {
console.log("Running pre-version 2 DB inserts");
// Make schema changes
transaction.executeSql("ALTER TABLE ...");
// Lastly, update the database version
transaction.executeSql("INSERT INTO Version (version) VALUES (2);");
}
*/
}