Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0x8890
committed
Aug 15, 2015
1 parent
ab93fda
commit 8d53039
Showing
21 changed files
with
247 additions
and
400 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
node_modules/ | ||
coverage/ | ||
dist/ | ||
*.db | ||
*.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import clone from 'clone' | ||
import { generateId } from '../common' | ||
|
||
|
||
export function inputRecord (type, record) { | ||
const { recordTypes, keys } = this | ||
const result = {} | ||
const fields = recordTypes[type] | ||
|
||
// ID business. | ||
result[keys.primary] = keys.primary in record ? | ||
record[keys.primary] : generateId() | ||
|
||
for (let field of Object.getOwnPropertyNames(fields)) { | ||
if (!(field in record)) { | ||
result[field] = fields[field][keys.isArray] ? [] : null | ||
continue | ||
} | ||
|
||
result[field] = clone(record[field]) | ||
} | ||
|
||
return result | ||
} | ||
|
||
|
||
export function outputRecord (type, record) { | ||
const { recordTypes, keys } = this | ||
const result = {} | ||
const fields = recordTypes[type] | ||
|
||
// ID business. | ||
result[keys.primary] = record[keys.primary] | ||
|
||
for (let field in record) { | ||
if (!(field in fields)) continue | ||
|
||
const value = clone(record[field]) | ||
|
||
// Do not enumerate denormalized fields. | ||
if (fields[field][keys.denormalizedInverse]) { | ||
Object.defineProperty(result, field, { | ||
configurable: true, writable: true, value | ||
}) | ||
continue | ||
} | ||
|
||
result[field] = value | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
import { applyOptions } from '../common' | ||
import applyUpdate from '../../../common/apply_update' | ||
import { inputRecord, outputRecord } from './helpers' | ||
|
||
|
||
/** | ||
* Memory adapter. | ||
*/ | ||
export default Adapter => class MemoryAdapter extends Adapter { | ||
|
||
connect () { | ||
this.db = {} | ||
|
||
const { recordTypes } = this | ||
|
||
for (let type in recordTypes) | ||
this.db[type] = {} | ||
|
||
return Promise.resolve() | ||
} | ||
|
||
|
||
disconnect () { | ||
delete this.db | ||
return Promise.resolve() | ||
} | ||
|
||
|
||
find (type, ids, options = {}) { | ||
if (ids && !ids.length) return super.find() | ||
|
||
const { db, recordTypes } = this | ||
const fields = recordTypes[type] | ||
const collection = db[type] | ||
let records = [] | ||
let count = 0 | ||
|
||
if (ids) for (let id of ids) { | ||
const record = collection[id] | ||
if (record) { | ||
count++ | ||
records.push(outputRecord.call(this, type, record)) | ||
} | ||
} | ||
else for (let id in collection) { | ||
count++ | ||
records.push(outputRecord.call(this, type, collection[id])) | ||
} | ||
|
||
return Promise.resolve(applyOptions(count, fields, records, options)) | ||
} | ||
|
||
|
||
create (type, records) { | ||
records = records.map(inputRecord.bind(this, type)) | ||
|
||
const { db, keys, errors: { ConflictError } } = this | ||
const collection = db[type] | ||
|
||
for (let record of records) { | ||
const id = record[keys.primary] | ||
|
||
if (id in collection) | ||
return Promise.reject(new ConflictError( | ||
`Record with ID "${id}" already exists.`)) | ||
} | ||
|
||
for (let record of records) collection[record[keys.primary]] = record | ||
|
||
return Promise.resolve(records.map(outputRecord.bind(this, type))) | ||
} | ||
|
||
|
||
update (type, updates) { | ||
if (!updates.length) return super.update() | ||
|
||
const { db, keys } = this | ||
const collection = db[type] | ||
let count = 0 | ||
|
||
for (let update of updates) { | ||
const id = update[keys.primary] | ||
let record = collection[id] | ||
|
||
if (!record) continue | ||
|
||
count++ | ||
record = outputRecord.call(this, type, record) | ||
|
||
applyUpdate(record, update) | ||
|
||
collection[id] = inputRecord.call(this, type, record) | ||
} | ||
|
||
return Promise.resolve(count) | ||
} | ||
|
||
|
||
delete (type, ids) { | ||
if (ids && !ids.length) return super.delete() | ||
|
||
const { db } = this | ||
const collection = db[type] | ||
let count = 0 | ||
|
||
if (ids) for (let id of ids) { | ||
if (id in collection) { | ||
count++ | ||
delete collection[id] | ||
} | ||
} | ||
else for (let id in collection) { | ||
count++ | ||
delete collection[id] | ||
} | ||
|
||
return Promise.resolve(count) | ||
} | ||
|
||
} |
Oops, something went wrong.