-
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #2500 Change hashtags database to sqlite3
- Loading branch information
Showing
7 changed files
with
83 additions
and
58 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 |
---|---|---|
@@ -1,44 +1,63 @@ | ||
import Datastore from 'nedb' | ||
import sqlite3 from 'sqlite3' | ||
import { LocalTag } from '~/src/types/localTag' | ||
|
||
export default class Hashtags { | ||
private db: Datastore | ||
export const listTags = (db: sqlite3.Database, accountId: number): Promise<Array<LocalTag>> => { | ||
return new Promise((resolve, reject) => { | ||
db.all('SELECT * FROM hashtags WHERE account_id = ?', accountId, (err, rows) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
resolve( | ||
rows.map(r => ({ | ||
id: r.id, | ||
tagName: r.tag, | ||
accountId: r.account_id | ||
})) | ||
) | ||
}) | ||
}) | ||
} | ||
|
||
constructor(db: Datastore) { | ||
this.db = db | ||
this.db.ensureIndex({ fieldName: 'tagName', unique: true }, _ => {}) | ||
} | ||
export const insertTag = (db: sqlite3.Database, accountId: number, tag: string): Promise<LocalTag> => { | ||
return new Promise((resolve, reject) => { | ||
db.serialize(() => { | ||
db.run('BEGIN TRANSACTION') | ||
|
||
listTags(): Promise<Array<LocalTag>> { | ||
return new Promise((resolve, reject) => { | ||
this.db.find<LocalTag>({}, (err, docs) => { | ||
if (err) return reject(err) | ||
resolve(docs) | ||
}) | ||
}) | ||
} | ||
db.get('SELECT * FROM hashtags WHERE id = ? AND tag = ?', [accountId, tag], (err, row) => { | ||
if (err) { | ||
reject(err) | ||
} | ||
if (row) { | ||
resolve({ | ||
id: row.id, | ||
tagName: row.tag, | ||
accountId: row.account_id | ||
}) | ||
} | ||
|
||
insertTag(tag: string): Promise<LocalTag> { | ||
return new Promise((resolve, reject) => { | ||
this.db.insert({ tagName: tag }, (err, doc) => { | ||
if (err) return reject(err) | ||
resolve(doc) | ||
db.run('INSERT INTO hashtags(tag, account_id) VALUES (?, ?)', [accountId, tag], function (err) { | ||
if (err) { | ||
reject(err) | ||
} | ||
db.run('COMMIT') | ||
resolve({ | ||
id: this.lastID, | ||
tagName: tag, | ||
accountId: accountId | ||
}) | ||
}) | ||
}) | ||
}) | ||
} | ||
}) | ||
} | ||
|
||
removeTag(localTag: LocalTag): Promise<number> { | ||
return new Promise((resolve, reject) => { | ||
this.db.remove( | ||
{ | ||
tagName: localTag.tagName | ||
}, | ||
{ multi: true }, | ||
(err, numRemoved) => { | ||
if (err) return reject(err) | ||
resolve(numRemoved) | ||
} | ||
) | ||
export const removeTag = (db: sqlite3.Database, tag: LocalTag): Promise<null> => { | ||
return new Promise((resolve, reject) => { | ||
db.run('DELETE FROM hashtags WHERE id = ?', tag.id, err => { | ||
if (err) { | ||
reject(err) | ||
} | ||
resolve(null) | ||
}) | ||
} | ||
}) | ||
} |
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export type LocalTag = { | ||
id: number | ||
tagName: string | ||
_id?: string | ||
accountId: number | ||
} |