forked from LNReader/lnreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Repositories * feat: Repositories * Cleanup * Cleanup * Cleanup
- Loading branch information
1 parent
c77dc52
commit 09f7319
Showing
14 changed files
with
557 additions
and
283 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import * as SQLite from 'expo-sqlite'; | ||
|
||
import { Repository } from '@database/types'; | ||
|
||
import { txnErrorCallback } from '../utils/helpers'; | ||
import { noop } from 'lodash-es'; | ||
|
||
const db = SQLite.openDatabase('lnreader.db'); | ||
|
||
const getRepositoriesQuery = 'SELECT * FROM Repository'; | ||
|
||
export const getRepositoriesFromDb = async (): Promise<Repository[]> => { | ||
return new Promise(resolve => | ||
db.transaction(tx => { | ||
tx.executeSql( | ||
getRepositoriesQuery, | ||
[], | ||
(txObj, { rows }) => resolve((rows as any)._array), | ||
txnErrorCallback, | ||
); | ||
}), | ||
); | ||
}; | ||
|
||
const isRepoUrlDuplicateQuery = ` | ||
SELECT COUNT(*) as isDuplicate FROM Repository WHERE url = ? | ||
`; | ||
|
||
export const isRepoUrlDuplicate = (repoUrl: string): Promise<boolean> => { | ||
return new Promise(resolve => | ||
db.transaction(tx => { | ||
tx.executeSql( | ||
isRepoUrlDuplicateQuery, | ||
[repoUrl], | ||
(txObj, { rows }) => { | ||
const { _array } = rows as any; | ||
resolve(Boolean(_array[0]?.isDuplicate)); | ||
}, | ||
txnErrorCallback, | ||
); | ||
}), | ||
); | ||
}; | ||
|
||
const createRepositoryQuery = 'INSERT INTO Repository (url) VALUES (?)'; | ||
|
||
export const createRepository = (repoUrl: string): void => | ||
db.transaction(tx => | ||
tx.executeSql(createRepositoryQuery, [repoUrl], noop, txnErrorCallback), | ||
); | ||
|
||
const deleteRepositoryQuery = 'DELETE FROM Repository WHERE id = ?'; | ||
|
||
export const deleteRepositoryById = (id: number): void => { | ||
db.transaction(tx => { | ||
tx.executeSql(deleteRepositoryQuery, [id], noop, txnErrorCallback); | ||
}); | ||
}; | ||
|
||
const updateRepositoryQuery = 'UPDATE Repository SET name = ? WHERE id = ?'; | ||
|
||
export const updateRepository = (id: number, url: string): void => | ||
db.transaction(tx => | ||
tx.executeSql(updateRepositoryQuery, [url, id], noop, txnErrorCallback), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export const createRepositoryTableQuery = ` | ||
CREATE TABLE IF NOT EXISTS Repository ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
url TEXT NOT NULL, | ||
UNIQUE(url) | ||
); | ||
`; | ||
|
||
export const insertDefaultRepository = | ||
'INSERT OR REPLACE INTO Repository (id, url) VALUES (1, "https://raw.githubusercontent.com/LNReader/lnreader-plugins/plugins/v2.1.0/.dist/plugins.min.json");'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.