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

@tenry/graphql-decorators-mongo

v0.1.2

Published

Provide mongo access using annotated classes and the GraphQL query language.

Downloads

1

Readme

@tenry/graphql-decorators-mongo

This package provides useful TypeScript decorators for creating class-based entities, persisted to a mongo database and queryable and manipulable using the GraphQL query language.

It is based on the @tenry/graphql-decorators package.

Example

// import the libraries
import {MongoClient} from 'mongodb';
import {decorators} from '@tenry/graphql-decorators';
import {Manager, name} from '@tenry/graphql-decorators-mongo';

// define an entity
@decorators.entity()
// define a name; class name is used by default
@name('user')
class User {
  @decorators.field('ID')
  id: string;

  // primitive types like string or number is automatically detected,
  // if the emitDecoratorMetadata flag is enabled
  @decorators.field()
  name: string;

  @decorators.field('JSON')
  data: Object;

  // use this syntax, if the data type is an array of something
  @decorators.field({list: User})
  friends: User[];
}

// now set everything up
const mongo = await MongoClient.connect('mongodb://localhost');
const db = mongo.db('my_database');
const manager = new Manager(db);

// register all available entities
manager.registerEntity(User);

// get GraphQL schema
const schema = manager.createSchema();

// now do whatever you would do with a GraphQL schema
graphql(schema, someAwesomeGraphqlQuery).then(response => {
  console.log(response);
});

// or (using express and express-graphql):
const app = express();

app.use('/graphql', graphqlHTTP({
  schema,
}));

app.listen(8080);

Using this example the library would create the following GraphQL schema:

input MongoFilterInput {
  field: String
  operator: String
  value: JSON
}

input MongoOrderInput {
  field: String
  order: String
}

type Mutation {
  addUser(user: UserInput): UserType
  updateUser(id: ID, user: UserInput): UserType
  removeUser(id: ID): UserType
}

type Query {
  users(filter: [MongoFilterInput], order: [MongoOrderInput], limit: Int, offset: Int): [UserType]
}

input UserInput {
  data: JSON
  friends: [UserInput]
  id: ID
  name: String
}

type UserType {
  data: JSON
  friends: [UserType]
  id: ID
  name: String
}

Installation and Usage

Use npm to install the package:

$ npm install graphql graphql-type-json mongodb @tenry/graphql-decorators @tenry/graphql-decorators-mongo

Now import the Manager, the name decorator along with the decorators from @tenry/graphql-decorators:

import {Manager, name} from '@tenry/graphql-decorators-mongo';
import {decorators} from '@tenry/graphql-decorators';

import {MongoClient} from 'mongodb';

const mongo = await MongoClient.connect('mongodb://localhost');
const db = mongo.db('my_database');

const manager = new Manager(db);

// define entities here
// register entities to the manager via manager.registerEntity(MyEntity); here

// retrieve GraphQL schema
const schema = manager.createSchema();

License

@tenry/graphql-decorators-mongo is licensed under the MIT License.