Last active
December 26, 2016 19:09
-
-
Save prashaantt/8b1255fdf18a49e1bb4ce8398bad18b3 to your computer and use it in GitHub Desktop.
Experiments with a tiny DB wrapper
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
// abstract: don't allow direct instantiation | |
abstract class BaseTable<T> { | |
// model holds the schema | |
protected model: T; | |
// convert kebab_case to camelCase | |
private camelCase(key: string) { | |
return key.replace(/_(\w)/g, function (...captures: string[]) { | |
return captures[1].toUpperCase(); | |
}); | |
} | |
// convert camelCase to kebab_case (naïve impl) | |
private kebabCase(key: string) { | |
let kebabKey = ''; | |
for (let i = 0; i < key.length; i++) { | |
if (key[i] === key[i].toLowerCase()) { | |
kebabKey += key[i]; | |
} | |
else { | |
kebabKey += '_' + key[i].toLowerCase(); | |
} | |
} | |
return kebabKey; | |
} | |
constructor(model?: T) { | |
if (model) { | |
this.model = model; | |
} | |
} | |
// init table from a database object | |
initFromDb(model: any) { | |
if (this.model) { | |
throw new Error('Model already constructed via constructor!'); | |
} | |
const newModel = {} as T; | |
for (let key in model) { | |
// convert snake_cased db field to camelCase | |
const camelKey = this.camelCase(key); | |
Object.assign(newModel, { [camelKey]: model[key] }); | |
} | |
this.model = newModel; | |
} | |
// serialise to kebab_cased db object | |
toDbObject() { | |
const dbObj = {}; | |
const model = this.model; | |
for (let key in model) { | |
const kebabKey = this.kebabCase(key); | |
Object.assign(dbObj, { [kebabKey]: model[key] }); | |
} | |
return dbObj; | |
} | |
// serialise to camelCased JSON | |
toString() { | |
return JSON.stringify(this.model); | |
} | |
} |
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
interface Model { | |
id: number; | |
postId: number; | |
parentName: string; | |
} | |
class Table extends BaseTable<Model> { | |
show() { | |
return this.model.postId + ': ' + this.model.parentName; | |
} | |
} |
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
let table = new Table({ | |
id: 99, | |
postId: 77, | |
parentName: 'the parent' | |
}); | |
console.log(table.toDbObject()); | |
console.log(table.toString()); | |
console.log(table.show()); | |
table = new Table(); | |
table.initFromDb({ | |
id: 99, | |
post_id: 77, | |
parent_name: 'the parent' | |
}); | |
console.log(table.toDbObject()); | |
console.log(table.toString()); | |
console.log(table.show()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment