forked from drizzle-team/drizzle-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Separated different libsql versions into different import paths, adapted tests, added `@libsql/client-wasm` peer and dev dep * Switched default libsql driver initializer to new initializer type, adapted tests * Fixed tests * Excluded ESM-only libsql/wasm package from CJS import tests * Tests for libsql submodules * Fixed imports in tests * Added remote libsql envs * Modified workflows to include required env params * Modified tests for `libsql/http`,`libsql/ws` * Crash prevention on tests after failed view tests in sqlite, increased msDelay for `libsql/ws`/`libsql/http` tests * Add 0.35.3 version release notes * Fixed planetscale driver rejecting `Connection` on type level * No Connection in planetscale, only client * Remove connection tests --------- Co-authored-by: AndriiSherman <[email protected]>
- Loading branch information
1 parent
6c4c3c5
commit 0fcf975
Showing
23 changed files
with
1,189 additions
and
248 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# New LibSQL driver modules | ||
|
||
Drizzle now has native support for all `@libsql/client` driver variations: | ||
|
||
1. `@libsql/client` - defaults to node import, automatically changes to web if target or platform is set for bundler, e.g. `esbuild --platform=browser` | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` | ||
|
||
2. `@libsql/client/node` node compatible module, supports :memory:, file, wss, http and turso connection protocols | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql/node'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` | ||
|
||
3. `@libsql/client/web` module for fullstack web frameworks like next, nuxt, astro, etc. | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql/web'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` | ||
|
||
4. `@libsql/client/http` module for http and https connection protocols | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql/http'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` | ||
|
||
5. `@libsql/client/ws` module for ws and wss connection protocols | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql/ws'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` | ||
|
||
6. `@libsql/client/sqlite3` module for :memory: and file connection protocols | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql/wasm'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` | ||
|
||
7. `@libsql/client-wasm` Separate experimental package for WASM | ||
|
||
```ts | ||
import { drizzle } from 'drizzle-orm/libsql'; | ||
|
||
const db = drizzle({ connection: { | ||
url: process.env.DATABASE_URL, | ||
authToken: process.env.DATABASE_AUTH_TOKEN | ||
}}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import type { Client, ResultSet } from '@libsql/client'; | ||
import type { BatchItem, BatchResponse } from '~/batch.ts'; | ||
import { entityKind } from '~/entity.ts'; | ||
import { DefaultLogger } from '~/logger.ts'; | ||
import { | ||
createTableRelationsHelpers, | ||
extractTablesRelationalConfig, | ||
type ExtractTablesWithRelations, | ||
type RelationalSchemaConfig, | ||
type TablesRelationalConfig, | ||
} from '~/relations.ts'; | ||
import { BaseSQLiteDatabase } from '~/sqlite-core/db.ts'; | ||
import { SQLiteAsyncDialect } from '~/sqlite-core/dialect.ts'; | ||
import type { DrizzleConfig } from '~/utils.ts'; | ||
import { LibSQLSession } from './session.ts'; | ||
|
||
export class LibSQLDatabase< | ||
TSchema extends Record<string, unknown> = Record<string, never>, | ||
> extends BaseSQLiteDatabase<'async', ResultSet, TSchema> { | ||
static override readonly [entityKind]: string = 'LibSQLDatabase'; | ||
|
||
/** @internal */ | ||
declare readonly session: LibSQLSession<TSchema, ExtractTablesWithRelations<TSchema>>; | ||
|
||
async batch<U extends BatchItem<'sqlite'>, T extends Readonly<[U, ...U[]]>>( | ||
batch: T, | ||
): Promise<BatchResponse<T>> { | ||
return this.session.batch(batch) as Promise<BatchResponse<T>>; | ||
} | ||
} | ||
|
||
/** @internal */ | ||
export function construct< | ||
TSchema extends Record<string, unknown> = Record<string, never>, | ||
>(client: Client, config: DrizzleConfig<TSchema> = {}): LibSQLDatabase<TSchema> & { | ||
$client: Client; | ||
} { | ||
const dialect = new SQLiteAsyncDialect({ casing: config.casing }); | ||
let logger; | ||
if (config.logger === true) { | ||
logger = new DefaultLogger(); | ||
} else if (config.logger !== false) { | ||
logger = config.logger; | ||
} | ||
|
||
let schema: RelationalSchemaConfig<TablesRelationalConfig> | undefined; | ||
if (config.schema) { | ||
const tablesConfig = extractTablesRelationalConfig( | ||
config.schema, | ||
createTableRelationsHelpers, | ||
); | ||
schema = { | ||
fullSchema: config.schema, | ||
schema: tablesConfig.tables, | ||
tableNamesMap: tablesConfig.tableNamesMap, | ||
}; | ||
} | ||
|
||
const session = new LibSQLSession(client, dialect, schema, { logger }, undefined); | ||
const db = new LibSQLDatabase('async', dialect, session, schema) as LibSQLDatabase<TSchema>; | ||
(<any> db).$client = client; | ||
|
||
return db as any; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { type Client, type Config, createClient } from '@libsql/client/http'; | ||
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts'; | ||
import { construct, type LibSQLDatabase } from '../driver-core.ts'; | ||
|
||
export function drizzle< | ||
TSchema extends Record<string, unknown> = Record<string, never>, | ||
TClient extends Client = Client, | ||
>( | ||
...params: IfNotImported< | ||
Client, | ||
[ImportTypeError<'@libsql/client'>], | ||
[ | ||
TClient | string, | ||
] | [ | ||
TClient | string, | ||
DrizzleConfig<TSchema>, | ||
] | [ | ||
( | ||
& DrizzleConfig<TSchema> | ||
& ({ | ||
connection: string | Config; | ||
} | { | ||
client: TClient; | ||
}) | ||
), | ||
] | ||
> | ||
): LibSQLDatabase<TSchema> & { | ||
$client: TClient; | ||
} { | ||
if (typeof params[0] === 'string') { | ||
const instance = createClient({ | ||
url: params[0], | ||
}); | ||
|
||
return construct(instance, params[1]) as any; | ||
} | ||
|
||
if (isConfig(params[0])) { | ||
const { connection, client, ...drizzleConfig } = params[0] as | ||
& { connection?: Config; client?: TClient } | ||
& DrizzleConfig<TSchema>; | ||
|
||
if (client) return construct(client, drizzleConfig) as any; | ||
|
||
const instance = typeof connection === 'string' ? createClient({ url: connection }) : createClient(connection!); | ||
|
||
return construct(instance, drizzleConfig) as any; | ||
} | ||
|
||
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any; | ||
} | ||
|
||
export namespace drizzle { | ||
export function mock<TSchema extends Record<string, unknown> = Record<string, never>>( | ||
config?: DrizzleConfig<TSchema>, | ||
): LibSQLDatabase<TSchema> & { | ||
$client: '$client is not available on drizzle.mock()'; | ||
} { | ||
return construct({} as any, config) as any; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { type Client, type Config, createClient } from '@libsql/client/node'; | ||
import { type DrizzleConfig, type IfNotImported, type ImportTypeError, isConfig } from '~/utils.ts'; | ||
import { construct, type LibSQLDatabase } from '../driver-core.ts'; | ||
|
||
export function drizzle< | ||
TSchema extends Record<string, unknown> = Record<string, never>, | ||
TClient extends Client = Client, | ||
>( | ||
...params: IfNotImported< | ||
Client, | ||
[ImportTypeError<'@libsql/client'>], | ||
[ | ||
TClient | string, | ||
] | [ | ||
TClient | string, | ||
DrizzleConfig<TSchema>, | ||
] | [ | ||
( | ||
& DrizzleConfig<TSchema> | ||
& ({ | ||
connection: string | Config; | ||
} | { | ||
client: TClient; | ||
}) | ||
), | ||
] | ||
> | ||
): LibSQLDatabase<TSchema> & { | ||
$client: TClient; | ||
} { | ||
if (typeof params[0] === 'string') { | ||
const instance = createClient({ | ||
url: params[0], | ||
}); | ||
|
||
return construct(instance, params[1]) as any; | ||
} | ||
|
||
if (isConfig(params[0])) { | ||
const { connection, client, ...drizzleConfig } = params[0] as | ||
& { connection?: Config; client?: TClient } | ||
& DrizzleConfig<TSchema>; | ||
|
||
if (client) return construct(client, drizzleConfig) as any; | ||
|
||
const instance = typeof connection === 'string' ? createClient({ url: connection }) : createClient(connection!); | ||
|
||
return construct(instance, drizzleConfig) as any; | ||
} | ||
|
||
return construct(params[0] as TClient, params[1] as DrizzleConfig<TSchema> | undefined) as any; | ||
} | ||
|
||
export namespace drizzle { | ||
export function mock<TSchema extends Record<string, unknown> = Record<string, never>>( | ||
config?: DrizzleConfig<TSchema>, | ||
): LibSQLDatabase<TSchema> & { | ||
$client: '$client is not available on drizzle.mock()'; | ||
} { | ||
return construct({} as any, config) as any; | ||
} | ||
} |
Oops, something went wrong.