Skip to content

Commit

Permalink
Next
Browse files Browse the repository at this point in the history
  • Loading branch information
greguz committed Aug 8, 2024
1 parent e08dcde commit 08f1d95
Show file tree
Hide file tree
Showing 13 changed files with 567 additions and 358 deletions.
9 changes: 7 additions & 2 deletions docs/ecosystem.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@

## Core

### Adapters

- [`mutent-array`](https://github.com/greguz/mutent-array) Simple in memory array adapter.
- [`mutent-couchdb`](https://github.com/greguz/mutent-couchdb) CouchDB adapter for Mutent.
- [`mutent-mongodb`](https://github.com/greguz/mutent-mongodb) MongoDB adapter for Mutent.

### Plugins

- [`mutent-json-schema`](https://github.com/greguz/mutent-json-schema) JSON Schema validation plugin with [Ajv](https://github.com/ajv-validator/ajv).
- [`mutent-migration`](https://github.com/greguz/mutent-migration) Data versioning and migration for Mutent.
- [`mutent-mongodb`](https://github.com/greguz/mutent-mongodb) MongoDB adapter for Mutent.
- [`mutent-couchdb`](https://github.com/greguz/mutent-couchdb) CouchDB adapter for Mutent.

<!--
## Community
Expand Down
14 changes: 1 addition & 13 deletions docs/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Let's create a simple [Store](./store.md) instance that uses an Array as Datasto

```javascript
import { Store } from 'mutent'
import { ArrayAdapter } from 'mutent-array'
import ArrayAdapter from 'mutent-array'

const store = new Store({
adapter: new ArrayAdapter()
Expand Down Expand Up @@ -109,15 +109,3 @@ const deleted = await store
console.log(deleted) // { id: 42, name: 'Towel', updatedAt: ISODate }
console.log(store.adapter.items) // []
```

## Sections

1. [Store](./store.md)
2. [Adapter](./adapter.md)
3. [Entity](./entity.md)
4. [Mutation](./mutation.md)
5. [Mutator](./mutator.md)
6. [Context](./context.md)
7. [Hooks](./hooks.md)
8. [Errors](./errors.md)
9. [Ecosystem](./ecosystem.md)
71 changes: 34 additions & 37 deletions lib/adapter.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MutentError } from './error.mjs'
import { isFunction } from './util.mjs'

/**
* Return the customized Adapter's name (if any) or something descriptive
Expand All @@ -13,15 +14,14 @@ export function getAdapterName (adapter) {
async function * findEntity (ctx) {
const { adapter, argument, hooks, intent, options } = ctx

if (!adapter.find) {
if (!isFunction(adapter.find) && !isFunction(adapter.findEntity)) {
throw new MutentError(
'EMUT_PARTIAL_ADAPTER',
'The adapter does not implement ".find" method',
'The adapter does not implement both ".find" and ".findEntity" methods',
{
adapter: getAdapterName(adapter),
intent,
argument,
options
argument
}
)
}
Expand All @@ -30,7 +30,9 @@ async function * findEntity (ctx) {
await hook(argument, ctx)
}

const data = await adapter.find(argument, options)
const data = isFunction(adapter.findEntity)
? await adapter.findEntity(argument, ctx)
: await adapter.find(argument, options)

if (data !== null && data !== undefined) {
yield data
Expand All @@ -40,15 +42,14 @@ async function * findEntity (ctx) {
async function * filterEntities (ctx) {
const { adapter, argument, hooks, intent, options } = ctx

if (!adapter.filter) {
if (!isFunction(adapter.filter) && !isFunction(adapter.filterEntities)) {
throw new MutentError(
'EMUT_PARTIAL_ADAPTER',
'The adapter does not implement ".filter" method',
'The adapter does not implement both ".filter" and ".filterEntities" methods',
{
adapter: getAdapterName(adapter),
intent,
argument,
options
argument
}
)
}
Expand All @@ -57,7 +58,11 @@ async function * filterEntities (ctx) {
await hook(argument, ctx)
}

yield * adapter.filter(argument, options)
if (isFunction(adapter.filterEntities)) {
yield * adapter.filterEntities(argument, ctx)
} else {
yield * adapter.filter(argument, options)
}
}

export function iterateContext (ctx) {
Expand Down Expand Up @@ -87,15 +92,14 @@ async function * asIterable (argument) {
async function createEntity (entity, ctx) {
const { adapter, argument, hooks, intent, options } = ctx

if (!adapter.create && !adapter.createEntity) {
if (!isFunction(adapter.create) && !isFunction(adapter.createEntity)) {
throw new MutentError(
'EMUT_PARTIAL_ADAPTER',
'The Adapter does not implement both the ".create" and ".createEntity" methods',
{
adapter: getAdapterName(adapter),
intent,
argument,
options
argument
}
)
}
Expand All @@ -104,7 +108,7 @@ async function createEntity (entity, ctx) {
await hook(entity, ctx)
}

if (adapter.createEntity) {
if (isFunction(adapter.createEntity)) {
await adapter.createEntity(entity, ctx)
} else {
const result = await adapter.create(entity.target, options)
Expand All @@ -121,15 +125,14 @@ async function createEntity (entity, ctx) {
async function updateEntity (entity, ctx) {
const { adapter, argument, hooks, intent, options } = ctx

if (!adapter.update && !adapter.updateEntity) {
if (!isFunction(adapter.update) && !isFunction(adapter.updateEntity)) {
throw new MutentError(
'EMUT_PARTIAL_ADAPTER',
'The Adapter does not implement both the ".update" and ".updateEntity" methods',
{
adapter: getAdapterName(adapter),
intent,
argument,
options
argument
}
)
}
Expand All @@ -138,7 +141,7 @@ async function updateEntity (entity, ctx) {
await hook(entity, ctx)
}

if (adapter.updateEntity) {
if (isFunction(adapter.updateEntity)) {
await adapter.updateEntity(entity, ctx)
} else {
const result = await adapter.update(entity.source, entity.target, options)
Expand All @@ -155,15 +158,14 @@ async function updateEntity (entity, ctx) {
async function deleteEntity (entity, ctx) {
const { adapter, argument, hooks, intent, options } = ctx

if (!adapter.delete && !adapter.deleteEntity) {
if (!isFunction(adapter.delete) && !isFunction(adapter.deleteEntity)) {
throw new MutentError(
'EMUT_PARTIAL_ADAPTER',
'The Adapter does not implement both the ".delete" and ".deleteEntity" methods',
{
adapter: getAdapterName(adapter),
intent,
argument,
options
argument
}
)
}
Expand All @@ -172,7 +174,7 @@ async function deleteEntity (entity, ctx) {
await hook(entity, ctx)
}

if (adapter.deleteEntity) {
if (isFunction(adapter.deleteEntity)) {
await adapter.deleteEntity(entity, ctx)
} else {
await adapter.delete(entity.source, options)
Expand Down Expand Up @@ -207,42 +209,37 @@ export async function * concurrentWrite (iterable, ctx) {
const { writeSize } = ctx

let buffer = []

for await (const entity of iterable) {
buffer.push(entity)

if (buffer.length >= writeSize) {
const results = await Promise.all(
buffer.map(item => writeEntity(item, ctx))
yield * await Promise.all(
buffer.map(e => writeEntity(e, ctx))
)
for (const result of results) {
yield result
}
buffer = []
}
}

if (buffer.length > 0) {
const results = await Promise.all(
buffer.map(item => writeEntity(item, ctx))
yield * await Promise.all(
buffer.map(e => writeEntity(e, ctx))
)
for (const result of results) {
yield result
}
buffer = []
}
}

export async function * bulkWrite (iterable, ctx) {
const { adapter, argument, hooks, intent, options, writeSize } = ctx
const { adapter, argument, hooks, intent, writeSize } = ctx

if (!adapter.bulk && !adapter.bulkEntities) {
if (!isFunction(adapter.bulk) && !isFunction(adapter.bulkEntities)) {
throw new MutentError(
'EMUT_PARTIAL_ADAPTER',
'The Adapter does not implement both the ".bulk" and ".bulkEntities" methods',
{
adapter: getAdapterName(adapter),
intent,
argument,
options
argument
}
)
}
Expand Down Expand Up @@ -337,7 +334,7 @@ async function * flushQueue (queue, ctx) {

const pending = queue.filter(e => e.shouldCommit)
if (pending.length > 0) {
if (adapter.bulkEntities) {
if (isFunction(adapter.bulkEntities)) {
await adapter.bulkEntities(pending, ctx)
} else {
const result = Object(
Expand Down
Loading

0 comments on commit 08f1d95

Please sign in to comment.