-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaas.ts
More file actions
283 lines (248 loc) · 8.7 KB
/
Copy pathsaas.ts
File metadata and controls
283 lines (248 loc) · 8.7 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
import {
Stabilize,
defineModel,
DataTypes,
RelationType,
DBType,
LogLevel,
type DBConfig,
generateUUID,
} from "../index";
// ─── CONFIG ──────────────────────────────────────────────────────────
const dbConfig: DBConfig = {
type: DBType.SQLite,
connectionString: "./data/saas.db",
};
const orm = new Stabilize(
dbConfig,
{ enabled: false, ttl: 60 },
{ level: LogLevel.Info },
);
// ─── MODELS ──────────────────────────────────────────────────────────
const Tenant = defineModel({
tableName: "tenants",
timestamps: { createdAt: "createdAt", updatedAt: "updatedAt" },
columns: {
id: { type: DataTypes.STRING, required: true, unique: true },
name: { type: DataTypes.STRING, length: 100, required: true },
slug: { type: DataTypes.STRING, length: 50, required: true, unique: true },
plan: {
type: DataTypes.STRING,
length: 20,
required: true,
defaultValue: "free",
},
isActive: { type: DataTypes.BOOLEAN, defaultValue: true },
maxUsers: { type: DataTypes.INTEGER, defaultValue: 5 },
deletedAt: { type: DataTypes.DATETIME, softDelete: true },
},
relations: [
{
type: RelationType.OneToMany,
target: () => Member,
property: "members",
foreignKey: "tenantId",
},
{
type: RelationType.OneToMany,
target: () => Project,
property: "projects",
foreignKey: "tenantId",
},
],
scopes: {
active: (qb) => qb.where("isActive = ?", true),
byPlan: (qb, plan: string) => qb.where("plan = ?", plan),
},
});
const User = defineModel({
tableName: "users",
timestamps: { createdAt: "createdAt", updatedAt: "updatedAt" },
columns: {
id: { type: DataTypes.STRING, required: true, unique: true },
email: {
type: DataTypes.STRING,
length: 255,
required: true,
unique: true,
},
name: { type: DataTypes.STRING, length: 100, required: true },
deletedAt: { type: DataTypes.DATETIME, softDelete: true },
},
relations: [
{
type: RelationType.OneToMany,
target: () => Member,
property: "memberships",
foreignKey: "userId",
},
],
});
const Member = defineModel({
tableName: "members",
timestamps: { createdAt: "createdAt", updatedAt: "updatedAt" },
columns: {
id: { type: DataTypes.STRING, required: true, unique: true },
tenantId: { type: DataTypes.STRING, required: true },
userId: { type: DataTypes.STRING, required: true },
role: {
type: DataTypes.STRING,
length: 20,
defaultValue: "member",
},
},
relations: [
{
type: RelationType.ManyToOne,
target: () => Tenant,
property: "tenant",
foreignKey: "tenantId",
},
{
type: RelationType.ManyToOne,
target: () => User,
property: "user",
foreignKey: "userId",
},
],
});
const Project = defineModel({
tableName: "projects",
timestamps: { createdAt: "createdAt", updatedAt: "updatedAt" },
columns: {
id: { type: DataTypes.STRING, required: true, unique: true },
tenantId: { type: DataTypes.STRING, required: true },
name: { type: DataTypes.STRING, length: 200, required: true },
description: { type: DataTypes.TEXT },
status: {
type: DataTypes.STRING,
length: 20,
defaultValue: "active",
},
deletedAt: { type: DataTypes.DATETIME, softDelete: true },
},
relations: [
{
type: RelationType.ManyToOne,
target: () => Tenant,
property: "tenant",
foreignKey: "tenantId",
},
],
scopes: {
active: (qb) => qb.where("status = ?", "active"),
byTenant: (qb, tenantId: string) => qb.where("tenantId = ?", tenantId),
},
});
// ─── MAIN ────────────────────────────────────────────────────────────
async function main() {
await orm.client.migrationQuery(`CREATE TABLE IF NOT EXISTS tenants (
id TEXT PRIMARY KEY, name TEXT NOT NULL, slug TEXT NOT NULL UNIQUE,
plan TEXT NOT NULL DEFAULT 'free', isActive INTEGER DEFAULT 1, maxUsers INTEGER DEFAULT 5,
deletedAt TEXT,
createdAt TEXT NOT NULL DEFAULT (datetime('now')), updatedAt TEXT NOT NULL DEFAULT (datetime('now'))
)`);
await orm.client.migrationQuery(`CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY, email TEXT NOT NULL UNIQUE, name TEXT NOT NULL,
deletedAt TEXT,
createdAt TEXT NOT NULL DEFAULT (datetime('now')), updatedAt TEXT NOT NULL DEFAULT (datetime('now'))
)`);
await orm.client.migrationQuery(`CREATE TABLE IF NOT EXISTS members (
id TEXT PRIMARY KEY, tenantId TEXT NOT NULL, userId TEXT NOT NULL,
role TEXT NOT NULL DEFAULT 'member',
createdAt TEXT NOT NULL DEFAULT (datetime('now')), updatedAt TEXT NOT NULL DEFAULT (datetime('now'))
)`);
await orm.client.migrationQuery(`CREATE TABLE IF NOT EXISTS projects (
id TEXT PRIMARY KEY, tenantId TEXT NOT NULL, name TEXT NOT NULL,
description TEXT, status TEXT NOT NULL DEFAULT 'active', deletedAt TEXT,
createdAt TEXT NOT NULL DEFAULT (datetime('now')), updatedAt TEXT NOT NULL DEFAULT (datetime('now'))
)`);
console.log("Tables created.");
const tenantRepo = orm.getRepository(Tenant);
const userRepo = orm.getRepository(User);
const memberRepo = orm.getRepository(Member);
const projectRepo = orm.getRepository(Project);
// ─── CREATE TENANTS ─────────────────────────────────────────────
const acme = await tenantRepo.create({
id: generateUUID(),
name: "Acme Corp",
slug: "acme",
plan: "pro",
maxUsers: 50,
});
const globex = await tenantRepo.create({
id: generateUUID(),
name: "Globex Inc",
slug: "globex",
plan: "free",
maxUsers: 5,
});
console.log("Created tenants:", acme.name, globex.name);
// ─── CREATE USERS & MEMBERS ─────────────────────────────────────
const user1 = await userRepo.create({
id: generateUUID(),
email: "alice@acme.com",
name: "Alice Johnson",
});
const user2 = await userRepo.create({
id: generateUUID(),
email: "bob@globex.com",
name: "Bob Smith",
});
await memberRepo.create({
id: generateUUID(),
tenantId: acme.id,
userId: user1.id,
role: "admin",
});
await memberRepo.create({
id: generateUUID(),
tenantId: globex.id,
userId: user2.id,
role: "member",
});
console.log("Created users and memberships");
// ─── CREATE PROJECTS (tenant-scoped) ────────────────────────────
const project1 = await projectRepo.create({
id: generateUUID(),
tenantId: acme.id,
name: "Website Redesign",
description: "Redesign company website",
});
const project2 = await projectRepo.create({
id: generateUUID(),
tenantId: acme.id,
name: "Mobile App",
description: "Build mobile application",
});
console.log("Created projects:", project1.name, project2.name);
// ─── MULTI-TENANT QUERIES ──────────────────────────────────────
// Get all projects for a tenant
const acmeProjects = await projectRepo
.scope("byTenant", acme.id)
.scope("active")
.orderBy("createdAt", "DESC")
.execute(orm.client);
console.log("Acme projects:", acmeProjects.length);
// Get tenant with members
const acmeMembers = await memberRepo
.find()
.where("tenantId = ?", acme.id)
.execute(orm.client);
console.log("Acme members:", acmeMembers.length);
// ─── PLAN-BASED QUERIES ────────────────────────────────────────
const proTenants = await tenantRepo
.scope("active")
.scope("byPlan", "pro")
.execute(orm.client);
console.log("Pro tenants:", proTenants.length);
// ─── AGGREGATE ─────────────────────────────────────────────────
const tenantStats = await tenantRepo.aggregate({
count: "*",
});
console.log("Total tenants:", tenantStats.count_all);
// ─── CLEANUP ───────────────────────────────────────────────────
await orm.close();
console.log("Done.");
}
main().catch(console.error);