Skip to content

Commit 483eeb0

Browse files
authored
Merge pull request #482 from drizzle-team/eslint
2 parents 291e857 + b4cebd9 commit 483eeb0

File tree

115 files changed

+1375
-1019
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

115 files changed

+1375
-1019
lines changed

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
22
dist
3+
examples

.eslintrc.yaml

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
root: true
22
extends:
3-
- 'plugin:@typescript-eslint/base'
3+
- 'eslint:recommended'
4+
- 'plugin:@typescript-eslint/recommended'
5+
- 'plugin:unicorn/recommended'
46
parser: '@typescript-eslint/parser'
57
plugins:
68
- import
@@ -17,3 +19,37 @@ rules:
1719
import/no-useless-path-segments: error
1820
import/newline-after-import: error
1921
import/no-duplicates: error
22+
'@typescript-eslint/no-explicit-any': 'off'
23+
'@typescript-eslint/no-non-null-assertion': 'off'
24+
'@typescript-eslint/no-namespace': 'off'
25+
'@typescript-eslint/no-unused-vars':
26+
- error
27+
- argsIgnorePattern: '^_'
28+
varsIgnorePattern: '^_'
29+
'@typescript-eslint/ban-types':
30+
- error
31+
- extendDefaults: true
32+
types:
33+
'{}' : false
34+
'@typescript-eslint/no-this-alias': 'off'
35+
'@typescript-eslint/no-var-requires': 'off'
36+
'unicorn/prefer-node-protocol': 'off'
37+
'unicorn/prefer-top-level-await': 'off'
38+
'unicorn/prevent-abbreviations': 'off'
39+
'unicorn/prefer-switch': 'off'
40+
'unicorn/catch-error-name': 'off'
41+
'unicorn/no-null': 'off'
42+
'unicorn/numeric-separators-style': 'off'
43+
'unicorn/explicit-length-check': 'off'
44+
'unicorn/filename-case': 'off'
45+
'unicorn/prefer-module': 'off'
46+
'unicorn/no-array-reduce': 'off'
47+
'unicorn/no-nested-ternary': 'off'
48+
'unicorn/no-useless-undefined':
49+
- error
50+
- checkArguments: false
51+
'unicorn/no-this-assignment': 'off'
52+
'unicorn/empty-brace-spaces': 'off'
53+
'unicorn/no-thenable': 'off'
54+
'unicorn/consistent-function-scoping': 'off'
55+
'unicorn/prefer-type-error': 'off'

drizzle-orm/src/alias.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { type View, ViewBaseConfig } from './view';
66
export class ColumnAliasProxyHandler<TColumn extends AnyColumn> implements ProxyHandler<TColumn> {
77
constructor(private table: Table | View) {}
88

9-
get(columnObj: TColumn, prop: string | symbol, receiver: any): any {
9+
get(columnObj: TColumn, prop: string | symbol): any {
1010
if (prop === 'table') {
1111
return this.table;
1212
}
@@ -18,7 +18,7 @@ export class ColumnAliasProxyHandler<TColumn extends AnyColumn> implements Proxy
1818
export class TableAliasProxyHandler<T extends Table | View> implements ProxyHandler<T> {
1919
constructor(private alias: string, private replaceOriginalName: boolean) {}
2020

21-
get(tableObj: T, prop: string | symbol, receiver: any): any {
21+
get(tableObj: T, prop: string | symbol): any {
2222
if (prop === Table.Symbol.Name) {
2323
return this.alias;
2424
}

drizzle-orm/src/aws-data-api/common/index.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
import type { Field} from '@aws-sdk/client-rds-data';
1+
import type { Field } from '@aws-sdk/client-rds-data';
22
import { TypeHint } from '@aws-sdk/client-rds-data';
33
import type { QueryTypingsValue } from '~/sql';
44

55
export function getValueFromDataApi(row: Field) {
6-
if (typeof row.stringValue !== 'undefined') {
6+
if (row.stringValue !== undefined) {
77
return row.stringValue;
8-
} else if (typeof row.booleanValue !== 'undefined') {
8+
} else if (row.booleanValue !== undefined) {
99
return row.booleanValue;
10-
} else if (typeof row.doubleValue !== 'undefined') {
10+
} else if (row.doubleValue !== undefined) {
1111
return row.doubleValue;
12-
} else if (typeof row.isNull !== 'undefined') {
12+
} else if (row.isNull !== undefined) {
1313
return null;
14-
} else if (typeof row.longValue !== 'undefined') {
14+
} else if (row.longValue !== undefined) {
1515
return row.longValue;
16-
} else if (typeof row.blobValue !== 'undefined') {
16+
} else if (row.blobValue !== undefined) {
1717
return row.blobValue;
18-
} else if (typeof row.arrayValue !== 'undefined') {
19-
if (typeof row.arrayValue.stringValues !== 'undefined') {
18+
// eslint-disable-next-line unicorn/no-negated-condition
19+
} else if (row.arrayValue !== undefined) {
20+
if (row.arrayValue.stringValues !== undefined) {
2021
return row.arrayValue.stringValues;
2122
}
22-
throw Error('Unknown array type');
23+
throw new Error('Unknown array type');
2324
} else {
24-
throw Error('Unknown type');
25+
throw new Error('Unknown type');
2526
}
2627
}
2728

@@ -44,19 +45,17 @@ export function typingsToAwsTypeHint(typings?: QueryTypingsValue): TypeHint | un
4445
}
4546

4647
export function toValueParam(value: any, typings?: QueryTypingsValue): { value: Field; typeHint?: TypeHint } {
47-
let response: { value: Field; typeHint?: TypeHint } = {
48+
const response: { value: Field; typeHint?: TypeHint } = {
4849
value: {} as any,
4950
typeHint: typingsToAwsTypeHint(typings),
5051
};
5152

5253
if (value === null) {
5354
response.value = { isNull: true };
5455
} else if (typeof value === 'string') {
55-
if (response.typeHint === 'DATE') {
56-
response.value = { stringValue: value.split('T')[0]! };
57-
} else {
58-
response.value = { stringValue: value };
59-
}
56+
response.value = response.typeHint === 'DATE'
57+
? { stringValue: value.split('T')[0]! }
58+
: { stringValue: value };
6059
} else if (typeof value === 'number' && Number.isInteger(value)) {
6160
response.value = { longValue: value };
6261
} else if (typeof value === 'number' && !Number.isInteger(value)) {
@@ -66,7 +65,7 @@ export function toValueParam(value: any, typings?: QueryTypingsValue): { value:
6665
} else if (value instanceof Date) {
6766
response.value = { stringValue: value.toISOString().replace('T', ' ').replace('Z', '') };
6867
} else {
69-
throw Error(`Unknown type for ${value}`);
68+
throw new Error(`Unknown type for ${value}`);
7069
}
7170

7271
return response;

drizzle-orm/src/aws-data-api/pg/session.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,21 @@ export class AwsDataApiPreparedQuery<T extends PreparedQueryConfig> extends Prep
5656

5757
this.options.logger?.logQuery(this.rawQuery.input.sql!, this.rawQuery.input.parameters);
5858

59-
const { fields } = this;
59+
const { fields, rawQuery, client, joinsNotNullableMap } = this;
6060
if (!fields) {
61-
return await this.client.send(this.rawQuery);
61+
return await client.send(rawQuery);
6262
}
6363

64-
const result = await this.client.send(this.rawQuery);
64+
const result = await client.send(rawQuery);
6565

6666
return result.records?.map((result: any) => {
6767
const mappedResult = result.map((res: any) => getValueFromDataApi(res));
68-
return mapResultRow<T['execute']>(fields, mappedResult, this.joinsNotNullableMap);
68+
return mapResultRow<T['execute']>(fields, mappedResult, joinsNotNullableMap);
6969
});
7070
}
7171

7272
all(placeholderValues?: Record<string, unknown> | undefined): Promise<T['all']> {
73-
return this.execute(placeholderValues)
73+
return this.execute(placeholderValues);
7474
}
7575
}
7676

drizzle-orm/src/better-sqlite3/session.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,32 +78,32 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
7878
}
7979

8080
all(placeholderValues?: Record<string, unknown>): T['all'] {
81-
const { fields } = this;
81+
const { fields, joinsNotNullableMap, queryString, logger, stmt } = this;
8282
if (fields) {
83-
return this.values(placeholderValues).map((row) => mapResultRow(fields, row, this.joinsNotNullableMap));
83+
return this.values(placeholderValues).map((row) => mapResultRow(fields, row, joinsNotNullableMap));
8484
}
8585

8686
const params = fillPlaceholders(this.params, placeholderValues ?? {});
87-
this.logger.logQuery(this.queryString, params);
88-
return this.stmt.all(...params);
87+
logger.logQuery(queryString, params);
88+
return stmt.all(...params);
8989
}
9090

9191
get(placeholderValues?: Record<string, unknown>): T['get'] {
9292
const params = fillPlaceholders(this.params, placeholderValues ?? {});
9393
this.logger.logQuery(this.queryString, params);
9494

95-
const { fields } = this;
95+
const { fields, stmt, joinsNotNullableMap } = this;
9696
if (!fields) {
97-
return this.stmt.get(...params);
97+
return stmt.get(...params);
9898
}
9999

100-
const value = this.stmt.raw().get(...params);
100+
const value = stmt.raw().get(...params);
101101

102102
if (!value) {
103103
return undefined;
104104
}
105105

106-
return mapResultRow(fields, value, this.joinsNotNullableMap);
106+
return mapResultRow(fields, value, joinsNotNullableMap);
107107
}
108108

109109
values(placeholderValues?: Record<string, unknown>): T['values'] {

drizzle-orm/src/bun-sqlite/session.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,14 +89,14 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
8989
}
9090

9191
all(placeholderValues?: Record<string, unknown>): T['all'] {
92-
const { fields } = this;
92+
const { fields, queryString, logger, joinsNotNullableMap, stmt } = this;
9393
if (fields) {
94-
return this.values(placeholderValues).map((row) => mapResultRow(fields, row, this.joinsNotNullableMap));
94+
return this.values(placeholderValues).map((row) => mapResultRow(fields, row, joinsNotNullableMap));
9595
}
9696

9797
const params = fillPlaceholders(this.params, placeholderValues ?? {});
98-
this.logger.logQuery(this.queryString, params);
99-
return this.stmt.all(...params);
98+
logger.logQuery(queryString, params);
99+
return stmt.all(...params);
100100
}
101101

102102
get(placeholderValues?: Record<string, unknown>): T['get'] {
@@ -108,12 +108,12 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
108108
return undefined;
109109
}
110110

111-
const { fields } = this;
111+
const { fields, joinsNotNullableMap } = this;
112112
if (!fields) {
113113
return value;
114114
}
115115

116-
return mapResultRow(fields, value, this.joinsNotNullableMap);
116+
return mapResultRow(fields, value, joinsNotNullableMap);
117117
}
118118

119119
values(placeholderValues?: Record<string, unknown>): T['values'] {

drizzle-orm/src/d1/session.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
9090
}
9191

9292
all(placeholderValues?: Record<string, unknown>): Promise<T['all']> {
93-
const { fields } = this;
93+
const { fields, joinsNotNullableMap, queryString, logger, stmt } = this;
9494
if (fields) {
9595
return this.values(placeholderValues).then((values) =>
96-
values.map((row) => mapResultRow(fields, row, this.joinsNotNullableMap))
96+
values.map((row) => mapResultRow(fields, row, joinsNotNullableMap))
9797
);
9898
}
9999

100100
const params = fillPlaceholders(this.params, placeholderValues ?? {});
101-
this.logger.logQuery(this.queryString, params);
102-
return this.stmt.bind(...params).all().then(({ results }) => results!);
101+
logger.logQuery(queryString, params);
102+
return stmt.bind(...params).all().then(({ results }) => results!);
103103
}
104104

105105
get(placeholderValues?: Record<string, unknown>): Promise<T['get']> {

drizzle-orm/src/errors.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,9 @@ export class DrizzleError extends Error {
55
}
66

77
static wrap(error: unknown, message?: string): DrizzleError {
8-
if (error instanceof Error) {
9-
return new DrizzleError(message ? `${message}: ${error.message}` : error.message, { cause: error });
10-
} else {
11-
return new DrizzleError(message ?? String(error));
12-
}
8+
return error instanceof Error
9+
? new DrizzleError(message ? `${message}: ${error.message}` : error.message, { cause: error })
10+
: new DrizzleError(message ?? String(error));
1311
}
1412
}
1513

drizzle-orm/src/libsql/session.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export class LibSQLSession extends SQLiteSession<'async', ResultSet> {
4848

4949
override async transaction<T>(
5050
transaction: (db: LibSQLTransaction) => T | Promise<T>,
51-
config?: SQLiteTransactionConfig,
51+
_config?: SQLiteTransactionConfig,
5252
): Promise<T> {
5353
// TODO: support transaction behavior
5454
const libsqlTx = await this.client.transaction();
@@ -103,25 +103,25 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
103103
}
104104

105105
all(placeholderValues?: Record<string, unknown>): Promise<T['all']> {
106-
const { fields } = this;
106+
const { fields, joinsNotNullableMap, logger, queryString, tx, client } = this;
107107
if (fields) {
108108
const values = this.values(placeholderValues);
109109

110110
return values.then((rows) =>
111111
rows.map((row) => {
112112
return mapResultRow(
113113
fields,
114-
Array.prototype.slice.call(row).map(normalizeFieldValue),
115-
this.joinsNotNullableMap,
114+
Array.prototype.slice.call(row).map((v) => normalizeFieldValue(v)),
115+
joinsNotNullableMap,
116116
);
117117
})
118118
);
119119
}
120120

121121
const params = fillPlaceholders(this.params, placeholderValues ?? {});
122-
this.logger.logQuery(this.queryString, params);
123-
const stmt: InStatement = { sql: this.queryString, args: params as InArgs };
124-
return (this.tx ? this.tx.execute(stmt) : this.client.execute(stmt)).then(({ rows }) => rows.map(normalizeRow));
122+
logger.logQuery(queryString, params);
123+
const stmt: InStatement = { sql: queryString, args: params as InArgs };
124+
return (tx ? tx.execute(stmt) : client.execute(stmt)).then(({ rows }) => rows.map((row) => normalizeRow(row)));
125125
}
126126

127127
get(placeholderValues?: Record<string, unknown>): Promise<T['get']> {
@@ -144,7 +144,7 @@ function normalizeRow(obj: any) {
144144
// turn them into objects what's what other SQLite drivers
145145
// do.
146146
return Object.keys(obj).reduce((acc: Record<string, any>, key) => {
147-
if (obj.propertyIsEnumerable(key)) {
147+
if (Object.prototype.propertyIsEnumerable.call(obj, key)) {
148148
acc[key] = obj[key];
149149
}
150150
return acc;

drizzle-orm/src/logger.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ export class ConsoleLogWriter implements LogWriter {
1515
export class DefaultLogger implements Logger {
1616
readonly writer: LogWriter;
1717

18-
constructor(config: { writer: LogWriter } = { writer: new ConsoleLogWriter() }) {
19-
this.writer = config.writer;
18+
constructor(config?: { writer: LogWriter }) {
19+
this.writer = config?.writer ?? new ConsoleLogWriter();
2020
}
2121

2222
logQuery(query: string, params: unknown[]): void {
2323
const stringifiedParams = params.map((p) => {
2424
try {
2525
return JSON.stringify(p);
26-
} catch (e) {
26+
} catch {
2727
return String(p);
2828
}
2929
});
@@ -33,5 +33,7 @@ export class DefaultLogger implements Logger {
3333
}
3434

3535
export class NoopLogger implements Logger {
36-
logQuery(): void {}
36+
logQuery(): void {
37+
// noop
38+
}
3739
}

drizzle-orm/src/migrator.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ export function readMigrationFiles(config: string | MigrationConfig): MigrationM
3030
}
3131

3232
if (!migrationFolderTo) {
33-
throw Error('no migration folder defined');
33+
throw new Error('no migration folder defined');
3434
}
3535

3636
const migrationQueries: MigrationMeta[] = [];
3737

3838
const journalPath = `${migrationFolderTo}/meta/_journal.json`;
3939
if (!fs.existsSync(journalPath)) {
40-
throw Error(`Can't find meta/_journal.json file`);
40+
throw new Error(`Can't find meta/_journal.json file`);
4141
}
4242

4343
const journalAsString = fs.readFileSync(`${migrationFolderTo}/meta/_journal.json`).toString();
@@ -62,8 +62,8 @@ export function readMigrationFiles(config: string | MigrationConfig): MigrationM
6262
folderMillis: journalEntry.when,
6363
hash: crypto.createHash('sha256').update(query).digest('hex'),
6464
});
65-
} catch (e) {
66-
throw Error(`No file ${migrationPath} found in ${migrationFolderTo} folder`);
65+
} catch {
66+
throw new Error(`No file ${migrationPath} found in ${migrationFolderTo} folder`);
6767
}
6868
}
6969

0 commit comments

Comments
 (0)