lamix - ORM for Node-express js
npm install lamix---
get all Usage in examples provided for any issue or support get in tourch @ andrewkhabweri@gmail.com
- Generate migration files dynamically with fields
- Run migrations
- Rollback last migration
- Generate seed files dynamically
- Run all unapplied seeders
- Refresh all seeds (rollback + rerun)
- Run specific seed files
- Built-in schema builder
---
For rules Validation below are the Avalable data validation rules used in Model:
---
required
string
boolean
numeric
email
min
max
confirmed
date
url
regex
in
unique
exists
phone
alpha
alpha_num
array
json
between
not_in
integer
ip
uuid
slug
after
before
size
mimes
image
file
Migrations table Supports chainable modifiers filled can be added Manually :
.notNullable()
.nullable()
.defaultTo(value)
.unsigned() (MySQL only)
.unique()
.primary()
.autoIncrement()
.comment('text') (MySQL only)
.after('columnName') (MySQL only)
- Node.js (v18+ recommended)
- MySQL database
- .env file configured with your database credentials (see example below)
---
1. download this project.
npm i lamix
2. Install dependencies (if any).
> This tool uses mysql2,pg,sqlite3 driver, so make sure you install your prefered driver:
DATABASE CONNECTION IN .envFILE
Configure DB via environment variables: 'DB_CONNECTION=mysql',DB_HOST=your db hast, DB_USER=your db user, DB_PASS=your db password, DB_NAME=your db name, DB_PORT=your db port, DB_CONNECTION_LIMIT=10.
DB_CONNECTION=mysql # if using mysql2DB_CONNECTION=pg # if using PostgreSQLDB_CONNECTION=sqlite # for sqliteDB_DATABASE=./database.sqlite # if using sqlite
1. Install dependencies:
```
npm install
`bash
npm i lamix
npm install mysql2 # or pg or sqlite3
#Basic CRUD & Query
const User = require('./models/User');
const user = await User.findBy('email', 'jane@example.com');
#QueryBuilder Advanced Features
const qb = User.query();
// Where Null / Not Null
const withNull = await User.query().whereNull('deleted_at').get();
const notNull = await User.query().whereNotNull('deleted_at').get();
#You can use relations:
const user = await User.find(1);
const posts = await user.posts().get(); // all posts for the user
class User extends BaseModel {
static table = 'users';
static primaryKey = 'id';
static timestamps = true;
static softDeletes = false; # Optional if you don't need it
static fillable = ['name', 'email', 'password']; # password is Auto encrypted when creating.
static rules = { # Optional if you don't need it
name: 'required|string',
email: 'required|email|unique:users,email',
password: 'required|string|min:6',
phone: 'nullable|phone',
status: 'boolean'
};
profile() {
const Profile = require('./Profile');
return this.hasOne(Profile).onDelete('restrict');
}
# Many-to-many: User ↔ Role via pivot user_roles (user_id, role_id)
roles() {
const Role = require('./Role');
return this.belongsToMany(
Role,
'user_roles',
'user_id',
'role_id'
).onDelete('detach');
}
# One-to-many: User -> Post
posts() {
const Post = require('./Post');
return this.hasMany(Post', 'user_id', 'id').onDelete('cascade');
}
# migrate sessions Table(whenever migration is run session is auto generated if missing)
npx lamix migrate
➡️ sessions table + index are guaranteed to exist.
# Session setup
const express = require('express');
const session = require('express-session');
const { DB, LamixSessionStore } = require('lamix');
DB.initFromEnv();
(async () => {
await DB.connect();
})();
const app = express();
app.use(
session({
name: 'lamix.sid',
secret: process.env.SESSION_SECRET || 'dev-secret',
resave: false,
saveUninitialized: false,
store: new LamixSessionStore({
ttl: 60 60 24, // 1 day
}),
cookie: {
httpOnly: true,
secure: false, // true behind HTTPS
maxAge: 1000 60 60 * 24,
},
})
);
app.get('/', (req, res) => {
req.session.views = (req.session.views || 0) + 1;
res.json({ views: req.session.views });
});
app.listen(3000);
}