sqlite-orm v0.2.4
sqlite-orm
the ORM framework for sqlite
Install
$ npm install --save sqlite-ormUsage
The coffeescript sample code
Mapper = require 'sqlite-orm'
path = require 'path'
Migration = Mapper.Migration
ModelBase = Mapper.ModelBase
Migration.createTable 'ParentModel', (t) ->
t.addColumn 'name', 'TEXT'
Migration.createTable 'ChildModel', (t) ->
t.addColumn 'name', 'TEXT'
t.addReference 'parentModelId', 'ParentModel'
class ChildModel
ModelBase.includeInto this
constructor: (params) -> @initModel params
@initAssos: ->
@belongsTo ParentModel
class ParentModel
ModelBase.includeInto this
constructor: (params) -> @initModel params
@initAssos: ->
@hasOne ChildModel
mapper = new Mapper path.resolve(__dirname, 'test.db')
mapper.sync()The corresponding javascript code.
var Mapper = require('sqlite-orm');
var Migration = Mapper.Migration;
var ModelBase = Mapper.ModelBase;
var path = require('path');
Migration.createTable('ParentModel', function(t) {
t.addColumn('name', 'TEXT');
});
Migration.createTable('ChildModel', function(t) {
t.addColumn('name', 'TEXT');
t.addReference('parentModelId', 'ParentModel');
});
function ParentModel(params) {
this.initModel(params);
}
ModelBase.includeInto(ParentModel);
function ChildModel(params) {
this.initModel(params);
}
ModelBase.includeInto(ChildModel);
ParentModel.initAssos(function() {
ParentModel.hasOne(ChildModel);
});
ChildModel.initAssos(function() {
ChildModel.belongsTo(ParentModel);
});
mapper = new Mapper(path.resolve(__dirname, 'test.db'));
mapper.sync().then(function() {
});More sample can refer to below sites:
API
Mapper
sync
function()synchronize the model definition and the database- return:
Promise
- return:
close
function()close the database- return:
Promise
- return:
beginTransaction:
function()begin the transaction.endTransaction:
function()end the transaction.scopeTransaction:
function(callback)make the callback invoke in the transaction, after this callback complete,endTransactionwill invoke automatically.@Migration
Migrationget the Migration class@ModelBase
ModelBaseget the ModelBase class@INTEGER
Stringthe INTEGER data type@REAL
Stringthe REAL data type@TEXT
Stringthe TEXT data type@BLOB
Stringthe BLOB data typeDATETIME
Stringthe Date date type. If declareDATETIME, then the type of this attribute will beDate.BOOL
Stringthe Bool date type.
Migration
@createTable:
function(tableName, callback)create the database tabletableName:
Stringcallback:
function(tableInfo)create the columns in this callback- tableInfo:
TableInfothe class to create the columns and index
- tableInfo:
@clear:
function()clear the table definition
TableInfo
addColumn:
function(name, type, opts)add the table columnname:
Stringthe column nametype:
Stringthe column data type, such asINTEGERorTEXTopts:
Objectthe column options
createIndex:
function(indexName, columns)add index for the specific column- indexName:
Stringthe index name. - columns:
Arrayeach item of columns is the column name that need index.
- indexName:
addReference:
function(name, tableName, opts)add foreign keyname: the column name that need index
tableName: the name of table that the index will point to
opts:
Objectthe index options
ModelBase
@initAssos:
function()declare the associationall the subclass must implement this interface to declare the association
@hasOne:
function(ChildModel, opts)declare this Model has one child ModelChildModel:
ModelBaseorStringthe child Model class or class name.opts:
Objectthe options used for hasOne association- as:
String(optional) the property name to refer to the ChildModel instance, the default value is "#{childModel}". e.g. ChildModel is 'ChildModel', then the as value ischildModel
- as:
@hasMany:
function(ChildModel, opts)declare this Model has many children.ChildModel:
ModelBaseorStringthe child Model class or class name.opts:
Objectthe options used for hasOne association- as:
String(optional) the property name to refer to the ChildModel instances, the default value is "#{childModels}". e.g. ChildModel is 'ChildModel', then the as value ischildModels
- as:
@hasManyBelongsTo:
function(TargetModel, opts)declare this Model and the target Model is N-N connection. The description of this connection can refer to rails association guideTargetModel:
ModelBaseorStringthe target Model class or class name.opts:
Objectthe options used for this association.- midTableName:
Stringthe reference table name. - sourceThrough:
Stringthe column foreign key name that refer the Model. - targetThrough:
Stringthe column foreign key name that refer the TargetModel. - as:
Stringjust like@hasManyor@hasOne
- midTableName:
@belongsTo:
function(ParentModel, opts)declare this Model is member of ParentModelParentModel:
ModelBaseorStringthe parent Model class or class name.opts:
Objectthe options used for hasOne associationthrough:
String(optional) the column name that used for foreign key, the default value is "#{ParentModel}#{primaryKey}". e.g. ParentModel name is 'ParentModel', primaryKey is 'id', then the foreign key isparentModelId.as:
String(optional) the property name to refer to the ParentModel instance, the default value is "#{ParentModel}". e.g. ParentModel is 'ParentModel', then the as value isparentModel
@new:
function(obj)create a new model object, not saved into databaseobj:
Objectthe attributes list@create:
function(obj)just like@new, but save into database@drop:
function()drop the table@destroy:
function()destroy this model object and delete the db row.@find:
function(where, opts)find the object that match thewherestatement@findAll:
function(where, opts)find all of object match the condition
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using gulp.
License
Copyright (c) 2015 liuxiong. Licensed under the MIT license.
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
