-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite_helper.js
More file actions
401 lines (358 loc) · 10.8 KB
/
Copy pathsqlite_helper.js
File metadata and controls
401 lines (358 loc) · 10.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
import {
knownFolders,
path,
File,
Folder,
Application,
} from "@nativescript/core";
import { openOrCreate } from "@nativescript-community/sqlite";
/*
* @Name : SQLite Helper {N}
* @Version : 3.2
* @Repo : https://github.com/dyazincahya/sqlite-helper-nativescript
* @Author : Kang Cahya (github.com/dyazincahya)
* @Blog : https://www.kang-cahya.com
* ===============================================================================================================
* @References : https://github.com/nativescript-community/sqlite
* ===============================================================================================================
*/
/**
* Configuration database
* @type {Object}
*/
const config = {
databaseName: "YOUR_DATABASE_NAME.db", // set your database name
debug: true, // set false for production and set true for development
paths: {
documentsFolder: knownFolders.documents(),
assetsFolder: "assets/db",
},
};
/**
* Get native database path using platform-specific APIs
* @param {string} dbName
* @returns {string} absolute native path
*/
function getNativeDatabasePath(dbName) {
if (global.android) {
// Native path on Android (usually /data/data/<package>/databases/)
const context =
Application.android.context || Application.android.nativeApp;
const dbFile = context.getDatabasePath(dbName);
return dbFile.getAbsolutePath();
} else if (global.ios) {
// Standard documents folder for iOS
return path.join(knownFolders.documents().path, dbName);
}
// Fallback for other environments
return path.join(config.paths.documentsFolder.path, dbName);
}
/**
* Path database (Resolved during initialization)
* @type {String|null}
*/
let dbPath = null;
/**
* Variable sqlite instance
* @type {any}
*/
let sqlite = null;
/**
* Initialization lock to prevent concurrent opening attempts
* @type {Promise<any>|null}
*/
let initPromise = null;
/**
* Initialize database with singleton pattern and lock
* @async
* @function initializeDatabase
* @returns {Promise<any>} sqlite instance
*/
async function initializeDatabase() {
if (sqlite) return sqlite;
// If initialization is already in progress, wait for it
if (initPromise) return initPromise;
initInitPromise();
return initPromise;
}
function initInitPromise() {
initPromise = (async () => {
if (
!config.databaseName ||
config.databaseName === "YOUR_DATABASE_NAME.db"
) {
console.warn("SQLite: Database name is not defined or empty.");
return null;
}
try {
// Lazy resolve the native database path
if (!dbPath) {
dbPath = getNativeDatabasePath(config.databaseName);
}
const isFileDbExists = File.exists(dbPath);
if (!isFileDbExists) {
// Find seed database in assets
const assetsPath = knownFolders
.currentApp()
.getFolder(config.paths.assetsFolder).path;
const seedPath = path.join(assetsPath, config.databaseName);
if (File.exists(seedPath)) {
if (config.debug)
console.log(`SQLite: Seeding database from assets to ${dbPath}...`);
// Ensure destination folder exists (critical for Android /databases/ folder)
const destinationDir = path.dirname(dbPath);
if (!Folder.exists(destinationDir)) {
Folder.fromPath(destinationDir);
}
const seedFile = File.fromPath(seedPath);
await seedFile.copy(dbPath);
if (config.debug) console.log("SQLite: Seed copy successful.");
} else {
if (config.debug)
console.log(
"SQLite: No seed database found in assets. Starting with empty DB.",
);
}
} else {
if (config.debug)
console.log("SQLite: Existing database found, skipping seed.");
}
// Open database using absolute native path
sqlite = openOrCreate(dbPath);
if (config.debug)
console.log("SQLite: Database opened at native location:", dbPath);
return sqlite;
} catch (error) {
console.error("SQLite: Initialization error >> ", error);
throw error;
} finally {
initPromise = null;
}
})();
}
/*
* MAIN CRUD FUNCTIONS
* --------------------------------
*/
/**
* Perform a SELECT query
* @param {string} table - table name
* @param {string} fields - fields name (default: "*")
* @param {string} conditionalQuery - conditional query (default: "")
* @returns {Promise<any[]>} - array of records
*/
export async function SQL__select(table, fields = "*", conditionalQuery = "") {
await initializeDatabase();
if (!sqlite) return [];
const query = `SELECT ${fields} FROM ${table} ${conditionalQuery}`.trim();
try {
return await sqlite.select(query);
} catch (error) {
if (config.debug) console.error("SQL__select error >> ", error);
return [];
}
}
/**
* Perform a raw SELECT query
* @param {string} query - SQL query
* @returns {Promise<any[]>}
*/
export async function SQL__selectRaw(query) {
if (!query) return [];
await initializeDatabase();
if (!sqlite) return [];
try {
return await sqlite.select(query);
} catch (error) {
if (config.debug) console.error("SQL__selectRaw error >> ", error);
return [];
}
}
/**
* Perform an INSERT (Multiple rows supported via Transactions for bulk)
* @param {string} table - table name
* @param {any|any[]} data - object {field, value} array (legacy) or items (bulk)
* @returns {Promise<void>}
*/
export async function SQL__insert(table, data = []) {
await initializeDatabase();
if (!sqlite || !data) return;
try {
// Current legacy format: [{field: 'name', value: 'val'}, ...]
// If it's this format, we insert one row.
if (
Array.isArray(data) &&
data.length > 0 &&
typeof data[0].field === "string"
) {
const fields = data.map((item) => item.field).join(", ");
const holders = data.map(() => "?").join(", ");
const values = data.map((item) => item.value);
const query = `INSERT INTO ${table} (${fields}) VALUES (${holders})`;
await sqlite.execute(query, values);
} else if (Array.isArray(data)) {
// Potentially many rows of legacy format or objects
// For now, let's stick to consistent legacy if possible or use transaction
await SQL__transaction(async (db) => {
for (const entry of data) {
const fields = entry.map((item) => item.field).join(", ");
const holders = entry.map(() => "?").join(", ");
const values = entry.map((item) => item.value);
await db.execute(
`INSERT INTO ${table} (${fields}) VALUES (${holders})`,
values,
);
}
});
}
} catch (error) {
if (config.debug) console.error("SQL__insert error >> ", error);
}
}
/**
* Perform an UPDATE
* @param {string} table - table name
* @param {any[]} data - array of {field, value}
* @param {number|string} id - primary key ID (optional if conditionalQuery provided)
* @param {string} conditionalQuery - optional WHERE clause
* @returns {Promise<void>}
*/
export async function SQL__update(
table,
data = [],
id = null,
conditionalQuery = "",
) {
await initializeDatabase();
if (!sqlite || !data.length) return;
const dataSet = data.map((item) => `${item.field} = ?`).join(", ");
const values = data.map((item) => item.value);
const where = id ? `WHERE id = ${id}` : conditionalQuery;
const query = `UPDATE ${table} SET ${dataSet} ${where}`.trim();
try {
await sqlite.execute(query, values);
} catch (error) {
if (config.debug) console.error("SQL__update error >> ", error);
}
}
/**
* Delete records
* @param {string} table
* @param {number|string} id
* @param {string} conditionalQuery
* @returns {Promise<void>}
*/
export async function SQL__delete(table, id = null, conditionalQuery = "") {
await initializeDatabase();
if (!sqlite) return;
const where = id ? `WHERE id = ${id}` : conditionalQuery;
const query = `DELETE FROM ${table} ${where}`.trim();
try {
await sqlite.execute(query);
} catch (error) {
if (config.debug) console.error("SQL__delete error >> ", error);
}
}
/**
* Truncate table and vacuum
* @param {string} table
*/
export async function SQL__truncate(table) {
await initializeDatabase();
if (!sqlite) return;
try {
await sqlite.execute(`DELETE FROM ${table}`);
await sqlite.execute("VACUUM");
} catch (error) {
if (config.debug) console.error("SQL__truncate error >> ", error);
}
}
/**
* Drop Table
* @param {string} table
* @param {boolean} ifExist
*/
export async function SQL__dropTable(table, ifExist = false) {
await initializeDatabase();
if (!sqlite) return;
const query = ifExist
? `DROP TABLE IF EXISTS ${table}`
: `DROP TABLE ${table}`;
try {
await sqlite.execute(query);
} catch (error) {
if (config.debug) console.error("SQL__dropTable error >> ", error);
}
}
/**
* Execute custom query
* @param {string} query
* @param {any[]} values
* @returns {Promise<any>}
*/
export async function SQL__query(query, values = []) {
await initializeDatabase();
if (!sqlite) return;
try {
return await sqlite.execute(query, values);
} catch (error) {
if (config.debug) console.error("SQL__query error >> ", error);
}
}
/**
* TRANSACTION: For heavy bulk operations
* @param {function} callback - async function(db) returning operations
* @returns {Promise<any>}
*/
export async function SQL__transaction(callback) {
await initializeDatabase();
if (!sqlite) return;
try {
return await sqlite.transaction(async (cancel) => {
return await callback(sqlite, cancel);
});
} catch (error) {
if (config.debug) console.error("SQL__transaction error >> ", error);
throw error;
}
}
/**
* Execute Batch: Fast execution of multiple queries
* @param {any[]} queries - array of [query, params] or string query
*/
export async function SQL__executeBatch(queries) {
await initializeDatabase();
if (!sqlite) return;
try {
// Check plugin support for executeBatch or fallback to transaction
if (typeof sqlite.executeBatch === "function") {
return await sqlite.executeBatch(queries);
} else {
return await SQL__transaction(async (db) => {
for (const q of queries) {
if (Array.isArray(q)) {
await db.execute(q[0], q[1] || []);
} else {
await db.execute(q);
}
}
});
}
} catch (error) {
if (config.debug) console.error("SQL__executeBatch error >> ", error);
}
}
/**
* Close database connection
*/
export async function SQL__close() {
if (sqlite) {
try {
sqlite.close();
sqlite = null;
if (config.debug) console.log("SQLite: Database closed.");
} catch (error) {
console.error("SQLite: Close error >> ", error);
}
}
}