Skip to content

Commit fe9317a

Browse files
authored
chore(db-sqlite): enable TypeScript strict (#11831)
- I installed `@types/uuid` because typescript required it in a file - In `packages/db-sqlite/src/index.ts` I see four more errors in my IDE that don't appear when I run the typecheck in the CLI with `tsc --noEmit`. The same thing happened in #11560. Also referencing #11226 (comment) for traceability.
1 parent eb1434e commit fe9317a

File tree

14 files changed

+66
-78
lines changed

14 files changed

+66
-78
lines changed

packages/db-sqlite/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
"@payloadcms/eslint-config": "workspace:*",
8686
"@types/pg": "8.10.2",
8787
"@types/to-snake-case": "1.0.0",
88+
"@types/uuid": "10.0.0",
8889
"payload": "workspace:*"
8990
},
9091
"peerDependencies": {

packages/db-sqlite/src/columnToCodeConverter.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ export const columnToCodeConverter: ColumnToCodeConverter = ({
2323
case 'enum': {
2424
let options: string[]
2525
if ('locale' in column) {
26+
if (!locales?.length) {
27+
throw new Error('Locales must be defined for locale columns')
28+
}
2629
options = locales
2730
} else {
2831
options = column.options

packages/db-sqlite/src/connect.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
2-
import type { Connect } from 'payload'
2+
import type { Connect, Migration } from 'payload'
33

44
import { createClient } from '@libsql/client'
55
import { pushDevSchema } from '@payloadcms/drizzle'
@@ -36,7 +36,8 @@ export const connect: Connect = async function connect(
3636
}
3737
}
3838
} catch (err) {
39-
this.payload.logger.error({ err, msg: `Error: cannot connect to SQLite: ${err.message}` })
39+
const message = err instanceof Error ? err.message : String(err)
40+
this.payload.logger.error({ err, msg: `Error: cannot connect to SQLite: ${message}` })
4041
if (typeof this.rejectInitializing === 'function') {
4142
this.rejectInitializing()
4243
}
@@ -57,6 +58,6 @@ export const connect: Connect = async function connect(
5758
}
5859

5960
if (process.env.NODE_ENV === 'production' && this.prodMigrations) {
60-
await this.migrate({ migrations: this.prodMigrations })
61+
await this.migrate({ migrations: this.prodMigrations as Migration[] })
6162
}
6263
}

packages/db-sqlite/src/countDistinct.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export const countDistinct: CountDistinct = async function countDistinct(
1717
})
1818
.from(this.tables[tableName])
1919
.where(where)
20-
return Number(countResult[0].count)
20+
return Number(countResult[0]?.count)
2121
}
2222

2323
const chainedMethods: ChainedMethods = []
@@ -45,5 +45,5 @@ export const countDistinct: CountDistinct = async function countDistinct(
4545
.limit(1),
4646
})
4747

48-
return Number(countResult[0].count)
48+
return Number(countResult[0]?.count)
4949
}

packages/db-sqlite/src/createJSONQuery/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const createJSONQuery = ({
7474
treatAsArray,
7575
value,
7676
}: CreateJSONQueryArgs): string => {
77-
if (treatAsArray.includes(pathSegments[1])) {
77+
if (treatAsArray?.includes(pathSegments[1]!) && table) {
7878
return fromArray({
7979
operator,
8080
pathSegments,

packages/db-sqlite/src/deleteWhere.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import type { DeleteWhere } from './types.js'
1+
import type { DeleteWhere, SQLiteAdapter } from './types.js'
22

3-
export const deleteWhere: DeleteWhere = async function deleteWhere({ db, tableName, where }) {
3+
export const deleteWhere: DeleteWhere = async function (
4+
// Here 'this' is not a parameter. See:
5+
// https://www.typescriptlang.org/docs/handbook/2/classes.html#this-parameters
6+
this: SQLiteAdapter,
7+
{ db, tableName, where },
8+
) {
49
const table = this.tables[tableName]
510
await db.delete(table).where(where)
611
}
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1-
import type { DropDatabase } from './types.js'
1+
import type { Row } from '@libsql/client'
22

3-
const getTables = (adapter) => {
3+
import type { DropDatabase, SQLiteAdapter } from './types.js'
4+
5+
const getTables = (adapter: SQLiteAdapter) => {
46
return adapter.client.execute(`SELECT name
57
FROM sqlite_master
68
WHERE type = 'table'
79
AND name NOT LIKE 'sqlite_%';`)
810
}
911

10-
const dropTables = (adapter, rows) => {
12+
const dropTables = (adapter: SQLiteAdapter, rows: Row[]) => {
1113
const multi = `
1214
PRAGMA foreign_keys = OFF;\n
13-
${rows.map(({ name }) => `DROP TABLE IF EXISTS ${name}`).join(';\n ')};\n
15+
${rows.map(({ name }) => `DROP TABLE IF EXISTS ${name as string}`).join(';\n ')};\n
1416
PRAGMA foreign_keys = ON;`
1517
return adapter.client.executeMultiple(multi)
1618
}
1719

18-
export const dropDatabase: DropDatabase = async function dropDatabase({ adapter }) {
20+
export const dropDatabase: DropDatabase = async function ({ adapter }) {
1921
const result = await getTables(adapter)
2022
await dropTables(adapter, result.rows)
2123
}

packages/db-sqlite/src/execute.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ import { sql } from 'drizzle-orm'
33
import type { Execute } from './types.js'
44

55
export const execute: Execute<any> = function execute({ db, drizzle, raw, sql: statement }) {
6-
const executeFrom = db ?? drizzle
6+
const executeFrom = (db ?? drizzle)!
77

88
if (raw) {
99
const result = executeFrom.run(sql.raw(raw))
1010
return result
1111
} else {
12-
const result = executeFrom.run(statement)
12+
const result = executeFrom.run(statement!)
1313
return result
1414
}
1515
}

packages/db-sqlite/src/index.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,11 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
9292
allowIDOnCreate,
9393
autoIncrement: args.autoIncrement ?? false,
9494
beforeSchemaInit: args.beforeSchemaInit ?? [],
95+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
9596
client: undefined,
9697
clientConfig: args.client,
9798
defaultDrizzleSnapshot,
99+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
98100
drizzle: undefined,
99101
features: {
100102
json: true,
@@ -112,6 +114,7 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
112114
logger: args.logger,
113115
operators,
114116
prodMigrations: args.prodMigrations,
117+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
115118
push: args.push,
116119
rawRelations: {},
117120
rawTables: {},
@@ -122,6 +125,7 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
122125
sessions: {},
123126
tableNameMap: new Map<string, string>(),
124127
tables: {},
128+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
125129
transactionOptions: args.transactionOptions || undefined,
126130
updateMany,
127131
versionsSuffix: args.versionsSuffix || '_v',
@@ -160,6 +164,7 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
160164
find,
161165
findGlobal,
162166
findGlobalVersions,
167+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
163168
findOne,
164169
findVersions,
165170
indexes: new Set<string>(),
@@ -175,8 +180,10 @@ export function sqliteAdapter(args: Args): DatabaseAdapterObj<SQLiteAdapter> {
175180
packageName: '@payloadcms/db-sqlite',
176181
payload,
177182
queryDrafts,
183+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
178184
rejectInitializing,
179185
requireDrizzleKit,
186+
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
180187
resolveInitializing,
181188
rollbackTransaction,
182189
updateGlobal,

packages/db-sqlite/src/init.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const init: Init = async function init(this: SQLiteAdapter) {
2828
await executeSchemaHooks({ type: 'beforeSchemaInit', adapter: this })
2929

3030
for (const tableName in this.rawTables) {
31-
buildDrizzleTable({ adapter, locales, rawTable: this.rawTables[tableName] })
31+
buildDrizzleTable({ adapter, locales: locales!, rawTable: this.rawTables[tableName]! })
3232
}
3333

3434
buildDrizzleRelations({

0 commit comments

Comments
 (0)