A Node.js ORM for MySQL, SQLite, PostgreSQL, MongoDB, GitHub and serverless service like Deta, InspireCloud, CloudBase, LeanCloud.
npm install dittorm --save
const Model = require('dittorm')('leancloud');
const userModel = new Model('user', {
appId: 'xxx',
appKey: 'xxx',
masterKey: 'xxx'
});
const user = await userModel.add({
username: 'lizheming',
email: '[email protected]'
});
const findUser = await user.select({email: '[email protected]'});
const Model = require('dittorm')('leancloud');
const userModel = new Model('user', {
appId: 'xxx',
appKey: 'xxx',
masterKey: 'xxx'
});
Name |
Required |
Default |
Description |
appId |
✅ |
|
|
appKey |
✅ |
|
|
masterKey |
✅ |
|
|
const Model = require('dittorm')('deta');
const userModel = new Model('user', {
token: 'xxx'
});
Name |
Required |
Default |
Description |
token |
✅ |
|
Deta project secret key |
const Model = require('dittorm')('inspirecloud');
const userModel = new Model('user', {
serviceId: 'xxx',
serviceSecret: 'xxx'
});
Name |
Required |
Default |
Description |
serviceId |
✅ |
|
InspireCloud Service ID |
serviceSecret |
✅ |
|
InspireCloud Service Secret |
const Model = require('dittorm')('cloudbase');
const userModel = new Model('user', {
env: 'xxx',
secretId: 'xxx',
secretKey: 'xxx'
})
Name |
Required |
Default |
Description |
env |
✅ |
|
CloudBase enviroment ID |
secretId |
✅ |
|
CloudBase API secret Id, apply it at here |
secretKey |
✅ |
|
CloudBase API secret Key, apply it at here |
const Model = require('dittorm')('github');
const userModel = new Model('user', {
token: 'xxx',
repo: 'xxx',
path: 'xxx',
})
Name |
Required |
Default |
Description |
token |
✅ |
|
Personal access tokens |
repo |
✅ |
|
repository name, such as walinejs/dittorm |
path |
|
|
The data storage directory, such as data means it is stored in the data directory, root directory by default |
const Model = require('dittorm')('mysql');
const userModel = new Model('user', {
host: '127.0.0.1',
port: 3306,
database: 'blog',
user: 'admin',
password: 'admin',
prefix: 'dt_',
charset: 'utf8mb4',
dateString: true
})
Name |
Required |
Default |
Description |
host |
|
127.0.0.1 |
MySQL server address |
port |
|
3306 |
MySQL server port |
database |
✅ |
|
MySQL database name |
user |
✅ |
|
MySQL server username |
password |
✅ |
|
MySQL server password |
prefix |
|
|
MySQL table prefix |
charset |
|
|
MySQL table charset |
Name |
Required |
Default |
Description |
path |
✅ |
|
SQLite storage file path, not include file name |
database |
|
|
SQLite storage file name, change it if your filenamed is not waline |
prefix |
|
|
SQLite table prefix |
const Model = require('dittorm')('postgresql');
const userModel = new Model('user', {
host: '127.0.0.1',
port: 5432,
database: 'blog',
user: 'admin',
password: 'admin',
prefix: 'dt_',
connectionLimit: 1,
})
Name |
Required |
Default |
Description |
host |
|
127.0.0.1 |
PostgreSQL server address |
port |
|
3211 |
PostgreSQL server port |
database |
✅ |
|
PostgreSQL database name |
user |
✅ |
|
PostgreSQL server username |
password |
✅ |
|
PostgreSQL server password |
prefix |
|
|
PostgreSQL table prefix |
const Model = require('dittorm')('mongodb');
const userModel = new Model('user', {
host: '127.0.0.1',
port: 27017,
database: 'blog',
user: 'admin',
password: 'admin',
options: {
replicaset: 'xxx',
authSource: 'admin',
ssl: true
}
});
Name |
Required |
Default |
Description |
host |
|
127.0.0.1 |
MongoDB server address, support array format |
port |
|
27017 |
MongoDB server port, support array format |
database |
✅ |
|
MongoDB database name |
user |
✅ |
|
MongoDB server username |
password |
✅ |
|
MongoDB server password |
replicaset |
|
|
MongoDB replica set |
authSourcce |
|
|
MongoDB auth source |
ssl |
|
|
use SSL connection |
Save store data.
const data = await userModel.add({ username: 'lizheming', email: '[email protected]' });
console.log(data.id);
Find store data by condition.
// SELECT * FROM user WHERE username = 'lizheming';
const data = await userModel.select({ username: 'lizheming' });
// SELECT email FROM user WHERE username = 'lizheming' ORDER BY email DESC LIMIT 1 OFFSET 1;
const data = await userModel.select({ username: 'lizheming' }, {
field: ['email'],
desc: 'email',
limit: 1,
offset: 1
});
// SELECT * FROM user WHERE username != 'lizheming';
const data = await userModel.select({ username: ['!=', 'lizheming'] });
// SELECT * FROM user WHERE create_time > '2022-01-01 00:00:00';
const data = await userModel.select({ username: ['>', '2022-01-01 00:00:00'] });
// SELECT * FROM user WHERE username IN ('lizheming', 'michael');
const data = await userModel.select({ username: ['IN', ['lizheming', 'michael']] });
// SELECT * FROM user WHERE username NOT IN ('lizheming', 'michael');
const data = await userModel.select({ username: ['NOT IN', ['lizheming', 'michael']] });
// SELECT * FROM user WHERE username LIKE '%li%';
const data = await userModel.select({ username: ['LIKE', '%li%'] });
// SELECT * FROM user WHERE username = 'lizheming' AND create_time > '2022-01-01 00:00:00';
const data = await userModel.select({
username: 'lizheming',
create_time: ['>', '2022-01-01 00:00:00']
});
// SELECT * FROM user WHERE username = 'lizheming' OR create_time > '2022-01-01 00:00:00';
const data = await userModel.select({
_complex: {
username: 'lizheming',
create_time: ['>', '2022-01-01 00:00:00'],
_logic: 'OR'
}
});
Update store data by condition. where
format same as select(where, options)
.
Return store data count by condition. where
format same as select(where, options)
.
Clean store data by condition. where
format same as select(where, options)
.