@@ -5,13 +5,37 @@ var jsstoreCon = new JsStore.Connection(new Worker("scripts/jsstore.worker.js"))
55window . onload = function ( ) {
66 refreshTableData ( ) ;
77 registerEvents ( ) ;
8- initDb ( ) ;
8+ initDb ( getDbSchema ( ) ) ;
99} ;
1010
11- async function initDb ( ) {
12- var isDbCreated = await jsstoreCon . initDb ( getDbSchema ( ) ) ;
11+ async function initDb ( dbSchema ) {
12+ var isDbCreated = await jsstoreCon . initDb ( dbSchema ) ;
1313 if ( isDbCreated ) {
14+ localStorage . setItem ( "db_version" , dbSchema . version ) ;
15+
1416 console . log ( 'db created' ) ;
17+ if ( dbSchema . version === 2 ) {
18+ var datas = await jsstoreCon . select ( {
19+ from : "Student"
20+ } ) ;
21+ datas . forEach ( data => {
22+ const gender = data . gender ;
23+ if ( ! gender ) {
24+ data . gender = 0 ;
25+ }
26+ else {
27+ data . gender = gender === "male" ? 1 : 2 ;
28+ }
29+ } )
30+
31+ await jsstoreCon . insert ( {
32+ into : "Student" ,
33+ values : datas ,
34+ upsert : true
35+ } )
36+ refreshTableData ( ) ;
37+ console . log ( "data type updated" ) ;
38+ }
1539 }
1640 else {
1741 console . log ( 'db opened' ) ;
@@ -41,13 +65,25 @@ function getDbSchema() {
4165 city : {
4266 dataType : 'string' ,
4367 notNull : true
68+ } ,
69+ } ,
70+ alter : {
71+ // 2 is database version to target
72+ 2 : {
73+ modify : {
74+ gender : {
75+ //1 means male, 2 means female, 0 means unknown
76+ dataType : "number"
77+ }
78+ } ,
4479 }
4580 }
4681 }
47-
82+ const dbVersion = localStorage . getItem ( "db_version" ) ;
4883 var db = {
4984 name : 'My-Db' ,
50- tables : [ table ]
85+ tables : [ table ] ,
86+ version : dbVersion ? Number ( dbVersion ) : null
5187 }
5288 return db ;
5389}
@@ -198,107 +234,10 @@ function refreshFormData(student) {
198234 $ ( '#txtCity' ) . val ( student . city ) ;
199235}
200236
201- function getDbSchemaV2 ( ) {
202- var table = {
203- name : 'Student' ,
204- columns : {
205- id : {
206- primaryKey : true ,
207- autoIncrement : true
208- } ,
209- name : {
210- notNull : true ,
211- dataType : 'string'
212- } ,
213- gender : {
214- dataType : 'string' ,
215- default : 'male'
216- } ,
217- country : {
218- notNull : true ,
219- dataType : 'string'
220- } ,
221- city : {
222- dataType : 'string' ,
223- notNull : true
224- }
225- } ,
226- version : 2
227- }
228-
229- var db = {
230- name : 'My-Db' ,
231- tables : [ table ]
232- }
233- return db ;
234- }
235-
236- async function changeDbSchemaToV2AndRestoreUsingMemory ( ) {
237- var allData = await jsstoreCon . select ( {
238- from : 'Student'
239- } ) ;
240-
241- var isDbCreated = await jsstoreCon . initDb ( getDbSchemaV2 ( ) ) ;
242- if ( isDbCreated ) {
243- await jsstoreCon . insert ( {
244- into : 'Student' ,
245- values : allData
246- } )
247- alert ( "data successfully inserted from v1 to v2" )
248- }
249- }
250-
251- function getDbSchemaV2WithAnotherTable ( ) {
252- var table = {
253- name : 'Student' ,
254- columns : {
255- id : {
256- primaryKey : true ,
257- autoIncrement : true
258- } ,
259- name : {
260- notNull : true ,
261- dataType : 'string'
262- } ,
263- gender : {
264- dataType : 'string' ,
265- default : 'male'
266- } ,
267- country : {
268- notNull : true ,
269- dataType : 'string'
270- } ,
271- city : {
272- dataType : 'string' ,
273- notNull : true
274- }
275- }
276- } ;
277237
278- var newTable = Object . assign ( { } , table ) ;
279- newTable . name = "StudentV2" ;
280- newTable . version = 2
281238
282- var db = {
283- name : 'My-Db' ,
284- tables : [ table , newTable ]
285- }
286- return db ;
239+ function changeToV2 ( ) {
240+ const db = getDbSchema ( ) ;
241+ db . version = 2 ;
242+ initDb ( db ) ;
287243}
288-
289- async function changeDbSchemaToV2AndRestoreWithAnotherTable ( ) {
290-
291-
292- var isDbCreated = await jsstoreCon . initDb ( getDbSchemaV2WithAnotherTable ( ) ) ;
293- if ( isDbCreated ) {
294-
295- var allData = await jsstoreCon . select ( {
296- from : 'Student'
297- } ) ;
298- await jsstoreCon . insert ( {
299- into : 'StudentV2' ,
300- values : allData
301- } )
302- alert ( "data successfully inserted from v1 to v2" )
303- }
304- }
0 commit comments