drizzle-team / drizzle-team/drizzle-orm
[FEATURE]: Support WITH (NOLOCK) table hint in the MSSQL select query builder
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### Feature hasn't been suggested before.
- [x] I have verified this feature I'm about to request hasn't been suggested before.
### Describe the enhancement you want to request
#5884
The MSSQL dialect (`mssql-core`, available on the v1 `beta` branch) currently has no way to express SQL Server table hints most importantly `WITH (NOLOCK)`:
```sql
SELECT u.NAME, c.NAME
FROM USERS u WITH (NOLOCK)
LEFT JOIN CITIES c WITH (NOLOCK) ON c.ID = u.CITY_ID
```
`NOLOCK` (= `READUNCOMMITTED`) is idiomatic in many read-heavy legacy SQL Server databases, where virtually every read query carries the hint to avoid shared locks. Today the only way to produce such SQL with Drizzle is to drop down to raw SQL (`sql.raw`), which forfeits the type-safe query builder for a large class of everyday queries against existing MSSQL databases.
I searched the issues, PRs and discussions table hints / NOLOCK for MSSQL haven't been suggested before.
**Proposed API** (mirrors the existing MySQL index hints `useIndex` / `forceIndex` / `ignoreIndex` an optional hint config on `.from()` and join methods):
```ts
// per table
db.select()
.from(users, { withNoLock: true })
.leftJoin(cities, eq(cities.id, users.cityId), { withNoLock: true });
// or for the whole query: main table + all joined tables
db.select()
.from(users)
.leftJoin(cities, eq(cities.id, users.cityId))
.withNoLock();
```
Generated SQL:
```sql
select [users].[name], [cities].[name] from [users] with (nolock) left join [cities] with (nolock) on [cities].[id] = [users].[city_id]
```
Like the MySQL index hints, the hint config is accepted only for `MsSqlTable` sources and rejected at the type level for subqueries, views and raw SQL. The hint config object (`MsSqlTableHintConfig`) leaves room to add further table hints later (`READPAST`, `UPDLOCK`, …) without breaking the API.
I have a working implementation with type tests and integration tests, PR incoming right after this issue.
Contributor guide
Assessment
This issue has not been assessed yet.