Skip to content

Commit

Permalink
Merge pull request #12918 from strapi/chore/knex-v1
Browse files Browse the repository at this point in the history
Make strapi compatible with knex v1
  • Loading branch information
alexandrebodin committed Mar 29, 2022
2 parents f05ce9b + 2791a2a commit b2fa446
Show file tree
Hide file tree
Showing 18 changed files with 5,976 additions and 738 deletions.
4 changes: 2 additions & 2 deletions .github/actions/run-e2e-tests/script.sh
Expand Up @@ -7,5 +7,5 @@ export ENV_PATH="$(pwd)/testApp/.env"

opts=($DB_OPTIONS)

yarn run -s test:generate-app "${opts[@]}" $@
yarn run -s test:e2e
yarn run -s test:generate-app "${opts[@]}"
yarn run -s test:e2e $@
32 changes: 17 additions & 15 deletions .github/workflows/tests.yml
Expand Up @@ -15,9 +15,10 @@ jobs:
node: [12, 14, 16]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: yarn
- uses: ./.github/actions/install-modules
- name: Run lint
run: yarn run -s lint
Expand All @@ -33,9 +34,10 @@ jobs:
node: [12, 14, 16]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: yarn
- uses: ./.github/actions/install-modules
with:
globalPackages: codecov
Expand All @@ -53,9 +55,10 @@ jobs:
node: [12, 14, 16]
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: yarn
- uses: ./.github/actions/install-modules
with:
globalPackages: codecov
Expand All @@ -71,7 +74,6 @@ jobs:
strategy:
matrix:
node: [12, 14, 16]
max-parallel: 3
services:
postgres:
# Docker Hub image
Expand All @@ -92,9 +94,10 @@ jobs:
- 5432:5432
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2-beta
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node }}
cache: yarn
- uses: ./.github/actions/install-modules
- uses: ./.github/actions/run-e2e-tests
with:
Expand All @@ -107,7 +110,6 @@ jobs:
strategy:
matrix:
node: [12, 14, 16]
max-parallel: 3
services:
mysql:
image: mysql
Expand Down Expand Up @@ -143,7 +145,6 @@ jobs:
strategy:
matrix:
node: [12, 14, 16]
max-parallel: 3
services:
mysql:
image: mysql:5
Expand Down Expand Up @@ -175,11 +176,11 @@ jobs:
e2e_ce_sqlite:
runs-on: ubuntu-latest
needs: [lint, unit_back, unit_front]
name: '[CE] E2E (sqlite, node: ${{ matrix.node }})'
name: '[CE] E2E (sqlite: ${{ matrix.sqlite_pkg }}, node: ${{ matrix.node }})'
strategy:
matrix:
node: [12, 14, 16]
max-parallel: 3
sqlite_pkg: ['better-sqlite3', 'sqlite3', '@vscode/sqlite3']
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand All @@ -188,9 +189,10 @@ jobs:
cache: yarn
- uses: ./.github/actions/install-modules
- uses: ./.github/actions/run-e2e-tests
env:
SQLITE_PKG: ${{ matrix.sqlite_pkg }}
with:
dbOptions: '--dbclient=sqlite --dbfile=./tmp/data.db'

dbOptions: '--dbclient=sqlite-legacy --dbfile=./tmp/data.db'
# EE
e2e_ee_pg:
runs-on: ubuntu-latest
Expand All @@ -202,7 +204,6 @@ jobs:
strategy:
matrix:
node: [12, 14, 16]
max-parallel: 3
services:
postgres:
# Docker Hub image
Expand Down Expand Up @@ -243,7 +244,6 @@ jobs:
strategy:
matrix:
node: [12, 14, 16]
max-parallel: 3
services:
mysql:
image: mysql
Expand Down Expand Up @@ -276,14 +276,14 @@ jobs:
e2e_ee_sqlite:
runs-on: ubuntu-latest
needs: [lint, unit_back, unit_front]
name: '[EE] E2E (sqlite, node: ${{ matrix.node }})'
name: '[EE] E2E (sqlite: ${{ matrix.sqlite_pkg }}, node: ${{ matrix.node }})'
if: 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: [12, 14, 16]
max-parallel: 3
sqlite_pkg: ['better-sqlite3', 'sqlite3', '@vscode/sqlite3']
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
Expand All @@ -292,6 +292,8 @@ jobs:
cache: yarn
- uses: ./.github/actions/install-modules
- uses: ./.github/actions/run-e2e-tests
env:
SQLITE_PKG: ${{ matrix.sqlite_pkg }}
with:
dbOptions: '--dbclient=sqlite --dbfile=./tmp/data.db'
runEE: true
2 changes: 2 additions & 0 deletions examples/getstarted/package.json
Expand Up @@ -27,6 +27,8 @@
"mysql": "2.18.1",
"passport-google-oauth2": "0.2.0",
"pg": "8.6.0",
"better-sqlite3": "7.5.0",
"@vscode/sqlite3": "5.0.7",
"sqlite3": "5.0.2"
},
"strapi": {
Expand Down
12 changes: 6 additions & 6 deletions packages/core/admin/server/bootstrap.js
Expand Up @@ -12,12 +12,12 @@ const defaultAdminAuthSettings = {
},
};

const registerPermissionActions = () => {
getService('permission').actionProvider.registerMany(adminActions.actions);
const registerPermissionActions = async () => {
await getService('permission').actionProvider.registerMany(adminActions.actions);
};

const registerAdminConditions = () => {
getService('permission').conditionProvider.registerMany(adminConditions.conditions);
const registerAdminConditions = async () => {
await getService('permission').conditionProvider.registerMany(adminConditions.conditions);
};

const registerModelHooks = () => {
Expand Down Expand Up @@ -53,8 +53,8 @@ const syncAuthSettings = async () => {
};

module.exports = async () => {
registerAdminConditions();
registerPermissionActions();
await registerAdminConditions();
await registerPermissionActions();
registerModelHooks();

const permissionService = getService('permission');
Expand Down
64 changes: 64 additions & 0 deletions packages/core/database/lib/connection.js
@@ -0,0 +1,64 @@
/* eslint-disable node/no-missing-require */
/* eslint-disable node/no-extraneous-require */
'use strict';

const knex = require('knex');

const SqliteClient = require('knex/lib/dialects/sqlite3/index');

const trySqlitePackage = packageName => {
try {
require.resolve(packageName);
return packageName;
} catch (error) {
if (error.code === 'MODULE_NOT_FOUND') {
return false;
}
throw error;
}
};
class LegacySqliteClient extends SqliteClient {
_driver() {
return require('sqlite3');
}
}

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

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

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

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

knexConfig.client = clientMap[sqlitePackageName];
}

const knexInstance = knex(knexConfig);

return Object.assign(knexInstance, {
getSchemaName() {
return this.client.connectionSettings.schema;
},
});
};

module.exports = createConnection;
Expand Up @@ -48,7 +48,7 @@ const toStrapiType = column => {
switch (rootType) {
case 'int': {
if (column.column_key === 'PRI') {
return { type: 'increments', args: [{ primary: true }], unsigned: false };
return { type: 'increments', args: [{ primary: true, primaryKey: true }], unsigned: false };
}

return { type: 'integer' };
Expand Down
Expand Up @@ -16,7 +16,7 @@ const toStrapiType = column => {
switch (rootType) {
case 'integer': {
if (column.pk) {
return { type: 'increments', args: [{ primary: true }] };
return { type: 'increments', args: [{ primary: true, primaryKey: true }] };
}

return { type: 'integer' };
Expand Down
4 changes: 3 additions & 1 deletion packages/core/database/lib/entity-manager.js
Expand Up @@ -166,10 +166,12 @@ const createEntityManager = db => {

const dataToInsert = processData(metadata, data, { withDefaults: true });

const [id] = await this.createQueryBuilder(uid)
const res = await this.createQueryBuilder(uid)
.insert(dataToInsert)
.execute();

const id = res[0].id || res[0];

await this.attachRelations(uid, id, data);

// TODO: in case there is no select or populate specified return the inserted data ?
Expand Down
13 changes: 1 addition & 12 deletions packages/core/database/lib/index.js
@@ -1,28 +1,17 @@
'use strict';

const knex = require('knex');

const { getDialect } = require('./dialects');
const createSchemaProvider = require('./schema');
const createMetadata = require('./metadata');
const { createEntityManager } = require('./entity-manager');
const { createMigrationsProvider } = require('./migrations');
const { createLifecyclesProvider } = require('./lifecycles');
const createConnection = require('./connection');
const errors = require('./errors');

// TODO: move back into strapi
const { transformContentTypes } = require('./utils/content-types');

const createConnection = config => {
const knexInstance = knex(config);

return Object.assign(knexInstance, {
getSchemaName() {
return this.client.connectionSettings.schema;
},
});
};

class Database {
constructor(config) {
this.metadata = createMetadata(config.models);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/lib/schema/schema.js
Expand Up @@ -105,7 +105,7 @@ const getColumnType = attribute => {
case 'increments': {
return {
type: 'increments',
args: [{ primary: true }],
args: [{ primary: true, primaryKey: true }],
notNullable: true,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core/database/package.json
Expand Up @@ -34,7 +34,7 @@
"date-fns": "2.22.1",
"debug": "4.3.1",
"fs-extra": "10.0.0",
"knex": "0.95.6",
"knex": "1.0.4",
"lodash": "4.17.21",
"umzug": "2.3.0"
},
Expand Down
3 changes: 2 additions & 1 deletion packages/generators/app/lib/create-cli-db-project.js
Expand Up @@ -5,6 +5,7 @@ const { merge } = require('lodash');
const { trackUsage } = require('./utils/usage');
const defaultConfigs = require('./utils/db-configs');
const clientDependencies = require('./utils/db-client-dependencies');
const getClientName = require('./utils/db-client-name');
const createProject = require('./create-project');

module.exports = async scope => {
Expand All @@ -13,7 +14,7 @@ module.exports = async scope => {

const client = scope.database.client;
const configuration = {
client,
client: getClientName({ client }),
connection: merge({}, defaultConfigs[client] || {}, scope.database),
dependencies: clientDependencies({ scope, client }),
};
Expand Down
4 changes: 3 additions & 1 deletion packages/generators/app/lib/utils/db-client-dependencies.js
Expand Up @@ -3,7 +3,8 @@
const sqlClientModule = {
mysql: { mysql: '2.18.1' },
postgres: { pg: '8.6.0' },
sqlite: { sqlite3: '5.0.2' },
sqlite: { 'better-sqlite3': '^7.5.0' },
'sqlite-legacy': { sqlite3: '^5.0.2' },
};

/**
Expand All @@ -12,6 +13,7 @@ const sqlClientModule = {
module.exports = ({ client }) => {
switch (client) {
case 'sqlite':
case 'sqlite-legacy':
case 'postgres':
case 'mysql':
return {
Expand Down
13 changes: 13 additions & 0 deletions packages/generators/app/lib/utils/db-client-name.js
@@ -0,0 +1,13 @@
'use strict';

/**
* Client
*/
module.exports = ({ client }) => {
switch (client) {
case 'sqlite-legacy':
return 'sqlite';
default:
return client;
}
};
4 changes: 4 additions & 0 deletions packages/generators/app/lib/utils/usage.js
Expand Up @@ -54,6 +54,10 @@ function captureStderr(name, error) {
}

function trackEvent(event, body) {
if (process.env.NODE_ENV === 'test') {
return;
}

try {
return fetch('https://analytics.strapi.io/track', {
method: 'POST',
Expand Down
2 changes: 2 additions & 0 deletions test/create-test-app.js
@@ -1,5 +1,7 @@
'use strict';

process.env.NODE_ENV = 'test';

const yargs = require('yargs');
const { cleanTestApp, generateTestApp } = require('./helpers/test-app-generator');

Expand Down

0 comments on commit b2fa446

Please sign in to comment.