Skip to content

Commit

Permalink
Build
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Mar 23, 2023
1 parent 86eca9c commit caac7ea
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 10 deletions.
6 changes: 5 additions & 1 deletion lib/example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ const onlyUsernameAdmins = db.tables.users.find({
});
// everything except username and the default attributes (id, createdAt, updatedAt and functions) will be undefined
onlyUsernameAdmins[0].username;
db.tables.users.save({
id: 1,
username: "newAdmin",
});
db.tables.users.delete({
username: "admin",
username: "newAdmin",
});
6 changes: 3 additions & 3 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ type SortOut<I, E> = Pick<I, {
type Or<A, TA, B, TB> = A extends TA ? true : B extends TB ? true : false;
interface _TableFunctions<TT extends Tables, T extends Tables[string]> {
create: (cols: _Columns<TT, T, true>) => AddTableFx<T, _Columns<TT, T, true>>;
save: (cols: {
id?: number;
} & _Columns<TT, T, true>) => void;
save: (cols: _Columns<TT, T, true> | ({
id: number;
} & Partial<_Columns<TT, T, true>>)) => AddTableFx<T, AddColumnDefaults<_Columns<TT, T, true>>>[];
delete: (opts: Partial<_Columns<TT, T, true>>) => void;
find: <S extends (keyof T["columns"])[] | undefined, R extends Narrow<(keyof SortOut<T["columns"], {
type: "REL";
Expand Down
15 changes: 9 additions & 6 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,21 @@ export class BunORM {
.filter(([_, v]) => typeof v !== "function")
.map(([col, val]) => [
col,
Table.columns[col].type === "JSON" ? JSON.stringify(val) : val,
!["id", "createdAt", "updatedAt"].includes(col) &&
Table.columns[col].type === "JSON"
? JSON.stringify(val)
: val,
]));
cols.id &&
return executeGetMiddleware(injectFx(parseJSON((cols.id &&
this.db
.query(`SELECT COUNT(*) AS count FROM ${table} WHERE id = $id;`)
.get({ $id: cols.id }).count !== 0
? this.db
.query(`UPDATE ${table} SET ${Object.keys(cols)
.filter((x) => x !== "id")
.map((x, i) => `${x} = $S_${x}`)
.join()} WHERE id = $id;`)
.run(Object.fromEntries([
.join()} WHERE id = $id RETURNING *;`)
.all(Object.fromEntries([
...Object.entries(cols)
.filter(([k]) => k !== "id")
.map(([k, v]) => [`$S_${k}`, v]),
Expand All @@ -89,8 +92,8 @@ export class BunORM {
.query(`INSERT INTO ${table} ` +
(Object.keys(cols).length === 0
? "DEFAULT VALUES;"
: `('${Object.keys(cols).join("','")}') VALUES (${arr(Object.keys(cols).length, (i) => `?${i}`).join()});`))
.all(...Object.values(cols));
: `('${Object.keys(cols).join("','")}') VALUES (${arr(Object.keys(cols).length, (i) => `?${i}`).join()}) RETURNING *;`))
.all(...Object.values(cols))))));
},
delete: (opts) => this.db
.query(`DELETE FROM ${table} WHERE ${Object.keys(opts)
Expand Down

0 comments on commit caac7ea

Please sign in to comment.