Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add geometry column building #4776

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/schema/columncompiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ ColumnCompiler.prototype.date = 'date';
ColumnCompiler.prototype.datetime = 'datetime';
ColumnCompiler.prototype.time = 'time';
ColumnCompiler.prototype.timestamp = 'timestamp';
ColumnCompiler.prototype.geometry = 'geometry';
ColumnCompiler.prototype.geography = 'geography';
ColumnCompiler.prototype.point = 'point';
ColumnCompiler.prototype.enu = 'varchar';
ColumnCompiler.prototype.bit = ColumnCompiler.prototype.json = 'text';
ColumnCompiler.prototype.uuid = 'char(36)';
Expand Down
5 changes: 5 additions & 0 deletions lib/schema/tablebuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@ const columnTypes = [
'time',
'year',

// Geometry
'geometry',
'geography',
'point',

// String
'char',
'varchar',
Expand Down
2 changes: 1 addition & 1 deletion scripts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ version: '3'

services:
mssql:
image: mcr.microsoft.com/mssql/server:2017-latest
image: mcr.microsoft.com/mssql/server:2019-latest
ports:
- '21433:1433'
environment:
Expand Down
139 changes: 139 additions & 0 deletions test/integration2/schema/geometry-columns.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
const { expect } = require('chai');
const { getAllDbs, getKnexForDb } = require('../util/knex-instance-provider');
const {
isPostgreSQL,
isCockroachDB,
isPgBased,
isSQLite,
isMssql,
} = require('../../util/db-helpers');

describe('Schema', () => {
describe('geometry columns', () => {
getAllDbs().forEach((db) => {
describe(db, () => {
let knex;
const tblName = 'table_with_geo';

beforeEach(async () => {
knex = getKnexForDb(db);
await knex.schema.dropTableIfExists(tblName);
});

after(async () => {
return knex.destroy();
});

it('Creates POINT column for supported databases', async function () {
if (isCockroachDB(knex) || isMssql(knex)) {
return this.skip();
}

await knex.schema.createTable(tblName, (table) => {
table.point('pointColumn');
});

if (isPostgreSQL(knex)) {
await knex(tblName).insert({
pointColumn: '2, 3',
});

const result = await knex(tblName).select('*');
expect(result).to.eql([
{
pointColumn: {
x: 2,
y: 3,
},
},
]);
}
});

it('Creates GEOMETRY column for supported databases', async function () {
if (isPgBased(knex)) {
return this.skip();
}

await knex.schema.createTable(tblName, (table) => {
table.geometry('geometryColumn');
});

if (isPostgreSQL(knex)) {
await knex(tblName).insert({
geometryColumn: '2, 3',
});

const result = await knex(tblName).select('*');
expect(result).to.eql([
{
geometryColumn: {
x: 2,
y: 3,
},
},
]);
}

if (isSQLite(knex)) {
await knex(tblName).insert({
geometryColumn: '2, 3',
});

const result = await knex(tblName).select('*');
expect(result).to.eql([
{
geometryColumn: '2, 3',
},
]);
}

if (isMssql(knex)) {
await knex(tblName).insert({
geometryColumn: knex.raw('geometry::Point(1, 10, 0)'),
});

const result = await knex(tblName).select('*');
const geoData = result[0].geometryColumn;
expect(geoData.length).to.equal(22);
}
});

it('Creates GEOGRAPHY column for supported databases', async function () {
if (!isSQLite(knex) && !isMssql(knex)) {
return this.skip();
}

await knex.schema.createTable(tblName, (table) => {
table.geography('geoColumn');
});

if (isSQLite(knex)) {
await knex(tblName).insert({
geoColumn: '2, 3',
});

const result = await knex(tblName).select('*');
expect(result).to.eql([
{
geoColumn: '2, 3',
},
]);
}

if (isMssql(knex)) {
await knex(tblName).insert({
geoColumn: knex.raw(
"geography::STGeomFromText('LINESTRING(-122.360 47.656, -122.343 47.656 )', 4326)"
),
});

const result = await knex(tblName).select('*');
const geoData = result[0].geoColumn;
expect(geoData.length).to.equal(38);
}
});
});
});
});
});
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2023,6 +2023,9 @@ export declare namespace Knex {
useTimestampType?: boolean,
makeDefaultNow?: boolean
): void;
geometry(columnName: string): ColumnBuilder;
geography(columnName: string): ColumnBuilder;
point(columnName: string): ColumnBuilder;
binary(columnName: string, length?: number): ColumnBuilder;
enum(
columnName: string,
Expand Down