@neoimpulse/cap-js-restore-model-defined-types
A CAP plugin that casts values back to the types the CDS model declares after a READ, for the entities you name, where the database driver returned them as strings.
A cds.Decimal that arrives as a string is a familiar surprise, and the usual fix is a conversion in every handler that touches it. This does it once, on READ, for the entities you name.
Facts
Where it sits
Who is behind it
In the atlas: maintains 7@neoimpulseGmbHmaintainer
Before choosing this
Conditions
LicenceSAP Developer License Agreement 3.2 CAP§ 1, 2, 3A cds-plugin is used with SAP CAP materials, or an artifact is explicitly identified as a CAP plugin or application.
- § 1 License. Productive customer applications are limited to SAP BTP or another SAP-licensed platform, and mass extraction to a non-SAP product is restricted except where solely required for SAP interoperability.
- § 2 Intellectual Property. The agreement retains SAP's rights in the covered materials and limits making APIs, tools or software available to third parties, while preserving ownership of customer applications subject to SAP's underlying rights.
- § 3 Artificial Intelligence Training. The agreement prohibits use of the covered materials and customer applications for training artificial-intelligence models or systems.
Also, from Database connectors§ 1: The connector itself does not decide the licence boundary. Section 1 makes the destination and purpose of extraction relevant, including the scoped exception for interoperability with an SAP product.
What it declares
Every artifact
- Type
- CDS plugin
- Origin
- Independently maintained
- Runtime
- Node.js
As a CDS plugin
- Declared for
- >=9
- Distributed through
- npm
Connections
Connecting a data model and its queries to a database
Download
Install
npm add @neoimpulse/cap-js-restore-model-defined-typesDerived from the package name. No recipe recorded; the README has the configuration.
README
quoted from neoimpulseGmbH/cap-js-restore-model-defined-types under Apache-2.0, readCAP.js Restore Model Defined Types Plugin
This plugin for the SAP Cloud Application Programming Model (CAP) automatically restores model-defined types for numeric and structured data in READ operations. It ensures that data returned from the database matches the types defined in your CDS model.
Features
- Automatically converts string representations of numbers back to their proper types (
cds.Decimal→float,cds.Integer→int) - Handles nested structures including Associations and Compositions
- Processes structured types (
cds.Structured) recursively - Works with both single entities and arrays of entities
- Applies to all READ operations automatically
Installation
To install the plugin, add it to your CAP project:
npm install @neoimpulse/cap-js-restore-model-defined-types
Usage
The plugin works automatically once installed, but you can configure which entities should be processed.
Basic Usage (All Entities)
By default, the plugin processes all entities. No additional configuration required.
Configuration Options
You can configure the plugin in your package.json or .cdsrc.json:
{
"cds": {
"requires": {
"cap-js-restore-model-defined-types": {
"enabled": true,
"entities": "*",
"exclude": [],
"annotation": "@restoreTypes"
}
}
}
}
Configuration Properties
enabled(boolean, default:true): Enable or disable the pluginentities(string|array, default:"*"): Which entities to process"*": Process all entities["EntityA", "EntityB"]: Process specific entities by name"Book*": Process entities matching pattern (supports glob-like patterns)"annotation": Only process entities with the specified annotation
exclude(array, default:[]): Entity names to explicitly excludeannotation(string, default:"@restoreTypes"): Custom annotation to enable per entity
Configuration Examples
1. Process All Entities (Default)
{
"cds": {
"requires": {
"cap-js-restore-model-defined-types": {}
}
}
}
2. Process Specific Entities
{
"cds": {
"requires": {
"cap-js-restore-model-defined-types": {
"entities": ["Books", "Authors", "Orders"]
}
}
}
}
3. Process Entities with Pattern
{
"cds": {
"requires": {
"cap-js-restore-model-defined-types": {
"entities": "my.bookshop.*"
}
}
}
}
4. Use Annotation-Based Configuration
{
"cds": {
"requires": {
"cap-js-restore-model-defined-types": {
"entities": "annotation"
}
}
}
}
Then annotate your entities:
namespace my.bookshop;
entity Books @restoreTypes {
key ID : Integer;
title : String;
price : Decimal(10,2);
pages : Integer;
}
entity Authors {
key ID : Integer;
name : String;
// This entity will NOT be processed
}
5. Exclude Specific Entities
{
"cds": {
"requires": {
"cap-js-restore-model-defined-types": {
"entities": "*",
"exclude": ["TechnicalEntity", "LogEntry"]
}
}
}
}
Example Model
namespace my.bookshop;
entity Books {
key ID : Integer;
title : String;
price : Decimal(10,2);
pages : Integer;
author : Association to Authors;
}
entity Authors {
key ID : Integer;
name : String;
age : Integer;
books : Composition of many Books on books.author = $self;
}
What It Does
When you perform READ operations, the plugin ensures that:
pricevalues are returned as proper floating-point numbers (not strings)pagesandagevalues are returned as integers (not strings)- Associated and composed entities are processed recursively
- Structured types maintain their proper data types
How It Works
The plugin hooks into all READ operations and processes the results to restore the correct data types based on your CDS model definitions. It:
- Scans each attribute in the returned data
- Checks the corresponding CDS model definition
- Converts string representations back to proper numeric types
- Recursively processes nested structures (Associations, Compositions, Structured types)
The conversion happens automatically for:
- cds.Decimal: String values are converted to floating-point numbers using
parseFloat() - cds.Integer: String values are converted to integers using
parseInt() - cds.Association: Nested associated entities are processed recursively
- cds.Composition: Nested composed entities are processed recursively
- cds.Structured: Structured types are processed recursively
Technical Details
The plugin adds a restoreModelDefinedTypes method to each Application Service and hooks it into the after READ event handler. This ensures that all data returned from READ operations has the correct types according to your CDS model.
License
MIT
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Support
If you encounter any issues or have questions, please open an issue in the GitHub repository.```
License
This project is licensed under the Apache License 2.0. See the LICENSE file for more details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Acknowledgments
Thanks to the SAP CAP community for their support and contributions.
History
Others providing
Compare all 22 on the Database connectors page →- SAP HANA CloudSAP HANA CloudThe managed database under most of this: CAP's default target, the store Datasphere and Analytics Cloud sit on, and a service with plans and components.sap official, btpManaged SAP HANA database service on BTP
- PostgreSQL on SAP BTP, hyperscaler optionPostgreSQL on SAP BTP, hyperscaler optionA managed PostgreSQL from the underlying hyperscaler — AWS, Azure, Google Cloud or Alicloud — consumed through a BTP service binding.sap official, btpManaged PostgreSQL from the hyperscaler via a BTP service binding
- com.sap.cds:cds-feature-hanacom.sap.cds:cds-feature-hanaThe CAP Java feature that wires a Maven application to SAP HANA Cloud, the production default among CAP's supported databases.sap official, capSAP HANA Cloud datasource for CAP Java applications
- com.sap.cds:cds-feature-jdbccom.sap.cds:cds-feature-jdbcConnects the CAP Java runtime to JDBC databases through the CDS4j JDBC runtime: H2 and SQLite, which capire names for local development and CI tests rather than enterprise use.sap official, capJDBC runtime for H2 and SQLite in CAP Java
- com.sap.cds:cds-feature-postgresqlcom.sap.cds:cds-feature-postgresqlThe CAP Java feature that wires a Maven application to PostgreSQL as its datasource beside SAP HANA, with limitations capire lists: schema evolution first, and no multitenancy or extensibility yet.sap official, capPostgreSQL datasource for CAP Java applications
- com.sap.cds:cds-services-bomcom.sap.cds:cds-services-bomThe Maven bill of materials for CAP Java, pinning every com.sap.cds module in a release, from the API and core runtime to the Spring Boot starters and the CQN engine, to the same version.sap official, capMaven bill of materials pinning CAP Java module versions
- @cap-js/db-service@cap-js/db-serviceThe base every CAP database service is built on: the shared query translation and pooling that SQLite, PostgreSQL and HANA specialise rather than reimplement.sap official, capBase service shared by CAP's SQLite, PostgreSQL and HANA services
- @cap-js/hana@cap-js/hanaThe HANA database service — the production default, on the same streamlined architecture as the SQLite and PostgreSQL services.sap official, capSAP HANA database service for CAP
- @cap-js/postgres@cap-js/postgresThe PostgreSQL database service — the production option for running CAP off HANA, sharing the base service with the other three.sap official, capPostgreSQL database service for CAP
- @cap-js/sqlite@cap-js/sqliteThe SQLite service, in memory during the inner loop — which is why a CAP project runs before anyone has provisioned a database.sap official, capSQLite database service for CAP, in memory during development
- cap-postgres-btp-connectivitycap-postgres-btp-connectivityConnects a CAP Node.js application to a PostgreSQL database that is not publicly reachable, tunnelling through BTP Connectivity and the Cloud Connector.independent, capTunnels CAP Node.js to a private PostgreSQL via Cloud Connector
- cds-mongodbcds-mongodbMongoDB as a native CAP database service, through Mongoose — CQN reads and writes, managed associations as ObjectId references, $expand resolved by populate, and cds deploy --to mongoose.independent, capMongoDB as a CAP database service through Mongoose
- @cds-pgroll/postgres@cds-pgroll/postgresThe destructive half of PostgreSQL schema evolution — DROP and RENAME, which CAP's own schema_evolution: alter will not perform — driven from the command line beside @cap-js/postgres.independent, capCLI for DROP and RENAME schema changes beside @cap-js/postgres
- @quadrio/cds-db2@quadrio/cds-db2A DB2 adapter for CDS, so a CAP model can be deployed to and served from DB2, last released July 2023 and still on npm while the repository it was published from has been deleted.independent, capDB2 adapter for CDS
- @sap/cds-hana@sap/cds-hanaThe SAP Cloud Application Programming Model's database adapter for SAP HANA, the package a CAP project adds to run its model on a SAP HANA database.sap official, capDatabase adapter for SAP HANA in CAP
- @sap/cds@sap/cdsThe CAP Node.js runtime, which compiles the CDS model, serves the services declared in it, answers generic requests before any handler runs, and loads every cds-plugin.sap official, capCAP Node.js runtime
- @sap/hana-client@sap/hana-clientSAP's own Node.js driver for SAP HANA: connect, run SQL and prepared statements, synchronously, through callbacks or as promises, over native client libraries npm installs per platform.sap official, btpSAP's Node.js driver for SAP HANA
- SAP HANASAP HANASAP’s relational database platform, which stores application data and executes SQL and SQLScript in the database, available on-premise and as the managed service SAP HANA Cloud.sap official, btpSAP's relational database platform, on-premise and in HANA Cloud
- cds-pgcds-pgA PostgreSQL adapter for SAP CDS, built by the SAP Mentors community, which gave CAP a Postgres persistence option years before @cap-js/postgres shipped.independent, capPostgreSQL adapter for SAP CDS from the SAP Mentors community
- cap-snowflakecap-snowflakeA CAP database adapter for Snowflake with OData V4 support, which the maintainer marks as early development, so its contract is expected to move.independent, capCAP database adapter for Snowflake with OData V4 support
- cds-mysqlcds-mysqlA MySQL and MariaDB database adapter for CAP that connects to a database instance and translates incoming CQL/CQN requests to it; the repository the package declares has been deleted.independent, capMySQL and MariaDB database adapter for CAP
Details
Sources
Sources are graph entities: the same page may back several entries, and an unreachable source flags every row that rests on it.
Compatibility and provenance
History in the atlas
- First seen.
For your agent
- Ref
artifact:neoimpulse-cap-js-restore-model-defined-types- JSON
- /artifacts/neoimpulse-cap-js-restore-model-defined-types.json
- MCP
- Connecting an agent