Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

think-model

Package Overview
Dependencies
Maintainers
1
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

think-model

An adapter-based ORM for ThinkJS 3.x

  • 0.0.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
374
decreased by-46.26%
Maintainers
1
Weekly downloads
 
Created
Source

think-model

npm Travis Coveralls David

ThinkJS3.x model, support mysql.

Installation

npm install think-model

How To Use

Basic

const Model = require('think-model/mysql');
const ModelConfig = {
  database: 'test',
  prefix: 'fk_',
  encoding: 'utf8',
  nums_per_page: 10,
  host: '127.0.0.1',
  port: '',
  user: 'root',
  password: 'root'
};

let userModel = new Model('user', ModelConfig);
userModel
  .where({name: 'lizheming'})
  .find()
  .then(user => {
    console.log(user);
  });

Cache

think-model implement cache feature by using think-cache. You can confiure like:

const fileCache = require('think-cache-file');

const Model = require('think-model/mysql');
const ModelConfig = {
  database: 'test',
  prefix: 'fk_',
  encoding: 'utf8',
  nums_per_page: 10,
  host: '127.0.0.1',
  port: '',
  user: 'root',
  password: 'root',
  cache: {
    type: 'file',
    common: {
      timeout: 24 * 60 * 60 * 1000, // millisecond
    },
    file: {
      handle: fileCache,
      cachePath: './runtime/cache',  // absoulte path is necessarily required
      pathDepth: 1,
      gcInterval: 24 * 60 * 60 * 1000 // gc
    }
  }
};

let userModel = new Model('user', ModelConfig);
userModel
  .where({name: 'lizheming'})
  .cache('user_lizheming')
  .find()
  .then(user => {
    console.log(user);
  });

Advanced

If you want custom model, you can do like this:

const Model = require('think-model/mysql');
const ModelConfig = {
  database: 'test',
  prefix: 'fk_',
  encoding: 'utf8',
  nums_per_page: 10,
  host: '127.0.0.1',
  port: '',
  user: 'root',
  password: 'root'
};

class UserModel extends Model {
  getUser(name) {
    return this.where({name}).find();
  }
}

let userModel = new UserModel('user', ModelConfig);
userModel.getUser('lizheming').then(user => {
  console.log(user);
});

Adapters

Relation

Relation model is an special model type that relate to other tables easily.

There has Four relation type:

  • HAS_ONE: one to one model

  • BELONG_TO: one to one belong to

  • HAS_MANY: one to many

  • MANY_TO_MANY: many to many

    const RelationModel = require('think-model/mysql/relation');
    const ModelConfig = {
      database: 'test',
      prefix: 'fk_',
      encoding: 'utf8',
      nums_per_page: 10,
      host: '127.0.0.1',
      port: '',
      user: 'root',
      password: 'root'
    };
    
    class PostModel extends RelationModel {
      relation = {
        user: {
          type: RelationModel.BELONG_TO, //relation type
          model: "", //model name
          name: "user", //data name
          key: "id", 
          fKey: "user_id", //forign key
          field: "id,name",
          where: "name=xx",
          order: "",
          limit: "",
          rModel: "",
          rfKey: ""
        },
        comment: RelationModel.HAS_MANY,
      }
    }
    
    let postModel = new PostModel('post', ModelConfig);
    postModel.select().then(posts => {
      console.log(posts);
    });
    
Relation type configuration
keycomment
typetype of relation
modelmodel name of relation table, default is relation variable object key
keyrelated key of current model
fkeyrelated key of related table
fieldfield used to query related table, fKey must be included if you set this field
wherewhere condition used to query related table
orderorder used to query related table
limitlimit used to query related table
pagepage used to query related table
rModelrelated model name in many to many type
rKeykey in related table in many to many type

Contributing

Contributions welcome!

License

MIT

Keywords

FAQs

Package last updated on 07 Jun 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc