-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlite:bun: add suport for crud generation
- Loading branch information
Showing
6 changed files
with
148 additions
and
2 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,19 @@ | ||
import type { Database } from 'bun:sqlite'; | ||
|
||
export type DeleteFromMytable1Params = { | ||
id: number; | ||
} | ||
|
||
export type DeleteFromMytable1Result = { | ||
changes: number; | ||
} | ||
|
||
export function deleteFromMytable1(db: Database, params: DeleteFromMytable1Params): DeleteFromMytable1Result { | ||
|
||
const sql = `DELETE | ||
FROM mytable1 | ||
WHERE id = ?` | ||
|
||
return db.prepare(sql) | ||
.run(params.id) as DeleteFromMytable1Result; | ||
} |
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,25 @@ | ||
import type { Database } from 'bun:sqlite'; | ||
|
||
export type InsertIntoMytable1Params = { | ||
id?: number; | ||
value?: number | null; | ||
} | ||
|
||
export type InsertIntoMytable1Result = { | ||
changes: number; | ||
lastInsertRowid: number; | ||
} | ||
|
||
export function insertIntoMytable1(db: Database, params: InsertIntoMytable1Params): InsertIntoMytable1Result { | ||
|
||
const keys = Object.keys(params) as Array<keyof InsertIntoMytable1Params>; | ||
const columns = keys.filter(key => params[key] !== undefined); | ||
const values = columns.map(col => params[col]!); | ||
|
||
const sql = columns.length == 0 | ||
? `INSERT INTO mytable1 DEFAULT VALUES` | ||
: `INSERT INTO mytable1(${columns.join(',')}) VALUES(${columns.map(_ => '?').join(',')})` | ||
|
||
return db.prepare(sql) | ||
.run(...values) as InsertIntoMytable1Result; | ||
} |
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,31 @@ | ||
import type { Database } from 'bun:sqlite'; | ||
|
||
export type SelectFromMytable1Params = { | ||
id: number; | ||
} | ||
|
||
export type SelectFromMytable1Result = { | ||
id: number; | ||
value?: number; | ||
} | ||
|
||
export function selectFromMytable1(db: Database, params: SelectFromMytable1Params): SelectFromMytable1Result | null { | ||
|
||
const sql = `SELECT | ||
id, | ||
value | ||
FROM mytable1 | ||
WHERE id = ?` | ||
|
||
return db.prepare(sql) | ||
.values(params.id) | ||
.map(data => mapArrayToSelectFromMytable1Result(data))[0]; | ||
} | ||
|
||
function mapArrayToSelectFromMytable1Result(data: any) { | ||
const result: SelectFromMytable1Result = { | ||
id: data[0], | ||
value: data[1] | ||
} | ||
return result; | ||
} |
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,28 @@ | ||
import type { Database } from 'bun:sqlite'; | ||
|
||
export type UpdateMytable1Data = { | ||
value?: number | null; | ||
} | ||
|
||
export type UpdateMytable1Params = { | ||
id: number; | ||
} | ||
|
||
export type UpdateMytable1Result = { | ||
changes: number; | ||
} | ||
|
||
export function updateMytable1(db: Database, data: UpdateMytable1Data, params: UpdateMytable1Params): UpdateMytable1Result { | ||
|
||
const keys = Object.keys(data) as Array<keyof UpdateMytable1Data>; | ||
const columns = keys.filter(key => data[key] !== undefined); | ||
const values = columns.map(col => data[col]!).concat(params.id); | ||
|
||
const sql = ` | ||
UPDATE mytable1 | ||
SET ${columns.map(col => `${col} = ?`).join(', ')} | ||
WHERE id = ?` | ||
|
||
return db.prepare(sql) | ||
.run(...values) as UpdateMytable1Result; | ||
} |
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