npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

orm-js

v0.2.1

Published

Database abstraction layer using decorators.

Downloads

50

Readme

orm-js

Note: this module is still in the early alpha phase and still very experimental. Support for JavaScript (i.e., not using TypeScript) isn't provided yet.

This module allows you to map your entity classes, as written in JavaScript or TypeScript, to a database schema, using decorators. Support for SQLite is available through the separate module orm-js-sqlite, support for other databases is planned.

orm-js is designed to work with JavaScript Promise. It's suitable to work with async/await in TypeScript.

Introduction

Many web applications consist of code (such as JavaScript) and data stored in a database system (such as MySQL, SQLite). orm-js provides you some abstraction in order to save your data object to the database or retieve the data mapped back to an object. It also takes care for the database creation and the relations between the various tables (entities).

Take a look at the following code snippet:

import * as orm from 'orm-js/decorators';


@orm.entity() // mark the following class as an entity
export default class User {
  @orm.field()
  @orm.id()
  id: number;
  
  @orm.field()
  userName: string;
  
  @orm.field()
  emailAddress: string;
  
  @orm.field()
  passwordHash: string;
}

The entity name (User) as well as the property names and types are automatically determined by orm-js, you don't need to take care of the naming (unless you have two entities with the same name).

Properties missing the @orm.field() decorator will not be saved to the database, the field won't be created in the schema. You can optionally use @orm.id() for a single field to mark it as the primary key, which is used to uniquely identify a single data record.

You can declare your entities in several JavaScript or TypeScript files or put several of them in a single file. It's your responsibility to load all these files in order to use the orm-js system. For example, you can put all your entity files in a single directory, which you can scan for files to require(). In this example, a simple require('./user') is enough to tell orm-js about this entity (because of the @orm.entity() decorator).

After you've loaded all your entities, you can connect to your database and use orm-js.

Database Connection

orm-js itself does not provide connection to a database. Currently there's only orm-js-sqlite available. Before you can use orm-js (you may load your entities before, however), you have to bind a database connection:

import * as orm from 'orm-js/orm';
import SqliteDatabase from 'orm-js-sqlite';

let connection = new SqliteDatabase('database.db');
orm.setDatabase(connection);

(async () => {
  try {
    await orm.connect();
    console.log('Database is connected!');
  } catch(err) {
    console.error(err);
  }
})();

Schema Creation

The database schema (the tables, fields, relations etc.) is managed by orm-js, you usually can't use any existing schema. After you've loaded your entities and connected to your database, you use build() to create a new database schema:

import * as orm from 'orm-js/orm';

(async () => {
  try {
    await orm.build();
    console.log('Schema created!');
  } catch(err) {
    console.error(err);
  }
})();

Repositories

In order to get or save entity objects, you can use the Repository class provided by orm-js/orm. For each entity, you have to instanciate a new Repository with the entity class passed as its parameter. The following example creates a new user and retrieves it back from the database:

import * as orm from 'orm-js/orm';
import User from './user';

(async () => {
  let repo = new orm.Repository(User);
  
  let user = new User();
  user.userName = 'Hero Brain';
  user.emailAddress = '[email protected]';
  
  // you can use insert() for both new and updating entities
  await repo.insert(user);
  
  // a numeric ID field is automatically set by the insert operation
  console.log(`User got the id: ${user.id}`);
  
  // get an array of User entities
  let userList = await repo.findAll();
  console.log(userList);
})();

Install

Via npm:

$ npm install orm-js

You'll need to install a database connector for orm-js, in order to connect to an actual database. Currently there's only orm-js-sqlite available:

$ npm install orm-js-sqlite

When using TypeScript, make sure to enable both experimentalDecorators and emitDecoratorMetadata in your tsconfig.json:

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

License

orm-js is licensed under the MIT License.