So, I did it, trdb the json file database can now be used with pure js types. The trdb library is implemented using typescript. As before, the lib can be used with javascript. But how, it is possible to provide a type to a collection and in vsCode or other editors and IDEs with typescript support we can now have nice autocompletion hints.

With Typescript, it was already possible to pass a type to the collection method, but now this is also possible in pure JS. I did that with a second optional argument. Look at the following example

1
2
3
4
5
6
7
8
9
10
11
12
const baseExample = {
id: '', // type string
};
// create a db
const db = newFileDB('./db.json');

// create a collection
const users = db.collection('users', { name: '', ...baseExample });

const myUser = await users.findOne({ name: 'tobias' }); // ts tell you that name is a valid prop

myUser. // when using myUser. the editor will offer id and name.

And here is how I did it, before function signature for creating a collection looked like this:

1
collection<T>(collectionName: string) { ... }

Now, it has changed to this:

1
collection<T>(collectionName: string, example?: T) { ... }

And this is all that has changed for version 0.0.5. only the function declaration has changed. Using typescript it is still better to use the collection method traditionally like: const userCollection = db.collection<IUser>('user');. However in javascript you can provide types, and even constructed types like shown above. The module, otherwise did not change in this version. The example

I think this is a great example, how javascript developers actually can greatly profit from typescript, without actually using it.

back to developer log