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

File loader support for ESM #62

Merged
merged 5 commits into from
Nov 10, 2023
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
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v16.16.0
v18.18.2
3,269 changes: 1,791 additions & 1,478 deletions package-lock.json

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@
"license": "ISC",
"dependencies": {
"callsites": "^3.1.0",
"generic-pool": "^3.8.2",
"tedious": "^15.0.1"
"generic-pool": "^3.9.0",
"tedious": "^16.6.0"
},
"devDependencies": {
"app-module-path": "^2.2.0",
"chai": "^4.3.6",
"chai": "^4.3.10",
"chai-as-promised": "^7.1.1",
"coveralls": "^3.1.1",
"dirty-chai": "^2.0.1",
"eslint-config-leankit": "^6.0.0",
"mocha": "^10.0.0",
"nodemon": "^2.0.19",
"eslint-config-leankit": "^6.1.0",
"mocha": "^10.2.0",
"nodemon": "^3.0.1",
"nyc": "^15.1.0",
"proxyquire": "^2.1.3",
"sinon": "^14.0.0",
"sinon": "^17.0.1",
"sinon-chai": "^3.7.0"
},
"nyc": {
Expand Down
89 changes: 78 additions & 11 deletions spec/behavior/fileLoader.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,87 @@
const { proxyquire, sinon } = testHelpers;

describe( "fileLoader", () => {
it( "should cache file text", async () => {
const expected = "FIRST";
const fs = {
readFile: sinon.stub()
};
fs.readFile.callsArgWith( 2, null, expected );
const expected = "SELECT lol";

const loader = proxyquire( "src/fileLoader", {
fs
describe( "CommonJS", () => {
let loader, fs;
beforeEach( () => {
fs = {
readFile: sinon.stub().resolves( expected )
};
const callsite = {
getFileName: sinon.stub().returns( "/Users/josh/caller.cjs" )
};

loader = proxyquire( "src/fileLoader", {
"node:fs/promises": fs,
callsites: () => [ {}, callsite ]
} );
} );

[ "lol", "lol.sql", "./lol", "./lol.sql" ].forEach( f => {
it( `should load file from same directory: ${ f }`, async () => {
const query = await loader( f );
fs.readFile.should.be.calledOnceWithExactly( "/Users/josh/lol.sql", "utf8" );
query.should.equal( expected );
} );
} );

[ "queries/lol", "queries/lol.sql", "./queries/lol", "./queries/lol.sql" ].forEach( f => {
it( `should load file from nested directory: ${ f }`, async () => {
const query = await loader( f );
fs.readFile.should.be.calledOnceWithExactly( "/Users/josh/queries/lol.sql", "utf8" );
query.should.equal( expected );
} );
} );

it( "should cache file", async () => {
const query1 = await loader( "once" );
const query2 = await loader( "once.sql" );
fs.readFile.should.be.calledOnceWithExactly( "/Users/josh/once.sql", "utf8" );
query1.should.equal( expected );
query2.should.equal( expected );
} );
} );

await loader( "A" );
await loader( "A" );
describe( "ES modules", () => {
let loader, fs;
beforeEach( () => {
fs = {
readFile: sinon.stub().resolves( expected )
};
const callsite = {
getFileName: sinon.stub().returns( "file:///Users/josh/caller.cjs" )
};

fs.readFile.should.be.calledOnce();
loader = proxyquire( "src/fileLoader", {
"node:fs/promises": fs,
callsites: () => [ {}, callsite ]
} );
} );

[ "lol", "lol.sql", "./lol", "./lol.sql" ].forEach( f => {
it( `should load file from same directory: ${ f }`, async () => {
const query = await loader( f );
fs.readFile.should.be.calledOnceWithExactly( "/Users/josh/lol.sql", "utf8" );
query.should.equal( expected );
} );
} );

[ "queries/lol", "queries/lol.sql", "./queries/lol", "./queries/lol.sql" ].forEach( f => {
it( `should load file from nested directory: ${ f }`, async () => {
const query = await loader( f );
fs.readFile.should.be.calledOnceWithExactly( "/Users/josh/queries/lol.sql", "utf8" );
query.should.equal( expected );
} );
} );

it( "should cache file", async () => {
const query1 = await loader( "once" );
const query2 = await loader( "once.sql" );
fs.readFile.should.be.calledOnceWithExactly( "/Users/josh/once.sql", "utf8" );
query1.should.equal( expected );
query2.should.equal( expected );
} );
} );
} );
22 changes: 21 additions & 1 deletion spec/integration/types.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ describe( "Types - Integration", () => {
} );
} );

it( "should round trip tvp", async () => {
it( "should round trip tvp(sproc)", async () => {
await sql.executeBatch( `
IF TYPE_ID('IdTable') IS NULL BEGIN
CREATE TYPE IdTable
Expand All @@ -150,6 +150,26 @@ describe( "Types - Integration", () => {
ids.should.deep.equal( [ { value: "1" }, { value: "2" } ] );
} );

it( "should round trip tvp (no sproc)", async () => {
await sql.executeBatch( `
IF TYPE_ID('IdTable') IS NULL BEGIN
CREATE TYPE IdTable
AS TABLE
( value BIGINT );
END` );

const ids = await sql.query( `SELECT value FROM @ids`, {
ids: {
type: sql.tvp( "IdTable", {
value: sql.bigint
} ),
val: [ { value: "1" }, { value: "2" } ]
}
} );

ids.should.deep.equal( [ { value: "1" }, { value: "2" } ] );
} );


Object.keys( types ).forEach( name => {
it( `should have same ${ name } type for module and client`, () => {
Expand Down
4 changes: 3 additions & 1 deletion src/TvpType.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
const { TYPES } = require( "tedious" );
class TvpType {

constructor( cols ) {
constructor( name, cols ) {
this.name = name;
this.cols = cols;
}

Expand All @@ -25,6 +26,7 @@ class TvpType {
} );

return {
name: this.name,
columns,
rows: data.map( o => props.map( p => o[ p ] || null ) )
};
Expand Down
16 changes: 10 additions & 6 deletions src/fileLoader.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
const { readFile } = require( "fs" );
const { promisify } = require( "util" );
const path = require( "path" );
const { readFile } = require( "node:fs/promises" );
const path = require( "node:path" );
const { fileURLToPath } = require( "node:url" );

const callsites = require( "callsites" );

const readFileAsync = promisify( readFile );
const cache = new Map();

module.exports = async function( relativeFile ) {
const relativeTo = path.dirname( callsites()[ 1 ].getFileName() );
let callerFileName = callsites()[ 1 ].getFileName();

if ( callerFileName.startsWith( "file://" ) ) {
// ESM
callerFileName = fileURLToPath( callerFileName );
}
const relativeTo = path.dirname( callerFileName );
if ( path.extname( relativeFile ) === "" ) {
relativeFile += ".sql";
}
const fileName = path.resolve( relativeTo, relativeFile );

let result = cache.get( fileName );
if ( !result ) {
result = await readFileAsync( fileName, "utf8" );
result = await readFile( fileName, "utf8" );
cache.set( fileName, result );
}
return result;
Expand Down
8 changes: 7 additions & 1 deletion src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ const types = {
variant: () => new TypeWrapper( TYPES.Variant ),
xml: () => new TypeWrapper( TYPES.Xml )
};
types.tvp = cols => new TvpType( cols );
types.tvp = ( name, cols ) => {
if ( typeof( name ) !== "string" ) {
cols = name;
name = "";
}
return new TvpType( name, cols );
};

Object.keys( types ).forEach( key => {
types[ key ].nullable = function( val ) {
Expand Down