Skip to content

Commit

Permalink
Knex adapter internal updates
Browse files Browse the repository at this point in the history
  • Loading branch information
timleslie committed Mar 6, 2020
1 parent d2ccaba commit b615e7a
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 29 deletions.
5 changes: 5 additions & 0 deletions .changeset/fast-windows-kneel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/adapter-knex': patch
---

Refactors internals to prepare for future changes.
5 changes: 5 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -275,8 +275,11 @@ We also build commonjs builds to run in node (for testing with jest or etc.) and
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore-start -->

<!-- markdownlint-disable -->

<table>
<tr>
<td align="center"><a href="http://www.thinkmill.com.au"><img src="https://avatars3.githubusercontent.com/u/872310?v=4" width="80px;" alt=""/><br /><sub><b>Jed Watson</b></sub></a><br /><a href="https://github.com/keystonejs/keystone/commits?author=JedWatson" title="Code">💻</a></td>
Expand Down Expand Up @@ -335,7 +338,9 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
</table>

<!-- markdownlint-enable -->

<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,11 @@ We'd like to start by thanking all our wonderful contributors:
([emoji key](https://allcontributors.org/docs/en/emoji-key)):

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->

<!-- prettier-ignore-start -->

<!-- markdownlint-disable -->

<table>
<tr>
<td align="center"><a href="http://www.thinkmill.com.au"><img src="https://avatars3.githubusercontent.com/u/872310?v=4" width="80px;" alt=""/><br /><sub><b>Jed Watson</b></sub></a><br /><a href="https://github.com/keystonejs/keystone/commits?author=JedWatson" title="Code">💻</a></td>
Expand Down Expand Up @@ -126,7 +129,9 @@ We'd like to start by thanking all our wonderful contributors:
</table>

<!-- markdownlint-enable -->

<!-- prettier-ignore-end -->

<!-- ALL-CONTRIBUTORS-LIST:END -->

### Demo Projects
Expand Down
69 changes: 47 additions & 22 deletions packages/adapter-knex/lib/adapter-knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@ class KnexListAdapter extends BaseListAdapter {
});
}

////////// Mutations //////////

async _processNonRealFields(data, processFunction) {
return resolveAllKeys(
arrayToObject(
Expand All @@ -301,27 +303,49 @@ class KnexListAdapter extends BaseListAdapter {
);
}

////////// Mutations //////////
_getNearFar(fieldAdapter) {
const { rel, listAdapter } = fieldAdapter;
const { columnNames } = rel;
const columnKey = listAdapter.key;
return columnNames[columnKey];
}

async _createSingle(realData) {
const item = (
await this._query()
.insert(realData)
.into(this.tableName)
.returning('*')
)[0];
const itemId = item.id;
return { item, itemId };
}

async _setNullByValue({ tableName, columnName, value }) {
return this._query()
.table(tableName)
.where(columnName, value)
.update({ [columnName]: null });
}

async _createOrUpdateField({ value, adapter, itemId }) {
const rel = {
cardinality: 'N:N',
tableName: this._manyTable(adapter.path),
columnNames: { [this.key]: { near: `${this.key}_id`, far: adapter.refListId } },
};
const { cardinality, tableName, columnNames } = rel;
const { tableName, cardinality } = rel;
if (cardinality === '1:1') {
// Implement me
} else {
const values = value; // Rename this because we have a many situation
if (values && values.length) {
if (cardinality === 'N:N') {
const itemCol = columnNames[this.key].near;
const otherCol = columnNames[this.key].far;
const { near, far } = this._getNearFar(adapter);
return this._query()
.insert(values.map(id => ({ [itemCol]: itemId, [otherCol]: id })))
.insert(values.map(id => ({ [near]: itemId, [far]: id })))
.into(tableName)
.returning(otherCol);
.returning(far);
} else {
// Implement me
}
Expand All @@ -335,23 +359,19 @@ class KnexListAdapter extends BaseListAdapter {
const realData = pick(data, this.realKeys);

// Insert the real data into the table
const item = (
await this._query()
.insert(realData)
.into(this.tableName)
.returning('*')
)[0];
const { item, itemId } = await this._createSingle(realData);

// For every many-field, update the many-table
const manyItem = await this._processNonRealFields(data, async ({ value, adapter }) =>
this._createOrUpdateField({ value, adapter, itemId: item.id })
this._createOrUpdateField({ value, adapter, itemId })
);

return { ...item, ...manyItem };
}

async _update(id, data) {
const realData = pick(data, this.realKeys);

// Update the real data
const query = this._query()
.table(this.tableName)
Expand All @@ -369,15 +389,21 @@ class KnexListAdapter extends BaseListAdapter {
tableName: this._manyTable(path),
columnNames: { [this.key]: { near: `${this.key}_id`, far: refListId } },
};
const { cardinality, tableName, columnNames } = rel;
const { cardinality, tableName } = rel;
let value;
// Future task: Is there some way to combine the following three
// operations into a single query?

if (cardinality !== '1:1') {
// Work out what we've currently got
const selectCol = columnNames[this.key].far;
const matchCol = columnNames[this.key].near;
let matchCol, selectCol;
if (cardinality === 'N:N') {
const { near, far } = this._getNearFar(adapter);
matchCol = near;
selectCol = far;
} else {
// Implement me
}
const currentRefIds = (
await this._query()
.select(selectCol)
Expand Down Expand Up @@ -422,22 +448,21 @@ class KnexListAdapter extends BaseListAdapter {
tableName: a.config.many ? adapter._manyTable(a.path) : adapter.tableName,
columnNames: { [this.key]: { near: a.refListId } },
};
const { cardinality, columnName, tableName, columnNames } = rel;
const { cardinality, columnName, tableName } = rel;
if (cardinality === 'N:N') {
const { near } = this._getNearFar(a);
return this._query()
.table(tableName)
.where(columnNames[this.key].near, id)
.where(near, id)
.del();
} else {
return this._query()
.table(tableName)
.where(columnName, id)
.update({ [columnName]: null });
return this._setNullByValue({ tableName, columnName, value: id });
}
})
)
)
);

// Delete the actual item
return this._query()
.table(this.tableName)
Expand Down
12 changes: 6 additions & 6 deletions packages/fields-mongoid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ We reuse the interface implementation from the native `Text` field.

### Input Fields

| Field name | Type | Description |
| :--------- | :--- | :---------------------------------------- |
| `${path}` | `ID` | The ID in its 24 char hex representation |
| Field name | Type | Description |
| :--------- | :--- | :--------------------------------------- |
| `${path}` | `ID` | The ID in its 24 char hex representation |

### Output Fields

| Field name | Type | Description |
| :--------- | :--- | :---------------------------------------- |
| `${path}` | `ID` | The ID in its 24 char hex representation |
| Field name | Type | Description |
| :--------- | :--- | :--------------------------------------- |
| `${path}` | `ID` | The ID in its 24 char hex representation |

### Filters

Expand Down
2 changes: 1 addition & 1 deletion packages/fields-wysiwyg-tinymce/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const { Wysiwyg } = require('@keystonejs/fields-wysiwyg-tinymce');

### `editorConfig`

_*Default:*_ `{}`
__Default:__ `{}`

Accepts any [TinyMCE config options](https://www.tiny.cloud/docs/configure/). These will be passed to `tinymce.init` and can be used to override Keystone.js' default editor appearance and functionality.

Expand Down

0 comments on commit b615e7a

Please sign in to comment.