fastorm
v0.0.8
Published
MySQL ORM for Node.js based on node-mysql2.
Downloads
16
Readme
FastORM
MySQL ORM (or wrapper) based on node-mysql2 client. Node MySQL2 is focus on performance, we focus on make easy get information on tables. Supports find, insert, delete, update and join methods with limit, order by and projection of columns.
Table of contents
- History and Why MySQL2
- Installation
- First Query
- Not only find method available
- Paginate results
- Authors
- Documentation
- Acknowledgements
- Contributing
History and Why MySQL2
FastORM project it is a necessity of have a fast and easy tool to work with MySQL in our company (and maintainable). We need migrate Sequalize (good ORM, but in our experience with great memory consumption). Our tool have great influence of MongoDB syntax.
FastORM is mostly compatible (and dependent) of node-mysql2 and supports majority of features. We also offers these features and more
- Faster / Low latency
- Multi-connections
- MySQL Binary Log Protocol
- Async/await support
- Extended support for Encoding and Collation
- Use objects as properties like some MongoDB ORM's
- Values as objects in where properties
- Compression (inherited from node-mysql2)
- Joins between two tables (for now)
- Common methods for MySQL (find, insert, delete...)
- Easy!
Installation
FastORM is free from native bindings and can be installed on Linux (tested), Mac OS or Windows without any issues.
npm install --save fastorm
First Query
// get the ORM
import { Model, createConnection } from 'fastorm';
// create the connection to database
const connection = await createConnection({
host: 'localhost',
user: 'root',
database: 'test'
});
// Create model to User table
const User = new Model('User', connection);
// Get all records on User
const users = User.find();
// Get one record
const only_one_user = User.find({ limit: 1 });
// Get all user with name Juan and alive
const all_juanes = User.find({ where: { name: 'juan', alive: true } })
// Same with limit 5 and order by age DESC
const only_five_juanes = User.find({ where: { name: 'juan', alive: true }, limit: 5, order: { age: 0 } })
// Get all user with name not equal to Juan and dead
// Accept $ne, $lt, $lte, $gt, $gte as properties in value
const all_no_juanes = User.find({ where: { name: { $ne: 'juan' }, alive: false } })
Not only find
method available
With FastORM you can also insert, delete and join records since the model. It's possible execute "raw" queries too, the module provides query
method to this.
FastORM provides insert
, delete
, join
and other methods within the model to work with the records on tables.
// get the ORM
import { Model, createConnection } from 'fastorm';
// create the connection to database
const connection = await createConnection({
host: 'localhost',
user: 'root',
database: 'test'
});
// create other connection to other database and server
const other_connection = await createConnection({
host: 'other_server',
user: 'root',
database: 'post'
});
// Create model to User table
const User = new Model('User', connection);
// Create model to Post table
const Post = new Model('Post', connection);
// Create model to Other table in other server (multiple connections, cool!)
const Other = new Model('Other', other_connection);
// Create user Maria with age 36 and not alive
// IMPORTANT: automatically created_at and updated_at columns
// are inserted, you must have these DATETIME columns on the table
const users = await User.insert({
name: 'maria',
age: 36,
alive: false
});
// Delete users with name pedro and not alive
const deleted = await User.delete({
name: 'pedro',
alive: false
})
// Get all posts of user juan with columns: name, title and description
const posts = await User.join({
table: 'post',
inner: [ 'id:user_id' ],
where: { name: 'juan' },
columns: { name: 1, title: 1, description: 1 }
})
Paginate results
We create a paginate
method to facilite managing cursors with lots of information or simply paginate results of any query. In a moment of this history we create an paginate to Sequalize available here.
// Paginate all posts of user juan with columns: name, title and description
// Return { objects, nextCursor } when objects are data found, and nextCursor
// is the property keyPaginated of the next row used to paginate again (sinceId)
const juanes_paginated = await User.paginate({
sinceId: null, // Not necessary in the first call to paginate
limit: 10, // Default 1
where: { name: 'juan' },
select: { name: 1, title: 1, description: 1 },
keyPaginated: 'document_id', // Optional used to order, default id
reverse: true // Optional, default false
})
Authors
- Jinme Mirabal @mirabalj
- Leon Peña @ldpenal
- Sergio Cruz @sergiocruza
Documentation
You can find more detailed documentation here. You should also check various code examples to understand advanced concepts.
Acknowledgements
- To @sidorares and your team by node-mysql2 module node-mysql2
Contributing
Want to improve something in fastorm
. Please check Contributing.md for detailed instruction on how to get started.