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

@similie/model-connect-entities

v1.1.4

Published

Creates a common interface for model conmections, offering common CRUD operations accross deployments

Downloads

764

Readme

Model Connect Entities

License: MIT npm Version

Table of Contents

Introduction

Model Connectors is an open-source library that provides a unified framework for implementing common CRUD (Create, Read, Update, Delete) functionality across various implementation environments. By abstracting the data layer, Model Connectors allows you to manage your models consistently, regardless of the underlying technology stack.

Whether you're working with HTTP APIs, TypeORM, Drizzle, PostgreSQL, or any other data source, Model Connectors ensures that your model interactions remain uniform and predictable.

Features

  • Unified CRUD Operations: Perform create, read, update, and delete operations consistently across different connectors.
  • Extensible Framework: Easily add support for new connectors tailored to your specific needs.
  • Type Safety: Leverage TypeScript for type-safe model interactions.
  • Flexible Querying: Use the same querying syntax regardless of the underlying data source.
  • Open Source: MIT licensed, allowing for free use and modification.

Installation

Install the model-connectors package via npm:

npm install @similie/model-connect-entites

Getting Started

Creating a Connection

In your entry-point script for establishing model connections, i.e. (app/main/index).ts we build the following:

/**
* Create a static instance of the GlobalConnection class
*/
import { GlobalConnection } from "@similie/model-connect-entites";
import { HTTPConnector } from "@similie/http-connector";
GlobalConnection.startInstance(new HTTPConnector("https://api.example.com"));

Using the Connectors

We defind our models in the following way:


/**
* Define the model interface
*/
import { IEntity, IModelType, IModelCollection } from '@similie/model-connect-entites';
import { IUser, IStation } from "./types"
export interface IAsset extends IEntity {
  name: string;
  user: IModelType<IUser>;
  active?: boolean;
  stations?: IModelCollection<IStation>;
  returned?: boolean;
  meta?: any;
}

/**
* Define the model class
*/
import { IAssetscheduler } from '../model-interfaces';

import {LiveConnectionConstruct, Model } from '@similie/model-connect-entites';
export class AssetModel extends Model<IAsset> {
  constructor() {
    super(LiveConnectionConstruct);
    this.modelname = "assset";
  }
 // you can override or define additional methods here
}

const assset = new AssetModel();
const allAssets = asset.find({where: {active: true}}).fetch(); // returns all active assets

Available Connectors

API Reference

const user = new User();
const passport = new Passport();
// returns all users
const results = await user.find().fetch();
// returns one user
const results = await user.find().limit(1).fetch();
// returns all users with a skip 2 limit of 1
const results = await user.find().skip(2).limit(1).fetch();
// returns the users with first name Code,
const results = await user
  .find({
    firstName: 'Code',
  })
  .fetch();
// returns users whose name contains code
const results = await user
  .find({
    firstName: { contains: 'Code' },
  })
  .fetch();
// returns users whi name is either Code or Ninja
const results = await user
  .find({
    or: [
      { firstName: { contains: 'Code' } },
      { firstName: { contains: 'Ninja' } },
    ],
  })
  .fetch();
// we can do a greather-than clause
const results = await user
  .find({
    or: [
      { firstName: { contains: 'Fast' } },
      { firstName: { contains: 'Fun' } },
      { id: { '>': 1 } },
    ],
  })
  .fetch();
// just fine one with a particular id
const results = await user.initWithId(1).fetch();
// just find one unique, returns the object not an array
const results = await user.find({ firstName: 'Code' }).fetchOne();
// now we can find one and populate a relation
const results = await user
  .find({ firstName: 'Code' })
  .populate('requestor')
  .fetchOne();
// or we can populate all attributes
const results = await user.find().populateAll().fetch();
// let's create a user
const createdUser = await user.create({
  firstName: 'Adam',
  lastName: 'Smith',
  userName: 'boomo',
  email: '[email protected]',
  active: true,
  requestor: 1,
  role: 3,
});
// lets create a bunch of users
const createdUsers = await user.createMany([
  {
    firstName: 'Adam',
    lastName: 'Smith',
    userName: 'boomo',
    active: true,
    email: '[email protected]',
    requestor: 1,
  },
  {
    firstName: 'Code',
    lastName: 'Ninja',
    userName: 'gazoomo',
    email: '[email protected]',
    active: true,
    requestor: 2,
  },
  {
    firstName: 'Steve',
    lastName: 'WonderSnack',
    userName: 'boomoToTheGazoomo',
    email: '[email protected]',
    active: true,
    requestor: 3,
  },
]);
// lets update a user
const newUsername = 'boomo1000';
createdUsers[0].userName = newUsername;
const updatedUser = await user.save(extras.createdUsers[0]);
// or we can search with an update
const newUsername = 'boomo1000';
const updatedUsers = await user.update(
  { userName: createdUsers[0].userName },
  { userName: newUsername}
);
// what's our user count
const userCount = await user.count();
// what's our count with query
const userCount = await user.count({ id: { '>': 2 } });
// delete a user instance
const deleteUser = createdUsers[0];
const deletedUser = await user.destroy(deleteUser);
// delete a bunch of them
const deletedUsers = await user.destroyAll({ id: { '>': 3 } });
// what are the attributes of the user
const attrs = await user.attr();
// sum the id attributes
const sum = await user.sum('id');
// sum the id attributes with a query
const newSum = await user.sum('id', { id: { '<': 3 } });
// average the id attributes
const avg = await user.avg('id');
// or average the id attributes with a query
const newAvg = await user.avg('id', { id: { '<': 3 } });
// lets populate a relation
const user = await user.initWithId(1).fetch();
const createdPassport = await passport.create({
    password: 'Boomo!',
});
await user.passport.addToCollection(createdPassport);
// we can also remove it
user.passport.removeFromCollection(
  extras.createdPassport
);
// we can stream our values
await user
  .stream({ id: { '>': 1 } })
  .eachRecord((user: IUser) => {
    // do something with the user
  })
  .fetch();
// we can also batch an records
await user
  .stream({})
  .eachBatch(10, (user: IUser[]) => {
    // do something with our 10 user
  })
  .fetch();
// find or create a user
const newFind = {
  firstName: 'Adam',
  lastName: 'Smith',
  userName: 'boomo',
  email: '[email protected]',
  active: true,
  requestor: 2,
};

const foundUser = await user.findOrCreate(
  {
    userName: 'boomo',
  },
  newFind
);

Contributing

We welcome contributions from the community and encourage you to help improve this project! Whether you’re fixing bugs, adding features, or proposing enhancements, your input is highly valued.

Pull Requests

We gladly accept pull requests. If you have a fix, enhancement, or idea, follow these steps to contribute: 1. Fork the Repository: Clone your fork locally to begin development. 2. Create a Branch: Use a descriptive name for your branch, such as feature/new-connector-type or bugfix/issue-123. 3. Write and Test Your Code: Ensure that your changes meet our standards and include tests where appropriate. 4. Submit a Pull Request: When your work is ready, open a pull request to the main branch, describing your changes clearly.

Adding New Connector Types

We especially encourage the development of new connector types to expand use cases. If you’re implementing a connector for a specific system, database, or platform, here’s how you can contribute: 1. Review Existing Connectors: Familiarize yourself with the structure and design of current connectors. 2. Follow the Guidelines: Maintain consistent naming conventions, design patterns, and documentation practices. 3. Provide Documentation: Include a clear README or guide detailing how to configure and use your connector. 4. Submit Tests: Provide test cases to verify functionality and ensure compatibility with the system.

Need Help?

If you’re unsure where to start or have questions about your contribution, don’t hesitate to open a discussion or issue. We’re here to collaborate and make this project better together.

Thank you for your interest in contributing! We’re excited to see what you’ll build.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Similie

Similie is a technology company based out of Timor-Leste, dedicated to developing innovative solutions that support international development initiatives and climate-change adaption. Our mission is to harness the power of technology to drive positive change and improve lives around the world. With a focus on sustainability, community engagement, and social impact, we strive to create products and services that make a real difference in people's lives.