Skip to content

Commit

Permalink
Prettify, and generated changeset
Browse files Browse the repository at this point in the history
  • Loading branch information
kingmesal committed Sep 27, 2022
1 parent 64b2a1a commit 9049500
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/three-chairs-move.md
@@ -0,0 +1,5 @@
---
"d1-orm": minor
---

Adding insert or replace
10 changes: 8 additions & 2 deletions src/model.ts
Expand Up @@ -140,7 +140,10 @@ export class Model<T extends object> {
/**
* @param data The data to insert into the table, as an object with the column names as keys and the values as values.
*/
public async InsertOne(data: Partial<T>, orReplace = false): Promise<D1Result<T>> {
public async InsertOne(
data: Partial<T>,
orReplace = false
): Promise<D1Result<T>> {
const qt = orReplace ? QueryType.INSERT_OR_REPLACE : QueryType.INSERT;
const statement = GenerateQuery(qt, this.tableName, { data });
return this.#D1Orm
Expand All @@ -152,7 +155,10 @@ export class Model<T extends object> {
/**
* @param data The data to insert into the table, as an array of objects with the column names as keys and the values as values.
*/
public async InsertMany(data: Partial<T>[], orReplace = false): Promise<D1Result<T>[]> {
public async InsertMany(
data: Partial<T>[],
orReplace = false
): Promise<D1Result<T>[]> {
const qt = orReplace ? QueryType.INSERT_OR_REPLACE : QueryType.INSERT;
const stmts: D1PreparedStatement[] = [];
for (const row of data) {
Expand Down
10 changes: 6 additions & 4 deletions test/querybuilder.test.js
Expand Up @@ -179,9 +179,9 @@ describe("Query Builder", () => {
});
describe(QueryType.INSERT_OR_REPLACE, () => {
it("should throw an error if no data is provided", () => {
expect(() => GenerateQuery(QueryType.INSERT_OR_REPLACE, "test")).to.throw(
"Must provide data to insert"
);
expect(() =>
GenerateQuery(QueryType.INSERT_OR_REPLACE, "test")
).to.throw("Must provide data to insert");
});
it("should throw an error if empty data is provided", () => {
expect(() =>
Expand All @@ -192,7 +192,9 @@ describe("Query Builder", () => {
const statement = GenerateQuery(QueryType.INSERT_OR_REPLACE, "test", {
data: { id: 1 },
});
expect(statement.query).to.equal("INSERT or REPLACE INTO `test` (id) VALUES (?)");
expect(statement.query).to.equal(
"INSERT or REPLACE INTO `test` (id) VALUES (?)"
);
expect(statement.bindings.length).to.equal(1);
expect(statement.bindings[0]).to.equal(1);
});
Expand Down

0 comments on commit 9049500

Please sign in to comment.