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

@appstrax/database-admin

v0.0.2

Published

The server side Appstrax database nodejs library

Downloads

8

Readme

@appstrax/database-admin

This library integrates with the Appstrax Database API. The is meant to be used in a nodejs application.

Getting Started

Create your Appstrax API here: https://codecapsules.io/.

Installation

npm install @appstrax/database-admin --save

Setup

Initialize the database library:

// import the database library
import { database } from '@appstrax/database-admin';

// initialize the database service
const apiUrl = 'YOUR API URL HERE'; // eg. appstrax-database-api-snidtu.codecapsules.co.za
database.init(apiUrl);

Model

interface DocumentDto {
  id: string;
  data: any;
  createdAt: Date;
  updatedAt: Date;
}

Fetch from database

import { database } from '@appstrax/database-admin';

try {
  // fetch all records
  let documents = await database.find('collection');

  // fetch all records where name is equal to 'appstrax'
  documents = await database.find('collection', { name: 'appstrax' });

  // documents are a DocumentDto[]
} catch (err) {
  console.log(err);
}
import { database } from '@appstrax/database-admin';

try {
  const document = await database.findById('collection', id);
  // document is a DocumentDto
} catch (err) {
  console.log(err);
}

Write to database

import { database } from '@appstrax/database-admin';

try {
  const document = await database.create('collection', {
    // any custom fields
    field1: 'value 1',
    field2: 'value 2',
    ...
  });

  // document is a DocumentDto
} catch (err) {
  console.log(err);
}
import { database } from '@appstrax/database-admin';

try {
  const document = await database.edit('collection', id, {
    // whole object to be updated
    field1: 'value 1',
    field2: 'value 2',
    ...
  });

  // document is a DocumentDto
} catch (err) {
  console.log(err);
}

Remove from database

import { database } from '@appstrax/database-admin';

try {
  await database.delete('collection', id);
} catch (err) {
  console.log(err);
}

CRUD Service

This is a helper class which wraps the database calls above. Below shows an example of how to use this service.

import { CrudService, Model, Ignore } from '@appstrax/database-admin';

// your model must extend Model
export class User extends Model {
  // all fields must have a default value
  name: string = '';
  surname: string = '';
  email: string = '';
  role: string = 'user';

  // any fields you do now want to save to the database, prefix with @Ignore
  @Ignore age: number = 0;

  getFullName() {
    return this.name + ' ' + this.surname;
  }
}

export class UserService extends CrudService<User> {
  constructor() {
    // 'users' is the collection
    super('users', User);
  }

  // extend the service with any custom functions here
}

to use the service, here are some examples

try {
  const userService = new UserService();
  
  let user = new User();
  user.name = 'Test';
  user.surname = 'User';
  user.email = '[email protected]';


  // create a user
  user = await userService.save(user);
  console.log(JSON.stringify(user));
  /*
    example output
    {
      id: '610a4ff4f2cac700146571b6',
      name: 'Test',
      surname: 'User',
      email: '[email protected]',
      role: 'user',
      age: 0,
      createdAt: '2021-08-23T10:05:02.233Z',
      updatedAt: '2021-08-23T10:05:02.233Z'
    }
  */


  user.name = 'Joe';
  user.surname = 'Soap';
  // update a user
  user = await userService.save(user);
  console.log(JSON.stringify(user));
  /*
    example output
    {
      id: '610a4ff4f2cac700146571b6',
      name: 'Joe',
      surname: 'Soap',
      email: '[email protected]',
      role: 'user',
      age: 0,
      createdAt: '2021-08-23T10:05:02.233Z',
      updatedAt: '2021-08-23T10:06:49.047Z'
    }
  */


  // fetch all users
  const users = await userService.find();
  // the fetched users will be an array of instantiated User objects, this allows:
  console.log(users[0].getFullName());
  /*
    example output
    'Joe Soap'
  */


  // fetch a user by id
  user = await userService.findById(users[0].id);
  // the fetched user will be an instantiated User object


  // remove a user
  await userService.delete(user.id);
} catch(err) {
  // something went wrong
}