Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFACTORED to the same spec of php norm (partially)
- Loading branch information
Showing
24 changed files
with
640 additions
and
1,045 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"strict": true, | ||
"esnext": true, | ||
"node": true | ||
} |
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,62 @@ | ||
'use strict'; | ||
|
||
const Memory = require('./memory'); | ||
const fs = require('fs-promise'); | ||
const path = require('path'); | ||
|
||
class Disk extends Memory { | ||
constructor(repository, id, options) { | ||
super(repository, id, options); | ||
|
||
this.file = this.options.file; | ||
} | ||
|
||
ensureData() { | ||
if (!this._ensured) { | ||
var file = this.file; | ||
return fs.ensureFile(file) | ||
.then(() => fs.readFile(file)) | ||
.then(buffer => JSON.parse(buffer)) | ||
.catch(err => {}) | ||
.then(function(data) { | ||
this._ensured = true; | ||
this.data = data || {}; | ||
}.bind(this)); | ||
} else { | ||
return Promise.resolve(); | ||
} | ||
} | ||
|
||
persist(collectionId, row) { | ||
var superPersist = super.persist.bind(this); | ||
return this.ensureData() | ||
.then(function() { | ||
return superPersist(collectionId, row); | ||
}) | ||
.then(function(row) { | ||
return fs.writeFile(this.file, JSON.stringify(this.data, null, 2)) | ||
.then(() => row); | ||
}.bind(this)); | ||
} | ||
|
||
remove(cursor) { | ||
var superRemove = super.remove.bind(this); | ||
return this.ensureData() | ||
.then(function() { | ||
return superRemove(cursor); | ||
}) | ||
.then(function(row) { | ||
return fs.writeFile(this.file, JSON.stringify(this.data, null, 2)); | ||
}.bind(this)); | ||
} | ||
|
||
fetch(cursor) { | ||
var superFetch = super.fetch.bind(this); | ||
return this.ensureData() | ||
.then(function() { | ||
return superFetch(cursor); | ||
}); | ||
} | ||
} | ||
|
||
module.exports = Disk; |
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,183 @@ | ||
'use strict'; | ||
|
||
const uuid = require('node-uuid'); | ||
const Connection = require('../connection'); | ||
|
||
class Memory extends Connection { | ||
|
||
constructor(repository, id, options) { | ||
super(repository, id); | ||
|
||
this.options = options || {}; | ||
this.data = this.options.data || {}; | ||
} | ||
|
||
persist(collectionId, row) { | ||
return new Promise(function (resolve, reject) { | ||
if (!row.$id) { | ||
row.$id = uuid.v1(); | ||
} | ||
|
||
// marshall row | ||
var data = this.data[collectionId] = this.data[collectionId] || []; | ||
var foundIndex; | ||
if (data.some((drow, index) => { | ||
if (drow.$id === row.$id) { | ||
foundIndex = index; | ||
return true; | ||
} | ||
})) { | ||
data[foundIndex] = row; | ||
} else { | ||
data.push(row); | ||
} | ||
|
||
// unmarshall row | ||
resolve(row); | ||
}.bind(this)); | ||
} | ||
|
||
remove(cursor) { | ||
return this.fetch(cursor).then(function(rows) { | ||
var data = this.data[cursor.collection.id] = this.data[cursor.collection.id] || []; | ||
|
||
rows.forEach(function (row) { | ||
data.some((drow, index) => { | ||
if (drow.$id === row.$id) { | ||
data.splice(index, 1); | ||
return true; | ||
} | ||
}); | ||
}.bind(this)); | ||
}.bind(this)); | ||
} | ||
|
||
matchCriteria(criteria, row) { | ||
if (!criteria) { | ||
return true; | ||
} | ||
|
||
for(var i in criteria) { | ||
if (criteria[i] !== row[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
fetch(cursor) { | ||
return new Promise(function(resolve, reject) { | ||
var data = this.data[cursor.collection.id] || []; | ||
var rows = []; | ||
var limit = cursor.limit; | ||
var skip = cursor.skip; | ||
|
||
for(let row of data) { | ||
if (skip) { | ||
skip--; | ||
} else { | ||
if (this.matchCriteria(cursor.criteria, row)) { | ||
if (limit !== -1) { | ||
if (limit <= 0) { | ||
break; | ||
} | ||
limit--; | ||
} | ||
|
||
rows.push(row); | ||
} | ||
} | ||
} | ||
resolve(rows); | ||
}.bind(this)); | ||
} | ||
|
||
} | ||
|
||
module.exports = Memory; | ||
// (function(root, factory) { | ||
// 'use strict'; | ||
|
||
// if (typeof module !== 'undefined' && module.exports) { | ||
// var uuid = require('node-uuid'); | ||
// var _ = require('lodash'); | ||
|
||
// module.exports = factory(uuid, _); | ||
// } else { | ||
// root.norm.adapters.memory = factory(root.uuid, root._); | ||
// } | ||
// })(this, function(uuid, _) { | ||
// 'use strict'; | ||
|
||
// var memoryAdapter = function(options) { | ||
// return { | ||
// persist: function(collection, model) { | ||
// this.data = this.data || {}; | ||
// this.data[collection.id] = this.data[collection.id] || []; | ||
|
||
// if (!model.$id) { | ||
// model.$id = uuid.v1(); | ||
// this.data[collection.id].push(model); | ||
// } else { | ||
// var row = _.find(this.data[collection.id], function(row) { | ||
// return row.$id === model.$id; | ||
// }); | ||
// _.merge(row, model); | ||
// } | ||
// return Promise.resolve(model); | ||
// }, | ||
|
||
// remove: function(collection, model) { | ||
// this.data = this.data || {}; | ||
// this.data[collection.id] = this.data[collection.id] || []; | ||
|
||
// for(var i in this.data[collection.id]) { | ||
// var row = this.data[collection.id][i]; | ||
|
||
// if (row.$id === model.$id) { | ||
// this.data[collection.id].splice(i, 1); | ||
// break; | ||
// } | ||
// } | ||
|
||
// return Promise.resolve(); | ||
// }, | ||
|
||
// query: function(cursor, options) { | ||
// this.data = this.data || {}; | ||
// this.data[cursor.collection.id] = this.data[cursor.collection.id] || []; | ||
|
||
// var criteria = cursor.criteria || {}; | ||
// var results = []; | ||
// var maxLimit = cursor.limit(); | ||
// for(var i in this.data[cursor.collection.id]) { | ||
// if (maxLimit <= 0) { | ||
// break; | ||
// } | ||
// var row = this.data[cursor.collection.id][i]; | ||
// var caught = _.some(criteria, function(v, i) { | ||
// var x = i.split('!'); | ||
// var k = x[0]; | ||
// var op = x[1] || 'eq'; | ||
// switch(op) { | ||
// case 'eq': | ||
// if (row[k] !== v) { | ||
// return true; | ||
// } | ||
// break; | ||
// default: | ||
// throw new Error('Unimplemented'); | ||
// } | ||
// }); | ||
// if (!caught) { | ||
// maxLimit--; | ||
// results.push(row); | ||
// } | ||
// } | ||
// return Promise.resolve(results); | ||
// } | ||
// }; | ||
// }; | ||
|
||
// return memoryAdapter; | ||
// }); |
Oops, something went wrong.