Skip to content

Getting Started

James Birtles edited this page Feb 16, 2018 · 2 revisions

Install

You install pims like any other node module using your favourite package manager.

npm i pims

Create the adapter

PIMS is designed to support many document-oriented databases. The following databases are currently supported:

PIMS interacts with your database through the use of an adapter. For example, using the Arango adapter:

import { ArangoAdapter } from 'pims-arangodb';

const adapter = new ArangoAdapter({
    host: 'localhost',
    port: 8529,
    database: 'mydb',
    username: 'root',
    password: 'arango123',
    models: [],
});

Other adapters construct similarly, with their database specific connection options.

Create a model

A model in PIMS represents a table or collection in your database. For example, this record in your users collection

{
  "id": 12345,
  "firstName": "James",
  "username: "UnwrittenFun"
}

would be represented in your code by this model

import { Model, Column } from 'pims';

@Model({
    table: 'users',
})
export class User {
    @Column({ primary: true })
    public id: string;

    @Column() public firstName: string;
    @Column() public username: string;
}

Register your model

Now that you have a model, you need to tell your adapter about it. You do this by passing the class into the models array in the adapter's constructor

const adapter = new ArangoAdapter({
    // ...
    models: [ User ],
});

[Optional] Setup collections and indexes

If desired, PIMS can create the collections and indexes you have set up for you if they don't already exist. To do this on app startup you simply call

await adapter.ensure();

This returns a promise that will resolve when the collections and indexes are ready.

Using your adapter

If you got this far, then you have successfully set up your first model. You can now use the adapter to query the collection using this model.

E.g.

// Fetch all users in the collection
const users = await adapter.all(User);

// Fetch all users with the first name `James`
const allJames = await adapter.find(User, { firstName: 'James' });

// Fetch a single user by username.
const user = await adapter.findOne(User, { username: 'UnwrittenFun' });