Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on May 26, 2023. It is now read-only.
/Fluorite.jsPublic archive

Lightweight ORM based on Knex.js query builder.

License

NotificationsYou must be signed in to change notification settings

pyldin601/Fluorite.js

Repository files navigation

Build StatusCoverage StatusCode ClimateDependency Status

Fluorite is a lightweight ORM based on Knex.js query builder.It features promise based interface, provides transactions support,bulk updating and deleting, and support for one-to-one, one-to-many and many-to-many relations.

Requirements

  • node 8+

Installation

npm install fluorite

Configuration

First of all you'll need a copy ofknex.js query builder to be configured.Next, you'll need to create a database representing your domain model, andthen create models.

constknex=require('knex')({client:'sqlite3',connection:{filename:'./db.sqlite3',},});awaitknex.schema.createTable('users',(table)=>{table.increments();table.string('name');table.number('age').unsigned();});awaitknex.schema.createTable('posts',(table)=>{table.increments();table.string('content');table.integer('user_id').unsigned().references('users.id');});constfluorite=require('fluorite')(knex);classUserextendsfluorite.Model{statictable='users';posts(){returnthis.hasMany(Post);}}classPostextendsfluorite.Model{statictable='posts';author(){returnthis.belongsTo(User);}}

You should use thefluorite instance returned throughout your librarybecause it creates a connection pool for the current database.

Basic actions

Creating objects

To create an model object, instantiate it with object representing attributes and then callsave().

constuser=newUser({name:'John Doe',age:28});awaituser.save();

Updating objects

To save changes to an object that is already in the database, call object's methodsave().

user.set('age',29);awaituser.save();

You also can also pass an object with attributes toset method:

user.set({age:29,name:'Bob Doe'});awaituser.save();

Or shorthand 'set and update':

awaituser.save({age:29,name:'Bob Doe'});

Deleting objects

To delete object from database use methodremove().

awaituser.remove();

Querying

Querying multiple objects

Each Model hasobjects property that by default returns newMultipleRowsQuery object thatcan be used to retrieve or bulk update group of objects.

Retrieving all objects

To retrieve all objects useasync/await orthen promise syntax on the query:

constusers=awaitUser.objects();// orUser.objects().then(users=>console.log(users));

You can also use experimentalasyncInterator syntax to iterate over database rows:

forawait(constuserofUser.objects()){console.log(user.get('name'));}

Filtering objects

To filter query result use methodfilter() passing to it object with attributes for refining.

constmen=awaitUser.objects().filter({gender:'male'});

By default used= operator for comparing. But you alter this behavior.Just add double underscore and operator name after property name (example:age__gt).

Supported operators:

  • eq evaluates to=
  • ne evaluates to!=
  • gt evaluates to>
  • gte evaluates to>=
  • lt evaluates to<
  • lte evaluates to<=
  • in evaluates toIN
  • like evaluates toLIKE
constadults=awaitUser.objects().filter({age__gte:18});constusers=awaitUser.objects().filter({id__in:[1,2,3]});constirish=awaitUser.objects().filter({name__like:'Mac%'});

Chaining filters

constadultFemales=awaitUser.objects.filter({age__gte:18}).filter({gender:'female'});

All filters areimmutable. Each time you refine your criteria you get new copy of query.

All filters arelazy. It means that query will run only when you callthen or iterateover query.

Limits and Offsets

To limit amount of objects to be returned uselimit().You could also useoffset() to specify offset for objects query.

constfirstFiveUsers=awaitUser.objects().limit(5);constnextFiveUsers=awaitUser.objects().limit(5).offset(5);

Retrieve single object

There are three different ways to retrieve single object from database.

  1. If you want to retrieve single object using primary key:
constuser=awaitUser.find(5);
  1. If you expect to retrieve only single row:
constuser=awaitUser.objects().single({name:'John Doe'});

It will fail withUser.IntegrityError if SQL statement returned more than one row.

  1. If you want to get only first row in multi-row statement:
constuser=awaitUser.objects().first({name:'John Doe'});

If object matching your criteria does not existModel.NotFoundError will be thrown.

Transactions

Use of transactions is very simple:

import{fluorite}from'fluorite';awaitfluorite.transaction(async()=>{constuser=awaitUser.find(10);awaituser.save({name:'John Doe'});});

Nested transactions

You can nest transactions as many as your database supports.

About

Lightweight ORM based on Knex.js query builder.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp