开发者

Are there any ORM frameworks for Web SQL (javascript)? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.

We don’t allow questions seeking recommendations for books, tools, software libraries, and more. You can edit the question so it can be answered with facts and citations.

Closed 2 years ago.

开发者_Python百科 Improve this question

Anyone knows if there's one available now or will be in a near future?


There's a new one called JayData library, this one is like EntityFramework (or NHibernate) for the JavaScript platform: provides JavaScript Language Query (JSLQ) and JavaScript CRUD. Also supports model definitions, navigationProperties and 1..1.0, 1..m, m..n relations.

I copy a short codesnippet on how to use it:

//define storage model: Department and Employee in a 1..m relation

$data.Entity.extend("$org.types.Department", {
  Id: { type: "int", key: true, computed: true },
  Name: { type: "string", required: true },
  Address: { type: "string" },
  Employees: { type: "Array", elementType: "$org.types.Employee", inverseProperty:"Department" }
});


$data.Entity.extend("$org.types.Employee", {
  Id: { type: "int", key: true, computed: true },
  FirstName: { type: "string", required: true },
  LastName: { type: "string", required: true }, 
  Department: { type: "$org.types.Department", inverseProperty:"Employees"}
});

$data.EntityContext.extend("$org.types.OrgContext", {
  Department: { type: $data.EntitySet, elementType: $org.types.Department },
  Employee: { type: $data.EntitySet, elementType: $org.types.Employee }
});

You can code against the OrdContext and the collections in it. The following line will create a context instance that is backed by local WebSQL (you have other options like indexeddb or OData)

var context = new $org.types.OrgContext({ name: "webSql", databaseName: "OrgDB" });

Add some data

var department = new $org.types.Department({ Name: 'Finance', Employees: [] });

var emp1 = new $org.types.Employee({ FirstName: 'John', LastName: 'Smith'});
department.Employees.push(emp1);

var emp2 = new $org.types.Employee({ FirstName: 'Jane', LastName: 'Smith'});
emp2.Department = department;

context.add(department);
context.add(emp2);

context.saveChanges();

Now that you have data in the store you can query it. JSLQ queries are supported on entity fields, plus navigation fields that point in the m..1 direction. (In version 1.0 you can not directly against 1..m navProperties. You can circumvent this with the in expression

//filter
context.Employees
  .filter( function(emp) { return emp.LastName == 'Smith' })
  .toArray(...);

//filter
context.Employees
  .filter( function(emp) { return emp.FirstName.startsWith('J') ||
                                  emp.LastName.toLowerCase.contains('mith') })
  .toArray(...);

//filter2
context.Employees
  .filter( function(emp) { return emp.Department.Id == 1 })
  .toArray( function(emps) { } );

//filter2 + eager load
context.Employees
  .include("Department")
  .filter( function(emp) { return emp.Department.Id == 1 })
  .toArray( function(emps) { } );


//map/project
context.Employees
  .filter( function(emp) { return emp.Department.Id == 1 }).toArray(...)
  .map( function(emp) { return { EmployeeName: emp.FirstName + emp.LastName, 
                                DepartmentName: emp.Department.Name }})
  .forEach( function(item) { ... })


I am looking for the same thing. It seems like slim pickings. The one that looked the most promising to me is persistence.js. Impel also looks good but, unfortunately, it looks like it hasn't been updated in a year and a half. ActiveRecord.js could end up working out, but it doesn't seem like they are supporting Web SQL yet. Hopefully someone will post some more options.


I am also looking for the same thing. JazzRecord looks like a likely candidate.


An implementation based on JazzRecord is the joli.js implementation designed for the Appcelerator Titanium framework.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜