Registry / database / json-schema-entity

json-schema-entity

JSON →
library7.2.0jsnpmunverified

Manage a group of SQL tables with parent-child relations as a document/entity (NoSQL-style). Current stable version 7.2.0. Release cadence is irregular. Key differentiator: define entity schemas using JSON Schema and automatically create relational tables with CRUD operations. Requires a CR layer driver like pg-cr-layer or mssql-cr-layer. Not to be confused with JSON Schema validation libraries.

npm install json-schema-entity
INSTALL
IMPORT
SIG · JSON-SCHEMA-ENTITY
J
json-schema-entity
databasejavascriptv7.2.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.

default
import jse from 'json-schema-entity'
const { jse } = require('json-schema-entity')
Default export is a function. In CommonJS use require('json-schema-entity')
jse
const jse = require('json-schema-entity')
import jse from 'json-schema-entity'
CommonJS default import, no named exports
default
import jse from 'json-schema-entity'; const invoiceClass = jse('invoice', schema)
const invoiceClass = new jse('invoice', schema)
jse is a factory function, not a constructor

Creates an entity class for 'customer' with hasMany relation to 'orders', then creates tables, inserts data, and saves.

const jse = require('json-schema-entity'); const PgCrLayer = require('pg-cr-layer'); const db = new PgCrLayer({ user: 'test', password: 'test', host: 'localhost', port: 5432, database: 'test', pool: { max: 10, idleTimeout: 30000 } }); const customerSchema = { properties: { id: { type: 'integer', autoIncrement: true, primaryKey: true }, name: { type: 'string' } } }; const Customer = jse('customer', customerSchema); Customer.hasMany('orders', { properties: { id: { type: 'integer', autoIncrement: true, primaryKey: true }, total: { type: 'number' }, customerId: { type: 'integer', $ref: 'customer' } } }); const customer = Customer.new(db); customer.createTables() .then(() => customer.syncTables()) .then(() => { const instance = customer.createInstance({ name: 'Acme', orders: [{ total: 100 }] }); return instance.save(); }) .then(() => console.log('Entity saved'));
Debug
Known issues
breakingv7 changed the API: 'syncTables' must be called after 'createTables', and schema validation is more strict.
fix
Ensure createTables() resolves before syncTables() and update schema definitions to conform to JSON Schema draft-04.
affects: >=7.0.0
gotchaMissing required dependency: pg-cr-layer or mssql-cr-layer
fix
Install either pg-cr-layer or mssql-cr-layer as a peer dependency.
affects: >=1.0.0
deprecatedThe global jse function is the only API; named exports removed in v6.
fix
Use const jse = require('json-schema-entity') or import jse from 'json-schema-entity'.
affects: >=6.0.0
gotchaautoIncrement property only works with integer primary keys.
fix
Ensure autoIncrement properties are integers and have primaryKey: true.
affects: >=1.0.0
Errors
Common errors & fixes
Cannot find module 'pg-cr-layer'
pg-cr-layer not installed
fix
npm install pg-cr-layer
jse is not a constructor
Using new jse instead of calling jse as a function
fix
const MyClass = jse('name', schema); // no new
Cannot read property 'new' of undefined
jse returned undefined due to missing schema properties
fix
Ensure schema has valid properties and types.
Upgrade
Version history
7.2.0latest on npm
Audit
Dependencies
pg-cr-layerrequiredRequired for PostgreSQL database operations
mssql-cr-layeroptionalAlternative driver for MSSQL, one of these cr-layer packages is required
Agent activity
9 hits · last 30 days
node
8
Amazon
1
Resources
json-schema-entity — npm install json-schema-entity · libregistry