react-sqlite-hook
Advanced tools
Comparing version 0.0.1-alpha.1 to 0.0.1-alpha.2
@@ -15,3 +15,6 @@ //Inspired from https://github.com/capacitor-community/react-hooks/blob/master/src/storage/ | ||
platform === "electron" ? CCSPlugin.CapacitorSQLiteElectron : CCSPlugin.CapacitorSQLite; | ||
if (!availableFeatures.useSQLite) { | ||
const availableFeaturesN = { | ||
useSQLite: isFeatureAvailable('CapacitorSQLite', 'useSQLite') | ||
}; | ||
if (!availableFeaturesN.useSQLite) { | ||
return Object.assign({ openDB: featureNotAvailableError, createSyncTable: featureNotAvailableError, close: featureNotAvailableError, execute: featureNotAvailableError, executeSet: featureNotAvailableError, run: featureNotAvailableError, query: featureNotAvailableError, isDBExists: featureNotAvailableError, deleteDB: featureNotAvailableError, isJsonValid: featureNotAvailableError, importFromJson: featureNotAvailableError, exportToJson: featureNotAvailableError, setSyncDate: featureNotAvailableError }, notAvailable); | ||
@@ -134,3 +137,3 @@ } | ||
if (r) { | ||
if (typeof r.changes != 'undefined') { | ||
if (typeof r.values != 'undefined') { | ||
return r; | ||
@@ -137,0 +140,0 @@ } |
declare const featureMap: { | ||
CapacitorSQLite: { | ||
open: { | ||
web: boolean; | ||
ios: boolean; | ||
android: boolean; | ||
electron: boolean; | ||
}; | ||
useSQLite: { | ||
@@ -4,0 +10,0 @@ web: boolean; |
@@ -11,2 +11,3 @@ import { Capacitor } from '@capacitor/core'; | ||
CapacitorSQLite: { | ||
open: Object.assign(Object.assign({}, allTrue), { web: false }), | ||
useSQLite: Object.assign(Object.assign({}, allTrue), { web: false }), | ||
@@ -16,3 +17,3 @@ } | ||
export function isFeatureAvailable(plugin, method) { | ||
if (Capacitor.isPluginAvailable(plugin) && !!featureMap[plugin][method][Capacitor.platform]) { | ||
if (Capacitor.isPluginAvailable(plugin) && !!featureMap[plugin][method][Capacitor.getPlatform()]) { | ||
return true; | ||
@@ -19,0 +20,0 @@ } |
{ | ||
"name": "react-sqlite-hook", | ||
"version": "0.0.1-alpha.1", | ||
"version": "0.0.1-alpha.2", | ||
"description": "React Hook for @Capacitor-community/sqlite plugin", | ||
@@ -5,0 +5,0 @@ "repository": { |
203
README.md
@@ -28,2 +28,203 @@ # React Hook for @Capacitor-community/sqlite | ||
To come ... | ||
```js | ||
import React, { useState, useEffect} from 'react'; | ||
import { IonContent, IonHeader, IonPage, IonTitle, IonToolbar, IonButton, IonList, IonItem, IonLabel } from '@ionic/react'; | ||
import './Tab2.css'; | ||
import { useSQLite } from 'react-sqlite-hook/dist'; | ||
import { createTablesNoEncryption, importTwoUsers, importThreeMessages, | ||
dropTablesTablesNoEncryption } from '../Utils/utils-db-no-encryption'; | ||
const Tab2: React.FC = () => { | ||
const [log, setLog] = useState<string[]>([]); | ||
const [start, setStart] = useState(0); | ||
const startTest = () => { | ||
setStart(prev => prev + 1); | ||
} | ||
const {openDB, createSyncTable, close, execute, executeSet, run, query, | ||
isDBExists, deleteDB, isJsonValid, importFromJson, exportToJson, setSyncDate} = useSQLite(); | ||
useEffect( () => { | ||
async function testDatabaseNoEncryption(): Promise<Boolean> { | ||
setLog((log) => log.concat("*** Starting testDatabaseNoEncryption ***\n")); | ||
let result: any = await openDB("test-sqlite"); | ||
if(result.result) { | ||
setLog((log) => log.concat("Database 'test-sqlite' Opened\n")); | ||
// Drop tables if exists | ||
result = await execute(dropTablesTablesNoEncryption); | ||
console.log("execute1 ",result) | ||
if(result.changes.changes !== 0 && result.changes.changes !== 1) { | ||
setLog((log) => log.concat("Execute2 failed\n")); | ||
return false; | ||
} | ||
// Create tables | ||
result = await execute(createTablesNoEncryption); | ||
console.log("execute2 ",result) | ||
if(result.changes.changes !== 0 && result.changes.changes !== 1) { | ||
setLog((log) => log.concat("Execute2 failed\n")); | ||
return false; | ||
} | ||
// Insert two users with execute method | ||
result = await execute(importTwoUsers); | ||
console.log("execute3 ",result) | ||
if(result.changes.changes !== 2) { | ||
setLog((log) => log.concat("Execute3 failed\n")); | ||
return false; | ||
} | ||
// Select all Users | ||
result = await query("SELECT * FROM users"); | ||
console.log('result.values.length ',result.values.length) | ||
console.log('result.values[0].name ',result.values[0].name) | ||
console.log('result.values[1].name ',result.values[1].name) | ||
if(result.values.length !== 2 || | ||
result.values[0].name !== "Whiteley" || result.values[1].name !== "Jones") { | ||
setLog((log) => log.concat("Select1 failed\n")); | ||
return false; | ||
} | ||
// add one user with statement and values | ||
let sqlcmd = "INSERT INTO users (name,email,age) VALUES (?,?,?)"; | ||
let values: Array<any> = ["Simpson","Simpson@example.com",69]; | ||
result = await run(sqlcmd,values); | ||
if(result.changes.changes !== 1 ||result.changes.lastId !== 3) { | ||
setLog((log) => log.concat("Run1 failed\n")); | ||
return false; | ||
} | ||
// add one user with statement | ||
sqlcmd = `INSERT INTO users (name,email,age) VALUES ("Brown","Brown@example.com",15)`; | ||
result = await run(sqlcmd); | ||
if(result.changes.changes !== 1 || result.changes.lastId !== 4) { | ||
setLog((log) => log.concat("Run2 failed\n")); | ||
return false; | ||
} | ||
// Select all Users | ||
result = await query("SELECT * FROM users"); | ||
if(result.values.length !== 4) { | ||
setLog((log) => log.concat("Select2 failed\n")); | ||
return false; | ||
} | ||
// Select Users with age > 35 | ||
sqlcmd = "SELECT name,email,age FROM users WHERE age > ?"; | ||
values = ["35"]; | ||
result = await query(sqlcmd,values); | ||
if(result.values.length !== 2) { | ||
setLog((log) => log.concat("Select with filter on age failed\n")); | ||
return false; | ||
} | ||
// Import three messages | ||
result = await execute(importThreeMessages); | ||
if(result.changes.changes !== 3) { | ||
setLog((log) => log.concat("Insert messages failed\n")); | ||
return false; | ||
} | ||
// Select all Messages | ||
result = await query("SELECT * FROM messages"); | ||
if(result.values.length !== 3 || | ||
result.values[0].title !== "test post 1" || result.values[1].title !== "test post 2" | ||
|| result.values[2].title !== "test post 3") { | ||
setLog((log) => log.concat("Select messages failed\n")); | ||
return false; | ||
} | ||
// Close the Database | ||
result = await close("test-sqlite") | ||
if(!result.result) { | ||
setLog((log) => log.concat("Failed to close the database\n")); | ||
return false; | ||
} | ||
} | ||
setLog((log) => log.concat("*** Ending testDatabaseNoEncryption ***\n")); | ||
return true; | ||
} | ||
if(start > 0) { | ||
testDatabaseNoEncryption().then(res => { | ||
if(res) { | ||
setLog((log) => log.concat("*** The set of tests was successful ***\n")); | ||
} else { | ||
setLog((log) => log.concat("*** The set of tests failed ***\n")); | ||
} | ||
}); | ||
} | ||
}, [openDB, createSyncTable, close, execute, executeSet, run, query, isDBExists, deleteDB, | ||
isJsonValid, importFromJson, exportToJson, setSyncDate, start]); | ||
return ( | ||
<IonPage> | ||
<IonHeader> | ||
<IonToolbar> | ||
<IonTitle>Tab Two</IonTitle> | ||
</IonToolbar> | ||
</IonHeader> | ||
<IonContent fullscreen> | ||
<IonHeader collapse="condense"> | ||
<IonToolbar> | ||
<IonTitle size="large">Tab 2</IonTitle> | ||
</IonToolbar> | ||
</IonHeader> | ||
<IonList> | ||
<IonItem routerLink="/tab2/details"> | ||
<IonLabel> | ||
<h2>Go to detail</h2> | ||
</IonLabel> | ||
</IonItem> | ||
<IonItem> | ||
<IonButton onClick={startTest} expand="block">SQLite Test</IonButton> | ||
</IonItem> | ||
</IonList> | ||
<pre> | ||
<p>{log}</p> | ||
</pre> | ||
</IonContent> | ||
</IonPage> | ||
); | ||
}; | ||
export default Tab2; | ||
``` | ||
with `../Utils/utils-db-no-encryption` | ||
```js | ||
export const createTablesNoEncryption: string = ` | ||
BEGIN TRANSACTION; | ||
CREATE TABLE IF NOT EXISTS users ( | ||
id INTEGER PRIMARY KEY NOT NULL, | ||
email TEXT UNIQUE NOT NULL, | ||
name TEXT, | ||
company TEXT, | ||
size FLOAT, | ||
age INTEGER | ||
); | ||
CREATE TABLE IF NOT EXISTS messages ( | ||
id INTEGER PRIMARY KEY NOT NULL, | ||
userid INTEGER, | ||
title TEXT NOT NULL, | ||
body TEXT NOT NULL, | ||
FOREIGN KEY (userid) REFERENCES users(id) ON DELETE SET DEFAULT | ||
); | ||
PRAGMA user_version = 1; | ||
COMMIT TRANSACTION; | ||
`; | ||
export const importTwoUsers: string = ` | ||
BEGIN TRANSACTION; | ||
DELETE FROM users; | ||
INSERT INTO users (name,email,age) VALUES ("Whiteley","Whiteley.com",30); | ||
INSERT INTO users (name,email,age) VALUES ("Jones","Jones.com",44); | ||
COMMIT TRANSACTION; | ||
`; | ||
export const importThreeMessages: string = ` | ||
BEGIN TRANSACTION; | ||
DELETE FROM messages; | ||
INSERT INTO messages (userid,title,body) VALUES (1,"test post 1","content test post 1"); | ||
INSERT INTO messages (userid,title,body) VALUES (2,"test post 2","content test post 2"); | ||
INSERT INTO messages (userid,title,body) VALUES (1,"test post 3","content test post 3"); | ||
COMMIT TRANSACTION; | ||
`; | ||
export const dropTablesTablesNoEncryption: string = ` | ||
PRAGMA foreign_keys = OFF; | ||
DROP TABLE IF EXISTS users; | ||
DROP TABLE IF EXISTS messages; | ||
PRAGMA foreign_keys = ON; | ||
`; | ||
``` | ||
@@ -173,3 +173,3 @@ //Inspired from https://github.com/capacitor-community/react-hooks/blob/master/src/storage/ | ||
if(r) { | ||
if( typeof r.changes != 'undefined') { | ||
if( typeof r.values != 'undefined') { | ||
return r; | ||
@@ -176,0 +176,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
54482
24
863
230