deno.land / x / dormlite@0.0.2 / mod.ts

نووسراو ببینە
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { DB } from "https://deno.land/x/sqlite@v3.7.0/mod.ts";
export class Database { static db: DB;
// deno-lint-ignore no-explicit-any static async init(filename: string, models: any[]) { const databaseExists = await Deno.stat(`${filename}.db`).catch(() => false);
this.db = new DB(`${filename}.db`);
if (!databaseExists) { await models.forEach(async (model) => { await model.init(model); }); } }}
export class Model { static autoincrement = 0; static tableName = "models";
id: number; created_at: string; updated_at: string;
constructor() { this.id = getAutoincrement(this); this.created_at = new Date().toISOString(); this.updated_at = new Date().toISOString(); }
static async init<T extends Model>(model: new () => T) { const temp: any = new model();
const columns = Object.keys(temp).map((column) => { if (column === "id") { return `${column} SERIAL PRIMARY KEY`; } else if (column === "tableName") { return false; }
const type = typeof temp[column]; let sqlType = "";
switch (type) { case "string": sqlType = "TEXT"; break; case "number": sqlType = "INT"; break; case "boolean": sqlType = "BOOLEAN"; break; default: sqlType = "TEXT"; }
return `${column} ${sqlType}`; }).filter(Boolean);
await Database.db.query( ` CREATE TABLE IF NOT EXISTS ${this.tableName} ( ${columns.join(", ")} ) `, ); }
static async all(): Promise<Model[]> { const results = await Database.db.query( `SELECT * FROM ${this.tableName}`, );
return results as unknown[] as Model[]; }
static async find(id: number): Promise<Model> { const results = await Database.db.query( `SELECT * FROM ${this.tableName} WHERE id = $1`, [id], );
return results[0] as unknown as Model; }
static async where( column: string, value: string | number, ): Promise<Model[]> { const results = await Database.db.query( `SELECT * FROM ${this.tableName} WHERE ${column} = $1`, [value], );
return results as unknown[] as Model[]; }
static async create( model: Model, ): Promise<Model> { const columns = Object.keys(model); const values = Object.values(model);
const indexOfTableName = columns.indexOf("tableName"); if (indexOfTableName! > -1) { columns.splice(indexOfTableName, 1); values.splice(indexOfTableName, 1); }
const result = await Database.db.query( `INSERT INTO ${this.tableName} (${columns.join(", ")}) VALUES (${ values.map((_, i) => `$${i + 1}`).join(", ") }) RETURNING *`, values, );
return result[0] as unknown as Model; }
static async clear(): Promise<void> { await Database.db.query( `DELETE FROM ${this.tableName}`, ); }}
// Source: https://stackoverflow.com/questions/33387318/access-to-static-properties-via-this-constructor-in-typescriptfunction getAutoincrement<T>(target: T) { type Type = { constructor: Type; autoincrement: any; } & T;
return (target as Type).constructor.autoincrement++;}
dormlite

Version Info

Tagged at
a year ago