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

Add querystring search. #10

Merged
merged 8 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
- Add preview link support @robgietema
- Add block types index @robgietema
- Expand image data in block data @robgietema
- Add querystring search @robgietema

### Bugfix

Expand Down Expand Up @@ -76,6 +77,7 @@
- Split profiles in core and default @robgietema
- Move apply behaviors to the document class @robgietema
- Node 22 support @sneridagh
- Fetch indexes from database @robgietema

### Documentation

Expand Down
12 changes: 12 additions & 0 deletions docs/examples/form/post.req
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
POST /@schemaform-data HTTP/1.1
Accept: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJhZG1pbiIsImZ1bGxuYW1lIjoiQWRtaW4iLCJpYXQiOjE2NDkzMTI0NDl9.RS1Ny_r0v7vIylFfK6q0JVJrkiDuTOh9iG9IL8xbzAk
Content-Type: application/json

{
"block_id": "669530d8-d319-48cc-ad4f-cd690ab7e472",
"data": {
"myfield": "Lorem Ipsum"
},
"captcha": {}
}
1 change: 1 addition & 0 deletions docs/examples/form/post.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
HTTP/1.1 200 No Content
4 changes: 4 additions & 0 deletions docs/examples/vocabularies/list.res
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ Content-Type: application/json
"@id": "http://localhost:8080/@vocabularies/boolean",
"title": "boolean"
},
{
"@id": "http://localhost:8080/@vocabularies/captchaProviders",
"title": "captchaProviders"
},
{
"@id": "http://localhost:8080/@vocabularies/groups",
"title": "groups"
Expand Down
18 changes: 18 additions & 0 deletions docs/form.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
nav_order: 26
permalink: /form
---

# Form

Form support is included in Nick.

```
{% include_relative examples/form/post.req %}
```

The API will return a 200 response:

```
{% include_relative examples/form/post.res %}
```
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import events from './src/events';

export const config = {
connection: {
port: 5432,
Expand Down Expand Up @@ -36,4 +38,5 @@ export const config = {
`${__dirname}/src/develop/nick/src/profiles/core`,
`${__dirname}/src/profiles/default`,
],
events,
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Point of contact for events.
* @module events
* @example import events from './events';
*/

import events from '@robgietema/nick/src/events';

export default events;
5 changes: 5 additions & 0 deletions scripts/convert.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,8 @@ const convertDocuments = (input, path) => {

// Convert to string
let json = JSON.stringify(document);

// Resolve targets
json = json.replaceAll(/\"[^"]*resolveuid\/([^"]*)\"/g, (result, uuid) => {
const targetfile = `${input}/content/${uuid}/data.json`;
if (existsSync(targetfile)) {
Expand All @@ -189,6 +191,9 @@ const convertDocuments = (input, path) => {
return `"${uuid}"`;
});

// Convert index operations
json = json.replaceAll('plone.app.querystring.operation.', '');

const output =
document['@id'] === '/Plone'
? `${path}/documents/_root.json`
Expand Down
2 changes: 1 addition & 1 deletion src/collections/index/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class IndexCollection extends Collection {

// Add index to return json
this.map((index) => {
json[index.id] = index.toJSON(req);
json[index.name] = index.toJSON(req);
});

// Return json
Expand Down
3 changes: 0 additions & 3 deletions src/helpers/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,7 @@ export async function handleBlockReferences(json, trx) {
if (isObject(output.blocks[block].slides)) {
await Promise.all(
map(output.blocks[block].slides, async (slide, index) => {
console.log('image');
console.log(index);
if (isObject(output.blocks[block].slides[index].image)) {
console.log('image');
output.blocks[block].slides[index].image[0] = await extendHref(
output.blocks[block].slides[index].image[0],
trx,
Expand Down
17 changes: 17 additions & 0 deletions src/migrations/202412031416_metadata.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const up = async (knex) => {
await knex.schema.alterTable('index', (table) => {
table.string('name');
table.string('type');
table.boolean('metadata').index();
table.string('attr');
});
};

export const down = async (knex) => {
await knex.schema.alterTable('index', (table) => {
table.dropColumn('name');
table.dropColumn('type');
table.dropColumn('metadata');
table.dropColumn('attr');
});
};
6 changes: 5 additions & 1 deletion src/models/_model/_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,14 @@ export class Model extends mixin(ObjectionModel, [
mapKeys(where, (value, key) => {
// user and group are reserved words so need to be wrapper in quotes
const attribute = formatAttribute(key);
const operator = isArray(value) ? value[0] : '=';
let operator = isArray(value) ? value[0] : '=';
const values = isArray(value) ? value[1] : value;
let valueWrapper =
isArray(values) && operator !== '&&' ? 'any(?)' : '?';
if (isArray(values) && operator === 'all') {
operator = '=';
valueWrapper = 'all(?)';
}
if (operator === '@@') {
valueWrapper = 'to_tsquery(?)';
}
Expand Down
83 changes: 29 additions & 54 deletions src/models/document/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,15 @@ import _, {
import { v4 as uuid } from 'uuid';

import languages from '../../constants/languages';
import { Catalog, Model, Permission, Redirect, Role, User } from '../../models';
import {
Catalog,
Index,
Model,
Permission,
Redirect,
Role,
User,
} from '../../models';
import {
copyFile,
fileExists,
Expand Down Expand Up @@ -962,60 +970,27 @@ export class Document extends Model {
await this._type.fetchRelated('_workflow', trx);
}

const indexes = await Index.fetchAll({}, {}, trx);
await Promise.all(
map(config.profiles, async (profilePath) => {
if (fileExists(`${profilePath}/catalog`)) {
const profile = stripI18n(require(`${profilePath}/catalog`));

// Loop indexes
await Promise.all(
map(profile.indexes, async (index) => {
if (index.attr in this) {
fields[`_${index.name}`] = { type: index.type };
if (isFunction(this[index.attr])) {
const value = this[index.attr](trx);
fields[`_${index.name}`].value = isPromise(value)
? await value
: value;
} else {
fields[`_${index.name}`].value = this[index.attr];
}
} else if (index.attr in this._type._schema.properties) {
fields[`_${index.name}`] = {
type: index.type,
value:
index.type === 'boolean'
? !!this.json[index.attr]
: this.json[index.attr],
};
}
}),
);

// Loop metadata
await Promise.all(
map(profile.metadata, async (metadata) => {
if (metadata.attr in this) {
fields[metadata.name] = { type: metadata.type };
if (isFunction(this[metadata.attr])) {
const value = this[metadata.attr](trx);
fields[metadata.name].value = isPromise(value)
? await value
: value;
} else {
fields[metadata.name].value = this[metadata.attr];
}
} else if (metadata.attr in this._type._schema.properties) {
fields[metadata.name] = {
type: metadata.type,
value:
metadata.type === 'boolean'
? !!this.json[metadata.attr]
: this.json[metadata.attr],
};
}
}),
);
// Loop indexes
indexes.map(async (index) => {
const name = index.metadata ? index.name : `_${index.name}`;
if (index.attr in this) {
fields[name] = { type: index.type };
if (isFunction(this[index.attr])) {
const value = this[index.attr](trx);
fields[name].value = isPromise(value) ? await value : value;
} else {
fields[name].value = this[index.attr];
}
} else if (index.attr in this._type._schema.properties) {
fields[name] = {
type: index.type,
value:
index.type === 'boolean'
? !!this.json[index.attr]
: this.json[index.attr],
};
}
}),
);
Expand Down
17 changes: 17 additions & 0 deletions src/routes/form/form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* Form routes.
* @module routes/form/form
*/

export default [
{
op: 'post',
view: '/@schemaform-data',
permission: 'View',
handler: async (req, trx) => {
return {
json: {},
};
},
},
];
6 changes: 6 additions & 0 deletions src/routes/form/form.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import app from '../../app';
import { testRequest } from '../../helpers';

describe('Form', () => {
it('should submit a form', () => testRequest(app, 'search/post'));
});
4 changes: 2 additions & 2 deletions src/routes/querystring/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ export default [
handler: async (req, trx) => {
// Get all enabled indexes
const indexes = await Index.fetchAll(
{ enabled: true },
{ enabled: true, metadata: false },
{ order: 'title' },
trx,
);

// Get all enabled and sortable indexes
const sortableIndexes = await Index.fetchAll(
{ sortable: true, enabled: true },
{ sortable: true, enabled: true, metadata: false },
{ order: 'title' },
trx,
);
Expand Down
Loading
Loading