Skip to content

Commit 941af76

Browse files
authored
Adds .deleteAll convenience method to client.schema (#91)
1 parent 1af7777 commit 941af76

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

src/schema/deleteAll.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import SchemaGetter from './getter';
2+
import ClassDeleter from './classDeleter';
3+
import Connection from '../connection';
4+
5+
export default async (client: Connection) => {
6+
const getter = new SchemaGetter(client);
7+
const schema = await getter.do();
8+
await Promise.all(
9+
schema.classes
10+
? schema.classes.map((c) => {
11+
const deleter = new ClassDeleter(client);
12+
return deleter.withClassName(c.class as string).do();
13+
})
14+
: []
15+
);
16+
};

src/schema/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import TenantsGetter from './tenantsGetter';
1212
import TenantsUpdater from './tenantsUpdater';
1313
import TenantsDeleter from './tenantsDeleter';
1414
import Connection from '../connection';
15+
import deleteAll from './deleteAll';
1516
import { Tenant } from '../openapi/types';
1617

1718
export interface Schema {
@@ -21,6 +22,7 @@ export interface Schema {
2122
exists: (className: string) => Promise<boolean>;
2223
getter: () => SchemaGetter;
2324
propertyCreator: () => PropertyCreator;
25+
deleteAll: () => Promise<void>;
2426
shardsGetter: () => ShardsGetter;
2527
shardUpdater: () => ShardUpdater;
2628
shardsUpdater: () => ShardsUpdater;
@@ -38,6 +40,7 @@ const schema = (client: Connection): Schema => {
3840
exists: (className: string) => new ClassExists(client).withClassName(className).do(),
3941
getter: () => new SchemaGetter(client),
4042
propertyCreator: () => new PropertyCreator(client),
43+
deleteAll: () => deleteAll(client),
4144
shardsGetter: () => new ShardsGetter(client),
4245
shardUpdater: () => new ShardUpdater(client),
4346
shardsUpdater: () => new ShardsUpdater(client),

src/schema/journey.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,23 @@ describe('schema', () => {
395395

396396
return deleteClass(client, newClass.class);
397397
});
398+
399+
it('delete all data from the schema', () => {
400+
const newClass: any = newClassObject('LetsDeleteThisClass');
401+
const newClass2: any = newClassObject('LetsDeleteThisClassToo');
402+
const classNames = [newClass.class, newClass2.class];
403+
Promise.all([
404+
client.schema.classCreator().withClass(newClass).do(),
405+
client.schema.classCreator().withClass(newClass2).do(),
406+
])
407+
.then(() => client.schema.getter().do())
408+
.then((schema) => classNames.forEach((cn) => expect(schema.classes?.map((c) => c.class)).toContain(cn)))
409+
.then(() => client.schema.deleteAll())
410+
.then(() => client.schema.getter().do())
411+
.then((schema) =>
412+
classNames.forEach((cn) => expect(schema.classes?.map((c) => c.class)).not.toContain(cn))
413+
);
414+
});
398415
});
399416

400417
describe('property setting defaults and migrations', () => {

0 commit comments

Comments
 (0)