Skip to content

Commit

Permalink
sqlite:bun: add suport for crud generation
Browse files Browse the repository at this point in the history
  • Loading branch information
wsporto committed Jul 22, 2024
1 parent 3c00221 commit a17721b
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/sqlite-query-analyzer/code-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,7 @@ function generateCodeFromTsDescriptor(client: SQLiteClient, queryName: string, t
tableName,
tsDescriptor.columns,
idColumn,
queryParams,
client == 'bun:sqlite' ? queryParamsWithoutBrackets : queryParams,
paramsTypeName,
dataTypeName,
resultTypeName,
Expand Down Expand Up @@ -896,6 +896,12 @@ function writeExecutSelectCrudBlock(
writer.indent().write('.raw(true)').newLine();
writer.indent().write(`.all(${queryParams})`).newLine();
writer.indent().write(`.map(data => mapArrayTo${resultTypeName}(data))[0];`);
}
else if (client === 'bun:sqlite') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.values(${queryParams})`).newLine();
writer.indent().write(`.map(data => mapArrayTo${resultTypeName}(data))[0];`);

} else {
writer.write(`return client.execute({ sql, args: ${queryParams} })`).newLine();
writer.indent().write('.then(res => res.rows)').newLine();
Expand All @@ -922,6 +928,9 @@ function writeExecuteInsertCrudBlock(
if (client === 'better-sqlite3') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.run(values) as ${resultTypeName};`);
} else if (client == 'bun:sqlite') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.run(...values) as ${resultTypeName};`);
} else {
writer.write('return client.execute({ sql, args: values })').newLine();
writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`).newLine();
Expand Down Expand Up @@ -949,7 +958,11 @@ function writeExecuteUpdateCrudBlock(
if (client === 'better-sqlite3') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.run(values) as ${resultTypeName};`);
} else {
} else if (client === 'bun:sqlite') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.run(...values) as ${resultTypeName};`);
}
else {
writer.write('return client.execute({ sql, args: values })').newLine();
writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`).newLine();
}
Expand All @@ -971,6 +984,10 @@ function writeExecutDeleteCrudBlock(
if (client === 'better-sqlite3') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.run(${queryParams}) as ${resultTypeName};`).newLine();
}
else if (client === 'bun:sqlite') {
writer.write('return db.prepare(sql)').newLine();
writer.indent().write(`.run(${queryParams}) as ${resultTypeName};`).newLine();
} else {
writer.write(`return client.execute({ sql, args: ${queryParams} })`).newLine();
writer.indent().write(`.then(res => mapArrayTo${resultTypeName}(res));`).newLine();
Expand Down
19 changes: 19 additions & 0 deletions tests/sqlite/expected-code/crud-delete01-bun.ts.txt
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;
}
25 changes: 25 additions & 0 deletions tests/sqlite/expected-code/crud-insert01-bun.ts.txt
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;
}
31 changes: 31 additions & 0 deletions tests/sqlite/expected-code/crud-select01-bun.ts.txt
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;
}
28 changes: 28 additions & 0 deletions tests/sqlite/expected-code/crud-update01-bun.ts.txt
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;
}
26 changes: 26 additions & 0 deletions tests/sqlite/sqlite-code-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ describe('sqlite-code-generator', () => {
assert.deepStrictEqual(actual, expected);
});

it('crud-select01-bun', () => {
const actual = generateCrud('bun:sqlite', 'Select', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-select01-bun.ts.txt', 'utf-8').replace(/\r/gm, '');
assert.deepStrictEqual(actual, expected);
});

it('crud-insert01', () => {
const actual = generateCrud('better-sqlite3', 'Insert', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-insert01.ts.txt', 'utf-8').replace(/\r/gm, '');
Expand All @@ -298,6 +304,12 @@ describe('sqlite-code-generator', () => {
assert.deepStrictEqual(actual, expected);
});

it('crud-insert01-bun', () => {//
const actual = generateCrud('bun:sqlite', 'Insert', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-insert01-bun.ts.txt', 'utf-8').replace(/\r/gm, '');
assert.deepStrictEqual(actual, expected);
});

it('crud-update01', () => {
const actual = generateCrud('better-sqlite3', 'Update', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-update01.ts.txt', 'utf-8').replace(/\r/gm, '');
Expand All @@ -312,6 +324,13 @@ describe('sqlite-code-generator', () => {
assert.deepStrictEqual(actual, expected);
});

it('crud-update01-bun', () => {
const actual = generateCrud('bun:sqlite', 'Update', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-update01-bun.ts.txt', 'utf-8').replace(/\r/gm, '');

assert.deepStrictEqual(actual, expected);
});

it('crud-delete01', () => {
const actual = generateCrud('better-sqlite3', 'Delete', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-delete01.ts.txt', 'utf-8').replace(/\r/gm, '');
Expand All @@ -326,6 +345,13 @@ describe('sqlite-code-generator', () => {
assert.deepStrictEqual(actual, expected);
});

it('crud-delete01-bun', () => {
const actual = generateCrud('bun:sqlite', 'Delete', 'mytable1', sqliteDbSchema);
const expected = readFileSync('tests/sqlite/expected-code/crud-delete01-bun.ts.txt', 'utf-8').replace(/\r/gm, '');

assert.deepStrictEqual(actual, expected);
});

it('select05 - SELECT id FROM mytable1 ORDER BY ?', () => {
const sql = 'SELECT id FROM mytable1 ORDER BY ?';

Expand Down

0 comments on commit a17721b

Please sign in to comment.