PHP Classes

File: test/js/test-mysql.js

Recommend this page to a friend!
  Packages of Nikos M.   Dialect PHP ORM and ODM Library   test/js/test-mysql.js   Download  
File: test/js/test-mysql.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Dialect PHP ORM and ODM Library
Store and retrieve objects in database using ORM
Author: By
Last change: v.2.1.0

* Entity-Attribute-Value relationship support
* various edits, corrections
v.2.1.0, progress

* EAV suppolrt almost complete
* fix js this/proxy reference
* js async/await, py sync
* fix some typos
Date: 7 months ago
Size: 5,014 bytes
 

Contents

Class file image Download
"use strict"; const DialectORM = require('../../src/js/DialectORM.js'); const Dialect = require('../../../Dialect/src/js/Dialect.js'); const MysqlDb = require('./sql/mysql.js')(DialectORM); DialectORM.dependencies({ 'Dialect' : Dialect // provide actual class, i.e Dialect or path of module }); DialectORM.DBHandler(new MysqlDb({ 'database' : 'dialectorm', 'user' : 'dialectorm', 'password' : 'dialectorm' }, 'mysql')); class Post extends DialectORM { static table = 'posts'; static pk = ['id']; static fields = ['id', 'content']; static extra_fields = ['postmeta', 'post_id', 'id', 'key', 'value']; static relationships = {}; typeId(x) { return parseInt(x, 10) || 0; } typeContent(x) { return String(x); } validateContent(x) { return 0 < x.length; } } class PostStatus extends DialectORM { static table = 'poststatus'; static pk = ['id']; static fields = ['id', 'status', 'type', 'post_id']; static relationships = {}; typeId(x) { return parseInt(x, 10) || 0; } typePostId(x) { return parseInt(x, 10) || 0; } typeStatus(x) { return String(x).toLowerCase(); } typeType(x) { return String(x).toLowerCase(); } validateStatus(x) { return -1 !== ['approved', 'published', 'suspended'].indexOf(x); } validateType(x) { return -1 !== ['article', 'tutorial', 'general'].indexOf(x); } } class Comment extends DialectORM { static table = 'comments'; static pk = ['id']; static fields = ['id', 'content', 'post_id']; static relationships = {}; typeId(x) { return parseInt(x, 10) || 0; } typeContent(x) { return String(x); } typePostId(x) { return parseInt(x, 10) || 0; } validateContent(x) { return 0 < x.length; } } class User extends DialectORM { static table = 'users'; static pk = ['id']; static fields = ['id', 'name']; static relationships = {}; typeId(x) { return parseInt(x, 10) || 0; } typeName(x) { return String(x); } validateName(x) { return 0 < x.length; } } Post.relationships = { 'status' : ['hasOne', PostStatus, ['post_id']], 'comments' : ['hasMany', Comment, ['post_id']], 'authors' : ['belongsToMany', User, ['user_id'], ['post_id'], 'user_post'] }; PostStatus.relationships = { 'post' : ['belongsTo', Post, ['post_id']] }; Comment.relationships = { 'post' : ['belongsTo', Post, ['post_id']] }; User.relationships = { 'posts' : ['belongsToMany', Post, ['post_id'], ['user_id'], 'user_post'] }; function print(x) { console.log(x); } function output(data) { if (Array.isArray(data)) print(JSON.stringify(data.map(d => d.toObj(true)), null, 4)); else if (data instanceof DialectORM) print(JSON.stringify(data.toObj(true), null, 4)); else print(String(data)); } async function test() { output('Posts: ' + String(await Post.count())); output('Users: ' + String(await User.count())); let post = await Post.fetchAll({'conditions' : {'content' : 'a js post..'},'single' : true}); if (!post) { post = new Post({'content':'a js post..'}); post.setCustomField2('custom value 2'); post.setComments([new Comment({'content':'a js comment..'})]); post.setComments([new Comment({'content':'one more js comment..'})], {'merge':true}); post.setAuthors([new User({'name':'a js user'})]); post.setStatus(new PostStatus({'status':'approved','type':'article'})); await post.save({'withRelated':true}); } else { switch (await post.getCustomField2()) { case 'custom value 2': post.setCustomField2('custom value 22'); await post.save({'withRelated':true}); break; case 'custom value 22': post.setCustomField2('custom value 2'); await post.save({'withRelated':true}); break; } } output(post); print('Posts:'); output(await Post.fetchAll({'withRelated' : ['status', 'comments', 'authors']})); print('Posts:'); output(await Post.fetchAll({ 'conditions' : {'condition' : {'or' : [ {'custom_field2' : 'custom value 2'}, {'custom_field2' : 'custom value 22'} ]}}, 'withRelated' : ['status', 'comments', 'authors'], 'related' : { 'authors' : {'conditions':{'clause':{'or':[ {'name':{'like':'user'}}, {'name':{'like':'foo'}}, {'name':{'like':'bar'}} ]}}}, 'comments' : {'limit':1} // eager relationship loading with extra conditions, see `Dialect` lib on how to define conditions } })); } test().then(() => process.exit()).catch(e => {print(e); process.exit();});