ORM written in Typescript, inspired by Laravel Eloquent, supports Mongodb/Mongoose.
Warning: This is a documentation for v0.4.x, if you are using v0.3.x please checkout readme for v0.3.x in here.
If you are Laravel Eloquent lover and want to use it in Node JS
you will love Najs Eloquent
. Najs Eloquent
is Laravel Eloquent, written in Typescript
.
Installation
Add najs-binding
, najs-eloquent
yarn add najs-binding najs-eloquent
or
npm install najs-binding najs-eloquent
That's it.
Drivers
NajsEloquent runs with Memory driver by default that means all data is saved in memory. There are some available drivers that you can use
I'm writing detail documentation, please try the library and give me a hand if you can.
Quick Usage
I. Define a model
Simply create new class extends from a Model
class
// Register the User classModel.registerUser
II. Querying
You can query entries from database via static Query or instance query via .newQuery()
.
Retrieve entries via static query
Retrieve entries via instance query
.newQuery .where'id', '>', 10 .get
You can build grouped query via sub-query, like this
// a = 1 AND (b = 2 OR c = 3) .where .get
All querying functions support auto-completed/auto-suggestion to help you prevent any typo mistake. If you are familiar with Laravel Query Builder you can start write query without learning anything.
III. ActiveRecord - Create, Update & Delete
Create new model
user.email = 'email@test.com'await user.save
Update a model
user.email = 'email@test.com'await user.save
Delete a model
await user.delete // or you can use query builderawait User.where'id', 'deleted-id'.delete
IV. Accessors and Mutators
Accessors
You can define an accessor by getter
or a function like Laravel Eloquent
// file: Accessor.ts
Mutators
You can define a mutator by setter
or a function like Laravel Eloquent
// file: Mutator.ts
V. Settings Properties
fillable
& guarded
Just like Laravel, you can define mass assignable fields by fillable
or guarded
property
Only defined properties in fillable can be filled with .fill()
user.fill console.loguser.toObject // => { email: a }
You can skip fillable
setting by using .forceFill()
visible
& hidden
You can define serialized fields by visible
or hidden
property
When using .toObject()
or .attributesToObject()
the password
field will be skipped
user.forceFill console.loguser.toObject // => { email: a }
timestamps
You can simply define timestamps for models by changing static (or protected) variable named timestamps
. By using this feature every time the model get saved, created_at
and updated_at
will updated automatically
By default, Najs Eloquent will create 2 fields named created_at
and updated_at
, you can custom the fields' name:
softDeletes
You can simply define soft deletes feature for models by changing static (or protected) variable named softDeletes
.
By using this feature every time the model get deleted the data ind database not actually deleted, it update deleted_at
from null
to date object. You can custom the deleted_at
field name:
With soft deletes model all retrieve operators like find()
or get()
automatically return non-deleted entries only, you can use .withTrashed()
or .onlyTrashed()
to receive deleted entries
VI. Events
You can listen to events creating
, created
, saving
, saved
, updating
, updated
, deleting
, deleted
, restoring
, restored
by using .on()
or just trigger for 1 event only by .once()
. All event listener is async
user.on'created', user.email = '...'user.save
You can listen to global event like this
User.on'created',
VII. Relationships
najs-eloquent
supports HasOne
, HasMany
, ManyToMany
, MorphOne
and MorphMany
relationships. This feature doesn't look like Laravel. We couldn't give the same name the data and relation in Typescript, then you have to separate it
So we can assign new Post to user like this
user.postsRelation.associatepost // when you save the user model, post will be saved and associate to User automaticallyawait user.save // eager load posts when loading usersconsole.logresult.posts // a collection of posts belongs to current user // lazy load posts from userawait user.postsRelation.lazyLoad // or just load relation, najs-eloquent uses eager loading if possibleawait user.load'posts'
I'm writing detail documentation, please try the library and give me a hand if you can.
VIII. Factory
najs-eloquent
has Factory feature which use chance
as a faker:
// define a factory for User modelFactory.defineUser, // create model and save to database by factory()await factoryUser.create // make model instance only by factory() // make 3 model instances by factory() .times3 .make
IX. Builtin classes
All classes you need to implement new driver are available in NajsEloquent
object.
I'm writing detail documentation, please try the library and give me a hand if you can.
Contribute
PRs are welcomed to this project, and help is needed in order to keep up with the changes of Laravel Eloquent. If you want to improve the library, add functionality or improve the docs please feel free to submit a PR.
Sponsors
If you want to become a sponsor please let me know.
You can buy me a beer via Paypal or Patreon.
Thanks in advance!
License
MIT © Nhat Phan