Created
June 24, 2021 16:20
-
-
Save marcj/b694afa7097c144a03b19f85159dfc54 to your computer and use it in GitHub Desktop.
deepkit: simple database example
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 characters
#!/usr/bin/env ts-node-script | |
import 'reflect-metadata'; | |
import { Application, KernelModule } from '@deepkit/framework'; | |
import { http } from '@deepkit/http'; | |
import { entity, t } from '@deepkit/type'; | |
import { Database } from '@deepkit/orm'; | |
import { SQLiteDatabaseAdapter } from '@deepkit/sqlite'; | |
@entity.name('user') | |
class User { | |
@t.autoIncrement.primary public id: number = 0; | |
constructor( | |
@t public username: string, | |
) { | |
} | |
} | |
class SQLiteDatabase extends Database { | |
constructor() { | |
super(new SQLiteDatabaseAdapter(':memory:'), [User]); | |
} | |
} | |
@http.controller() | |
class MyWebsite { | |
constructor(protected db: SQLiteDatabase) { | |
} | |
@http.POST('/user') | |
async addUser(@http.body() user: User) { | |
await this.db.persist(user); | |
return "Added " + user.id; | |
} | |
@http.GET('/user') | |
@t.array(User) | |
async getUsers() { | |
return await this.db.query(User).find(); | |
} | |
} | |
Application.create({ | |
providers: [], | |
controllers: [MyWebsite], | |
imports: [ | |
KernelModule.configure({ | |
databases: [SQLiteDatabase], | |
migrateOnStartup: true, | |
}) | |
] | |
}).run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: