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

@egomobile/orm-mssql

v0.0.1

Published

A MSSQL data adapter and other utilities for @egomobile/orm module.

Downloads

5

Readme

npm last build PRs Welcome

@egomobile/orm-mssql

A Microsoft SQL Server data adapter and other utilities for @egomobile/orm module.

Install

Execute the following command from your project folder, where your package.json file is stored:

npm install --save @egomobile/orm-mssql

The following modules are defined in peerDependencies and have to be installed manually:

Usage

import { createDataContext } from "@egomobile/orm";
import { MSSQLDataAdapter } from "@egomobile/orm-mssql";
import type { IResult } from "mssql";

class User {
  // non-nullable fields
  public id: number = undefined!;
  public first_name: string = undefined!;
  public last_name: string = undefined!;

  // nullable fields
  public email: string | null = undefined!;
}

async function main() {
  const context = await createDataContext({
    adapter: new MSSQLDataAdapter(),
    entities: {
      // name of the entity / table
      users: {
        ids: ["id"], // list of column(s) which represent the ID
        type: User, // the class / type to use to create objects from
      },
    },
    noDbNull: true,
  });

  const listOfUsers: User[] = await context.find(User, {
    // WHERE clause
    where: "[is_active]=@_1 AND [is_deleted]=@_2",
    params: [true, false], // @_1, @_2

    offset: 1, // skip the first
    limit: 100, // only return 100 rows
  });

  // return a user with ID 5979
  const specificUser: User | null = await context.findOne(User, {
    where: "[id]=@_1",
    params: [5979], // @_1
  });

  if (specificUser !== null) {
    // update with new data
    specificUser.last_name = "Doe";
    specificUser.first_name = "Jane";
    specificUser.email = null;
    await context.update(specificUser);

    // remove from database
    await context.remove(specificUser);
  } else {
    console.log("User not found");
  }
}

// create new POCO
const newUser = new User();
newUser.first_name = "John";
newUser.last_name = "Doe";
// ... and add it to database
await context.insert(newUser);

// do raw queries
const result: IResult<any> = await context.query(
  "SELECT * FROM [users] WHERE [id]=@_1 AND [is_active]=@_2;",
  23979,
  true
);
console.log(result);

main().catch(console.error);

Migrations

Before you can use migrations, first keep sure to have an existing migrations table in your database:

CREATE TABLE [dbo].[migrations] (
    [id]        BIGINT         IDENTITY (1, 1) NOT NULL,
    [timestamp] BIGINT         NOT NULL,
    [name]      NVARCHAR (MAX) NOT NULL,
    CONSTRAINT [PK_migrations] PRIMARY KEY CLUSTERED ([id] ASC)
);

A quick example how to use MSSQLMigrationContext class:

import { MSSQLDataAdapter, MSSQLMigrationContext } from "@egomobile/orm-mssql";

async function main() {
  const context = new MSSQLMigrationContext({
    // a default adapter
    adapter: new MSSQLDataAdapter(),

    // scan for .js files
    // inside ./migration subfolder
    // with the following format:
    //
    // <UNIX-TIMESTAMP>-<NAME-OF-THE-MIGRATION>.js
    //
    // example: 1746942104690-CreateUserTable.js
    migrations: __dirname + "/migration",

    table: "migrations",
  });

  // UP-GRADE database
  await context.up();

  // DOWN-GRADE database
  await context.down();
}

main().catch(console.error);

A migration file looks like this:

/**
 * Function to UP-GRADE the database.
 */
module.exports.up = async (context) => {
  // context => https://egomobile.github.io/node-orm/interfaces/IDataContext.html

  await context.query(`
CREATE TABLE [dbo].[tdta_user]
(
    [id] BIGINT IDENTITY (1, 1) NOT NULL,
    [email] NVARCHAR (MAX) NOT NULL
);
`);
};

/**
 * Function to DOWN-GRADE the database.
 */
module.exports.down = async (context) => {
  // context => https://egomobile.github.io/node-orm/interfaces/IDataContext.html

  await context.query(`DROP TABLE [dbo].[tdta_user];`);
};

You are also able to create a migration file programmatically:

import { createNewMigrationFile } from "@egomobile/orm-mssql";

const newFilePath = await createNewMigrationFile("the name of the migration", {
  // create output file inside ./migrations sub folder
  dir: __dirname + "/migrations",

  // generate and add optional header and footer to the file
  header: ({ name, timestamp }) =>
    `// Hello, this is migration '${name}' created on ${timestamp}\n\n`,
  footer: "\n\n// Copyright (x) e.GO Mobile SE, Aachen, Germany\n\n",
});

console.log("Migration file has been created in", newFilePath);

Documentation

The API documentation can be found here.