Skip to content

Commit be79f52

Browse files
db schema example to version - 4.2.1
1 parent 95304df commit be79f52

File tree

6 files changed

+1200
-1079
lines changed

6 files changed

+1200
-1079
lines changed

change-db-schema/index.html

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
<div class="col-xs-12 col-sm-10 col-centered ">
2020
<div style="text-align:center;">
2121
<button id="btnAddStudent" class="btn btn-success">
22-
<i class="fa fa-plus" aria-hidden="true"></i> Add Student</button>
22+
<i class="fa fa-plus" aria-hidden="true"></i> Add Student
23+
</button>
24+
<button class="btn btn-primary" onclick="changeToV2()">
25+
Change db to version 2
26+
</button>
2327
</div>
2428
<table id="tblGrid" class="table table-hover ">
2529
<caption
@@ -79,14 +83,6 @@
7983
</div>
8084
</form>
8185
</div>
82-
83-
<div class="col-centered">
84-
<button class="btn btn-primary" onclick="changeDbSchemaToV2AndRestoreUsingMemory()">Change Schema and
85-
restore
86-
data using memory</button>
87-
<button class="btn" onclick="changeDbSchemaToV2AndRestoreWithAnotherTable()">Change Schema and restore data
88-
without memory</button>
89-
</div>
9086
</div>
9187

9288
<link href="style/main.css " rel="stylesheet " type="text/css " />

change-db-schema/scripts/index.js

Lines changed: 45 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@ var jsstoreCon = new JsStore.Connection(new Worker("scripts/jsstore.worker.js"))
55
window.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

Comments
 (0)