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.
EntityManager
✓ import { EntityManager } from 'front-orm'
✗ const EntityManager = require('front-orm')
ESM-only; no CommonJS support declared. Ensure bundler or Node version supports ESM.
PrimaryKey, StringField, NumberField, CollectionField, EntityField
✓ const PrimaryKey = em.defaultClasses.fields.PrimaryKey; const StringField = em.defaultClasses.fields.StringField; const NumberField = em.defaultClasses.fields.NumberField; const CollectionField = em.defaultClasses.fields.CollectionField; const EntityField = em.defaultClasses.fields.EntityField
✗ import { PrimaryKey } from 'front-orm'
These field classes are not exported from the package root; they are accessed via em.defaultClasses.fields after creating an EntityManager instance.
Collection, Entity
✓ const Collection = em.defaultClasses.types.Collection; const Entity = em.defaultClasses.types.Entity
✗ import { Collection } from 'front-orm'
Similar to fields, these types are accessed via em.defaultClasses.types, not directly imported.
Sets up EntityManager, defines Author and Book models with fields, adds CRUD hooks using fetch, and demonstrates finding an author and creating a new one via flush.
import { EntityManager } from 'front-orm';
const setReactivity = (data) => {
if (typeof window !== 'undefined' && (window as any).watch) {
(window as any).watch(data, (newVal: any, oldVal: any, prop: string) => {
console.log(`${prop} changed from ${oldVal} to ${newVal}`);
});
}
return data;
};
const em = new EntityManager(setReactivity);
const PrimaryKey = em.defaultClasses.fields.PrimaryKey;
const StringField = em.defaultClasses.fields.StringField;
const NumberField = em.defaultClasses.fields.NumberField;
const CollectionField = em.defaultClasses.fields.CollectionField;
const EntityField = em.defaultClasses.fields.EntityField;
function Author(em: any) {
return {
id: new PrimaryKey(em),
name: new StringField(em),
age: new NumberField(em),
books: new CollectionField(em, 'Book')
};
}
function Book(em: any) {
return {
id: new PrimaryKey(em),
title: new StringField(em),
author: new EntityField(em, 'Author')
};
}
const Collection = em.defaultClasses.types.Collection;
const Entity = em.defaultClasses.types.Entity;
em.setModel(Author, {
find: new Entity(em, async (value: any) => {
const response = await fetch('/api/authors/' + value.id);
return response.json();
})
});
em.setModel(Book, {
findAll: new Collection(em, async (params: any) => {
const response = await fetch('/api/books', { method: 'GET' });
return response.json();
})
});
em.setHooks({
get: async (model: any, pk: any) => {
const response = await fetch(`/api/${model.name}/${pk}`);
return response.json();
},
create: async (modelData: any, value: any) => {
const response = await fetch(`/api/${modelData.name}`, { method: 'POST', body: JSON.stringify(value), headers: { 'Content-Type': 'application/json' } });
return response.json();
},
update: async (modelData: any, value: any) => {
const response = await fetch(`/api/${modelData.name}/${value.id}`, { method: 'PUT', body: JSON.stringify(value), headers: { 'Content-Type': 'application/json' } });
return response.json();
},
delete: async (modelData: any, pk: any) => {
const response = await fetch(`/api/${modelData.name}/${pk}`, { method: 'DELETE' });
return response.json();
}
});
(async () => {
const author = await em.repositories.Author.find({ id: 1 });
console.log('Author:', author);
const newAuthor = em.post({ name: 'New Author', age: 30 }, em.models.Author);
await em.flush();
console.log('Created author:', newAuthor);
})();
Errors
Common errors & fixes
Cannot find module 'front-orm' or its corresponding type declarations.
TypeScript cannot resolve the module; the package may not have type declarations or they are not installed.
fixInstall types: npm install --save-dev @types/front-orm (if available) or declare module 'front-orm' in a .d.ts file.
Uncaught ReferenceError: require is not defined
Using require() in an ESM-only environment.
fixUse import syntax: import { EntityManager } from 'front-orm'; Property 'defaultClasses' does not exist on type 'EntityManager'
TypeScript strict mode or incorrect type definitions. The package's types may be incomplete or outdated.
fixCast to any: (em as any).defaultClasses.fields.PrimaryKey or extend the types.
Cannot read properties of undefined (reading 'fields')
Accessing em.defaultClasses before creating EntityManager instance.
fixEnsure em is created with new EntityManager() before accessing defaultClasses.
Upgrade
Version history
0.3.0-beta.9latest on npm
Audit
Dependencies
watch-objectoptionalOptional dependency for reactivity; used in setReactivity example