Skip to content

Commit

Permalink
Merge pull request #1033 from drizzle-team/sqlite-async-api
Browse files Browse the repository at this point in the history
  • Loading branch information
dankochetov authored Aug 13, 2023
2 parents ddb1408 + c063144 commit 07400cc
Show file tree
Hide file tree
Showing 57 changed files with 4,526 additions and 2,513 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
name: "CodeQL"

on:
push:
branches: [ 'main', 'beta' ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ 'main' ]
schedule:
- cron: '44 16 * * 0'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'javascript' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Use only 'java' to analyze code written in Java, Kotlin or both
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.

# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
# queries: security-extended,security-and-quality


# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun

# If the Autobuild fails above, remove it and uncomment the following three lines.
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.

# - run: |
# echo "Run, Build Application using script"
# ./location_of_script_within_repo/buildscript.sh

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{matrix.language}}"
6 changes: 5 additions & 1 deletion .github/workflows/release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
package:
- drizzle-orm
- drizzle-zod
- drizzle-typebox
runs-on: ubuntu-20.04
permissions:
contents: read
Expand Down Expand Up @@ -88,10 +89,13 @@ jobs:
PLANETSCALE_CONNECTION_STRING: ${{ secrets.PLANETSCALE_CONNECTION_STRING }}
LIBSQL_URL: file:local.db
run: |
if [[ ${{ github.event_name }} != "push" && "${{ github.event.pull_request.head.repo.full_name }}" != "${{ github.repository }}" ]]; then
export SKIP_PLANETSCALE_TESTS=1
fi
if [[ "${{ matrix.package }}" == "drizzle-orm" ]]; then
pnpm test --filter ${{ matrix.package }} --filter integration-tests
else
pnpm test --filter ${{ matrix.package }}
pnpm test --filter ${{ matrix.package }}
fi
- name: Publish
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-latest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ jobs:
package:
- drizzle-orm
- drizzle-zod
- drizzle-typebox
runs-on: ubuntu-20.04
services:
postgres:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/unpublish-release-feature-branch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
package:
- drizzle-orm
- drizzle-zod
- drizzle-typebox
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3
Expand Down
1 change: 1 addition & 0 deletions changelogs/drizzle-orm/0.28.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- 🐛 Fixed Postgres array-related issues introduced by 0.28.0 (#983, #992)
16 changes: 16 additions & 0 deletions changelogs/drizzle-orm/0.28.2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
## The community contributions release 🎉

### Internal Features and Changes
1. Added a set of tests for d1. Thanks to @AdiRishi!
2. Fixed issues in internal documentation. Thanks to @balazsorban44 and @pyk!

### Bug Fixes
1. Resolved the issue of truncating timestamp milliseconds for MySQL. Thanks to @steviec!
2. Corrected the type of the get() method for sqlite-based dialects. Issue #565 has been closed. Thanks to @stefanmaric!
3. Rectified the sqlite-proxy bug that caused the query to execute twice. Thanks to @mosch!

### New packages 🎉

Added a support for [Typebox](https://github.com/sinclairzx81/typebox) in [drizzle-typebox](https://orm.drizzle.team/docs/typebox) package. Thanks to @Bulbang!

Please check documentation page for more usage examples: https://orm.drizzle.team/docs/typebox
3 changes: 3 additions & 0 deletions changelogs/drizzle-typebox/0.1.0.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# drizzle-typebox 0.1.0

- Initial release
2 changes: 1 addition & 1 deletion drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "drizzle-orm",
"version": "0.28.0",
"version": "0.28.2",
"description": "Drizzle ORM package for SQL databases",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/alias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { SQL, sql } from './sql';
import { Table } from './table';
import { type View, ViewBaseConfig } from './view';

export class ColumnAliasProxyHandler<TColumn extends AnyColumn> implements ProxyHandler<TColumn> {
export class ColumnAliasProxyHandler<TColumn extends Column> implements ProxyHandler<TColumn> {
static readonly [entityKind]: string = 'ColumnAliasProxyHandler';

constructor(private table: Table | View) {}
Expand Down
19 changes: 13 additions & 6 deletions drizzle-orm/src/better-sqlite3/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,13 @@ import { fillPlaceholders, type Query, sql } from '~/sql';
import { SQLiteTransaction } from '~/sqlite-core';
import type { SQLiteSyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import type { PreparedQueryConfig as PreparedQueryConfigBase, SQLiteTransactionConfig } from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
import {
PreparedQuery as PreparedQueryBase,
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
SQLiteSession,
type SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { mapResultRow } from '~/utils';

export interface BetterSQLiteSessionOptions {
Expand Down Expand Up @@ -38,10 +43,11 @@ export class BetterSQLiteSession<
prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, customResultMapper);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}

override transaction<T>(
Expand Down Expand Up @@ -76,7 +82,7 @@ export class BetterSQLiteTransaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: RunResult; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'sync'; run: RunResult; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'BetterSQLitePreparedQuery';

Expand All @@ -86,9 +92,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private params: unknown[],
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super();
super('sync', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): RunResult {
Expand All @@ -105,7 +112,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return stmt.all(...params);
}

const rows = this.values(placeholderValues);
const rows = this.values(placeholderValues) as unknown[][];
if (customResultMapper) {
return customResultMapper(rows) as T['all'];
}
Expand Down
18 changes: 12 additions & 6 deletions drizzle-orm/src/bun-sqlite/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import { fillPlaceholders, type Query, sql } from '~/sql';
import { SQLiteTransaction } from '~/sqlite-core';
import type { SQLiteSyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import type { PreparedQueryConfig as PreparedQueryConfigBase, SQLiteTransactionConfig } from '~/sqlite-core/session';
import type {
PreparedQueryConfig as PreparedQueryConfigBase,
SQLiteExecuteMethod,
SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
import { mapResultRow } from '~/utils';

Expand Down Expand Up @@ -44,11 +48,12 @@ export class SQLiteBunSession<

prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields?: SelectedFieldsOrdered,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, customResultMapper);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}

override transaction<T>(
Expand Down Expand Up @@ -87,7 +92,7 @@ export class SQLiteBunTransaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'sync'; run: void; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'SQLiteBunPreparedQuery';

Expand All @@ -97,9 +102,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private params: unknown[],
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super();
super('sync', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): void {
Expand All @@ -116,7 +122,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return stmt.all(...params);
}

const rows = this.values(placeholderValues);
const rows = this.values(placeholderValues) as unknown[][];

if (customResultMapper) {
return customResultMapper(rows) as T['all'];
Expand Down
2 changes: 1 addition & 1 deletion drizzle-orm/src/column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export type ColumnTypeConfig<T extends ColumnBaseConfig<ColumnDataType, string>,
notNull: T['notNull'];
hasDefault: T['hasDefault'];
enumValues: T['enumValues'];
baseColumn: T extends { baseColumn: infer U } ? U : never;
baseColumn: T extends { baseColumn: infer U } ? U : unknown;
} & TTypeConfig;

export type ColumnRuntimeConfig<TData, TRuntimeConfig extends object> = ColumnBuilderRuntimeConfig<
Expand Down
11 changes: 7 additions & 4 deletions drizzle-orm/src/d1/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import {
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
type SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
Expand Down Expand Up @@ -42,11 +43,12 @@ export class SQLiteD1Session<

prepareQuery(
query: Query,
fields?: SelectedFieldsOrdered,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery {
const stmt = this.client.prepare(query.sql);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, customResultMapper);
return new PreparedQuery(stmt, query.sql, query.params, this.logger, fields, executeMethod, customResultMapper);
}

override async transaction<T>(
Expand Down Expand Up @@ -88,7 +90,7 @@ export class D1Transaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'async'; run: D1Result; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'async'; run: D1Result; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'D1PreparedQuery';

Expand All @@ -98,9 +100,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private params: unknown[],
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][]) => unknown,
) {
super();
super('async', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): Promise<D1Result> {
Expand Down
24 changes: 18 additions & 6 deletions drizzle-orm/src/libsql/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type { SQLiteAsyncDialect } from '~/sqlite-core/dialect';
import type { SelectedFieldsOrdered } from '~/sqlite-core/query-builders/select.types';
import {
type PreparedQueryConfig as PreparedQueryConfigBase,
type SQLiteExecuteMethod,
type SQLiteTransactionConfig,
} from '~/sqlite-core/session';
import { PreparedQuery as PreparedQueryBase, SQLiteSession } from '~/sqlite-core/session';
Expand Down Expand Up @@ -41,10 +42,20 @@ export class LibSQLSession<

prepareQuery<T extends Omit<PreparedQueryConfig, 'run'>>(
query: Query,
fields: SelectedFieldsOrdered,
fields: SelectedFieldsOrdered | undefined,
executeMethod: SQLiteExecuteMethod,
customResultMapper?: (rows: unknown[][]) => unknown,
): PreparedQuery<T> {
return new PreparedQuery(this.client, query.sql, query.params, this.logger, fields, this.tx, customResultMapper);
return new PreparedQuery(
this.client,
query.sql,
query.params,
this.logger,
fields,
this.tx,
executeMethod,
customResultMapper,
);
}

/*override */ batch(queries: SQL[]): Promise<ResultSet[]> {
Expand Down Expand Up @@ -96,7 +107,7 @@ export class LibSQLTransaction<
}

export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig> extends PreparedQueryBase<
{ type: 'async'; run: ResultSet; all: T['all']; get: T['get']; values: T['values'] }
{ type: 'async'; run: ResultSet; all: T['all']; get: T['get']; values: T['values']; execute: T['execute'] }
> {
static readonly [entityKind]: string = 'LibSQLPreparedQuery';

Expand All @@ -107,9 +118,10 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
private logger: Logger,
private fields: SelectedFieldsOrdered | undefined,
private tx: Transaction | undefined,
executeMethod: SQLiteExecuteMethod,
private customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown,
) {
super();
super('async', executeMethod);
}

run(placeholderValues?: Record<string, unknown>): Promise<ResultSet> {
Expand All @@ -128,7 +140,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return (tx ? tx.execute(stmt) : client.execute(stmt)).then(({ rows }) => rows.map((row) => normalizeRow(row)));
}

const rows = await this.values(placeholderValues);
const rows = await this.values(placeholderValues) as unknown[][];

if (customResultMapper) {
return customResultMapper(rows, normalizeFieldValue) as T['all'];
Expand All @@ -152,7 +164,7 @@ export class PreparedQuery<T extends PreparedQueryConfig = PreparedQueryConfig>
return (tx ? tx.execute(stmt) : client.execute(stmt)).then(({ rows }) => normalizeRow(rows[0]));
}

const rows = await this.values(placeholderValues);
const rows = await this.values(placeholderValues) as unknown[][];

if (!rows[0]) {
return undefined;
Expand Down
Loading

0 comments on commit 07400cc

Please sign in to comment.