Skip to content

Commit 70e29b8

Browse files
committed
Better tests
1 parent bed8417 commit 70e29b8

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

resources/tjs.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env -S deno run -A
2+
3+
// Example of invoking TypeScript JSON Schema
4+
5+
import * as TJS from "npm:typescript-json-schema";
6+
import { DDL } from "../mod.ts";
7+
8+
// See https://github.com/YousefED/typescript-json-schema/issues/251
9+
const settings: TJS.PartialArgs = { required: true, defaultNumberType: "integer", ignoreErrors: true, validationKeywords: DDL.EXTENSIONS };
10+
const compilerOptions = { lib: [ "es2020" ], target: "es2020" };
11+
const program = TJS.getProgramFromFiles([ import.meta.dirname! + "/account.ts" ], compilerOptions);
12+
13+
// We can either get the schema for one file and one type...
14+
const schema = TJS.generateSchema(program, "Account", settings);
15+
console.log(schema);

src/repository.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,10 @@ export class Repository<T extends Identifiable> extends EventTarget {
119119

120120
// https://dev.mysql.com/doc/refman/8.0/en/select.html
121121
// Follow Loopback model (see https://loopback.io/doc/en/lb4/Querying-data.html)
122-
async find(filter: Filter<T> = {}, debug?: boolean): Promise<T[]> {
122+
async find(filterOrWhere?: Filter<T> | Where<T>, debug?: boolean): Promise<T[]> {
123123
// If there is no filter, we are better off returning the results from all
124-
if (!filter) return this.all();
124+
if (!filterOrWhere) return this.all();
125+
const filter: Filter<T> = Object.hasOwn(filterOrWhere, "where") ? filterOrWhere as Filter<T> : { where: filterOrWhere as Where<T> };
125126

126127
assert(typeof filter === "object" && !Array.isArray(filter), "Parameter 'filter' must be an object");
127128

@@ -166,7 +167,8 @@ export class Repository<T extends Identifiable> extends EventTarget {
166167
}
167168

168169
// https://dev.mysql.com/doc/refman/8.0/en/select.html
169-
async findOne(filter: Filter<T> = {}, debug?: boolean): Promise<T | undefined> {
170+
async findOne(filterOrWhere: Filter<T> | Where<T> = {}, debug?: boolean): Promise<T | undefined> {
171+
const filter: Filter<T> = Object.hasOwn(filterOrWhere, "where") ? filterOrWhere as Filter<T> : { where: filterOrWhere as Where<T> };
170172
filter.limit = 1;
171173
return (await this.find(filter, debug)).pop();
172174
}

test/basic.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ test("Retrieve via generic find", options, async function () {
4242
assertEquals(account.length, 1);
4343
});
4444

45+
test("Retrieve via simplified filter (where clause)", options, async function () {
46+
const account = await repo.find({ id });
47+
assertEquals(account.length, 1);
48+
});
49+
4550
test("Retrieve via another column", options, async function () {
4651
const account = await repo.findOne({ where: { name: NAME } }) as AccountModel;
4752
assertExists(account);
@@ -54,8 +59,13 @@ test("Retrieve via SQL query", options, async function () {
5459
assertEquals(accounts[0].name, NAME);
5560
});
5661

62+
test("Retrieve via SQL query with named parameter(s)", options, async function () {
63+
const records = await DB.query(`SELECT * FROM accounts WHERE name = :n`, { n: NAME });
64+
assertEquals(records.length, 1);
65+
});
66+
5767
test("Boolean Values", options, async function () {
58-
const accounts = await repo.find({ where: { name: "xyz", enabled: true } });
68+
const accounts = await repo.find({ name: "xyz", enabled: true });
5969
assertEquals(accounts.length, 0);
6070
});
6171

test/helpers.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ export async function createTables(schemas: Schema[]) {
4242
for (const schema of schemas) {
4343
if (DB.type !== DB.Provider.SQLITE) await DB.execute(`DROP TABLE IF EXISTS ${schema.table} CASCADE;`);
4444
const sql = DDL.createTable(schema, DB.type as DB.Provider);
45-
console.log(sql);
4645
await dbExec(sql);
4746
}
4847
}

0 commit comments

Comments
 (0)