Skip to content

Commit 84255b0

Browse files
committed
kinda working
1 parent 03eca41 commit 84255b0

File tree

2 files changed

+199
-10
lines changed

2 files changed

+199
-10
lines changed

packages/mql-typescript/tests/real.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
'use strict';
2+
var __awaiter =
3+
(this && this.__awaiter) ||
4+
function (thisArg, _arguments, P, generator) {
5+
function adopt(value) {
6+
return value instanceof P
7+
? value
8+
: new P(function (resolve) {
9+
resolve(value);
10+
});
11+
}
12+
return new (P || (P = Promise))(function (resolve, reject) {
13+
function fulfilled(value) {
14+
try {
15+
step(generator.next(value));
16+
} catch (e) {
17+
reject(e);
18+
}
19+
}
20+
function rejected(value) {
21+
try {
22+
step(generator['throw'](value));
23+
} catch (e) {
24+
reject(e);
25+
}
26+
}
27+
function step(result) {
28+
result.done
29+
? resolve(result.value)
30+
: adopt(result.value).then(fulfilled, rejected);
31+
}
32+
step((generator = generator.apply(thisArg, _arguments || [])).next());
33+
});
34+
};
35+
var __generator =
36+
(this && this.__generator) ||
37+
function (thisArg, body) {
38+
var _ = {
39+
label: 0,
40+
sent: function () {
41+
if (t[0] & 1) throw t[1];
42+
return t[1];
43+
},
44+
trys: [],
45+
ops: [],
46+
},
47+
f,
48+
y,
49+
t,
50+
g = Object.create(
51+
(typeof Iterator === 'function' ? Iterator : Object).prototype,
52+
);
53+
return (
54+
(g.next = verb(0)),
55+
(g['throw'] = verb(1)),
56+
(g['return'] = verb(2)),
57+
typeof Symbol === 'function' &&
58+
(g[Symbol.iterator] = function () {
59+
return this;
60+
}),
61+
g
62+
);
63+
function verb(n) {
64+
return function (v) {
65+
return step([n, v]);
66+
};
67+
}
68+
function step(op) {
69+
if (f) throw new TypeError('Generator is already executing.');
70+
while ((g && ((g = 0), op[0] && (_ = 0)), _))
71+
try {
72+
if (
73+
((f = 1),
74+
y &&
75+
(t =
76+
op[0] & 2
77+
? y['return']
78+
: op[0]
79+
? y['throw'] || ((t = y['return']) && t.call(y), 0)
80+
: y.next) &&
81+
!(t = t.call(y, op[1])).done)
82+
)
83+
return t;
84+
if (((y = 0), t)) op = [op[0] & 2, t.value];
85+
switch (op[0]) {
86+
case 0:
87+
case 1:
88+
t = op;
89+
break;
90+
case 4:
91+
_.label++;
92+
return { value: op[1], done: false };
93+
case 5:
94+
_.label++;
95+
y = op[1];
96+
op = [0];
97+
continue;
98+
case 7:
99+
op = _.ops.pop();
100+
_.trys.pop();
101+
continue;
102+
default:
103+
if (
104+
!((t = _.trys), (t = t.length > 0 && t[t.length - 1])) &&
105+
(op[0] === 6 || op[0] === 2)
106+
) {
107+
_ = 0;
108+
continue;
109+
}
110+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) {
111+
_.label = op[1];
112+
break;
113+
}
114+
if (op[0] === 6 && _.label < t[1]) {
115+
_.label = t[1];
116+
t = op;
117+
break;
118+
}
119+
if (t && _.label < t[2]) {
120+
_.label = t[2];
121+
_.ops.push(op);
122+
break;
123+
}
124+
if (t[2]) _.ops.pop();
125+
_.trys.pop();
126+
continue;
127+
}
128+
op = body.call(thisArg, _);
129+
} catch (e) {
130+
op = [6, e];
131+
y = 0;
132+
} finally {
133+
f = t = 0;
134+
}
135+
if (op[0] & 5) throw op[1];
136+
return { value: op[0] ? op[1] : void 0, done: true };
137+
}
138+
};
139+
Object.defineProperty(exports, '__esModule', { value: true });
140+
var Database = /** @class */ (function () {
141+
function Database() {
142+
var collections = Object.create(null);
143+
this._collections = collections;
144+
var proxy = new Proxy(this, {
145+
get: function (target, prop) {
146+
if (prop in target) {
147+
return target[prop];
148+
}
149+
if (typeof prop !== 'string' || prop.startsWith('_')) {
150+
return;
151+
}
152+
if (!collections[prop]) {
153+
collections[prop] = new Collection();
154+
}
155+
return collections[prop];
156+
},
157+
});
158+
return proxy;
159+
}
160+
return Database;
161+
})();
162+
var Collection = /** @class */ (function () {
163+
function Collection() {}
164+
Collection.prototype.find = function (query) {
165+
return Promise.resolve(query);
166+
};
167+
return Collection;
168+
})();
169+
function run() {
170+
return __awaiter(this, void 0, void 0, function () {
171+
var database, coll, _a, _b;
172+
return __generator(this, function (_c) {
173+
switch (_c.label) {
174+
case 0:
175+
database = new Database();
176+
coll = database.myCollection;
177+
_b = (_a = console).log;
178+
return [4 /*yield*/, database.myCollection.find({ name: 'foo' })];
179+
case 1:
180+
_b.apply(_a, [_c.sent()]);
181+
return [2 /*return*/];
182+
}
183+
});
184+
});
185+
}
186+
run().catch(console.error);

packages/mql-typescript/tests/real.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ class Database<D extends GenericDatabaseSchema = GenericDatabaseSchema> {
2020
);
2121
this._collections = collections;
2222
const proxy = new Proxy(this, {
23-
get: (target, prop): any => {
23+
get(target: any, prop: string): any {
2424
if (prop in target) {
25-
return (target as any)[prop];
25+
return target[prop];
2626
}
2727

2828
if (typeof prop !== 'string' || prop.startsWith('_')) {
2929
return;
3030
}
3131

3232
if (!collections[prop]) {
33-
collections[prop] = new Collection<
34-
D,
35-
D[typeof prop]
36-
>() as CollectionWithSchema<D, D[typeof prop]>;
33+
collections[prop] =
34+
new Collection<D>() as unknown as CollectionWithSchema<D>;
3735
}
3836

3937
return collections[prop];
@@ -45,7 +43,9 @@ class Database<D extends GenericDatabaseSchema = GenericDatabaseSchema> {
4543

4644
type DatabaseWithSchema<
4745
D extends GenericDatabaseSchema = GenericDatabaseSchema,
48-
> = Database<D>;
46+
> = Database<D> & {
47+
[k in StringKey<D>]: Collection<D, D[k]>;
48+
};
4949
class Collection<
5050
D extends GenericDatabaseSchema = GenericDatabaseSchema,
5151
C extends GenericCollectionSchema = GenericCollectionSchema,
@@ -59,10 +59,13 @@ type CollectionWithSchema<
5959
C extends GenericCollectionSchema = D[keyof D],
6060
> = Collection<D, C>;
6161

62+
type dbSchema = {
63+
myCollection: { schema: { name: string } };
64+
};
65+
6266
async function run() {
63-
const database = new Database<{
64-
myCollection: { schema: { name: string } };
65-
}>();
67+
const database = new Database<dbSchema>() as DatabaseWithSchema<dbSchema>;
68+
const coll = database.myCollection;
6669
console.log(await database.myCollection.find({ name: 'foo' }));
6770
}
6871

0 commit comments

Comments
 (0)