文件名称:跨数据库的ORM框架JugglingDB.zip
文件大小:116KB
文件格式:ZIP
更新时间:2022-08-07 05:19:10
开源项目
JugglingDB 是一个跨数据库的 ORM 框架,提供了访问大多数数据库的通用接口,支持包括:mysql, mongodb, redis, neo4j and js-memory-storage. 你可扩展其他数据库的适配器,支持回调和钩子。 示例代码: var Schema = require('./jugglingdb').Schema; var schema = new Schema('redis', {port: 6379}); //port number depends on your configuration // define models var Post = schema.define('Post', { title: { type: String, length: 255 }, content: { type: Schema.Text }, date: { type: Date, default: Date.now }, published: { type: Boolean, default: false } }); // simplier way to describe model var User = schema.define('User', { name: String, bio: Schema.Text, approved: Boolean, joinedAt: Date, age: Number }); // setup relationships User.hasMany(Post, {as: 'posts', foreignKey: 'userId'}); // creates instance methods: // user.posts(conds) // user.posts.build(data) // like new Post({userId: user.id}); // user.posts.create(data) // build and save Post.belongsTo(User, {as: 'author', foreignKey: 'userId'}); // creates instance methods: // post.author(callback) -- getter when called with function // post.author() -- sync getter when called without params // post.author(user) -- setter when called with object schema.automigrate(); // required only for mysql NOTE: it will drop User and Post tables // work with models: var user = new User; user.save(function (err) { var post = user.posts.build({title: 'Hello world'}); post.save(console.log); }); // or just call it as function (with the same result): var user = User(); user.save(...); // Common API methods // just instantiate model new Post // save model (of course async) Post.create(cb); // all posts Post.all(cb) // all posts by user Post.all({where: {userId: user.id}, order: 'id', limit: 10, skip: 20}); // the same as prev user.posts(cb) // same as new Post({userId: user.id}); user.posts.build // save as Post.create({userId: user.id}, cb); user.posts.create(cb) // find instance by id User.find(1, cb) // count instances User.count([conditions, ]cb) // destroy instance user.destroy(cb); // destroy all instances User.destroyAll(cb); // Setup validations User.validatesPresenceOf('name', 'email') User.validatesLengthOf('password', {min: 5, message: {min: 'Password is too short'}}); User.validatesInclusionOf('gender', {in: ['male', 'female']}); User.validatesExclusionOf('domain', {in: ['www', 'billing', 'admin']}); User.validatesNumericalityOf('age', {int: true}); User.validatesUniquenessOf('email', {message: 'email is not unique'}); user.isValid(function (valid) { if (!valid) { user.errors // hash of errors {attr: [errmessage, errmessage, ...], attr: ...} } }) 标签:JugglingDB
【文件预览】:
jugglingdb-master
----.gitignore(154B)
----README.md(18KB)
----.gitmodules(0B)
----test()
--------common_test.js(44KB)
--------defaults.test.js(945B)
--------json.test.js(1KB)
--------validations.test.js(15KB)
--------include.test.js(9KB)
--------model.test.js(6KB)
--------spec_helper.js(956B)
--------scope.test.js(3KB)
--------i18n.test.js(2KB)
--------hooks.test.js(13KB)
--------basic-querying.test.js(16KB)
--------datatype.test.js(2KB)
--------manipulation.test.js(11KB)
--------schema.test.js(6KB)
--------init.js(190B)
--------performance.coffee(2KB)
--------common.batch.js(128B)
--------relations.test.js(13KB)
--------jugglingdb.test.js(284B)
----main.js(200B)
----docs()
--------schema.md(4KB)
--------model.md(6KB)
--------ga.html(442B)
--------index.txt(2KB)
--------footer.html(2KB)
--------hooks.md(4KB)
--------changelog.md(3KB)
--------adapter.md(67B)
--------jugglingdb.md(4KB)
--------validations.md(149B)
--------roadmap.md(278B)
----.eslintrc(22B)
----package.json(2KB)
----legacy-compound-schema-loader.js(1KB)
----.babelrc(26B)
----index.js(787B)
----scripts()
--------doc.sh(557B)
----.npmignore(105B)
----.travis.yml(52B)
----yarn.lock(96KB)
----Makefile(3KB)
----lib()
--------sql.js(5KB)
--------railway.js(8KB)
--------validations.js(17KB)
--------relations.js(12KB)
--------schema.js(18KB)
--------adapters()
--------jutil.js(327B)
--------utils.js(292B)
--------include.js(7KB)
--------hooks.js(2KB)
--------scope.js(5KB)
--------model.js(26KB)
----legacy-compound-init.js(1KB)
----circle.yml(595B)