Skip to content

Commit 4a9a9e7

Browse files
committed
throw an error when model is not registered
1 parent c95a171 commit 4a9a9e7

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

src/errors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
import { SqlTable } from './orm.ts';
2+
13
export class DBError extends Error {}
24

35
export class DBNotFound extends DBError {}
6+
export class DBModelNotFound extends DBError {
7+
constructor(model: typeof SqlTable) {
8+
super(`${model.name} is not registered`);
9+
}
10+
}
411
export class DBInvalidTable extends DBError {}
512
export class DBInvalidData extends DBError {}

src/orm.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Database as SqliteDatabase, DatabaseOpenOptions } from 'https://deno.land/x/sqlite3@0.6.1/mod.ts';
22
import { buildAggregateQuery, buildAlterQuery, buildCountWhereQuery, buildDeleteQuery, buildInsertQuery, buildModelFromData, buildSelectQuery, buildTableQuery, buildUpdateQuery, isProvidedTypeValid } from './builder.ts';
3-
import { DBInvalidData, DBInvalidTable, DBNotFound } from './errors.ts';
3+
import { DBInvalidData, DBInvalidTable, DBModelNotFound, DBNotFound } from './errors.ts';
44
import { dejsonify, jsonify } from './json.ts';
55
import { prettyPrintDiff } from './util.ts';
66
import { basename, join } from 'https://deno.land/std@0.170.0/path/mod.ts';
@@ -195,6 +195,8 @@ export class SqliteOrm {
195195
//#region table logic
196196

197197
public findOne<T extends SqlTable>(table: new () => T, idOrQuery: PrimitiveTypes | SelectQuery): T {
198+
if (this.models[table.name] == null) throw new DBModelNotFound(table);
199+
198200
const col = this.models[table.name].columns.find((c) => c.isPrimaryKey);
199201
if (col == null) throw new DBInvalidTable(`${this.models[table.name].tableName} does not have primary key`);
200202
if (typeof idOrQuery !== 'object' && !isProvidedTypeValid(idOrQuery, col)) throw new DBInvalidData(`${this.models[table.name].tableName}.${col.name} has a different type`);
@@ -240,6 +242,8 @@ export class SqliteOrm {
240242
}
241243

242244
public findMany<T extends SqlTable>(table: new () => T, query: SelectQuery): T[] {
245+
if (this.models[table.name] == null) throw new DBModelNotFound(table);
246+
243247
const builtQuery = buildSelectQuery(query, this.models[table.name].tableName);
244248

245249
const data = this.db.prepare(builtQuery.query).all(...builtQuery.params);
@@ -258,17 +262,22 @@ export class SqliteOrm {
258262
}
259263

260264
public countWhere<T extends SqlTable>(table: new () => T, query: WhereClause): number {
265+
if (this.models[table.name] == null) throw new DBModelNotFound(table);
266+
261267
const builtQuery = buildCountWhereQuery(query, this.models[table.name].tableName);
262268
return this.db.prepare(builtQuery.query).get<{ 'COUNT(*)': number }>(...builtQuery.params)!['COUNT(*)'];
263269
}
264270

265271
public aggregateSelect<Row extends Array<any>, T extends SqlTable = SqlTable>(table: new () => T, query: AggregateSelectQuery): Row[] {
272+
if (this.models[table.name] == null) throw new DBModelNotFound(table);
273+
266274
const builtQuery = buildAggregateQuery(query, this.models[table.name].tableName);
267275
return this.db.prepare(builtQuery.query).values(...builtQuery.params);
268276
}
269277

270278
public save<T extends SqlTable>(obj: T): T {
271279
const model = this.models[obj.constructor.name];
280+
if (model == null) throw new DBModelNotFound(obj.constructor as typeof SqlTable);
272281

273282
const builtData: Record<string, unknown> = {};
274283
model.columns.forEach((col) => {

0 commit comments

Comments
 (0)