-
Notifications
You must be signed in to change notification settings - Fork 309
SQLite #19
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
Comments
Thank you 🍻 You can take a look at the postgres dialect to see how that's implemented and go from there if you feel like it. I was thinking that we should probably use better-sqlite as the driver. Let me know if you start working on it. |
To be fair, I started with SQLite literally yesterday, after I discovered awesome jlongster/absurd-sql. I need to refresh my SQLness, but I'm quite versatile in functional TypeScript. That's why I love your project so much. I will start with the postgres first and probably in the next few weeks I will be able to actively do something. I will let you know. Thank you again. |
Some notes that may help: So basically you need to implement the four interfaces returned by the different methods of the export class SqliteDialect implements Dialect {
readonly #config: SqliteDialectConfig
constructor(config: SqliteDialectConfig) {
this.#config = config
}
createDriver(): Driver {
return new SqliteDriver(this.#config)
}
createQueryCompiler(): QueryCompiler {
return new SqliteQueryCompiler()
}
createAdapter(): DialectAdapter {
return new SqliteAdapter()
}
createIntrospector(db: Kysely<any>): DatabaseIntrospector {
return new SqliteIntrospector(db)
}
}
// This interface needs to be a copy-paste of the sqlite driver config.
// We can't use and export the actual type from sqlite types because
// then all Kysely users would need to install sqlite types even if they
// use some other dialect.
export interface SqliteDialectConfig {
} The interfaces are DialectAdapterThis one is easy on sqlite: export class SqliteAdapter implements DialectAdapter {
get supportsTransactionalDdl(): boolean {
return true
}
get supportsReturning(): boolean {
return false
}
async acquireMigrationLock(db: Kysely<any>): Promise<void> {
// Nothing to do here.
}
async releaseMigrationLock(db: Kysely<any>): Promise<void> {
// Nothing to do here.
}
} QueryCompilerIt's easy to get a basic version going by doing something like this: export class SqliteQueryCompiler extends DefaultQueryCompiler {
protected override visitColumnDefinition(node: ColumnDefinitionNode): void {
this.visitNode(node.column)
this.append(' ')
this.visitNode(node.dataType)
if (node.defaultTo) {
this.append(' default ')
this.visitNode(node.defaultTo)
}
if (!node.isNullable) {
this.append(' not null')
}
if (node.isAutoIncrementing) {
this.append(' autoincrement')
}
if (node.isUnique) {
this.append(' unique')
}
if (node.isPrimaryKey) {
this.append(' primary key')
}
if (node.references) {
this.append(' ')
this.visitNode(node.references)
}
if (node.check) {
this.append(' ')
this.visitNode(node.check)
}
}
protected override visitReturning(): void {
// Do nothing.
}
protected override getCurrentParameterPlaceholder() {
return '?'
}
} Then there are probably some other stuff you need to add to the query compiler, bu the above should get you started. DriverThe driver is the actual glue between Kysely and the export class SqliteDriver implements Driver {
init(): Promise<void> {
// Here you should import and initialize the better-sqlite3 library.
// See how it's done in the other drivers.
}
acquireConnection(): Promise<DatabaseConnection> {
// This should only ever allow one connection to be acquired! Once
// a connection is acquired, all calls to this method should await for the
// single connection to be released and then the next one in the queue
// gets the connection. This is because Sqlite basically has one single
// connection (actually it doesn't have the concept of connections at all).
}
beginTransaction(
connection: DatabaseConnection,
settings: TransactionSettings
): Promise<void> {
// This you can copy paste from postgres.
}
commitTransaction(connection: DatabaseConnection): Promise<void> {
// This you can copy paste from postgres.
}
rollbackTransaction(connection: DatabaseConnection): Promise<void> {
// This you can copy paste from postgres.
}
releaseConnection(connection: DatabaseConnection): Promise<void> {
// See acquireConnection
}
destroy(): Promise<void> {
// Destroy and release everything.
}
} DatabaseIntrospectorThis one should probably use this and to get the columns, something like |
@koskimas Awesome, thank you! Unfortunately, I'm blocked by sql-js/sql.js#481, small changes for jlongster/absurd-sql or I would have to write some non-standard driver. Hmm... maybe I can use generated SQL only for now. I will try it. |
Ok, so you want to run this in a browser against sql.js? That's not something that should be added to the core. Feel free to create a third party dialect though. Kysely is primarily a node.js library and browser compatibility is not considered at all at the moment. |
@koskimas I agree. But SQL generation shouldn't be tied to Node.js I suppose, am I correct? API should be the same so third-party dialect could help someone write server version I guess. Anyway, thank you for the hints and the awesome lib. |
Sure, everything except the |
I will take a look at what should be extended... |
You need to |
My use case is special, I admit that, but it would be great to have QueryCompiler in the browser. The lib has hardcoded dependencies to mysql2 and pg. I can instruct Webpack to remove them, then it works in the browser (it adds 24kb gzipped) :-) config.resolve.fallback = {
...config.resolve.fallback,
mysql2: false,
pg: false,
}; There is a warning |
Yeah, can't really remove those I'm afraid. Only way to remove them is to have zero dialects supported out of the box . Those packages are only imported conditionally if you use the dialect. Otherwise they are never used. You can safely ignore all but the dialect you are using. |
So I hacked it and now I use the whole kysely in the browser. The panacea for the local data 😊 |
@koskimas What is the proper way to add WITHOUT ROWID which is SQLite only feature? |
Kysely has some features specific to postgres and mysql as well. I'd add a |
This project is so awesome, thank you!
How can I help with SQLite?
The text was updated successfully, but these errors were encountered: