Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mssql docs #724

Merged
merged 17 commits into from
Dec 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion site/docs/dialects.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ A dialect is the glue between Kysely and the underlying database engine. Check t

| Dialect | Link |
| --- | --- |
| MySQL | https://kysely-org.github.io/kysely-apidoc/classes/MysqlDialect.html |
| PostgreSQL | https://kysely-org.github.io/kysely-apidoc/classes/PostgresDialect.html |
| MySQL | https://kysely-org.github.io/kysely-apidoc/classes/MysqlDialect.html |
| Microsoft SQL Server (MSSQL) | https://kysely-org.github.io/kysely-apidoc/classes/MssqlDialect.html |
| SQLite | https://kysely-org.github.io/kysely-apidoc/classes/SqliteDialect.html |

## 3rd party dialects
Expand Down
30 changes: 26 additions & 4 deletions site/docs/getting-started/Dialects.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Tabs from '@theme/Tabs'
import { IUseADifferentPackageManager } from './IUseADifferentPackageManager'
import {
DRIVER_NPM_PACKAGE_NAMES,
DRIVER_ADDITIONAL_NPM_PACKAGE_NAMES,
getBashCommand,
getDenoCommand,
isDialectSupported,
Expand Down Expand Up @@ -42,6 +43,10 @@ const builtInDialects: BuiltInDialect[] = [
driverDocsURL:
'https://github.com/WiseLibs/better-sqlite3/blob/master/docs/api.md',
},
{
value: 'mssql',
driverDocsURL: 'https://tediousjs.github.io/tedious/index.html',
},
]

export function Dialects(props: DialectsProps) {
Expand All @@ -55,9 +60,10 @@ export function Dialects(props: DialectsProps) {
it. This requires a <code>Dialect</code> implementation.
<br />
<br />
There are 3 built-in Node.js dialects for PostgreSQL, MySQL and SQLite.
Additionally, the community has implemented several dialects to choose
from. Find out more at <Link to="/docs/dialects">"Dialects"</Link>.
There are 4 built-in dialects for PostgreSQL, MySQL, Microsoft SQL Server
(MSSQL), and SQLite. Additionally, the community has implemented several
dialects to choose from. Find out more at{' '}
<Link to="/docs/dialects">"Dialects"</Link>.
</p>
<Heading as="h3">Driver installation</Heading>
<p>
Expand All @@ -69,6 +75,7 @@ export function Dialects(props: DialectsProps) {
<Tabs queryString="dialect">
{builtInDialects.map(({ driverDocsURL, value }) => {
const driverNPMPackage = DRIVER_NPM_PACKAGE_NAMES[value]
const additionalPackages = DRIVER_ADDITIONAL_NPM_PACKAGE_NAMES[value]
const prettyDialectName = PRETTY_DIALECT_NAMES[value]
const installationCommand =
packageManager === 'deno'
Expand All @@ -77,7 +84,11 @@ export function Dialects(props: DialectsProps) {
[`${driverNPMPackage}-pool`]:
driverNPMPackage === 'pg' ? 'npm:pg-pool' : undefined,
})
: getBashCommand(packageManager, driverNPMPackage)
: getBashCommand(
packageManager,
driverNPMPackage,
additionalPackages
)

return (
// @ts-ignore For some odd reason, TabItem doesn't accept children in this file.
Expand All @@ -97,6 +108,17 @@ export function Dialects(props: DialectsProps) {
<Link to={driverDocsURL}>official documentation</Link> for
configuration options.
</p>
{additionalPackages && value === 'mssql' ? (
<p>
Additionally, kysely's {prettyDialectName} dialect uses
gittgott marked this conversation as resolved.
Show resolved Hide resolved
the "{additionalPackages[0]}" resource pool package for
connection pooling. Please refer to its{' '}
<Link to="https://github.com/vincit/tarn.js">
official documentation
</Link>{' '}
for configuration options.
</p>
) : null}
<p>
<strong>{installationCommand.intro}</strong>
</p>
Expand Down
42 changes: 42 additions & 0 deletions site/docs/getting-started/Instantiation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { IUseADifferentDatabase } from './IUseADifferentDatabase'
import { IUseADifferentPackageManager } from './IUseADifferentPackageManager'
import {
DRIVER_NPM_PACKAGE_NAMES,
DRIVER_ADDITIONAL_NPM_PACKAGE_NAMES,
isDialectSupported,
PRETTY_PACKAGE_MANAGER_NAMES,
type Dialect,
Expand Down Expand Up @@ -57,6 +58,45 @@ const dialect = new SqliteDialect({
} out of the box. Import a community dialect that does here. */
import { Kysely } from 'kysely'

const dialect = /* instantiate the dialect here */`,
mssql: (packageManager) =>
isDialectSupported('mssql', packageManager)
? `import * as Tedious from '${DRIVER_NPM_PACKAGE_NAMES.mssql}'
import * as Tarn from '${DRIVER_ADDITIONAL_NPM_PACKAGE_NAMES.mssql[0]}'
import { Kysely, MssqlDialect } from 'kysely'

const dialect = new MssqlDialect({
tarn: {
...Tarn,
options: {
min: 0,
max: 10,
},
},
tedious: {
...Tedious,
connectionFactory: () => new Tedious.Connection({
authentication: {
options: {
password: 'password',
userName: 'username',
},
type: 'default',
},
options: {
database: 'some_db',
port: 1433,
trustServerCertificate: true,
},
server: 'localhost',
}),
},
})`
: `/* Kysely doesn't support MSSQL + ${
PRETTY_PACKAGE_MANAGER_NAMES[packageManager || 'npm']
} out of the box. Import a community dialect that does here. */
import { Kysely } from 'kysely'

const dialect = /* instantiate the dialect here */`,
}

Expand All @@ -68,6 +108,8 @@ const dialectClassNames: Record<
mysql: () => 'MysqlDialect',
sqlite: (packageManager) =>
isDialectSupported('sqlite', packageManager) ? 'SqliteDialect' : null,
mssql: (packageManager) =>
isDialectSupported('mssql', packageManager) ? 'MssqlDialect' : null,
}

export function Instantiation(
Expand Down
10 changes: 10 additions & 0 deletions site/docs/getting-started/Summary.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ const dialectSpecificCodeSnippets: Record<Dialect, string> = {
cb.notNull().defaultTo(sql\`current_timestamp\`)
)
.execute()`,
// TODO: Update line 42's IDENTITY once identity(1,1) is added to core.
mssql: ` await db.schema.createTable('person')
.addColumn('id', 'integer', (cb) => cb.primaryKey().modifyEnd(sql\`IDENTITY(1,1)\`))
gittgott marked this conversation as resolved.
Show resolved Hide resolved
.addColumn('first_name', 'varchar(255)', (cb) => cb.notNull())
.addColumn('last_name', 'varchar(255)')
.addColumn('gender', 'varchar(50)', (cb) => cb.notNull())
.addColumn('created_at', 'datetime', (cb) =>
cb.notNull().defaultTo(sql\`GETDATE()\`)
)
.execute()`,
}

export function Summary(props: PropsWithDialect) {
Expand Down
27 changes: 22 additions & 5 deletions site/docs/getting-started/shared.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { type ReactNode } from 'react'
import packageJson from '../../package.json'

export type Dialect = 'postgresql' | 'mysql' | 'sqlite'
export type Dialect = 'postgresql' | 'mysql' | 'sqlite' | 'mssql'

export type PropsWithDialect<P = {}> = P & {
dialect: Dialect | undefined
Expand All @@ -12,8 +12,8 @@ export type PackageManager = 'npm' | 'pnpm' | 'yarn' | 'deno' | 'bun'

const PACKAGE_MANAGER_UNSUPPORTED_DIALECTS: Record<PackageManager, Dialect[]> =
{
bun: ['sqlite'],
deno: ['sqlite'],
bun: ['sqlite', 'mssql'],
deno: ['sqlite', 'mssql'],
npm: [],
pnpm: [],
yarn: [],
Expand All @@ -30,12 +30,24 @@ export const DRIVER_NPM_PACKAGE_NAMES: Record<Dialect, string> = {
postgresql: 'pg',
mysql: 'mysql2',
sqlite: 'better-sqlite3',
mssql: 'tedious',
}

export const DRIVER_ADDITIONAL_NPM_PACKAGE_NAMES: Record<
Dialect,
string[] | undefined
> = {
postgresql: undefined,
mysql: undefined,
sqlite: undefined,
mssql: ['tarn'],
}

export const PRETTY_DIALECT_NAMES: Record<Dialect, string> = {
postgresql: 'PostgreSQL',
mysql: 'MySQL',
sqlite: 'SQLite',
mssql: 'Microsoft SQL Server (MSSQL)',
}

export const PRETTY_PACKAGE_MANAGER_NAMES: Record<PackageManager, string> = {
Expand Down Expand Up @@ -65,14 +77,19 @@ export interface Command {

export function getBashCommand(
packageManager: PackageManager,
installedPackage: string
installedPackage: string,
additionalPackages?: string[]
): Command {
if (packageManager === 'deno') {
throw new Error('Deno has no bash command')
}

return {
content: `${PACKAGE_MANAGER_INSTALL_COMMANDS[packageManager]} ${installedPackage}`,
content: `${
PACKAGE_MANAGER_INSTALL_COMMANDS[packageManager]
} ${installedPackage} ${
additionalPackages ? additionalPackages.join(' ') : ''
}`,
intro: 'Run the following command in your terminal:',
language: 'bash',
title: 'terminal',
Expand Down
Loading