Ideal & idomatic javascript interface for RESTful API

I am trying to write an angular service to interface with a RESTful API.
For sake of simplicity, lets assume the API is

+ Company
|___+ Department
|   |____ Person
|
|____ Person

Notice how person can be under Company > Department or directly underCompany.
Each of the entities (Company, Department and Person) support add, edit, list and get_by_id.

Which of the following interfaces is more idiomatic?

Option 1:

// In all cases, get(), put() return $http promise

companyApi().get() // List all companies
companyApi(1).get() // Get company with ID 1
companyApi(1).departments().get() // List all departments
companyApi(1).departments(2).get() // Get department with ID 2
companyApi(1).departments(2).persons().get() // List all persons
companyApi(1).departments(2).persons().put(p) //Add a new person in department 2
companyApi(1).persons().put(p) //Add a new person in company 1
companyApi(1).persons(3).put(p) //Edit person with ID=3 in company 1
companyApi(1).persons(1).remove(p) // Delete a person

Option 2:

// In all cases, get(), put() return $http promise

// List all companies
companyApi({
    type: 'company'
}).get();

// Get company with ID=1
companyApi({
    type: 'company',
    companyId: 1
}).get();

// Get department with ID=2 under company 1
companyApi({
    type: 'department',
    companyId: 1,
    departmentId: 2
}).get();

// List persons under department with ID=2 under company 1
companyApi({
    type: 'person',
    companyId: 1,
    departmentId: 2
}).get();

// Get person with ID=3 under department with ID=2 under company 1
companyApi({
    type: 'person',
    companyId: 1,
    departmentId: 2,
    personId: 3
}).get();

// Get person with ID=3 under department with ID=2 under company 1
companyApi({
    type: 'person',
    companyId: 1,
    departmentId: 2,
    personId: 3
}).get();

// Add person under department with ID=2 under company 1
companyApi({
    type: 'person',
    companyId: 1,
    departmentId: 2,
}).put(personObj);

//Edit person with ID=3 in company 1
companyApi({
    type: 'person',
    companyId: 1,
    departmentId: 2,
    personId: 3
}).put(personObj);

// Add person under directly under company 1
companyApi({
    type: 'person',
    companyId: 1
}).put(personObj);

First of all, whatever you do, please use unique ids. In your current scheme you can only reference an entity through the exact path through the tree, that is generally annoying to work with. What happens if a person is moved from one department to another? Or if there is a department restructure? Nothing will retain its handle.

Second, don’t make entities that basically work the same way different. Company, department and any other organisation entity is just a container in your data model, there is no reason to distinguish between them in your basic data model.

The simplest way of implementing a tree structure is to make a data table with the fields id and parentId and have the parentId of any row be the id of its parent.

You can of course make a host of different functions to interface this data, you could start out with something like:

get(id)
getChildren(id)
getChildrenIds(id)
getDescendants(id)
create(parentId, ...)
move(id,newParentId)

9

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật