Last active
January 25, 2025 15:12
Revisions
-
WebReflection revised this gist
Jun 19, 2024 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -47,6 +47,9 @@ class User { } } // just asserting expectations console.assert(new User().name === ''); // an actual result const results = db.query('SELECT name FROM users').as(User); for (const user of results.all()) { -
WebReflection revised this gist
Jun 19, 2024 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -50,10 +50,10 @@ class User { // an actual result const results = db.query('SELECT name FROM users').as(User); for (const user of results.all()) { console.assert(user instanceof User); console.assert(user.name !== ''); if (Math.random() < .7) user.birthday(); console.log(`${user}`); } ``` -
WebReflection revised this gist
Jun 19, 2024 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,7 +4,7 @@ This is a simple demo of how latest *Bun* could've implemented `.as(Class)` inst // what `as(Class)` should return const { assign } = Object; const asClass = new WeakMap; const setAsClass = Class => { class As extends Class { constructor(fields) { assign(super(), fields); @@ -13,7 +13,7 @@ const setClass = Class => { asClass.set(Class, As); return As; }; const as = Class => asClass.get(Class) || setAsClass(Class); // a db mock for this demo const db = { -
WebReflection revised this gist
Jun 19, 2024 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -65,4 +65,6 @@ Bob is 0 Chuck is 1 ``` The only contract around this *ORM* like logic is that classes should have no constructor or one that has defaults or regular setup without implying result fields are already known AOT. Right now instead, we have a mechanism that really doesn't play well at all with modern classes abilities 😥 -
WebReflection revised this gist
Jun 19, 2024 . 1 changed file with 3 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -63,4 +63,6 @@ The output would be likely similar to this one with no assertions failed whatsoe Alice is 1 Bob is 0 Chuck is 1 ``` The only contract around this *ORM* like logic is that classes should have no constructor or one that has defaults or regular setup without implying result fields are already known AOT. -
WebReflection revised this gist
Jun 19, 2024 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -55,4 +55,12 @@ for (const user of results.all()) { if (Math.random() < .7) user.birthday(); console.log(`${user}`); } ``` The output would be likely similar to this one with no assertions failed whatsoever: ``` Alice is 1 Bob is 0 Chuck is 1 ``` -
WebReflection created this gist
Jun 19, 2024 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,58 @@ This is a simple demo of how latest *Bun* could've implemented `.as(Class)` instead of using the quite jurassic `Object.create` which is slow, bloated due descriptors logic, if used at all, and incapable to play nicely with features introduced *after* `Object.create` was even specified. ```js // what `as(Class)` should return const { assign } = Object; const asClass = new WeakMap; const setClass = Class => { class As extends Class { constructor(fields) { assign(super(), fields); } } asClass.set(Class, As); return As; }; const as = Class => asClass.get(Class) || setClass(Class); // a db mock for this demo const db = { query(_) { return { as(Class) { const As = as(Class); return { all() { return [ new As({ name: 'Alice' }), new As({ name: 'Bob' }), new As({ name: 'Chuck' }), ]; } }; } }; } }; // a class for this demo class User { name = ''; #age = 0; birthday() { this.#age++; } toString() { return `${this.name} is ${this.#age}`; } } // an actual result const results = db.query('SELECT name FROM users').as(User); for (const user of results.all()) { console.assert(user instanceof User); console.assert(user.name !== ''); if (Math.random() < .7) user.birthday(); console.log(`${user}`); } ```