-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSchema.js
More file actions
124 lines (107 loc) · 3.8 KB
/
Schema.js
File metadata and controls
124 lines (107 loc) · 3.8 KB
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
import SQLite from 'react-native-sqlite-storage';
import { toSqlField } from './utils/fields';
let _databaseName = new WeakMap();
let _version = new WeakMap();
let _description = new WeakMap();
let _size = new WeakMap();
let _databaseInstance = new WeakMap();
export class Schema {
constructor(props = {}) {
// SQLite configuration
SQLite.DEBUG(props.debug || false);
SQLite.enablePromise(true);
if (!props.databaseName) {
throw new Error('Database name is required.');
}
// Database configuration
_databaseName.set(this, props.databaseName);
_version.set(this, props.version || '1.0');
_description.set(this, props.description || `${ _databaseName.get(this) }; Version: ${ _version.get(this) }`);
_size.set(this, props.size || -1);
_databaseInstance.set(this, null);
this.open = this.open.bind(this);
this.createTable = this.createTable.bind(this);
}
/**
* Opens (if already exist) or creates (if does not exist) database
*/
async open() {
try {
const openDbRes = await SQLite.openDatabase(
_databaseName.get(this),
_version.get(this),
_description.get(this),
_size.get(this)
);
_databaseInstance.set(this, openDbRes);
return Promise.resolve({
statusCode: 200,
message: 'Database opened successfully.',
data: openDbRes
});
} catch (err) {
console.log('createDatabase error:', err);
return Promise.reject({
statusCode: 500,
message: 'Unable to open database.'
})
}
}
/**
* Creates new table via model
*
* @param {Model} model
*/
createTable(model) {
return new Promise(async (resolve, reject) => {
// Add default timestamps
const fields = {
...model.getModelFields(),
created_at: 'string',
updated_at: 'string',
deleted_at: 'string'
};
let sqlFieldFormat = '';
// Format to SQL field
Object.keys(fields).forEach((fieldVal, fieldIndex) => {
sqlFieldFormat += `${ fieldVal } ${toSqlField(fields[fieldVal])}`
+ (
fieldIndex === Object.keys(fields).length - 1
? ''
: ', '
);
});
try {
await (_databaseInstance.get(this)).transaction(async (tx) => {
try {
// Create table
await tx.executeSql('CREATE TABLE IF NOT EXISTS '
+ model.getModelName()
+ '(' + sqlFieldFormat + ');'
);
} catch (err) {
console.log('Table creation error:', err);
return reject({
statusCode: 500,
message: 'Table creation error.'
});
}
});
} catch (err) {
console.log('Database transaction error (createTable):', err);
return reject({
statusCode: 500,
message: 'An error occurred.'
});
}
return resolve({
statusCode: 200,
message: 'Table successfully created',
data: {
modelName: model.getModelName(),
fields
}
});
});
}
}