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

[v5] Only support better-sqlite3 package for sqlite #18500

Merged
merged 12 commits into from Oct 30, 2023
12 changes: 3 additions & 9 deletions .github/workflows/tests.yml
Expand Up @@ -257,11 +257,10 @@ jobs:
if: needs.changes.outputs.backend == 'true'
runs-on: ubuntu-latest
needs: [changes, build, typescript, unit_back, unit_front]
name: '[CE] API Integration (sqlite, client: ${{ matrix.sqlite_pkg }}, node: ${{ matrix.node }}, shard: ${{ matrix.shard }})'
name: '[CE] API Integration (sqlite, client: better-sqlite3, node: ${{ matrix.node }}, shard: ${{ matrix.shard }})'
strategy:
matrix:
node: [18, 20]
sqlite_pkg: ['better-sqlite3', 'sqlite3']
shard: [1/5, 2/5, 3/5, 4/5, 5/5]
steps:
- uses: actions/checkout@v4
Expand All @@ -273,10 +272,8 @@ jobs:
- name: Monorepo build
uses: ./.github/actions/run-build
- uses: ./.github/actions/run-api-tests
env:
SQLITE_PKG: ${{ matrix.sqlite_pkg }}
with:
dbOptions: '--dbclient=sqlite-legacy --dbfile=./tmp/data.db'
dbOptions: '--dbclient=sqlite --dbfile=./tmp/data.db'
jestOptions: '--shard=${{ matrix.shard }}'

# EE
Expand Down Expand Up @@ -372,14 +369,13 @@ jobs:
api_ee_sqlite:
runs-on: ubuntu-latest
needs: [changes, build, typescript, unit_back, unit_front]
name: '[EE] API Integration (sqlite, client: ${{ matrix.sqlite_pkg }}, node: ${{ matrix.node }}, shard: ${{ matrix.shard }})'
name: '[EE] API Integration (sqlite, client: better-sqlite3, node: ${{ matrix.node }}, shard: ${{ matrix.shard }})'
if: needs.changes.outputs.backend == 'true' && github.event.pull_request.head.repo.full_name == github.repository && !(github.actor == 'dependabot[bot]' || github.actor == 'dependabot-preview[bot]')
env:
STRAPI_LICENSE: ${{ secrets.strapiLicense }}
strategy:
matrix:
node: [18, 20]
sqlite_pkg: ['better-sqlite3', 'sqlite3']
shard: [1/5, 2/5, 3/5, 4/5, 5/5]
steps:
- uses: actions/checkout@v4
Expand All @@ -391,8 +387,6 @@ jobs:
- name: Monorepo build
uses: ./.github/actions/run-build
- uses: ./.github/actions/run-api-tests
env:
SQLITE_PKG: ${{ matrix.sqlite_pkg }}
with:
dbOptions: '--dbclient=sqlite --dbfile=./tmp/data.db'
runEE: true
Expand Down
14 changes: 7 additions & 7 deletions e2e/app-template/package.json
@@ -1,23 +1,23 @@
{
"name": "@strapi/e2e-test-app",
"private": true,
"version": "0.1.0",
"private": true,
"description": "E2E template application",
"license": "MIT",
"scripts": {
"build": "strapi build",
"develop": "strapi develop",
"start": "strapi start",
"build": "strapi build",
"strapi": "strapi"
},
"dependencies": {
"@strapi/strapi": "latest",
"@strapi/plugin-users-permissions": "latest",
"@strapi/plugin-i18n": "latest",
"better-sqlite3": "8.6.0"
"@strapi/plugin-users-permissions": "latest",
"@strapi/strapi": "latest",
"better-sqlite3": "9.0.0"
},
"engines": {
"node": ">=18.0.0 <=20.x.x",
"npm": ">=6.0.0"
},
"license": "MIT"
}
}
3 changes: 1 addition & 2 deletions examples/getstarted/package.json
Expand Up @@ -24,7 +24,7 @@
"@strapi/provider-upload-aws-s3": "4.14.5",
"@strapi/provider-upload-cloudinary": "4.14.5",
"@strapi/strapi": "4.14.5",
"better-sqlite3": "8.6.0",
"better-sqlite3": "9.0.0",
"lodash": "4.17.21",
"mysql": "2.18.1",
"mysql2": "3.6.0",
Expand All @@ -34,7 +34,6 @@
"react-dom": "^18.2.0",
"react-intl": "6.4.1",
"react-router-dom": "5.3.4",
"sqlite3": "5.1.2",
"styled-components": "5.3.3"
},
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion examples/kitchensink-ts/package.json
Expand Up @@ -17,7 +17,7 @@
"@strapi/plugin-i18n": "4.14.5",
"@strapi/plugin-users-permissions": "4.14.5",
"@strapi/strapi": "4.14.5",
"better-sqlite3": "8.6.0",
"better-sqlite3": "9.0.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "5.3.4",
Expand Down
2 changes: 1 addition & 1 deletion examples/kitchensink/package.json
Expand Up @@ -17,6 +17,7 @@
"@strapi/provider-upload-aws-s3": "4.14.5",
"@strapi/provider-upload-cloudinary": "4.14.5",
"@strapi/strapi": "4.14.5",
"better-sqlite3": "9.0.0",
"lodash": "4.17.21",
"mysql": "2.18.1",
"mysql2": "3.6.0",
Expand All @@ -25,7 +26,6 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "5.3.4",
"sqlite3": "5.1.2",
"styled-components": "5.3.3"
},
"engines": {
Expand Down
1 change: 0 additions & 1 deletion packages/core/database/packup.config.ts
Expand Up @@ -8,7 +8,6 @@ export default defineConfig({
* they will be included in the bundle which means they
* will fail...
*/
'knex/lib/dialects/sqlite3/index',
'knex/lib/query/querybuilder',
'knex/lib/raw',
],
Expand Down
59 changes: 10 additions & 49 deletions packages/core/database/src/connection.ts
@@ -1,61 +1,22 @@
import knex from 'knex';
import type { Knex } from 'knex';
import SqliteClient from 'knex/lib/dialects/sqlite3/index';

class LegacySqliteClient extends SqliteClient {
_driver() {
/* eslint-disable-next-line import/no-extraneous-dependencies */
return require('sqlite3');
}
}

const clientMap = {
'better-sqlite3': 'better-sqlite3',
'@vscode/sqlite3': 'sqlite',
sqlite3: LegacySqliteClient,
} as const;

type ClientKey = keyof typeof clientMap;

const trySqlitePackage = (packageName: ClientKey): ClientKey | false => {
try {
require.resolve(packageName);
return packageName;
} catch (error) {
if (error instanceof Error && 'code' in error && error.code === 'MODULE_NOT_FOUND') {
return false;
}
throw error;
}
sqlite: 'better-sqlite3',
mysql: 'mysql',
postgres: 'pg',
};

const getSqlitePackageName = (): ClientKey => {
// NOTE: allow forcing the package to use (mostly used for testing purposes)
if (typeof process.env.SQLITE_PKG !== 'undefined') {
return process.env.SQLITE_PKG as ClientKey;
}

// NOTE: this tries to find the best sqlite module possible to use
// while keeping retro compatibility
const matchingPackage: ClientKey | false =
trySqlitePackage('better-sqlite3') ||
trySqlitePackage('@vscode/sqlite3') ||
trySqlitePackage('sqlite3');

if (!matchingPackage) {
throw new Error('No sqlite package found');
}

return matchingPackage;
};
function isClientValid(config: { client?: unknown }): config is { client: keyof typeof clientMap } {
return Object.keys(clientMap).includes(config.client as string);
}

export const createConnection = (config: Knex.Config) => {
const knexConfig = { ...config };
if (knexConfig.client === 'sqlite') {
const sqlitePackageName = getSqlitePackageName();

knexConfig.client = clientMap[sqlitePackageName] as Knex.Config['client'];
if (!isClientValid(config)) {
throw new Error(`Unsupported database client ${config.client}`);
}

const knexConfig = { ...config, client: (clientMap as any)[config.client] };

return knex(knexConfig);
};
2 changes: 1 addition & 1 deletion packages/generators/app/src/types.ts
Expand Up @@ -45,7 +45,7 @@ export interface Configuration {
dependencies: Record<string, string>;
}

export type ClientName = 'mysql' | 'mysql2' | 'postgres' | 'sqlite' | 'sqlite-legacy';
export type ClientName = 'mysql' | 'mysql2' | 'postgres' | 'sqlite';

export interface DatabaseInfo {
client?: string;
Expand Down
3 changes: 1 addition & 2 deletions packages/generators/app/src/utils/db-client-dependencies.ts
Expand Up @@ -4,8 +4,7 @@ const sqlClientModule = {
mysql: { mysql: '2.18.1' },
mysql2: { mysql2: '3.6.0' },
postgres: { pg: '8.8.0' },
sqlite: { 'better-sqlite3': '8.6.0' },
'sqlite-legacy': { sqlite3: '5.1.2' },
sqlite: { 'better-sqlite3': '9.0.0' },
};

/**
Expand Down