UNPKG

fewer

Version:

A minimal ORM for Node.js.

169 lines 6.44 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const __1 = require("../../"); const mocks_1 = require("../../__tests__/mocks"); const schema = __1.createSchema() .table(mocks_1.database, 'users', { primaryKey: 'id' }, t => ({ id: t.number(), firstName: t.string(), lastName: t.string(), })) .table(mocks_1.database, 'posts', { primaryKey: 'id' }, t => ({ id: t.number(), title: t.string(), subtitle: t.maybeString(), userId: t.number(), })); const Users = __1.createRepository(schema.tables.users); const Posts = __1.createRepository(schema.tables.posts); const userPosts = __1.createHasMany(Users, Posts, 'userId'); const belongsToUser = __1.createBelongsTo(Users, 'userId'); describe('queryBuilding', () => { describe('where', () => { it('simple usage results in a correct SQ select', () => { const result = Users.where({ firstName: 'Emily' })["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { plucked: [], table: 'users', wheres: [{ firstName: 'Emily' }], } }); }); it('chained usage results in a correct SQ select', () => { const result = Users .where({ firstName: 'Emily' }) .where({ lastName: 'Dobervich' })["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { plucked: [], table: 'users', wheres: [ { firstName: 'Emily' }, { lastName: 'Dobervich' } ], } }); }); it('value in array results in a correct SQ select', () => { const result = Users.where({ firstName: ['Emily', 'Jordan'] })["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { plucked: [], table: 'users', wheres: [ { firstName: ['Emily', 'Jordan'] }, ], } }); }); }); describe('limit', () => { it('builds a correct SQ select', () => { const result = Users.limit(5)["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { plucked: [], table: 'users', wheres: [], limit: 5, } }); }); }); describe('offset', () => { it('builds a correct SQ select', () => { const result = Users.offset(5)["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { plucked: [], table: 'users', wheres: [], offset: 5, } }); }); }); describe('load', () => { it('builds a correct SQ select', () => { const result = Users.load('posts', userPosts)["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { loads: { 'posts': { keys: ['id', 'userId'], select: { context: { plucked: [], table: 'posts', wheres: [], }, }, } }, plucked: [], table: 'users', wheres: [], } }); }); it('with association query builds a correct SQ select', () => { const result = Users.load('posts', userPosts.where({ title: 'How to Use Fewer' }))["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { loads: { 'posts': { keys: ['id', 'userId'], select: { context: { plucked: [], table: 'posts', wheres: [{ title: 'How to Use Fewer' }], }, }, }, }, plucked: [], table: 'users', wheres: [], } }); }); it('with nested association builds a correct SQ select', () => { const result = Users .load('posts', userPosts.load('user', belongsToUser))["@@TO_SQ_SELECT" /* TO_SQ_SELECT */](); expect(result).toEqual({ context: { loads: { 'posts': { keys: ['id', 'userId'], select: { context: { plucked: [], table: 'posts', wheres: [], loads: { 'user': { keys: ['userId', 'id'], select: { context: { plucked: [], table: 'users', wheres: [], }, }, }, }, } } } }, plucked: [], table: 'users', wheres: [], } }); }); }); }); //# sourceMappingURL=queryBuilding.test.js.map