2010-08-31 27 views

Trả lời

3

Tôi đang tìm kiếm điều tương tự. Nó có vẻ giống như những chiếc nhẫn mỏng. Người trông có vẻ hứa hẹn nhất với tôi là persistence.js. Impel cũng có vẻ tốt nhưng, thật không may, có vẻ như nó chưa được cập nhật trong một năm rưỡi. ActiveRecord.js có thể kết thúc hoạt động, nhưng có vẻ như chúng chưa hỗ trợ Web SQL. Hy vọng rằng ai đó sẽ đăng một số tùy chọn khác.

+0

persistence.js. – bennedich

1

Tôi cũng đang tìm kiếm điều tương tự. JazzRecord trông giống như một ứng cử viên có khả năng.

7

Có một cái mới gọi là JayData library, cái này giống như EntityFramework (hoặc NHibernate) cho nền tảng JavaScript: cung cấp JavaScript Language Query (JSLQ) và JavaScript CRUD. Cũng hỗ trợ các định nghĩa mô hình, navigationProperties và 1..1.0, 1..m, m..n quan hệ.

tôi sao chép một codesnippet ngắn về cách sử dụng nó:

//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 } 
}); 

Bạn có thể mã hóa chống lại OrdContext và các bộ sưu tập trong đó. Dòng sau sẽ tạo ra một trường hợp ngữ cảnh được hỗ trợ bởi WebSQL địa phương (bạn có các tùy chọn khác như IndexedDB hoặc OData)

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

Thêm một số dữ liệu

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(); 

Bây giờ bạn có dữ liệu trong cửa hàng bạn có thể truy vấn nó. Các truy vấn JSLQ được hỗ trợ trên các trường thực thể, cộng với các trường điều hướng trỏ theo hướng m..1. (Trong phiên bản 1.0 bạn không thể trực tiếp chống lại 1..m navProperties. Bạn có thể phá vỡ điều này với biểu thức in

//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) { ... }) 
Các vấn đề liên quan