Registry / storage / can-rest-model

can-rest-model

JSON →
library2.0.0jsnpmunverified

can-rest-model connects a CanJS DefineMap to a RESTful API endpoint. Version 2.0.0 provides a simple API to create model layers with automatic CRUD operations, serialization, and real-time support. It is part of the CanJS ecosystem and is maintained by Bitovi. Unlike generic REST clients, it integrates deeply with CanJS's observable maps and lists, enabling reactive data binding and seamless synchronization. Release cadence is sporadic; updates follow CanJS releases.

npm install can-rest-model
INSTALL
IMPORT
SIG · CAN-REST-MODEL
C
can-rest-model
storagejavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

restModel
import { restModel } from 'can-rest-model'
const restModel = require('can-rest-model')
ESM-only in v2; CJS require is not supported.
DefineMap
import { DefineMap } from 'can-define'
import { DefineMap } from 'can-rest-model'
DefineMap is not exported by can-rest-model; import from can-define.
connect
import { connect } from 'can-connect'
import { connect } from 'can-rest-model'
connect is from can-connect; can-rest-model only exports restModel.

Demonstrates defining a Todo model with DefineMap, connecting it to a REST API via restModel, and performing save and findAll operations.

import { restModel } from 'can-rest-model'; import { DefineMap } from 'can-define'; import { connect } from 'can-connect'; const Todo = DefineMap.extend('Todo', { id: { type: 'number', identity: true }, name: 'string', completed: 'boolean' }); const todoConnection = restModel({ Map: Todo, url: '/api/todos/{id}', name: 'todo' }); // Create an instance const todo = new Todo({ name: 'Learn CanJS', completed: false }); todoConnection.save(todo).then(() => { console.log('Saved!', todo.isNew()); }); // Fetch all todoConnection.findAll({}).then(todos => { todos.forEach(t => console.log(t.name)); });
Debug
Known issues
breakingVersion 2.0.0 dropped support for CommonJS (require). Use ESM imports only.
fix
Replace const restModel = require('can-rest-model') with import { restModel } from 'can-rest-model'.
affects: >=2.0.0
deprecatedUse of can-connect/base is deprecated; use can-connect directly.
fix
Import from 'can-connect' instead of 'can-connect/base'.
affects: >=2.0.0
gotchaThe Map property in restModel must be a DefineMap with an identity property (id). Defining without identity will cause errors.
fix
Ensure the DefineMap has { identity: true } on the primary key field.
affects: *
gotcharestModel expects a url template like '/api/todos/{id}'; missing braces on id parameter will not resolve correctly.
fix
Use url: '/api/todos/{id}' with curly braces around the id placeholder.
affects: *
gotchaThe name property in restModel options is used for real-time events (e.g., 'todo created'). If omitted, events may not work.
fix
Provide a unique name (e.g., name: 'todo') for real-time support.
affects: *
Errors
Common errors & fixes
TypeError: restModel is not a function
Using CommonJS require which returns undefined in v2.
fix
Use ES6 import: import { restModel } from 'can-rest-model'.
Error: "url" must be a string.
Missing url option or empty value passed to restModel.
fix
Provide a url string with {id} placeholder, e.g., '/api/todos/{id}'.
Error: DefineMap must have an identity property defined.
The DefineMap passed to restModel does not have a property with identity: true.
fix
Add a property like id: {type: 'number', identity: true} to your DefineMap.
Error: "name" is required for real-time behavior.
The name option is missing from restModel options.
fix
Add name: 'todo' or similar to restModel options.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
can-definerequiredProvides DefineMap and DefineList types for observable models
can-connectrequiredProvides base connection logic used by rest-model
Agent activity
17 hits · last 30 days
node
16
OpenAI (training)
1
Resources
can-rest-model — npm install can-rest-model · libregistry