fix(connection-knex): Fix connection-knex plugin structure to work with version 4.

This commit is contained in:
João Correia 2022-01-25 20:06:06 +00:00
parent 371bd0b350
commit ffc9c35159
20 changed files with 172 additions and 145 deletions

1
.pnp.cjs generated
View File

@ -5206,6 +5206,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) {
["@lowdefy/blocks-loaders", "workspace:packages/plugins/blocks/blocks-loaders"],
["@lowdefy/blocks-markdown", "workspace:packages/plugins/blocks/blocks-markdown"],
["@lowdefy/connection-axios-http", "workspace:packages/plugins/connections/connection-axios-http"],
["@lowdefy/connection-knex", "workspace:packages/plugins/connections/connection-knex"],
["@lowdefy/connection-redis", "workspace:packages/plugins/connections/connection-redis"],
["@lowdefy/helpers", "workspace:packages/utils/helpers"],
["@lowdefy/node-utils", "workspace:packages/utils/node-utils"],

View File

@ -63,6 +63,7 @@
"@lowdefy/blocks-loaders": "4.0.0-alpha.6",
"@lowdefy/blocks-markdown": "4.0.0-alpha.6",
"@lowdefy/connection-axios-http": "4.0.0-alpha.6",
"@lowdefy/connection-knex": "4.0.0-alpha.6",
"@lowdefy/connection-redis": "4.0.0-alpha.6",
"@lowdefy/operators-change-case": "4.0.0-alpha.6",
"@lowdefy/operators-diff": "4.0.0-alpha.6",

View File

@ -27,6 +27,7 @@ const defaultPackages = [
'@lowdefy/blocks-loaders',
'@lowdefy/blocks-markdown',
'@lowdefy/connection-axios-http',
'@lowdefy/connection-knex',
'@lowdefy/connection-redis',
'@lowdefy/operators-change-case',
// '@lowdefy/operators-diff',

View File

@ -27,8 +27,8 @@
},
"type": "module",
"exports": {
".": "./dist/index.js",
"./connections/*": "./dist/connections/*"
"./connections": "./dist/connections.js",
"./types": "./dist/types.js"
},
"files": [
"dist/*"

View File

@ -14,13 +14,4 @@
limitations under the License.
*/
export default {
import: {
path: 'connections/Knex/KnexRaw/KnexRaw.js',
schema: 'connections/Knex/KnexRaw/KnexRawSchema.json',
},
meta: {
checkRead: false,
checkWrite: false,
},
};
export { default as Knex } from './connections/Knex/Knex.js';

View File

@ -17,10 +17,10 @@
import KnexBuilder from './KnexBuilder/KnexBuilder.js';
import KnexRaw from './KnexRaw/KnexRaw.js';
import schema from './schema.js';
export default {
import: {
schema: 'connections/Knex/KnexSchema.json',
},
schema,
requests: {
KnexBuilder,
KnexRaw,

View File

@ -16,7 +16,8 @@
import { validate } from '@lowdefy/ajv';
import Knex from './Knex.js';
import schema from './KnexSchema.json';
const schema = Knex.schema;
test('All requests are present', () => {
expect(Knex.requests.KnexRaw).toBeDefined();

View File

@ -16,8 +16,9 @@
import knex from 'knex';
import { type } from '@lowdefy/helpers';
import schema from './schema.js';
async function knexBuilder({ request, connection }) {
async function KnexBuilder({ request, connection }) {
let client = knex(connection);
if (request.tableName) {
client = client(request.tableName);
@ -47,4 +48,10 @@ async function knexBuilder({ request, connection }) {
return client;
}
export default knexBuilder;
KnexBuilder.schema = schema;
KnexBuilder.meta = {
checkRead: false,
checkWrite: false,
};
export default KnexBuilder;

View File

@ -16,8 +16,10 @@
import { validate } from '@lowdefy/ajv';
import knex from 'knex';
import knexBuilder from './KnexBuilder.js';
import schema from './KnexBuilderSchema.json';
import KnexBuilder from './KnexBuilder.js';
const { checkRead, checkWrite } = KnexBuilder.meta;
const schema = KnexBuilder.schema;
const mockKnexClient = jest.fn(() => mockKnexClient);
@ -44,7 +46,7 @@ test('KnexBuilder with tableName', async () => {
query: [{ select: ['*'] }, { where: ['name', 'steve'] }],
tableName: 'table',
};
const res = await knexBuilder({ request, connection });
const res = await KnexBuilder({ request, connection });
expect(knex.mock.calls).toEqual([
[
{
@ -74,7 +76,7 @@ test('KnexBuilder', async () => {
const request = {
query: [{ select: ['*'] }, { from: ['table'] }, { where: ['name', 'steve'] }],
};
const res = await knexBuilder({ request, connection });
const res = await KnexBuilder({ request, connection });
expect(knex.mock.calls).toEqual([
[
{
@ -97,7 +99,7 @@ test('KnexBuilder, invalid method', async () => {
const request = {
query: [{ invalid: ['*'] }],
};
await expect(knexBuilder({ request, connection })).rejects.toThrow(
await expect(KnexBuilder({ request, connection })).rejects.toThrow(
'Invalid query builder method "invalid".'
);
});
@ -106,7 +108,7 @@ test('KnexBuilder, more than one method', async () => {
const request = {
query: [{ select: ['*'], where: ['name', 'steve'] }],
};
await expect(knexBuilder({ request, connection })).rejects.toThrow(
await expect(KnexBuilder({ request, connection })).rejects.toThrow(
'Invalid query, more than one method defined in a method object, received ["select","where"].'
);
});
@ -115,7 +117,7 @@ test('KnexBuilder, method args not an array', async () => {
const request = {
query: [{ select: '*' }],
};
await expect(knexBuilder({ request, connection })).rejects.toThrow(
await expect(KnexBuilder({ request, connection })).rejects.toThrow(
'Invalid query, method "select" arguments should be an array, received "*".'
);
});
@ -143,3 +145,11 @@ test('query missing', () => {
'KnexBuilder request should have required property "query".'
);
});
test('checkRead should be false', async () => {
expect(checkRead).toBe(false);
});
test('checkWrite should be false', async () => {
expect(checkWrite).toBe(false);
});

View File

@ -1,28 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Lowdefy Request Schema - KnexBuilder",
"type": "object",
"required": ["query"],
"properties": {
"query": {
"type": "array",
"description": "SQL query builder array. An array of objects, with a single key which is the name of the knex builder function. The value should be an array of arguments to pass to the builder function.",
"errorMessage": {
"type": "KnexBuilder request property \"query\" should be an array."
}
},
"tableName": {
"type": ["string", "object"],
"description": "The name of the table to query from.",
"errorMessage": {
"type": "KnexBuilder request property \"tableName\" should be a string or object"
}
}
},
"errorMessage": {
"type": "KnexBuilder request properties should be an object.",
"required": {
"query": "KnexBuilder request should have required property \"query\"."
}
}
}

View File

@ -0,0 +1,29 @@
export default {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Lowdefy Request Schema - KnexBuilder',
type: 'object',
required: ['query'],
properties: {
query: {
type: 'array',
description:
'SQL query builder array. An array of objects, with a single key which is the name of the knex builder function. The value should be an array of arguments to pass to the builder function.',
errorMessage: {
type: 'KnexBuilder request property "query" should be an array.',
},
},
tableName: {
type: ['string', 'object'],
description: 'The name of the table to query from.',
errorMessage: {
type: 'KnexBuilder request property "tableName" should be a string or object',
},
},
},
errorMessage: {
type: 'KnexBuilder request properties should be an object.',
required: {
query: 'KnexBuilder request should have required property "query".',
},
},
};

View File

@ -15,8 +15,9 @@
*/
import knex from 'knex';
import schema from './schema.js';
async function knexRaw({ request, connection }) {
async function KnexRaw({ request, connection }) {
const client = knex(connection);
const res = await client.raw(request.query, request.parameters);
Object.keys(res).forEach((key) => {
@ -27,4 +28,10 @@ async function knexRaw({ request, connection }) {
return res;
}
export default knexRaw;
KnexRaw.schema = schema;
KnexRaw.meta = {
checkRead: false,
checkWrite: false,
};
export default KnexRaw;

View File

@ -16,8 +16,10 @@
import { validate } from '@lowdefy/ajv';
import knex from 'knex';
import knexRaw from './KnexRaw.js';
import schema from './KnexRawSchema.json';
import KnexRaw from './KnexRaw.js';
const { checkRead, checkWrite } = KnexRaw.meta;
const schema = KnexRaw.schema;
const mockRaw = jest.fn(() => {
return Promise.resolve({ rows: [{ name: 'name' }], _types: 'types' });
@ -39,7 +41,7 @@ const connection = {
};
test('knexRaw', async () => {
const res = await knexRaw({ request, connection });
const res = await KnexRaw({ request, connection });
expect(knex.mock.calls).toEqual([
[
{
@ -88,3 +90,11 @@ test('query missing', () => {
'KnexRaw request should have required property "query".'
);
});
test('checkRead should be false', async () => {
expect(checkRead).toBe(false);
});
test('checkWrite should be false', async () => {
expect(checkWrite).toBe(false);
});

View File

@ -1,28 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Lowdefy Request Schema - KnexRaw",
"type": "object",
"required": ["query"],
"properties": {
"query": {
"type": "string",
"description": "SQL query string.",
"errorMessage": {
"type": "KnexRaw request property \"query\" should be a string."
}
},
"parameters": {
"type": ["string","number", "array", "object"],
"description": "SQL query parameters.",
"errorMessage": {
"type": "KnexRaw request property \"parameters\" should be a string, number, array, or object."
}
}
},
"errorMessage": {
"type": "KnexRaw request properties should be an object.",
"required": {
"query": "KnexRaw request should have required property \"query\"."
}
}
}

View File

@ -0,0 +1,28 @@
export default {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Lowdefy Request Schema - KnexRaw',
type: 'object',
required: ['query'],
properties: {
query: {
type: 'string',
description: 'SQL query string.',
errorMessage: {
type: 'KnexRaw request property "query" should be a string.',
},
},
parameters: {
type: ['string', 'number', 'array', 'object'],
description: 'SQL query parameters.',
errorMessage: {
type: 'KnexRaw request property "parameters" should be a string, number, array, or object.',
},
},
},
errorMessage: {
type: 'KnexRaw request properties should be an object.',
required: {
query: 'KnexRaw request should have required property "query".',
},
},
};

View File

@ -1,43 +0,0 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "Lowdefy Connection Schema - Knex",
"type": "object",
"required": ["client", "connection"],
"properties": {
"client": {
"type": "string",
"description": "SQL query string.",
"errorMessage": {
"type": "Knex connection property \"client\" should be a string."
}
},
"connection": {
"type": ["string", "object"],
"description": "SQL query string.",
"errorMessage": {
"type": "Knex connection property \"connection\" should be a string or object."
}
},
"searchPath": {
"type": "string",
"description": "Set PostgreSQL search path.",
"errorMessage": {
"type": "Knex connection property \"searchPath\" should be a string."
}
},
"version": {
"type": "string",
"description": "Set database version.",
"errorMessage": {
"type": "Knex connection property \"version\" should be a string."
}
}
},
"errorMessage": {
"type": "Knex connection properties should be an object.",
"required": {
"client": "Knex connection should have required property \"client\".",
"connection": "Knex connection should have required property \"connection\"."
}
}
}

View File

@ -0,0 +1,43 @@
export default {
$schema: 'http://json-schema.org/draft-07/schema#',
title: 'Lowdefy Connection Schema - Knex',
type: 'object',
required: ['client', 'connection'],
properties: {
client: {
type: 'string',
description: 'SQL query string.',
errorMessage: {
type: 'Knex connection property "client" should be a string.',
},
},
connection: {
type: ['string', 'object'],
description: 'SQL query string.',
errorMessage: {
type: 'Knex connection property "connection" should be a string or object.',
},
},
searchPath: {
type: 'string',
description: 'Set PostgreSQL search path.',
errorMessage: {
type: 'Knex connection property "searchPath" should be a string.',
},
},
version: {
type: 'string',
description: 'Set database version.',
errorMessage: {
type: 'Knex connection property "version" should be a string.',
},
},
},
errorMessage: {
type: 'Knex connection properties should be an object.',
required: {
client: 'Knex connection should have required property "client".',
connection: 'Knex connection should have required property "connection".',
},
},
};

View File

@ -1,7 +0,0 @@
import Knex from './connections/Knex/Knex.js';
export const connections = {
Knex,
};
export default { connections };

View File

@ -1,3 +1,4 @@
/* eslint-disable import/namespace */
/*
Copyright 2020-2021 Lowdefy, Inc
@ -13,14 +14,16 @@
See the License for the specific language governing permissions and
limitations under the License.
*/
import * as connections from './connections.js';
export default {
import: {
path: 'connections/Knex/KnexBuilder/KnexBuilder.js',
schema: 'connections/Knex/KnexBuilder/KnexBuilderSchema.json',
},
meta: {
checkRead: false,
checkWrite: false,
},
connections: Object.keys(connections),
requests: Object.keys(connections)
.map((connection) => Object.keys(connections[connection].requests))
.flat(),
};
// export default {
// connections: ['Knex'],
// requests: ['KnexBuilder', 'KnexRaw'],
// };

View File

@ -3502,6 +3502,7 @@ __metadata:
"@lowdefy/blocks-loaders": 4.0.0-alpha.6
"@lowdefy/blocks-markdown": 4.0.0-alpha.6
"@lowdefy/connection-axios-http": 4.0.0-alpha.6
"@lowdefy/connection-knex": 4.0.0-alpha.6
"@lowdefy/connection-redis": 4.0.0-alpha.6
"@lowdefy/helpers": 4.0.0-alpha.6
"@lowdefy/node-utils": 4.0.0-alpha.6
@ -3571,7 +3572,7 @@ __metadata:
languageName: unknown
linkType: soft
"@lowdefy/connection-knex@workspace:packages/plugins/connections/connection-knex":
"@lowdefy/connection-knex@4.0.0-alpha.6, @lowdefy/connection-knex@workspace:packages/plugins/connections/connection-knex":
version: 0.0.0-use.local
resolution: "@lowdefy/connection-knex@workspace:packages/plugins/connections/connection-knex"
dependencies: