Sql-repository is a simple Knex based implementation of the repository pattern for Node.js. It abstracts access to a given database table with simple methods like load, list or create.
Simply install the library using :
npm install sql-repository --save
or
yarn add sql-repository
Sql-repository is supposed to work alongside a Knex installation. So please run :
npm install knex --save
or
yarn add knex
The repository constructor expects 3 parameters :
- an initialized Knex instance
- a table name
- a constructor corresponding to the model
You can directly instanciated a repository like this :
import Repository from 'sql-repository';
import knex from 'knex';
const db = knex({
client: 'postgresql',
connection: {
database: 'myDb',
host: 'myHost',
user: 'myUser',
password: 'myPass',
},
migrations: {
tablename: 'knex_migrations',
}
})
class Foo {
constructor(data) {
this.id = data.id;
this.name = data.name;
this.bar = data.bar;
}
}
const fooRepository = new Repository(db, 'foo', Foo);
// will generate an array of Foo objects from the content of the foo table
const foos = fooRepository.list();
To avoid repeating yourself it can be useful to create a class inheriting from sql-repository:
class FooRepository extends Repository {
constructor(db) {
super(db, 'foo', Foo);
}
}
const fooRepository = new FooRepository(db);
const foos = fooRepository.list();
The constructor will take two additionnal optionnal parameters:
- dataToPersist: a function allowing you to select which property to persists and how to map it to the database columns.
- formatDataForConstructor: a function allowing you to map data from the database columns to the object expected by your model's constructor.
Example:
const dataToPersist = foo => ({
name: foo.name,
bar_id: foo.bar,
});
const formatDataForConstructor = data => ({
id: data.id,
name: data.name,
bar: data.bar_id,
});
const fooRepository = new Repository(db, 'foo', Foo, dataToPersist, formatDataForConstructor);
Abstracts CRUD operations with the database, to allow easier implementation of the pattern
Initialize the Repository
Parameters
db
object The knex instancetable
string The name of the SQL tableconstructor
function The constructor for the associated modeldataToPersist
function Tells which data should be persisted in database for a given object (optional, defaultdata=>data
)formatDataForConstructor
function Change data format from database to constructor (ie. start_date -> startDate) (optional, defaultdata=>data
)
Loads an entity from the given id
Parameters
id
int The object's id
Returns object The corresponding entity
Lists the entities
Parameters
whereClause
object Conditions (optional, default{}
)
Returns array A list of entities
Persists a new entity in database
Parameters
object
object The entity to persist
Returns object An entity, with a database generated id
Updates an enitity in database
Parameters
object
object The entity to persist in database
Returns boolean True if the operation was a success
Deletes an entity from database
Parameters
id
number The entity's id
Returns boolean True if the operation was a success