Skip to content
This repository has been archived by the owner on Oct 24, 2021. It is now read-only.

dgsan/jdb-gfk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jdb-gfk

Generic foreign keys for JDB.

Usage

jdb-gfk provides generic foreign keys for JugglingDB by wrapping it.

To use, require jdb-gfk instead of jugglingdb.

var Schema = require('jdb-gfk').Schema;

Once required, generic relations need to be initialized on Schema instances by calling registryInit().

var jdb = (new Schema('memory')).registryInit();

This will give your Schema instance some additional methods and provide the SchemaEntity model.

After you've declared your models, call registry.registerModels() on your Schema instance.

jdb.registry.registerModels();

You can provide a callback or listen for the jdb-gfk:models_registered event.

jdb.on('jdb-gfk:models_registered', function(){
    Like.genericBelongsTo();
});

// or:

jdb.registry.registerModels(function(){
    Like.genericBelongsTo();
});

Models can use genericBelongsTo() much like belongsTo(), only the related entity can be any type of model using the same Schema instance. It optionaly takes an object as an argument like so: { schemaEntityIdColumn: <column_name>, relatedIdColumn: <column_name>, methodName: <method_name> }, where each property is also optional: Like.belongsTo({methodName: 'target'}), for example.

Once you have a Like object like, you can like.related(anotherObject) to set the relationship, or like.related(function(err, obj){ // do stuff }) to retrieve the related entity.

About

Generic foreign keys for JDB

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published