UNPKG

tspace-mysql

Version:

Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.

143 lines 7.18 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const chai_1 = __importStar(require("chai")); const mocha_1 = require("mocha"); const chai_json_schema_1 = __importDefault(require("chai-json-schema")); const lib_1 = require("../lib"); function isSortedByDesc(arr, key = 'email_username') { for (let i = 0; i < arr.length - 1; i++) { if (arr[i][key] < arr[i + 1][key]) return false; } return true; } function isSortedByAsc(arr, key = 'email_username') { for (let i = 0; i < arr.length - 1; i++) { if (arr[i][key] > arr[i + 1][key]) return false; } return true; } const virtualJsonSchema = { type: 'object', required: ['email_username'], properties: { email_username: { type: 'string' } } }; const schema = { id: lib_1.Blueprint.int().primary().autoIncrement(), uuid: lib_1.Blueprint.varchar(50).null(), email: lib_1.Blueprint.varchar(50).null(), name: lib_1.Blueprint.varchar(255).null(), username: lib_1.Blueprint.varchar(255).null(), password: lib_1.Blueprint.varchar(255).null(), created_at: lib_1.Blueprint.timestamp().null(), updated_at: lib_1.Blueprint.timestamp().null(), deleted_at: lib_1.Blueprint.timestamp().null(), email_username: new lib_1.Blueprint().virtualColumn(`CONCAT(email,' ', username)`), }; class User extends lib_1.Model { boot() { this.useSchema(schema); } } chai_1.default.use(chai_json_schema_1.default); (0, mocha_1.describe)('Testing Virtual Column', function () { /* ##################################################### */ (0, mocha_1.it)(`Virtual Column: Start to get virtual column 'email_username' from Model User`, async function () { const user = await new User().first(); (0, chai_1.expect)(user).to.be.an('object'); (0, chai_1.expect)(user?.email_username).to.be.equal(user?.email + ' ' + user?.username); (0, chai_1.expect)(user).to.be.jsonSchema(virtualJsonSchema); const users = await new User().get(); (0, chai_1.expect)(users).to.be.an('array'); (0, chai_1.expect)(users).to.be.jsonSchema({ type: 'array', items: { ...virtualJsonSchema } }); for (const user of users) { (0, chai_1.expect)(user?.email_username).to.be.equal(user?.email + ' ' + user?.username); } }); (0, mocha_1.it)(`Virtual Column: use method .select('email','username','email_username')`, async function () { const user = await new User().select('email', 'username', 'email_username').first(); (0, chai_1.expect)(user).to.be.an('object'); (0, chai_1.expect)(user?.email_username).to.be.equal(user?.email + ' ' + user?.username); (0, chai_1.expect)(user).to.be.jsonSchema(virtualJsonSchema); const users = await new User().select('email', 'username', 'email_username').get(); (0, chai_1.expect)(users).to.be.an('array'); (0, chai_1.expect)(users).to.be.jsonSchema({ type: 'array', items: { ...virtualJsonSchema } }); for (const user of users) { (0, chai_1.expect)(user?.email_username).to.be.equal(user?.email + ' ' + user?.username); } }); (0, mocha_1.it)(`Virtual Column: use method .where('email_username')`, async function () { const userNull = await new User().where('email_username', 'LIKE', '%xxxx%').first(); (0, chai_1.expect)(userNull).to.be.equal(null); const user = await new User().where('email_username', 'LIKE', '%test01%').first(); (0, chai_1.expect)(user).to.be.an('object'); (0, chai_1.expect)(user?.email_username).to.be.equal(user?.email + ' ' + user?.username); (0, chai_1.expect)(user).to.be.jsonSchema(virtualJsonSchema); const users = await new User().where('email_username', 'LIKE', '%test01%').get(); (0, chai_1.expect)(users).to.be.an('array'); (0, chai_1.expect)(users).to.be.jsonSchema({ type: 'array', items: { ...virtualJsonSchema } }); for (const user of users) { (0, chai_1.expect)(user?.email_username).to.be.equal(user?.email + ' ' + user?.username); } }); (0, mocha_1.it)(`Virtual Column: use method order column 'email_username' - orderBy - oldest - latest `, async function () { const userAsc = await new User().orderBy('email_username', 'asc').first(); (0, chai_1.expect)(userAsc).to.be.an('object'); (0, chai_1.expect)(userAsc?.id).to.be.equal(1); const usersAsc = await new User().orderBy('email_username', 'asc').get(); (0, chai_1.expect)(usersAsc).to.be.an('array'); (0, chai_1.expect)(isSortedByAsc(usersAsc)).to.be.true; const userDesc = await new User().orderBy('email_username', 'desc').first(); (0, chai_1.expect)(userDesc).to.be.an('object'); (0, chai_1.expect)(userDesc?.id).to.be.equal(6); const usersDesc = await new User().orderBy('email_username', 'desc').get(); (0, chai_1.expect)(usersDesc).to.be.an('array'); (0, chai_1.expect)(isSortedByDesc(usersDesc)).to.be.true; const userOldested = await new User().oldest('email_username').first(); (0, chai_1.expect)(userOldested).to.be.an('object'); (0, chai_1.expect)(userOldested?.id).to.be.equal(1); const usersOldested = await new User().oldest('email_username').get(); (0, chai_1.expect)(usersOldested).to.be.an('array'); (0, chai_1.expect)(isSortedByAsc(usersOldested)).to.be.true; const userLatested = await new User().latest('email_username').first(); (0, chai_1.expect)(userLatested).to.be.an('object'); (0, chai_1.expect)(userLatested?.id).to.be.equal(6); const usersLatested = await new User().latest('email_username').get(); (0, chai_1.expect)(usersLatested).to.be.an('array'); (0, chai_1.expect)(isSortedByDesc(usersLatested)).to.be.true; }); }); //# sourceMappingURL=07-Virtual-column.test.js.map