-
-
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.
change db client to better-sqlite3 on api mode for fixing error with …
…fix parallel requests
- Loading branch information
Showing
4 changed files
with
2,166 additions
and
1,583 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,59 @@ | ||
import Database from "better-sqlite3" | ||
import { join } from "path" | ||
|
||
const database = process.env.DATABASE_URL | ||
if (database === undefined) throw new Error() | ||
const databasePath = | ||
process.env.NODE_ENV !== "production" | ||
? join(process.cwd(), "db", database.replace("file:", "")) | ||
: database.replace("file:", "") | ||
|
||
const db = new Database(databasePath, { fileMustExist: true }) | ||
|
||
const getProjectIdByPreparedStatement = db.prepare("SELECT id FROM Project WHERE basePath = ?") | ||
const getProjectIdBy = (basePath: string): number | null => { | ||
const row = getProjectIdByPreparedStatement.get(basePath) | ||
return row !== undefined ? row.id : null | ||
} | ||
|
||
type StubTableType = { | ||
id: number | ||
createdAt: string | ||
updatedAt: string | ||
createdBy: string | ||
updatedBy: string | ||
path: string | ||
method: string | ||
contentType: string | ||
statusCode: string | ||
response: string | ||
sleep: number | ||
logs: string | ||
ntimesError: number | ||
ntimesErrorStatusCode: string | ||
ntimesErrorCounter: number | ||
memo: string | ||
projectId: number | ||
} | ||
|
||
const getStubsByPreparedStatement = db.prepare( | ||
"SELECT * FROM Stub WHERE projectId = ? AND path = ?" | ||
) | ||
const getStubsBy = (projectId: number, path: string): StubTableType[] => { | ||
return getStubsByPreparedStatement.all(projectId, path) | ||
} | ||
|
||
const updateStubWithPreparedStatement = db.prepare( | ||
"UPDATE Stub SET logs = ?, ntimesErrorCounter = ? WHERE id = ?" | ||
) | ||
const updateStubBy = (id: number) => { | ||
const updateStubWith = (logs: string, ntimesErrorCounter: number) => { | ||
return new Promise((resolve) => { | ||
const info = updateStubWithPreparedStatement.run(logs, ntimesErrorCounter, id) | ||
resolve(info.changes) | ||
}) | ||
} | ||
return { updateStubWith } | ||
} | ||
|
||
export { getProjectIdBy, getStubsBy, updateStubBy } |
Oops, something went wrong.