think-model
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,
},
file: {
handle: fileCache,
cachePath: './runtime/cache',
pathDepth: 1,
gcInterval: 24 * 60 * 60 * 1000
}
}
};
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,
model: "",
name: "user",
key: "id",
fKey: "user_id",
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
key | comment |
---|
type | type of relation |
model | model name of relation table, default is relation variable object key |
key | related key of current model |
fkey | related key of related table |
field | field used to query related table, fKey must be included if you set this field |
where | where condition used to query related table |
order | order used to query related table |
limit | limit used to query related table |
page | page used to query related table |
rModel | related model name in many to many type |
rKey | key in related table in many to many type |
Contributing
Contributions welcome!
License
MIT