js-core-data
Advanced tools
Comparing version 0.0.41 to 0.0.71
69
index.js
var PersistentStoreCoordinator = require('./lib/PersistentStoreCoordinator'), | ||
ManagedObjectModel = require('./lib/ManagedObjectModel'), | ||
ManagedObjectContext = require('./lib/ManagedObjectContext'), | ||
ManagedObject = require('./lib/ManagedObject'); | ||
ManagedObject = require('./lib/ManagedObject'), | ||
Predicate = require('./lib/FetchClasses/Predicate'), | ||
Pool = require('generic-pool'); | ||
@@ -9,2 +11,65 @@ module.exports.PersistentStoreCoordinator = PersistentStoreCoordinator; | ||
module.exports.ManagedObjectContext = ManagedObjectContext; | ||
module.exports.ManagedObject = ManagedObject; | ||
module.exports.ManagedObject = ManagedObject; | ||
module.exports.Predicate = Predicate; | ||
/** | ||
* helpers | ||
*/ | ||
//var persistentStoresCache = {} | ||
//module.exports.createContext = function(modelFile,storeURL,callback){ | ||
// var persistentStoreCoordinator = persistentStoresCache[modelFile + storeURL]; | ||
// if(!persistentStoreCoordinator){ | ||
// var objectModel = new ManagedObjectModel(modelFile); | ||
// persistentStoreCoordinator = new PersistentStoreCoordinator(objectModel); | ||
// if(storeURL.indexOf('mysql:') === 0){ | ||
// persistentStoreCoordinator.addStore(PersistentStoreCoordinator.STORE_TYPE_MYSQL,storeURL,function(err){ | ||
// if(err)return callback(err); | ||
// callback(null,new ManagedObjectContext(persistentStoreCoordinator)); | ||
// }) | ||
// }else{ | ||
// throw new Error('unknown store for url' + storeURL) | ||
// } | ||
// }else { | ||
// callback(null,new ManagedObjectContext(persistentStoreCoordinator)); | ||
// } | ||
//} | ||
module.exports.createContextPool = function(modelFile,storeURL,options,callback){ | ||
var objectModel = new ManagedObjectModel(modelFile); | ||
var persistentStoreCoordinator = new PersistentStoreCoordinator(objectModel); | ||
if(storeURL.indexOf('mysql:') === 0){ | ||
persistentStoreCoordinator.addStore(PersistentStoreCoordinator.STORE_TYPE_MYSQL,storeURL,function(err){ | ||
if(err)return callback(err); | ||
createAndSendPool() | ||
}) | ||
}else{ | ||
callback(new Error('unknown store for url' + storeURL)); | ||
} | ||
function createAndSendPool(){ | ||
options = options || {}; | ||
options.name = 'model:'+modelFile+';store:'+storeURL; | ||
options.max = options.max || 10; | ||
options.idleTimeoutMillis = options.idleTimeoutMillis || 1000; | ||
options.create = function(callback){ | ||
callback(null,new ManagedObjectContext(persistentStoreCoordinator)) | ||
}; | ||
options.destroy = function(context){ | ||
context.destroy(); | ||
} | ||
callback(null,new Pool.Pool(options)) | ||
} | ||
} | ||
//module.exports.createContextPool('./test/schemes/test-model.yaml','mysql://root@localhost/test',null,function(err,pool){ | ||
// pool.acquire(function(err,context){ | ||
// console.log('acquired context'); | ||
// pool.release(context); | ||
//// pool.drain(function(){ | ||
//// console.log('drained'); | ||
//// pool.destroyAllNow() | ||
//// }); | ||
// }) | ||
//}) |
@@ -49,2 +49,28 @@ // Generated by CoffeeScript 1.6.3 | ||
ManagedObject.prototype.setValues = function(values) { | ||
var attributeDescription, _i, _len, _ref, _results; | ||
_ref = this.entity.attributes; | ||
_results = []; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
attributeDescription = _ref[_i]; | ||
if (values[attributeDescription.name] != null) { | ||
_results.push(this[attributeDescription.name] = values[attributeDescription.name]); | ||
} else { | ||
_results.push(void 0); | ||
} | ||
} | ||
return _results; | ||
}; | ||
ManagedObject.prototype.toJSON = function() { | ||
var attributeDescription, json, _i, _len, _ref; | ||
json = {}; | ||
_ref = this.entity.attributes; | ||
for (_i = 0, _len = _ref.length; _i < _len; _i++) { | ||
attributeDescription = _ref[_i]; | ||
json[attributeDescription.name] = this[attributeDescription.name]; | ||
} | ||
return json; | ||
}; | ||
ManagedObject.addAttributeDescription = function(attributeDescription) { | ||
@@ -51,0 +77,0 @@ var capitalizedName; |
@@ -37,2 +37,5 @@ // Generated by CoffeeScript 1.6.3 | ||
ManagedObjectContext.prototype.insertObject = function(object) { | ||
if (this.locked) { | ||
throw new Error('context is locked'); | ||
} | ||
if (object.managedObjectContext !== this) { | ||
@@ -52,2 +55,9 @@ throw new Error('cannot insert object to another context'); | ||
ManagedObjectContext.prototype.deleteObject = function(object) { | ||
if (this.locked) { | ||
throw new Error('context is locked'); | ||
} | ||
return this._deleteObjectWithoutLockCheck(object); | ||
}; | ||
ManagedObjectContext.prototype._deleteObjectWithoutLockCheck = function(object) { | ||
if (object.managedObjectContext !== this) { | ||
@@ -152,2 +162,5 @@ throw new Error('cannot delete object from another context'); | ||
ManagedObjectContext.prototype.reset = function() { | ||
if (this.locked) { | ||
throw new Error('context is locked'); | ||
} | ||
this.registeredObjects = []; | ||
@@ -159,2 +172,5 @@ this.updatedObjects = []; | ||
ManagedObjectContext.prototype.destroy = function() { | ||
if (this.locked) { | ||
throw new Error('context is locked'); | ||
} | ||
delete this.registeredObjects; | ||
@@ -241,3 +257,3 @@ delete this.insertedObjects; | ||
return async.forEach(objects, function(obj, _cb) { | ||
_this.deleteObject(obj); | ||
_this._deleteObjectWithoutLockCheck(obj); | ||
return _this._deleteObjectsRelationships(obj, _cb); | ||
@@ -244,0 +260,0 @@ }, cb); |
@@ -33,3 +33,3 @@ { | ||
"scripts": { | ||
"test": "node ./test/every-test.js", | ||
"test": "node ./test/every-context-test.js", | ||
"build": "uglifyjs -o ./bignumber.min.js ./bignumber.js" | ||
@@ -36,0 +36,0 @@ }, |
{ | ||
"name": "js-core-data", | ||
"description": "ORM inspired by Apple's Objective-C CoreData", | ||
"version": "0.0.41", | ||
"version": "0.0.71", | ||
"dependencies":{ | ||
@@ -12,3 +12,4 @@ "moment":"x", | ||
"js-yaml":"x", | ||
"yaml-config":"x" | ||
"yaml-config":"x", | ||
"generic-pool":"x" | ||
}, | ||
@@ -15,0 +16,0 @@ "scripts":{ |
@@ -92,2 +92,38 @@ var assert = require("assert"), | ||
}) | ||
it('should create object and assign all valid values from object',function(done){ | ||
var context = new ManagedObjectContext(storeCoordinator) | ||
var obj = context.createObjectWithName('Hello') | ||
assert.doesNotThrow(function(){ | ||
obj.setValues({ | ||
bool:true, | ||
name:'test', | ||
int:1600, | ||
decim: 0.55, | ||
float: 10.505, | ||
double: 100.5054, | ||
email:'jackie@gmail.com', | ||
url:'http://www.google.com', | ||
timestamp: timestamp | ||
}) | ||
}) | ||
context.save(done); | ||
}) | ||
it('should load all attributes',function(done){ | ||
var context = new ManagedObjectContext(storeCoordinator) | ||
context.getObjects('Hello',null,null,function(err,objects){ | ||
if(err)return done(err); | ||
var obj = objects[0]; | ||
assert.strictEqual(obj.bool,true); | ||
assert.strictEqual(obj.name,'test'); | ||
assert.strictEqual(obj.int,1600); | ||
assert.strictEqual(obj.decim,0.55); | ||
assert.strictEqual(obj.float,10.505); | ||
assert.strictEqual(obj.double,100.5054); | ||
assert.strictEqual(obj.email,'jackie@gmail.com'); | ||
assert.strictEqual(obj.url,'http://www.google.com'); | ||
assert.equal(obj.date.toISOString(),date.toISOString()) | ||
assert.equal(obj.timestamp,timestamp) | ||
done(); | ||
}) | ||
}) | ||
it('shouldn\'t assign (in)valid date value',function(){ | ||
@@ -132,3 +168,3 @@ var context = new ManagedObjectContext(storeCoordinator) | ||
}) | ||
it('shouldn pass invalid string',function(){ | ||
it('shouldn\'t pass invalid string',function(){ | ||
var context = new ManagedObjectContext(storeCoordinator) | ||
@@ -172,2 +208,5 @@ var obj = context.createObjectWithName('Hello') | ||
}) | ||
assert.throws(function(){ | ||
obj.setValues({url:'sfgsg://www.google.com/aadfsdg'}); | ||
}) | ||
assert.doesNotThrow(function(){ | ||
@@ -186,2 +225,5 @@ obj.url = 'http://www.google.com'; | ||
assert.doesNotThrow(function(){ | ||
obj.setValues({url:'ftp://www.google.com/test/com.abcd?testmy&arg2=a'}); | ||
}) | ||
assert.doesNotThrow(function(){ | ||
obj.url = 'http://john:doe@www.google.com'; | ||
@@ -206,2 +248,5 @@ }) | ||
}) | ||
assert.throws(function(){ | ||
obj.setValues({email:'john@doe.'}); | ||
}) | ||
assert.doesNotThrow(function(){ | ||
@@ -250,2 +295,5 @@ obj.email = 'john@doe.com'; | ||
}) | ||
assert.throws(function(){ | ||
obj.setValues({int:'25.689'}); | ||
}) | ||
assert.doesNotThrow(function(){ | ||
@@ -270,2 +318,5 @@ obj.int = 25; | ||
}) | ||
assert.throws(function(){ | ||
obj.setValues({bool:'my false'}); | ||
}) | ||
assert.doesNotThrow(function(){ | ||
@@ -272,0 +323,0 @@ obj.bool = true; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Wildcard dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
7004007
8
216
79604
9
+ Addedgeneric-pool@x
+ Addedgeneric-pool@3.9.0(transitive)