Skip to content

Commit 1bb1d81

Browse files
committed
Fixing many typing issues
1 parent 0989808 commit 1bb1d81

File tree

7 files changed

+21
-23
lines changed

7 files changed

+21
-23
lines changed

src/db.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { blue, bold, white } from "@std/fmt/colors";
2-
import { ConsoleHandler, getLogger, LevelName } from "@std/log";
2+
import { ConsoleHandler, getLogger, type LevelName, type Logger } from "@std/log";
33
import { DDL } from "./ddl.ts";
4-
import { Class, Identifiable, Parameter, Row, Schema } from "./types.ts";
4+
import type { Class, Identifiable, Parameter, Row, Schema } from "./types.ts";
55
import { Repository } from "./repository.ts";
66

77
const TTY = Deno.stderr.isTerminal();
@@ -161,16 +161,16 @@ export class DB {
161161
static readonly ALL = Number.MAX_SAFE_INTEGER;
162162
static client: Client;
163163
static debug = false;
164-
static schemas = new Map<string, Schema>();
164+
static #schemas = new Map<string, Schema>();
165165
static type: string;
166166
static capacity = this.DEFAULT_CAPACITY;
167167

168-
static get logger() {
168+
static get logger(): Logger {
169169
return this.mainLogger();
170170
}
171171

172172
// Get parent logger and if the logger has not been set, it will add a handler and level
173-
static mainLogger(level: LevelName = "INFO") {
173+
static mainLogger(level: LevelName = "INFO"): Logger {
174174
const logger = getLogger("gateways");
175175
if (logger.handlers.length === 0) logger.handlers.push(new ConsoleHandler("DEBUG"));
176176
const debug = Deno.env.get("DEBUG")?.includes(logger.loggerName) || this.debug;
@@ -190,8 +190,8 @@ export class DB {
190190

191191
// Iterate over the schemas and map them by name and type if it exists
192192
schemas?.forEach((s) => {
193-
DB.schemas.set(s.name, s);
194-
if (s.type) DB.schemas.set(s.type, s);
193+
DB.#schemas.set(s.name, s);
194+
if (s.type) DB.#schemas.set(s.type, s);
195195
});
196196
if (DB.client) return Promise.resolve(DB.client);
197197
DB.type = config.type;
@@ -251,7 +251,7 @@ export class DB {
251251
}
252252
}
253253

254-
static execute(sql: string, parameters?: Parameter[] | { [key: string]: Parameter }, debug?: boolean) {
254+
static execute(sql: string, parameters?: Parameter[] | { [key: string]: Parameter }, debug?: boolean): Promise<{ affectedRows?: number; lastInsertId?: number }> {
255255
// If values are not an array, they need to be transformed (as well as the SQL)
256256
const arrayParameters: Parameter[] = [];
257257
if (parameters && !Array.isArray(parameters)) {
@@ -285,21 +285,21 @@ export class DB {
285285
}
286286

287287
// Repository cache
288-
static repositories = new Map<string | Class<Identifiable>, Repository<Identifiable>>();
288+
static #repositories = new Map<string | Class<Identifiable>, Repository<Identifiable>>();
289289

290290
// deno-lint-ignore no-explicit-any
291291
static getRepository(tableName: string): Repository<any>;
292292
static getRepository<T extends Identifiable>(target: Class<T>): Repository<T>;
293293
static getRepository<T extends Identifiable>(target: string | Class<T>, schema?: Schema): Repository<T> {
294-
let repository = this.repositories.get(target) as Repository<T>;
294+
let repository = this.#repositories.get(target) as Repository<T>;
295295
if (repository) return repository;
296296

297297
// Figure out target, schema and name
298298
const name = typeof target === "string" ? target : target.name;
299299
if (typeof target === "string") target = Object as unknown as Class<T>;
300-
if (!schema) schema = DB.schemas.get(name);
300+
if (!schema) schema = DB.#schemas.get(name);
301301
repository = new Repository(target, schema, schema?.name ?? name, this.capacity);
302-
this.repositories.set(target, repository as Repository<Identifiable>);
302+
this.#repositories.set(target, repository as Repository<Identifiable>);
303303

304304
// Return repository
305305
return repository;

src/ddl.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Column, Constraint, Index, Relation, Schema } from "./types.ts";
1+
import type { Column, Constraint, Index, Relation, Schema } from "./types.ts";
22
import DB from "./db.ts";
33

44
const dataTypes = {
@@ -161,14 +161,14 @@ export class DDL {
161161
if (fullTextColumns.length) sql += this.createFullTextIndex(dbType, fullTextColumns, 0, table);
162162

163163
const fixDanglingComma = (sql: string) => sql.replace(/,\n\)/, "\n);");
164-
if (dbType === DB.Provider.POSTGRES) sql = this.postgres(sql);
164+
if (dbType === DB.Provider.POSTGRES) sql = this.#postgres(sql);
165165
sql = fixDanglingComma(sql);
166166

167167
return sql;
168168
}
169169

170170
// Function to accommodate the differences between MySQL and Postgres
171-
static postgres(sql: string) {
171+
static #postgres(sql: string) {
172172
return sql.replace(/\w+/g, (m: string) => {
173173
if (m === "DATETIME") return "TIMESTAMP";
174174
if (m === "JSON_EXTRACT") return "JSONB_EXTRACT_PATH";

src/repository.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { assert } from "@std/assert";
22
import { DB } from "./db.ts";
3-
import { Class, Condition, Filter, Identifiable, Order, Primitive, Schema, Where } from "./types.ts";
3+
import type { Class, Condition, Filter, Identifiable, Order, Primitive, Schema, Where } from "./types.ts";
44

55
// Import direct version so that version plays well with other modules
66
// TODO: Port mnemonist to deno land?

test/basic.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env -S deno test -A
22

33
import { assert, assertEquals, assertExists } from "@std/assert";
4-
import { Schema } from "../src/types.ts";
4+
import type { Schema } from "../src/types.ts";
55
import { dbInit, getProvider } from "./helpers.ts";
66
import AccountModel from "../resources/account.ts";
77
import AccountSchema from "../resources/account.json" with { type: "json" };

test/ddl.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env -S deno test -A --no-check
22

33
import { assertEquals } from "@std/assert";
4-
import { Schema } from "../src/types.ts";
54
import { DDL } from "../src/ddl.ts";
5+
import type { Schema } from "../src/types.ts";
66
import { createTables, dbInit, getProvider } from "./helpers.ts";
77
import AccountSchema from "../resources/account.json" with { type: "json" };
88

test/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { ConsoleHandler, setup } from "@std/log";
22
import { DB } from "../src/db.ts";
33
import { DDL } from "../src/ddl.ts";
4-
import { Schema } from "../src/types.ts";
4+
import type { Schema } from "../src/types.ts";
55

66
const PROVIDER = Deno.env.get("TEST_PROVIDER") ?? Deno.args[0];
77
if (!PROVIDER) console.warn("\n⚠️ Assuming SQLITE provider. You can use 'TEST_PROVIDER=<provider>' or '-- <provider>' (mysql, mysql2, postgres, sqlite)\n");

test/schema.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
#!/usr/bin/env -S deno test -A --check
22

3-
import { JSONSchema7 } from "schema";
3+
import type { JSONSchema7 } from "schema";
44
import AccountSchema from "../resources/account.json" with { type: "json" };
55

6-
const test = Deno.test;
7-
8-
test("Schema Types are proper JSON Schema", function () {
6+
Deno.test("Schema Types are proper JSON Schema", function () {
97
// Validate that property is a JSON Schema
108
const _schema = AccountSchema as JSONSchema7;
119

0 commit comments

Comments
 (0)