Skip to content

Commit

Permalink
Add querystring search.
Browse files Browse the repository at this point in the history
  • Loading branch information
robgietema committed Dec 3, 2024
1 parent 475e699 commit 4f67dac
Show file tree
Hide file tree
Showing 19 changed files with 325 additions and 102 deletions.
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 @@ -75,6 +76,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
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');
});
};
9 changes: 8 additions & 1 deletion src/models/_model/_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,17 @@ 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 === 'like') {
valueWrapper = '%?%';
}
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: {},
};
},
},
];
Empty file added src/routes/form/form.test.js
Empty file.
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

0 comments on commit 4f67dac

Please sign in to comment.