Skip to content

Commit

Permalink
Merge pull request #31 from koyopro/feature/find
Browse files Browse the repository at this point in the history
Throw a RecordNotFound error when the id passed to find() is not finite.
  • Loading branch information
koyopro authored Jul 24, 2024
2 parents 18331d9 + d30c976 commit 782a407
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/accel-record-core/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export class RecordNotFound extends Error {}
13 changes: 8 additions & 5 deletions packages/accel-record-core/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Model } from "./index.js";
import { RecordNotFound } from "./errors.js";
import { type Model } from "./index.js";
import type { Meta } from "./meta.js";
import { Relation } from "./relation/index.js";

Expand Down Expand Up @@ -314,11 +315,13 @@ export class Query {
this: T,
id: number
): Meta<T>["Persisted"] {
const instance = this.all()
.setOption("wheres", [{ [this.primaryKeys[0]]: id }])
.first();
const instance = isFinite(id)
? this.all()
.setOption("wheres", [{ [this.primaryKeys[0]]: id }])
.first()
: undefined;
if (!instance) {
throw new Error("Record Not found");
throw new RecordNotFound("Record Not Found");
}
return instance;
}
Expand Down
10 changes: 10 additions & 0 deletions packages/accel-record/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
"type": "module",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./errors": {
"types": "./dist/errors.d.ts",
"default": "./dist/errors.js"
}
},
"scripts": {
"build": "tsc",
"test": "vitest",
Expand Down
1 change: 1 addition & 0 deletions packages/accel-record/src/errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "accel-record-core/dist/errors";
12 changes: 9 additions & 3 deletions tests/models/query.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RecordNotFound } from "accel-record/errors";
import { $post } from "../factories/post";
import { $user } from "../factories/user";
import { User } from "./index";
Expand Down Expand Up @@ -135,15 +136,20 @@ describe("Query", () => {
});

test(".find()", () => {
expect(() => {
try {
User.find(1);
}).toThrow("Record Not found");
const u = $user.create({ name: "hoge", email: "[email protected]" });
} catch (e) {
expect(e).toBeInstanceOf(RecordNotFound);
}
const u = $user.create({ id: 1, name: "hoge", email: "[email protected]" });
const s = User.find(u.id!);
expect(s).toBeInstanceOf(User);
expect(s.id).toBe(u.id!);
expect(s.name).toBe("hoge");
expect(s.email).toBe("[email protected]");

expect(() => User.find(NaN)).toThrowError("Record Not Found");
expect(User.find("1" as any)).toBeInstanceOf(User);
});

test(".findBy()", () => {
Expand Down

0 comments on commit 782a407

Please sign in to comment.