1.0.27 • Published 8 years ago
benben-model v1.0.27
Installation
Using npm:
$ npm i --save benben-model
Introduction
This is a simple model layer, it's convenient to access database. Note: now, it's just only support mysql!
Using steps:
Create a database connection file.
Frist we create a directory it's named config, then we create db.js in it. It's look like this:
var mysql=require("mysql");
exports = {
host: '127.0.0.1', // host of database server
user: 'root', // user of database
password: '123456', // user's password of database
database: 'tests', // database name
port: 3306, // connection port
tablePrefix: 'pre_' // table prefix
};
exports.pool = mysql.createPool({
host: exports.host,
user: exports.user,
password: exports.password,
database: exports.database,
port: exports.port,
bigNumberStrings: true
});
module.exports = exports;
- Create a base class for our model classes(in my project I put it in 'models' folder), it's help us to rquire database configure file. It's look like this:
var BenbenModel = require('benben-model');
var dbs = {};
module.exports = class Model extends BenbenModel{
get db(){
if(!dbs.hasOwnProperty(this.dbname))
{
dbs[this.dbname] = require('../config/' + this.dbname);
}
return dbs[this.dbname];
}
get dbname(){
return 'db';
}
};
- Create a model class in models folder. It's look like this:
var BaseModel = require('./Model');
module.exports = class User extends BaseModel{
get dbname(){
return 'db';
}
get table(){
return '{{user}}';
}
};
- Using 'User' Model
var UserModel = require('../models/User');
var user = new UserModel();
var userInfo = user.where({uid: 1}).one;
console.info(userInfo);
1.0.27
8 years ago
1.0.26
8 years ago
1.0.25
8 years ago
1.0.24
8 years ago
1.0.23
8 years ago
1.0.22
8 years ago
1.0.21
8 years ago
1.0.20
8 years ago
1.0.19
8 years ago
1.0.18
8 years ago
1.0.17
8 years ago
1.0.16
8 years ago
1.0.15
8 years ago
1.0.14
8 years ago
1.0.13
8 years ago
1.0.12
8 years ago
1.0.11
8 years ago
1.0.10
8 years ago
1.0.9
8 years ago
1.0.8
8 years ago
1.0.7
8 years ago
1.0.6
8 years ago
1.0.5
8 years ago
1.0.4
8 years ago
1.0.3
8 years ago
1.0.2
8 years ago
1.0.1
8 years ago
1.0.0
8 years ago