11import { Database as SqliteDatabase , DatabaseOpenOptions } from 'https://deno.land/x/sqlite3@0.6.1/mod.ts' ;
22import { 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' ;
44import { dejsonify , jsonify } from './json.ts' ;
55import { prettyPrintDiff } from './util.ts' ;
66import { 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