-
Notifications
You must be signed in to change notification settings - Fork 428
Expand file tree
/
Copy pathInstantiation.tsx
More file actions
223 lines (198 loc) · 6.67 KB
/
Copy pathInstantiation.tsx
File metadata and controls
223 lines (198 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import Admonition from '@theme/Admonition'
import CodeBlock from '@theme/CodeBlock'
import { IUseADifferentDialect } from './IUseADifferentDialect'
import { IUseADifferentPackageManager } from './IUseADifferentPackageManager'
import {
getDriverNPMPackageNames,
isDialectSupported,
POOL_NPM_PACKAGE_NAMES,
PRETTY_PACKAGE_MANAGER_NAMES,
type Dialect,
type PackageManager,
type PropsWithDialect,
DIALECT_CLASS_NAMES,
PRETTY_DIALECT_NAMES,
type PropsWithPackageManager,
useSearchState,
DIALECTS,
DEFAULT_PACKAGE_MANAGER,
DEFAULT_DIALECT,
PACKAGE_MANAGERS,
} from './shared'
export type InstantiationProps = PropsWithDialect<PropsWithPackageManager>
export function Instantiation(props: InstantiationProps) {
const { dialectSelectionID, packageManagerSelectionID } = props
const dialect = useSearchState({
defaultValue: DEFAULT_DIALECT,
searchParam: props.dialectSearchParam,
validator: (value) => DIALECTS.includes(value as never),
value: props.dialect,
})
const packageManager = useSearchState({
defaultValue: DEFAULT_PACKAGE_MANAGER,
searchParam: props.packageManagerSearchParam,
validator: (value) => PACKAGE_MANAGERS.includes(value as never),
value: props.packageManager,
})
const dialectSpecificCodeSnippet = !isDialectSupported(
dialect,
packageManager,
)
? getNotSupportedCode(dialect, packageManager)
: getDialectSpecificCodeSnippet(dialect, packageManager)
const dialectClassName = DIALECT_CLASS_NAMES[dialect]
return (
<>
<p>
<strong>Let's create a Kysely instance</strong>
{isDialectSupported(dialect, packageManager) ? (
<>
<strong> using the built-in </strong>
<code>{dialectClassName}</code>
<strong> dialect</strong>
</>
) : (
<strong> assuming a compatible community dialect exists</strong>
)}
<strong>:</strong>
</p>
<CodeBlock language="ts" title="src/database.ts">
{`import { Database } from './types.ts' // this is the Database interface we defined earlier
${dialectSpecificCodeSnippet}
// Database interface is passed to Kysely's constructor, and from now on, Kysely
// knows your database structure.
// Dialect is passed to Kysely's constructor, and from now on, Kysely knows how
// to communicate with your database.
export const db = new Kysely<Database>({
dialect,
})`}
</CodeBlock>
{dialectSelectionID || packageManagerSelectionID ? (
<div style={{ display: 'flex', gap: '25px', justifyContent: 'end' }}>
<IUseADifferentPackageManager
packageManager={packageManager}
packageManagerSelectionID={packageManagerSelectionID}
/>
<IUseADifferentDialect
dialect={dialect}
dialectSelectionID={dialectSelectionID}
/>
</div>
) : null}
<Admonition type="tip" title="Singleton">
In most cases, you should only create a single Kysely instance per
database. Most dialects use a connection pool internally, or no
connections at all, so there's no need to create a new instance for each
request.
</Admonition>
<Admonition type="warning" title="keeping secrets">
Use a secrets manager, environment variables (DO NOT commit `.env` files
to your repository), or a similar solution, to avoid hardcoding database
credentials in your code.
</Admonition>
<Admonition type="info" title="kill it with fire">
When needed, you can dispose of the Kysely instance, release resources
and close all connections by invoking the <code>db.destroy()</code>{' '}
function.
</Admonition>
</>
)
}
function getNotSupportedCode(
dialect: Dialect,
packageManager: PackageManager,
): string {
return `/* Kysely doesn't support ${PRETTY_DIALECT_NAMES[dialect]} + ${
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 */`
}
function getDialectSpecificCodeSnippet(
dialect: Dialect,
packageManager: PackageManager,
): string {
const driverNPMPackageName = getDriverNPMPackageNames(packageManager)[dialect]
const dialectClassName = DIALECT_CLASS_NAMES[dialect]
const poolClassName = 'Pool'
const poolClassImport =
packageManager === 'deno' ? poolClassName : `{ ${poolClassName} }`
if (dialect === 'postgresql') {
return `import ${poolClassImport} from '${driverNPMPackageName}'
import { Kysely, ${dialectClassName} } from 'kysely'
const dialect = new ${dialectClassName}({
pool: new ${poolClassName}({
database: 'test',
host: 'localhost',
user: 'admin',
port: 5434,
max: 10,
})
})`
}
if (dialect === 'mysql') {
const poolFactoryName = 'createPool'
return `import { ${poolFactoryName} } from '${driverNPMPackageName}/promise' // or just '${driverNPMPackageName}' — both work
import { Kysely, ${dialectClassName} } from 'kysely'
const dialect = new ${dialectClassName}({
pool: ${poolFactoryName}({
database: 'test',
host: 'localhost',
user: 'admin',
password: '123',
port: 3308,
connectionLimit: 10,
})
})`
}
if (dialect === 'mssql') {
const poolPackageName = POOL_NPM_PACKAGE_NAMES.mssql
return `import * as ${driverNPMPackageName} from '${driverNPMPackageName}'
import * as ${poolPackageName} from '${poolPackageName}'
import { Kysely, ${dialectClassName} } from 'kysely'
const dialect = new ${dialectClassName}({
${poolPackageName}: {
...${poolPackageName},
options: {
min: 0,
max: 10,
},
},
${driverNPMPackageName}: {
...${driverNPMPackageName},
connectionFactory: () => new ${driverNPMPackageName}.Connection({
authentication: {
options: {
password: 'password',
userName: 'username',
},
type: 'default',
},
options: {
database: 'some_db',
port: 1433,
trustServerCertificate: true,
},
server: 'localhost',
}),
},
})`
}
if (dialect === 'sqlite') {
const driverImportName = 'SQLite'
return `import ${driverImportName} from '${driverNPMPackageName}'
import { Kysely, ${dialectClassName} } from 'kysely'
const dialect = new ${dialectClassName}({
database: new ${driverImportName}(':memory:'),
})`
}
if (dialect === 'pglite') {
const driverImportName = 'PGlite'
return `import { ${driverImportName} } from '${driverNPMPackageName}'
import { Kysely, ${dialectClassName} } from 'kysely'
const dialect = new ${dialectClassName}({
pglite: new ${driverImportName}(),
})`
}
throw new Error(`Unsupported dialect: ${dialect}`)
}