-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Lars-Erik Roald
committed
Jul 11, 2023
1 parent
4e25b3f
commit 01e307d
Showing
35 changed files
with
2,352 additions
and
1,365 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
function map(fn) { | ||
const handler = { | ||
get(target, prop) { | ||
if (prop === 'map') { | ||
return (...args) => { | ||
console.log(`Called method "${prop}" with arguments:`, args); | ||
return new Proxy(onFinal, handler); | ||
}; | ||
} else if (typeof target[prop] !== 'undefined') { | ||
return target[prop]; | ||
} else { | ||
return (...args) => { | ||
console.log(`Called method "${prop}" with arguments:`, args); | ||
return new Proxy({}, handler); | ||
}; | ||
} | ||
}, | ||
apply(target, _thisArg, argumentsList) { | ||
if (target === onFinal) { | ||
return target(...argumentsList); | ||
} else { | ||
console.log(`Called function "${target.name}" with arguments:`, argumentsList); | ||
return new Proxy({}, handler); | ||
} | ||
}, | ||
set(target, prop, value) { | ||
console.log(`Set property "${prop}" to value:`, value); | ||
target[prop] = value; | ||
return true; | ||
}, | ||
}; | ||
|
||
const dbMap = { | ||
http: (url) => url, | ||
pg: throwDb, | ||
postgres: throwDb, | ||
mssql: throwDb, | ||
mssqlNative: throwDb, | ||
mysql: throwDb, | ||
sap: throwDb, | ||
sqlite: throwDb, | ||
}; | ||
|
||
function throwDb() { | ||
throw new Error('Cannot create pool for database outside node'); | ||
} | ||
|
||
function onFinal(arg) { | ||
if (arg.db && typeof arg === 'function') { | ||
arg.db = arg.db(dbMap); | ||
} | ||
return fn(arg); | ||
} | ||
} | ||
|
||
module.exports = map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const _newTable = require('../table'); | ||
|
||
function mapRoot(index, fn) { | ||
return map(index, context, fn); | ||
|
||
function context(arg) { | ||
const dbMap = { | ||
pg: createPool.bind(null, index.pg), | ||
postgres: createPool.bind(null, index.pg), | ||
mssql: createPool.bind(null, index.mssql), | ||
mssqlNative: createPool.bind(null, index.mssqlNative), | ||
mysql: createPool.bind(null, index.mysql), | ||
sap: createPool.bind(null, index.sap), | ||
sqlite: createPool.bind(null, index.sqlite), | ||
}; | ||
|
||
function createPool(provider, ...args) { | ||
const pool = provider.apply(null, args); | ||
const tables = {}; | ||
for (let name in context) { | ||
if (context[name] && context[name]._dbName) | ||
tables[name] = context[name]; | ||
} | ||
return index({ db: () => pool, tables }); | ||
} | ||
|
||
if (arg && arg.db && typeof arg.db === 'function') { | ||
return arg.db(dbMap); | ||
} | ||
else | ||
return context; | ||
} | ||
} | ||
|
||
function map(index, context, fn) { | ||
let next = fn({ table: newTable, ...context }); | ||
|
||
for (let name in next) { | ||
if (next[name] && next[name]._dbName) { | ||
context[name] = next[name]; | ||
context[name].map = mapTable.bind(null, context[name]); | ||
} | ||
} | ||
context.map = map.bind(null, index, context); | ||
return context; | ||
|
||
function newTable() { | ||
let table = _newTable.apply(null, arguments); | ||
table.map = mapTable.bind(null, table); | ||
return table; | ||
} | ||
} | ||
|
||
function mapTable(table, fn) { | ||
let next = fn({ column: table.column, primaryColumn: table.primaryColumn, references: table.join, hasMany, hasOne }); | ||
for (let name in next) { | ||
if (next[name].as) | ||
next[name] = next[name].as(name); | ||
} | ||
|
||
function hasMany(to) { | ||
if (!to) | ||
throw new Error('Missing \'to\' table'); | ||
return { by }; | ||
|
||
function by() { | ||
const join = to.join(table).by.apply(null, arguments); | ||
return table.hasMany(join); | ||
} | ||
} | ||
|
||
function hasOne(to) { | ||
if (!to) | ||
throw new Error('Missing \'to\' table'); | ||
return { by }; | ||
|
||
function by() { | ||
const join = to.join(table).by.apply(null, arguments); | ||
return table.hasOne(join); | ||
} | ||
} | ||
|
||
return table; | ||
|
||
} | ||
|
||
module.exports = mapRoot; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.